Browse Source

logging statt print()

master 0.3
Olli Graf 1 year ago
parent
commit
12d5a054f5
  1. 5
      db.ini.sample
  2. 37
      wstat.py

5
db.ini.sample

@ -0,0 +1,5 @@
[weatherdb]
host=database
user=ploeger
password=<passwort>
database=weather

37
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
port = 1
adresse_bme280 = 0x76
adresse_lcd1602 = 0x25
Press Ctrl+C to exit!
logging.basicConfig( format='%(asctime)s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
""")
port = 1
adresse = 0x76
# Initialise the BME280
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, adresse)
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)

Loading…
Cancel
Save