You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
901 B
39 lines
901 B
1 year ago
|
#!/usr/bin/env python
|
||
|
|
||
|
import time
|
||
|
try:
|
||
|
from smbus2 import SMBus
|
||
|
except ImportError:
|
||
|
from smbus import SMBus
|
||
|
from bme280 import BME280
|
||
|
import mysql.connector
|
||
|
|
||
|
print("""all-values.py - Read temperature, pressure, and humidity
|
||
|
|
||
|
Press Ctrl+C to exit!
|
||
|
|
||
|
""")
|
||
|
|
||
|
# Initialise the BME280
|
||
|
bus = SMBus(1)
|
||
|
bme280 = BME280(i2c_dev=bus)
|
||
|
mydb = mysql.connector.connect(host='lenny', user='ploeger',password='SW_Ep.6:ROTJ',database='weather')
|
||
|
mycursor = mydb.cursor()
|
||
|
|
||
|
print(mydb)
|
||
|
|
||
|
while True:
|
||
|
temperature = bme280.get_temperature()
|
||
|
pressure = bme280.get_pressure()
|
||
|
humidity = bme280.get_humidity()
|
||
|
print('{:05.2f}*C {:05.2f}hPa {:05.2f}%'.format(temperature, pressure, humidity))
|
||
|
|
||
|
sql = 'INSERT into stats (datum,temperature, humidity, pressure) VALUES(now(), %s, %s, %s)'
|
||
|
val = (temperature, humidity, pressure)
|
||
|
|
||
|
mycursor.execute(sql, val)
|
||
|
mydb.commit()
|
||
|
|
||
|
|
||
|
time.sleep(10)
|