我正在使用DHT11湿度传感器,Dallas18B20温度传感器,簧片开关和arduino孔效应传感器。我成功地将两个传感器数据(湿度和温度)插入到数据库中。但是当我尝试将四个传感器数据插入数据库时,它没有向数据库中插入任何数据。以下是我的代码。老实说,我知道件做了什么[ 0],碎片[1]在这里。我提到了一个示例代码并做了这个。我写了cursor.execute("INSERT INTO weatherdata(DateTime,HUMIDITY,TEMPERATUREc) VALUES (NOW(),%s,%s)",(pieces[0],pieces[1]))
的温度和湿度传感器,它工作得非常好。我检查了簧片开关和孔效应传感器。它们工作正常。请帮我解决这个问题。
#!/usr/bin/python
import serial
import time
import MySQLdb
#establish connection to MYSQL.
dbConn=MySQLdb.connect("localhost","root","raspberry","weather") or die ("could not connect to database")
#open a cursor to the database
device="""/dev/ttyACM0"""
try:
print "Trying...",device
arduino=serial.Serial(device,9600)
except:
print"Failed to connect on",device
while True:
time.sleep(1)
try:
data=arduino.readline() #read data from arduino
print data
pieces=data.split("\t") #split data by tab
try:
cursor=dbConn.cursor()
cursor.execute("INSERT INTO weatherdata(DateTime,WINDmph,HUMIDITY,RAINmm,TEMPERATUREc) VALUES (NOW(),%s,%s,%s,%s)",(pieces[0],pieces[1],pieces[2],pieces[3]))
dbConn.commit()
#commit the insert
cursor.close() #close the cursor
except MYSQLdb.IntegrityError:
print"failed to insert data"
finally:
cursor.close() # close incase it failed
except:
print"Processing!"