以防对其他人有帮助。。
我让操作符通过指定检索到的值的类型来工作。
我已更改以下内容:
def query():
# CONNECTION TO THE DB.
connection = pymysql.connect(host=host,
user=user,
password=pass1,
db=database,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor:
logf.write(dateandtime + "- 3 - Getting the sensor value\n")
# Fetch the sensor name
sql = "SELECT temp FROM expected_temp WHERE name = (select name from sensor where id = %s)"
cursor.execute(sql, (sens,))
result = cursor.fetchall()
for row in result:
na = "%s" % (row["temp"])
nam = str(na)
#nam = float(na)
#n = nam.rstrip()
return int(nam)
if __name__ == '__main__':
#Setup the GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17,GPIO.OUT)
try:
while True:
humidity, temperature = readAdafruitDHT('2302',17)
logf.write(dateandtime + "- 2 - Reading temperature\n")
target = query()
print target
print (dateandtime, target)
if ( temperature > target ):
print (dateandtime + ' - Current temperature: %f' % temperature)
logf.write(dateandtime + " - 3 - Current temperature: %f" % temperature + "\n")
print (dateandtime + ' - Changing to HIGH')
logf.write(dateandtime + ' - Changing to HIGH\n')
GPIO.output(18,GPIO.HIGH)
else:
print (dateandtime + ' - Current temperature: %f' % temperature)
logf.write(dateandtime + " - 3 - Current temperature: %f" % temperature + "\n")
print (dateandtime + ' - Changing to LOW')
logf.write(dateandtime + ' - Changing to LOW\n')
GPIO.output(18,GPIO.LOW)
time.sleep(20)
except KeyboardInterrupt:
GPIO.cleanup()
print("Bye")
无论如何,谢谢你。
乔。