From 81918fec280b9d3dfe785fbc78a64324f97e8f0c Mon Sep 17 00:00:00 2001 From: Olli Graf Date: Mon, 14 Aug 2023 10:31:54 +0200 Subject: [PATCH] Umstellung des BME280 Moduls auf pip install Rpi.bme280 --- bme280-python | 1 - wstat.py | 37 ++++++++++++++++++++----------------- 2 files changed, 20 insertions(+), 18 deletions(-) delete mode 160000 bme280-python diff --git a/bme280-python b/bme280-python deleted file mode 160000 index 3640070..0000000 --- a/bme280-python +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 36400706663c934aaebf5cfa37a132775b278ee3 diff --git a/wstat.py b/wstat.py index 11787b4..eaa5a31 100755 --- a/wstat.py +++ b/wstat.py @@ -1,11 +1,11 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import time +import bme280 try: - from smbus2 import SMBus + import smbus2 except ImportError: from smbus import SMBus -from bme280 import BME280 import mysql.connector print("""all-values.py - Read temperature, pressure, and humidity @@ -13,26 +13,29 @@ print("""all-values.py - Read temperature, pressure, and humidity Press Ctrl+C to exit! """) - +port = 1 +adresse = 0x76 # Initialise the BME280 -bus = SMBus(1) -bme280 = BME280(i2c_dev=bus) -mydb = mysql.connector.connect(host='lenny', user='ploeger',password='SW_Ep.6:ROTJ',database='weather') +bus = smbus2.SMBus(port) +calibration_params = bme280.load_calibration_params(bus, adresse) + +# Datenbankverbindung herstellen. +mydb = mysql.connector.connect(host='database', user='ploeger',password='SW_Ep.6:ROTJ',database='weather') mycursor = mydb.cursor() print(mydb) while True: - temperature = bme280.get_temperature() - pressure = bme280.get_pressure() - humidity = bme280.get_humidity() - print('{: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) + data = bme280.sample(bus, adresse, calibration_params) - mycursor.execute(sql, val) - mydb.commit() + temperature = data.temperature + pressure = data.pressure + humidity = data.humidity + print('{: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) - time.sleep(10) + mycursor.execute(sql, val) + mydb.commit() + time.sleep(10)