''' ____ __ __ _____ ____ ___ ___ | __ )| \/ | ____|___ \( _ ) / _ \ | _ \| |\/| | _| __) / _ \| | | | | |_) | | | | |___ / __/ (_) | |_| | |____/|_| |_|_____|_____\___/ \___/ Script to read data from BME280 Sensor connected to i2c on address 76 (use sudo i2cdetect -y 1 to get the address ) -> save data to database (table data_BME280 ) sudo python3 /var/www/nebuleair_pro_4g/BME280/get_data_v2.py ''' import board import busio import json import sqlite3 from adafruit_bme280 import basic as adafruit_bme280 # Connect to the SQLite database conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") cursor = conn.cursor() # Create I2C bus i2c = busio.I2C(board.SCL, board.SDA) bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76) # Configure settings bme280.sea_level_pressure = 1013.25 # Update this value for your location # Read sensor data #print(f"Temperature: {bme280.temperature:.2f} °C") #print(f"Humidity: {bme280.humidity:.2f} %") #print(f"Pressure: {bme280.pressure:.2f} hPa") #print(f"Altitude: {bme280.altitude:.2f} m") temperature = round(bme280.temperature, 2) humidity = round(bme280.humidity, 2) pressure = round(bme280.pressure, 2) sensor_data = { "temp": temperature, # Temperature in °C "hum": humidity, # Humidity in % "press": pressure # Pressure in hPa } #GET RTC TIME from SQlite cursor.execute("SELECT * FROM timestamp_table LIMIT 1") row = cursor.fetchone() # Get the first (and only) row rtc_time_str = row[1] # '2025-02-07 12:30:45' # Convert to JSON and print #print(json.dumps(sensor_data, indent=4)) #save to sqlite database try: cursor.execute(''' INSERT INTO data_BME280 (timestamp,temperature, humidity, pressure) VALUES (?,?,?,?)''' , (rtc_time_str,temperature,humidity,pressure)) # Commit and close the connection conn.commit() #print("Sensor data saved successfully!") except Exception as e: print(f"Database error: {e}") conn.close()