I have been working on a project which is getting the data from a temperature sensor that is connected to the arduino. I need to get that data to my raspberry pi and display it online. so I wrote python code that should get the data and put it in a sqlite database file. I try this code using python idle2 and idle 3 I cannot get it working. would be cause pf i have both python 2 and 3 installed to my rasbian? or there is a problem with the code?
here is the code;
#!/ussr/bin/python
# -*- coding: utf-8 -*-
import serial
import datetime
import sqlite3 as lite
import sys
import time
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
ser.open()
count = 0
con = lite.connect('data.db')
try:
while 1:
indata = ser.readline()
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
count = count + 1
print indata + current_time
print count
with con:
cur = con.cursor()
cur.execute("INSERT INTO Temperatures VALUES( ?, ?, ? )", (count, current_time, indata))
if count > 100:
cur.execute("DELETE FROM Temperatures")
count = 0
Interrupt:
ser.close()
尝试将“中断:”更改为“例外:”
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.