所以我遇到了一个问题,我认为 py 程序正在连接,但数据不在记录的表/数据库中。我意识到我的代码很糟糕,我有点盲目。 我通过树莓派安装了 apache2、MariaDB 和 PHPMyAdmin。 我试图让传感器每 5 分钟读取一次温度/湿度,然后将值记录到数据库中。我还没有到 5 分钟,我只是想确保首先记录数据。我在 pi 上使用 Thonny 并在互联网上搜索解决方案
我有一个名为“sensor”的数据库,其中包含“sensordht”表和列:ID、时间戳、温度和湿度。
如果有人能帮我输入数据/如何每 5 分钟输入一次数据,那就太好了。 帮助我,互联网向导,你是我唯一的希望。
import time
import sys
import Adafruit_DHT
import MySQLdb
import datetime
import os
from time import strftime
timestamp = time.strftime("%d-%m-%Y @ %H:%M:%S")
sensor = Adafruit_DHT.DHT11
sensor_pin = 4
running = True
humidity, temperature = Adafruit_DHT.read_retry(11, 4)
conn = MySQLdb.connect(host= "localhost",user= "root",passwd="Hummus9",db="sensor")
c = conn.cursor()
try:
if humidity is None and temperature is None:
c.execute("INSERT INTO sensordht(timestamp, temperature, humidity) VALUES (%s, %s, %s)",(timestamp, temperature, humidity))
conn.commit()
c.close
else:
print('Failed to get reading')
sys.exit(1)
except KeyboardInterrupt:
print ('Program stopped')
running = False
file.close() ```