|
|
@ -1,11 +1,11 @@ |
|
|
|
#!/usr/bin/env python |
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
|
|
|
import time |
|
|
|
import bme280 |
|
|
|
try: |
|
|
|
from smbus2 import SMBus |
|
|
|
import smbus2 |
|
|
|
except ImportError: |
|
|
|
from smbus import SMBus |
|
|
|
from bme280 import BME280 |
|
|
|
import mysql.connector |
|
|
|
|
|
|
|
print("""all-values.py - Read temperature, pressure, and humidity |
|
|
@ -13,26 +13,29 @@ print("""all-values.py - Read temperature, pressure, and humidity |
|
|
|
Press Ctrl+C to exit! |
|
|
|
|
|
|
|
""") |
|
|
|
|
|
|
|
port = 1 |
|
|
|
adresse = 0x76 |
|
|
|
# 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') |
|
|
|
bus = smbus2.SMBus(port) |
|
|
|
calibration_params = bme280.load_calibration_params(bus, adresse) |
|
|
|
|
|
|
|
# Datenbankverbindung herstellen. |
|
|
|
mydb = mysql.connector.connect(host='database', 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) |
|
|
|
data = bme280.sample(bus, adresse, calibration_params) |
|
|
|
|
|
|
|
mycursor.execute(sql, val) |
|
|
|
mydb.commit() |
|
|
|
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) |
|
|
|
|
|
|
|
time.sleep(10) |
|
|
|
mycursor.execute(sql, val) |
|
|
|
mydb.commit() |
|
|
|
time.sleep(10) |
|
|
|