''' Live value for the web "Get Data" button (CCS811 air-quality sensor). Prints {"eCO2": , "TVOC": , "timestamp": } or {"error": ""}. IMPORTANT: this does NOT read the I2C sensor. The CCS811 is owned by the long-running daemon (CCS811/daemon.py); opening the bus here would collide with it and corrupt the sensor. Instead we return the most recent row the daemon stored in data_CCS811. TVOC is the primary measurement. Usage: /usr/bin/python3 /var/www/nebuleair_pro_4g/CCS811/get_data.py ''' import json import sqlite3 DB_PATH = "/var/www/nebuleair_pro_4g/sqlite/sensors.db" def main(): try: conn = sqlite3.connect(DB_PATH, timeout=5) cursor = conn.cursor() cursor.execute( "SELECT timestamp, eCO2, TVOC FROM data_CCS811 ORDER BY timestamp DESC LIMIT 1" ) row = cursor.fetchone() conn.close() except Exception as e: print(json.dumps({"error": f"DB read error: {e}"})) return if not row: print(json.dumps({"error": "No CCS811 data yet (daemon warming up?)"})) return print(json.dumps({"timestamp": row[0], "eCO2": int(row[1]), "TVOC": int(row[2])})) if __name__ == "__main__": main()