logging statt print()
This commit is contained in:
5
db.ini.sample
Normal file
5
db.ini.sample
Normal file
@@ -0,0 +1,5 @@
|
||||
[weatherdb]
|
||||
host=database
|
||||
user=ploeger
|
||||
password=<passwort>
|
||||
database=weather
|
39
wstat.py
39
wstat.py
@@ -1,37 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
import configparser
|
||||
import logging
|
||||
import bme280
|
||||
try:
|
||||
import smbus2
|
||||
except ImportError:
|
||||
except ImportError: # Wenn smbus2 nicht zu importieren ist, nehmen wir smbus
|
||||
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
|
||||
# Initialise the BME280
|
||||
bus = smbus2.SMBus(port)
|
||||
calibration_params = bme280.load_calibration_params(bus, adresse)
|
||||
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.
|
||||
mydb = mysql.connector.connect(host='database', user='ploeger',password='<passwort>',database='weather')
|
||||
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.')
|
||||
|
||||
print(mydb)
|
||||
|
||||
# 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, calibration_params)
|
||||
data = bme280.sample(bus, adresse_bme280, calibration_params)
|
||||
|
||||
|
||||
temperature = data.temperature
|
||||
pressure = data.pressure
|
||||
humidity = data.humidity
|
||||
print('{:05.2f}°C {:05.2f}hPa {:05.2f}%'.format(temperature, pressure, 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)
|
||||
|
Reference in New Issue
Block a user