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.

42 lines
990 B

#!/usr/bin/env python3
1 year ago
import time
import bme280
1 year ago
try:
import smbus2
1 year ago
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
1 year ago
# Initialise the BME280
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, adresse)
# Datenbankverbindung herstellen.
1 year ago
mydb = mysql.connector.connect(host='database', user='ploeger',password='<passwort>',database='weather')
1 year ago
mycursor = mydb.cursor()
print(mydb)
while True:
data = bme280.sample(bus, adresse, calibration_params)
1 year ago
temperature = data.temperature
pressure = data.pressure
humidity = data.humidity
print('{:05.2f}°C {:05.2f}hPa {:05.2f}%'.format(temperature, pressure, humidity))
1 year ago
sql = 'INSERT into stats (datum,temperature, humidity, pressure) VALUES(now(), %s, %s, %s)'
val = (temperature, humidity, pressure)
1 year ago
mycursor.execute(sql, val)
mydb.commit()
time.sleep(10)