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.

67 lines
1.8 KiB

1 year ago
#!/usr/bin/env python3
# encoding: utf-8
import time
import configparser
import logging
import bme280
import LCD1602
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)
def print_weather(temperature, humidity, pressure):
lcd.setCursor(0, 0)
lcd.printout('{:05.2f}\223C'.format(temperature)
lcd.printout(' {:05.2f}hPa'.format(pressure))
lcd.setCursor(0, 1)
lcd.printout('{:05.2f}%'.format(humidity))
# 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.')
#LCD1602 initialisieren
lcd=LCD1602.LCD1602(16,2)
# BME280 initialisieren
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, adresse_bme280)
debug('Sensor kalibriert.')
try:
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))
print_weather(temperature,humidity, pressure)
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)
except(KeyboardInterrupt):
mydb.close()
lcd.clear()
del lcd