So I am having an issue where I believe the py program is connecting, but the data is not in the tables/database as recorded. I realize my code is crappy, I am flying a little blind.
I have apache2, MariaDB, and PHPMyAdmin through the raspberry pi.
I am trying to have the sensor read the temp/humidity every 5 mins and then log the value in the database. I hadn't got to the 5 minutes yet, I was just looking to ensure the data is being recorded first. I am using Thonny on the pi and trawling the internet for solutions
I have a database called "sensor" with the table "sensordht" with the Columns: ID, timestamp, temperature, and humidity.
If anyone could help me with the input data/how to input data every 5 mins, that would be ace.
Help me, internet wizards, you're my only hope.
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() ```
question from:
https://stackoverflow.com/questions/65557571/sensor-data-input-into-mysql-via-python-raspberry-pi 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…