Umstellung des BME280 Moduls auf

pip install Rpi.bme280
This commit is contained in:
2023-08-14 10:31:54 +02:00
parent 414d668e77
commit 81918fec28
2 changed files with 20 additions and 18 deletions

Submodule bme280-python deleted from 3640070666

View File

@@ -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))
data = bme280.sample(bus, adresse, calibration_params)
sql = 'INSERT into stats (datum,temperature, humidity, pressure) VALUES(now(), %s, %s, %s)'
val = (temperature, humidity, pressure)
temperature = data.temperature
pressure = data.pressure
humidity = data.humidity
print('{:05.2f}°C {:05.2f}hPa {:05.2f}%'.format(temperature, pressure, humidity))
mycursor.execute(sql, val)
mydb.commit()
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)