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.
41 lines
990 B
41 lines
990 B
#!/usr/bin/env python3
|
|
|
|
import time
|
|
import bme280
|
|
try:
|
|
import smbus2
|
|
except ImportError:
|
|
from smbus import SMBus
|
|
import mysql.connector
|
|
|
|
print("""all-values.py - Read temperature, pressure, and humidity
|
|
|
|
Press Ctrl+C to exit!
|
|
|
|
""")
|
|
port = 1
|
|
adresse = 0x76
|
|
# Initialise the BME280
|
|
bus = smbus2.SMBus(port)
|
|
calibration_params = bme280.load_calibration_params(bus, adresse)
|
|
|
|
# Datenbankverbindung herstellen.
|
|
mydb = mysql.connector.connect(host='database', user='ploeger',password='<passwort>',database='weather')
|
|
mycursor = mydb.cursor()
|
|
|
|
print(mydb)
|
|
|
|
while True:
|
|
data = bme280.sample(bus, adresse, calibration_params)
|
|
|
|
temperature = data.temperature
|
|
pressure = data.pressure
|
|
humidity = data.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)
|
|
|