|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import time
|
|
|
|
import configparser
|
|
|
|
import logging
|
|
|
|
import bme280
|
|
|
|
try:
|
|
|
|
import smbus2
|
|
|
|
except ImportError: # Wenn smbus2 nicht zu importieren ist, nehmen wir smbus
|
|
|
|
from smbus import SMBus
|
|
|
|
import mysql.connector
|
|
|
|
|
|
|
|
port = 1
|
|
|
|
# Der BME280 ist auf Meerehöhe kalibriert, so dass der gelieferte Luftdruck
|
|
|
|
# vom tatsächlichen Druck am Ort abweicht. Dies müssen wir hier ausgleichen.
|
|
|
|
__luftdruck_kalibrierung__ = 296 # Kalibrierung auf ortsbezogenen Luftdruck
|
|
|
|
|
|
|
|
adresse_bme280 = 0x76
|
|
|
|
adresse_lcd1602 = 0x25
|
|
|
|
|
|
|
|
logging.basicConfig( format='%(asctime)s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
|
|
|
|
|
|
|
|
|
|
|
|
def info(msg):
|
|
|
|
logging.info(msg)
|
|
|
|
|
|
|
|
def debug(msg):
|
|
|
|
logging.debug(msg)
|
|
|
|
|
|
|
|
# Datenbankverbindung herstellen.
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read('db.ini')
|
|
|
|
mydb = mysql.connector.connect(host=config['weatherdb']['host'], user=config['weatherdb']['user'],password=config['weatherdb']['password'],database =config['weatherdb']['database'])
|
|
|
|
mycursor = mydb.cursor()
|
|
|
|
debug('Datenverbindung hergestellt.')
|
|
|
|
|
|
|
|
|
|
|
|
# BME280 initialisieren
|
|
|
|
bus = smbus2.SMBus(port)
|
|
|
|
calibration_params = bme280.load_calibration_params(bus, adresse_bme280)
|
|
|
|
debug('Sensor kalibriert.')
|
|
|
|
while True:
|
|
|
|
data = bme280.sample(bus, adresse_bme280, calibration_params)
|
|
|
|
|
|
|
|
|
|
|
|
temperature = data.temperature
|
|
|
|
pressure = data.pressure + __luftdruck_kalibrierung__
|
|
|
|
humidity = data.humidity
|
|
|
|
info('{: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)
|