Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

Sensor Data Input into MySQL via Python, raspberry pi

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...