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.

52 lines
1.4 KiB

#!/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
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
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)