diff --git a/BME280/get_data_v2.py b/BME280/get_data_v2.py new file mode 100644 index 0000000..33c3331 --- /dev/null +++ b/BME280/get_data_v2.py @@ -0,0 +1,43 @@ +''' + ____ __ __ _____ ____ ___ ___ + | __ )| \/ | ____|___ \( _ ) / _ \ + | _ \| |\/| | _| __) / _ \| | | | + | |_) | | | | |___ / __/ (_) | |_| | + |____/|_| |_|_____|_____\___/ \___/ + +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 + +from adafruit_bme280 import basic as adafruit_bme280 + +# 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") + +sensor_data = { + "temp": round(bme280.temperature, 2), # Temperature in °C + "hum": round(bme280.humidity, 2), # Humidity in % + "press": round(bme280.pressure, 2), # Pressure in hPa +} + + +# Convert to JSON and print +print(json.dumps(sensor_data, indent=4)) \ No newline at end of file diff --git a/NPM/get_data_v2.py b/NPM/get_data_v2.py index ab7321e..577362f 100644 --- a/NPM/get_data_v2.py +++ b/NPM/get_data_v2.py @@ -1,12 +1,12 @@ ''' - ____ _____ _ _ ____ ___ ____ ____ - / ___|| ____| \ | / ___| / _ \| _ \/ ___| - \___ \| _| | \| \___ \| | | | |_) \___ \ - ___) | |___| |\ |___) | |_| | _ < ___) | - |____/|_____|_| \_|____/ \___/|_| \_\____/ - - -Script to get SENSORS values + _ _ ____ __ __ + | \ | | _ \| \/ | + | \| | |_) | |\/| | + | |\ | __/| | | | + |_| \_|_| |_| |_| + +Script to get NPM values +PM and the sensor temp/hum And store them inside sqlite database Uses RTC module for timing /usr/bin/python3 /var/www/nebuleair_pro_4g/NPM/get_data_v2.py @@ -72,70 +72,58 @@ ser = serial.Serial( #ser.write(b'\x81\x11\x6E') #data10s ser.write(b'\x81\x12\x6D') #data60s -while True: - try: - #print("Start get_data_v2.py script") - byte_data = ser.readline() - #print(byte_data) - stateByte = int.from_bytes(byte_data[2:3], byteorder='big') - Statebits = [int(bit) for bit in bin(stateByte)[2:].zfill(8)] - PM1 = int.from_bytes(byte_data[9:11], byteorder='big')/10 - PM25 = int.from_bytes(byte_data[11:13], byteorder='big')/10 - PM10 = int.from_bytes(byte_data[13:15], byteorder='big')/10 - #print(f"State: {Statebits}") - #print(f"PM1: {PM1}") - #print(f"PM25: {PM25}") - #print(f"PM10: {PM10}") - #create JSON - data = { - 'PM1': PM1, - 'PM25': PM25, - 'PM10': PM10, - 'sleep' : Statebits[0], - 'degradedState' : Statebits[1], - 'notReady' : Statebits[2], - 'heatError' : Statebits[3], - 't_rhError' : Statebits[4], - 'fanError' : Statebits[5], - 'memoryError' : Statebits[6], - 'laserError' : Statebits[7] - } - json_data = json.dumps(data) - #print(json_data) - #GET RTC TIME - # Read RTC time - bus = smbus2.SMBus(1) - # Try to read RTC time - rtc_time = read_time(bus) +#print("Start get_data_v2.py script") +byte_data = ser.readline() +#print(byte_data) +stateByte = int.from_bytes(byte_data[2:3], byteorder='big') +Statebits = [int(bit) for bit in bin(stateByte)[2:].zfill(8)] +PM1 = int.from_bytes(byte_data[9:11], byteorder='big')/10 +PM25 = int.from_bytes(byte_data[11:13], byteorder='big')/10 +PM10 = int.from_bytes(byte_data[13:15], byteorder='big')/10 - if rtc_time: - rtc_time_str = rtc_time.strftime('%Y-%m-%d %H:%M:%S') - #print(rtc_time_str) - else: - print("Error! RTC module not connected") - rtc_time_str = "1970-01-01 00:00:00" # Default fallback time +# Write command to retrieve temperature and humidity data +ser.write(b'\x81\x14\x6B') # Temp and humidity command +byte_data_temp_hum = ser.readline() + +# Decode temperature and humidity values +temperature = int.from_bytes(byte_data_temp_hum[3:5], byteorder='big') / 100.0 +humidity = int.from_bytes(byte_data_temp_hum[5:7], byteorder='big') / 100.0 + +#print(f"State: {Statebits}") +#print(f"PM1: {PM1}") +#print(f"PM25: {PM25}") +#print(f"PM10: {PM10}") +#print(f"temp: {temperature}") +#print(f"hum: {humidity}") +#GET RTC TIME +# Read RTC time +bus = smbus2.SMBus(1) +# Try to read RTC time +rtc_time = read_time(bus) + +if rtc_time: + rtc_time_str = rtc_time.strftime('%Y-%m-%d %H:%M:%S') + #print(rtc_time_str) +else: + print("Error! RTC module not connected") + rtc_time_str = "1970-01-01 00:00:00" # Default fallback time - #save to sqlite database - cursor.execute(''' - INSERT INTO data (timestamp,PM1, PM25, PM10) VALUES (?,?,?,?)''' - , (rtc_time_str,PM1,PM25,PM10)) +#save to sqlite database +try: + cursor.execute(''' + INSERT INTO data_NPM (timestamp,PM1, PM25, PM10, temp_npm, hum_npm) VALUES (?,?,?,?,?,?)''' + , (rtc_time_str,PM1,PM25,PM10,temperature,humidity )) - # Commit and close the connection - conn.commit() + # Commit and close the connection + conn.commit() - #print("Sensor data saved successfully!") + #print("Sensor data saved successfully!") + +except Exception as e: + print(f"Database error: {e}") - break # Exit loop after successful execution - except KeyboardInterrupt: - print("User interrupt encountered. Exiting...") - time.sleep(3) - exit() - except Exception as e: - print(f"Error: {e}") # Show actual error - time.sleep(3) - exit() conn.close() diff --git a/html/launcher.php b/html/launcher.php index b5b7a04..e698ab6 100755 --- a/html/launcher.php +++ b/html/launcher.php @@ -14,7 +14,7 @@ if ($type == "get_npm_sqlite_data") { $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Fetch the last 30 records - $stmt = $db->query("SELECT timestamp, PM1, PM25, PM10 FROM data ORDER BY timestamp DESC LIMIT 30"); + $stmt = $db->query("SELECT timestamp, PM1, PM25, PM10 FROM data_NPM ORDER BY timestamp DESC LIMIT 30"); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); $reversedData = array_reverse($data); // Reverse the order diff --git a/loop/SARA_send_data_v2.py b/loop/SARA_send_data_v2.py index 8f64685..e7f2e2b 100644 --- a/loop/SARA_send_data_v2.py +++ b/loop/SARA_send_data_v2.py @@ -47,6 +47,8 @@ CSV PAYLOAD (AirCarto Servers) 15 -> PM 1.0μm to 2.5μm quantity (Nb/L) 16 -> PM 2.5μm to 5.0μm quantity (Nb/L) 17 -> PM 5.0μm to 10μm quantity (Nb/L) + 18 -> NPM temp inside + 19 -> NPM hum inside JSON PAYLOAD (Micro-Spot Servers) Same as NebuleAir wifi @@ -111,7 +113,7 @@ if uptime_seconds < 120: sys.exit() #Payload CSV to be sent to data.nebuleair.fr -payload_csv = [None] * 20 +payload_csv = [None] * 25 #Payload JSON to be sent to uSpot payload_json = { "nebuleairid": "XXX", @@ -253,15 +255,39 @@ def read_complete_response(serial_connection, timeout=2, end_of_response_timeout return response.decode('utf-8', errors='replace') try: + ''' + _ ___ ___ ____ + | | / _ \ / _ \| _ \ + | | | | | | | | | |_) | + | |__| |_| | |_| | __/ + |_____\___/ \___/|_| + + ''' print('
') + print(response_SARA_3) + print("
") + + # si on recoit la réponse UHTTPCR + if "+UUHTTPCR" in response_SARA_3: + print("✅ Received +UUHTTPCR response.") + + # Les types de réponse + + # 1.La commande n'a pas fonctionné + # +CME ERROR: No connection to phone + # +CME ERROR: Operation not allowed + + # 2.La commande fonctionne: elle renvoie un code + # +UUHTTPCR:') + print(response_SARA_9) + print("
") + + ''' + +UHTTPER: profile_id,error_class,error_code + + error_class + 0 OK, no error + 3 HTTP Protocol error class + 10 Wrong HTTP API USAGE + + error_code (for error_class 3) + 0 No error + 11 Server connection error + 73 Secure socket connect error + ''' + + #Pas forcément un moyen de résoudre le soucis + #print("resetting the URL (domain name):") + #command = f'AT+UHTTP={aircarto_profile_id},1,"{url_nebuleair}"\r' + #ser_sara.write(command.encode('utf-8')) + #response_SARA_31 = read_complete_response(ser_sara) + #print(response_SARA_31) + + # 2.2 code 1 (HHTP succeded) + else: + # Si la commande HTTP a réussi + print('HTTP operation successful.') + update_json_key(config_file, "SARA_R4_network_status", "connected") + print("Blink blue LED") + led_thread = Thread(target=blink_led, args=(23, 5, 0.5)) + led_thread.start() + #4. Read reply from server + print("Reply from server:") + ser_sara.write(b'AT+URDFILE="server_response.txt"\r') + response_SARA_4 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) + print('') + print(response_SARA_4) + print('
') + else: + print('No UUHTTPCR response') + print("Blink red LED") + # Run LED blinking in a separate thread + led_thread = Thread(target=blink_led, args=(24, 5, 0.5)) + led_thread.start() + + + #5. empty json + print("Empty SARA memory:") + ser_sara.write(b'AT+UDELFILE="sensordata_csv.json"\r') + response_SARA_5 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) + print(response_SARA_5) diff --git a/sqlite/read.py b/sqlite/read.py index 947fe6a..ba8f5ec 100755 --- a/sqlite/read.py +++ b/sqlite/read.py @@ -17,7 +17,7 @@ conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") cursor = conn.cursor() # Retrieve the last 10 sensor readings -cursor.execute("SELECT * FROM data ORDER BY timestamp DESC LIMIT 10") +cursor.execute("SELECT * FROM data_NPM ORDER BY timestamp DESC LIMIT 10") rows = cursor.fetchall() rows.reverse() # Reverse the order in Python (to get ascending order)