17 Commits
v1.5.0 ... main

Author SHA1 Message Date
PaulVua
a68af89612 Page modem: retour visuel OK/erreur sur boutons LED status PCB
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 11:42:23 +02:00
PaulVua
7045adc7a6 v1.6.4: Page modem — progression reset hardware + boutons LED status PCB
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 11:29:02 +02:00
PaulVua
c062263b24 v1.6.3: Logs SARA en temps reel — auto-refresh + python3 -u unbuffered
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 18:36:54 +02:00
PaulVua
9f76e3b2de v1.6.2: Simplification script boot SARA — config modem deleguee au script principal
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 17:15:24 +01:00
PaulVua
0ed18dd5c1 Fix: bandeau mode configuration affiche a tort (d-flex !important ecrasait display:none)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 14:03:14 +01:00
PaulVua
cf10d20db5 Page modem: reset hardware GPIO 16 + alerte mode configuration + reset au boot
- Bouton Reset Hardware (GPIO 16) avec verification ATI apres redemarrage
- Bandeau d'alerte rouge quand mode configuration actif (transmission desactivee)
- Reset automatique de modem_config_mode a 0 au boot (SARA/reboot/start.py)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 13:43:45 +01:00
PaulVua
3f7d0c0816 Page database: affichage noise_status dans tableau et export CSV
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 11:45:01 +01:00
PaulVua
47d76be5df v1.6.1: NSRT MK4 deconnexion — noise_status en base + ERR_NOISE dans error_flags
Meme modele que NPM: ecriture en base avec valeurs a 0 et noise_status=0xFF
si capteur deconnecte, flag ERR_NOISE (bit 5) dans byte 66 UDP, messages
explicites sur page capteurs et self-test.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 11:42:10 +01:00
PaulVua
11585b4783 Error flags: NPM deconnecte (0xFF) → ERR_NPM bit 3 dans byte 66
- npm_status 0xFF = pas de reponse du capteur → flag ERR_NPM (byte 66 bit 3)
  et byte 67 reste a 0x00 (pas de status valide a transmettre)
- npm_status valide → byte 67 tel quel, pas de flag dans byte 66

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 16:38:56 +01:00
PaulVua
52b86dbc3d NPM 0xFF = capteur deconnecte sur page sensors et self-test
Quand npm_status = 0xFF (aucune reponse du capteur), affiche
"Capteur deconnecte" au lieu de lister tous les flags d'erreur.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 16:31:23 +01:00
PaulVua
361c0d1a76 Self-test NPM: decodage npm_status au lieu des anciens champs erreur
Adapte le self-test au nouveau format retourne par get_data_modbus_v3.py
(npm_status numerique decode bit par bit au lieu de notReady/fanError/etc.)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 15:12:50 +01:00
PaulVua
bd2e1f1eda v1.6.0: envoi npm_status dans payload UDP (byte 67)
- Lecture npm_status depuis derniere mesure en base (rowid DESC, pas de moyenne)
- Independant du RTC (pas de dependance au timestamp)
- Byte 67 du payload UDP = registre status NextPM

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 13:24:26 +01:00
PaulVua
2b4e9205c1 Fix: dry-run NPM silencieux (suppression print qui cassaient le JSON)
En mode --dry-run, les print d'erreur/warning/status sont desactives
pour que seul le JSON soit envoye en sortie standard.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 13:12:29 +01:00
PaulVua
b3c019c27b v1.5.2: page capteurs NPM via get_data_modbus_v3.py --dry-run
- NPM: mode --dry-run (print JSON sans ecriture en base)
- launcher.php: endpoint npm appelle get_data_modbus_v3.py --dry-run
- sensors.html: affichage PM + temp + humidite + status NPM decode
- Suppression unite ug/m3 sur le champ status

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 13:10:56 +01:00
PaulVua
e733cd27e8 Doc: parser Miotiq dans README + mise a jour error_flags.md
- README: ajout section parser Miotiq avec firmware version (bytes 69-71)
- error_flags.md: parser mis a jour (version_major/minor/patch + reserved 22)
- error_flags.md: correction note init bytes 66-68 a 0x00

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 11:46:32 +01:00
PaulVua
a9db7750b2 v1.5.1: envoi firmware version dans payload UDP (bytes 69-71)
- Lecture fichier VERSION et pack major.minor.patch dans bytes 69-71
- README: documentation complete structure 100 bytes + conso data
- Changelog mis a jour

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 11:44:24 +01:00
PaulVua
c42656e0ae gitignore: ajout .env pour exclure les secrets
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 11:03:47 +01:00
20 changed files with 812 additions and 379 deletions

3
.gitignore vendored
View File

@@ -18,5 +18,8 @@ sqlite/*.sql
tests/ tests/
# Secrets
.env
# Claude Code local settings # Claude Code local settings
.claude/settings.local.json .claude/settings.local.json

View File

@@ -40,6 +40,9 @@ import crcmod
import sqlite3 import sqlite3
import time import time
# Dry-run mode: print JSON output without writing to database
dry_run = "--dry-run" in sys.argv
# Connect to the SQLite database # Connect to the SQLite database
conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db")
cursor = conn.cursor() cursor = conn.cursor()
@@ -110,6 +113,7 @@ try:
# Validate response length # Validate response length
if len(byte_data) < response_length: if len(byte_data) < response_length:
if not dry_run:
print(f"[ERROR] Incomplete response received: {byte_data.hex()}") print(f"[ERROR] Incomplete response received: {byte_data.hex()}")
raise Exception("Incomplete response") raise Exception("Incomplete response")
@@ -118,6 +122,7 @@ try:
calculated_crc = crc16(byte_data[:-2]) calculated_crc = crc16(byte_data[:-2])
if received_crc != calculated_crc: if received_crc != calculated_crc:
if not dry_run:
print("[ERROR] CRC check failed! Corrupted data received.") print("[ERROR] CRC check failed! Corrupted data received.")
raise Exception("CRC check failed") raise Exception("CRC check failed")
@@ -194,20 +199,37 @@ try:
status_calc_crc = crc16(status_response[:-2]) status_calc_crc = crc16(status_response[:-2])
if status_recv_crc == status_calc_crc: if status_recv_crc == status_calc_crc:
npm_status = int.from_bytes(status_response[3:5], byteorder='big') & 0xFF npm_status = int.from_bytes(status_response[3:5], byteorder='big') & 0xFF
if not dry_run:
print(f"NPM status: 0x{npm_status:02X} ({npm_status})") print(f"NPM status: 0x{npm_status:02X} ({npm_status})")
else: else:
if not dry_run:
print("[WARNING] NPM status CRC check failed, keeping default") print("[WARNING] NPM status CRC check failed, keeping default")
else: else:
if not dry_run:
print(f"[WARNING] NPM status incomplete response ({len(status_response)} bytes)") print(f"[WARNING] NPM status incomplete response ({len(status_response)} bytes)")
ser.close() ser.close()
except Exception as e: except Exception as e:
if not dry_run:
print(f"[ERROR] Sensor communication failed: {e}") print(f"[ERROR] Sensor communication failed: {e}")
# Variables already set to -1 at the beginning # Variables already set to -1 at the beginning
finally: finally:
# Always save data to database, even if all values are -1 if dry_run:
# Print JSON output without writing to database
result = {
"PM1": pm1_10s,
"PM25": pm25_10s,
"PM10": pm10_10s,
"temperature": temperature,
"humidity": relative_humidity,
"npm_status": npm_status,
"npm_status_hex": f"0x{npm_status:02X}"
}
print(json.dumps(result))
else:
# Always save data to database, even if all values are 0
cursor.execute(''' cursor.execute('''
INSERT INTO data_NPM_5channels (timestamp,PM_ch1, PM_ch2, PM_ch3, PM_ch4, PM_ch5) VALUES (?,?,?,?,?,?)''' INSERT INTO data_NPM_5channels (timestamp,PM_ch1, PM_ch2, PM_ch3, PM_ch4, PM_ch5) VALUES (?,?,?,?,?,?)'''
, (rtc_time_str, channel_1, channel_2, channel_3, channel_4, channel_5)) , (rtc_time_str, channel_1, channel_2, channel_3, channel_4, channel_5))
@@ -218,4 +240,5 @@ finally:
# Commit and close the connection # Commit and close the connection
conn.commit() conn.commit()
conn.close() conn.close()

140
README.md
View File

@@ -181,6 +181,146 @@ And set the base URL for Sara R4 communication:
``` ```
## UDP Payload Miotiq — Structure 100 bytes
| Bytes | Taille | Nom | Format | Description |
|-------|--------|-----|--------|-------------|
| 0-7 | 8 | device_id | ASCII | Identifiant unique du capteur |
| 8 | 1 | signal_quality | uint8 | Qualite signal modem (AT+CSQ) |
| 9 | 1 | protocol_version | uint8 | Version protocole (0x01) |
| 10-11 | 2 | pm1 | uint16 BE | PM1.0 en ug/m3 (x10) |
| 12-13 | 2 | pm25 | uint16 BE | PM2.5 en ug/m3 (x10) |
| 14-15 | 2 | pm10 | uint16 BE | PM10 en ug/m3 (x10) |
| 16-17 | 2 | temperature | int16 BE | Temperature en C (x100, signe) |
| 18-19 | 2 | humidity | uint16 BE | Humidite en % (x100) |
| 20-21 | 2 | pressure | uint16 BE | Pression en hPa |
| 22-23 | 2 | noise_cur_leq | uint16 BE | Bruit LEQ en dB(A) (x10) |
| 24-25 | 2 | noise_cur_level | uint16 BE | Bruit instantane en dB(A) (x10) |
| 26-27 | 2 | noise_max | uint16 BE | Bruit max en dB(A) (x10) |
| 28-29 | 2 | envea_no2 | uint16 BE | NO2 en ppb |
| 30-31 | 2 | envea_h2s | uint16 BE | H2S en ppb |
| 32-33 | 2 | envea_nh3 | uint16 BE | NH3 en ppb |
| 34-35 | 2 | envea_co | uint16 BE | CO en ppb |
| 36-37 | 2 | envea_o3 | uint16 BE | O3 en ppb |
| 38-39 | 2 | npm_ch1 | uint16 BE | NPM canal 1 (5-channel) |
| 40-41 | 2 | npm_ch2 | uint16 BE | NPM canal 2 (5-channel) |
| 42-43 | 2 | npm_ch3 | uint16 BE | NPM canal 3 (5-channel) |
| 44-45 | 2 | npm_ch4 | uint16 BE | NPM canal 4 (5-channel) |
| 46-47 | 2 | npm_ch5 | uint16 BE | NPM canal 5 (5-channel) |
| 48-49 | 2 | mppt_temperature | int16 BE | Temperature MPPT en C (x10, signe) |
| 50-51 | 2 | mppt_humidity | uint16 BE | Humidite MPPT en % (x10) |
| 52-53 | 2 | battery_voltage | uint16 BE | Tension batterie en V (x100) |
| 54-55 | 2 | battery_current | int16 BE | Courant batterie en A (x100, signe) |
| 56-57 | 2 | solar_voltage | uint16 BE | Tension solaire en V (x100) |
| 58-59 | 2 | solar_power | uint16 BE | Puissance solaire en W |
| 60-61 | 2 | charger_status | uint16 BE | Status chargeur MPPT |
| 62-63 | 2 | wind_speed | uint16 BE | Vitesse vent en m/s (x10) |
| 64-65 | 2 | wind_direction | uint16 BE | Direction vent en degres |
| 66 | 1 | error_flags | uint8 | Erreurs systeme (voir detail) |
| 67 | 1 | npm_status | uint8 | Registre status NextPM |
| 68 | 1 | device_status | uint8 | Etat general du boitier |
| 69 | 1 | version_major | uint8 | Version firmware major |
| 70 | 1 | version_minor | uint8 | Version firmware minor |
| 71 | 1 | version_patch | uint8 | Version firmware patch |
| 72-99 | 28 | reserved | — | Reserve (initialise a 0xFF) |
### Consommation data (UDP Miotiq uniquement)
Taille par paquet : 100 bytes payload + 8 bytes UDP header + 20 bytes IP header = **128 bytes**
| | Toutes les 60s | Toutes les 10s |
|---|---|---|
| Paquets/jour | 1 440 | 8 640 |
| Par jour | ~180 KB | ~1.08 MB |
| Par mois | ~5.3 MB | ~32.4 MB |
| Par an | ~63.6 MB | ~388.8 MB |
> Note : ces chiffres ne comptent que l'UDP vers Miotiq. Les envois HTTP (AirCarto) et HTTPS (uSpot) consomment des donnees supplementaires.
### Parser Miotiq
```
16|device_id|string|||W
2|signal_quality|hex2dec|dB||
2|version|hex2dec|||W
4|ISO_68|hex2dec|ugm3|x/10|
4|ISO_39|hex2dec|ugm3|x/10|
4|ISO_24|hex2dec|ugm3|x/10|
4|ISO_54|hex2dec|degC|x/100|
4|ISO_55|hex2dec|%|x/100|
4|ISO_53|hex2dec|hPa||
4|noise_cur_leq|hex2dec|dB|x/10|
4|noise_cur_level|hex2dec|dB|x/10|
4|max_noise|hex2dec|dB|x/10|
4|ISO_03|hex2dec|ppb||
4|ISO_05|hex2dec|ppb||
4|ISO_21|hex2dec|ppb||
4|ISO_04|hex2dec|ppb||
4|ISO_08|hex2dec|ppb||
4|npm_ch1|hex2dec|count||
4|npm_ch2|hex2dec|count||
4|npm_ch3|hex2dec|count||
4|npm_ch4|hex2dec|count||
4|npm_ch5|hex2dec|count||
4|npm_temp|hex2dec|°C|x/10|
4|npm_humidity|hex2dec|%|x/10|
4|battery_voltage|hex2dec|V|x/100|
4|battery_current|hex2dec|A|x/100|
4|solar_voltage|hex2dec|V|x/100|
4|solar_power|hex2dec|W||
4|charger_status|hex2dec|||
4|wind_speed|hex2dec|m/s|x/10|
4|wind_direction|hex2dec|degrees||
2|error_flags|hex2dec|||
2|npm_status|hex2dec|||
2|device_status|hex2dec|||
2|version_major|hex2dec|||
2|version_minor|hex2dec|||
2|version_patch|hex2dec|||
22|reserved|skip|||
```
### Byte 66 — error_flags
| Bit | Masque | Description |
|-----|--------|-------------|
| 0 | 0x01 | RTC deconnecte |
| 1 | 0x02 | RTC reset (annee 2000) |
| 2 | 0x04 | BME280 erreur |
| 3 | 0x08 | NPM erreur |
| 4 | 0x10 | Envea erreur |
| 5 | 0x20 | Bruit erreur |
| 6 | 0x40 | MPPT erreur |
| 7 | 0x80 | Vent erreur |
### Byte 67 — npm_status
| Bit | Masque | Description |
|-----|--------|-------------|
| 0 | 0x01 | Sleep mode |
| 1 | 0x02 | Degraded mode |
| 2 | 0x04 | Not ready |
| 3 | 0x08 | Heater error |
| 4 | 0x10 | THP sensor error |
| 5 | 0x20 | Fan error |
| 6 | 0x40 | Memory error |
| 7 | 0x80 | Laser error |
### Byte 68 — device_status
| Bit | Masque | Description |
|-----|--------|-------------|
| 0 | 0x01 | Modem reboot au cycle precedent |
| 1 | 0x02 | WiFi connecte |
| 2 | 0x04 | Hotspot actif |
| 3 | 0x08 | Pas de fix GPS |
| 4 | 0x10 | Batterie faible |
| 5 | 0x20 | Disque plein |
| 6 | 0x40 | Erreur base SQLite |
| 7 | 0x80 | Boot recent (uptime < 5 min) |
---
# Notes # Notes
## Wifi Hotspot (AP) ## Wifi Hotspot (AP)

View File

@@ -0,0 +1,72 @@
'''
____ _ ____ _
/ ___| / \ | _ \ / \
\___ \ / _ \ | |_) | / _ \
___) / ___ \| _ < / ___ \
|____/_/ \_\_| \_\/_/ \_\
sudo /usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/reboot/hardware_reboot.py
Hardware reboot of the SARA R5 modem using GPIO 16 (GND control via transistor).
Cuts power for 3 seconds, then verifies modem is responsive with ATI command.
Returns JSON result for web interface.
'''
import RPi.GPIO as GPIO
import serial
import time
import json
import sqlite3
SARA_GND_GPIO = 16
# Load baudrate from config
conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db")
cursor = conn.cursor()
cursor.execute("SELECT value FROM config_table WHERE key='SaraR4_baudrate'")
row = cursor.fetchone()
baudrate = int(row[0]) if row else 115200
conn.close()
result = {
"reboot": False,
"modem_response": None,
"error": None
}
try:
# Step 1: Cut GND (modem off)
GPIO.setmode(GPIO.BCM)
GPIO.setup(SARA_GND_GPIO, GPIO.OUT)
GPIO.output(SARA_GND_GPIO, GPIO.LOW)
time.sleep(3)
# Step 2: Restore GND (modem on)
GPIO.output(SARA_GND_GPIO, GPIO.HIGH)
time.sleep(5) # wait for modem boot
# Step 3: Check modem with ATI
ser = serial.Serial('/dev/ttyAMA2', baudrate=baudrate, timeout=3)
ser.reset_input_buffer()
for attempt in range(5):
ser.write(b'ATI\r')
time.sleep(1)
response = ser.read(ser.in_waiting or 1).decode('utf-8', errors='replace')
if "OK" in response:
result["reboot"] = True
result["modem_response"] = response.strip()
break
time.sleep(2)
else:
result["error"] = "Modem ne repond pas apres le redemarrage"
ser.close()
except Exception as e:
result["error"] = str(e)
finally:
GPIO.cleanup(SARA_GND_GPIO)
print(json.dumps(result))

View File

@@ -11,12 +11,17 @@ Script that starts at the boot of the RPI (with cron)
/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/reboot/start.py /usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/reboot/start.py
Roles:
1. Reset modem_config_mode to 0 (boot safety)
2. Power on SARA modem via GPIO 16
3. Detect modem model (SARA R4 or R5) and save to SQLite
All other configuration (AirCarto URL, uSpot HTTPS, PDP setup, geolocation)
is handled by the main loop script: loop/SARA_send_data_v2.py
''' '''
import serial import serial
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import time import time
import sys
import json
import re import re
import sqlite3 import sqlite3
import traceback import traceback
@@ -24,72 +29,29 @@ import traceback
#GPIO #GPIO
SARA_power_GPIO = 16 SARA_power_GPIO = 16
SARA_ON_GPIO = 20
GPIO.setmode(GPIO.BCM) # Use BCM numbering GPIO.setmode(GPIO.BCM) # Use BCM numbering
GPIO.setup(SARA_power_GPIO, GPIO.OUT) # Set GPIO17 as an output GPIO.setup(SARA_power_GPIO, GPIO.OUT)
# database connection # database connection
conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db")
cursor = conn.cursor() cursor = conn.cursor()
#get config data from SQLite table
def load_config_sqlite():
"""
Load configuration data from SQLite config table
Returns:
dict: Configuration data with proper type conversion
"""
try:
# Query the config table
cursor.execute("SELECT key, value, type FROM config_table")
rows = cursor.fetchall()
# Create config dictionary
config_data = {}
for key, value, type_name in rows:
# Convert value based on its type
if type_name == 'bool':
config_data[key] = value == '1' or value == 'true'
elif type_name == 'int':
config_data[key] = int(value)
elif type_name == 'float':
config_data[key] = float(value)
else:
config_data[key] = value
return config_data
except Exception as e:
print(f"Error loading config from SQLite: {e}")
return {}
def update_sqlite_config(key, value): def update_sqlite_config(key, value):
""" """
Updates a specific key in the SQLite config_table with a new value. Updates a specific key in the SQLite config_table with a new value.
:param key: The key to update in the config_table.
:param value: The new value to assign to the key.
""" """
try: try:
# Check if the key exists and get its type
cursor.execute("SELECT type FROM config_table WHERE key = ?", (key,)) cursor.execute("SELECT type FROM config_table WHERE key = ?", (key,))
result = cursor.fetchone() result = cursor.fetchone()
if result is None: if result is None:
print(f"Key '{key}' not found in the config_table.") print(f"Key '{key}' not found in the config_table.")
conn.close()
return return
# Get the type of the value from the database
value_type = result[0] value_type = result[0]
# Convert the value to the appropriate string representation based on its type
if value_type == 'bool': if value_type == 'bool':
# Convert Python boolean or string 'true'/'false' to '1'/'0'
if isinstance(value, bool): if isinstance(value, bool):
str_value = '1' if value else '0' str_value = '1' if value else '0'
else: else:
@@ -101,28 +63,22 @@ def update_sqlite_config(key, value):
else: else:
str_value = str(value) str_value = str(value)
# Update the value in the database
cursor.execute("UPDATE config_table SET value = ? WHERE key = ?", (str_value, key)) cursor.execute("UPDATE config_table SET value = ? WHERE key = ?", (str_value, key))
# Commit the changes and close the connection
conn.commit() conn.commit()
print(f"💾 Updated '{key}' to '{value}' in database.") print(f"Updated '{key}' to '{value}' in database.")
except Exception as e: except Exception as e:
print(f"Error updating the SQLite database: {e}") print(f"Error updating the SQLite database: {e}")
#Load config # Load baudrate from config
config = load_config_sqlite() cursor.execute("SELECT value FROM config_table WHERE key = 'SaraR4_baudrate'")
#config row = cursor.fetchone()
baudrate = config.get('SaraR4_baudrate', 115200) #baudrate du sara R4 baudrate = int(row[0]) if row else 115200
device_id = config.get('deviceID', '').upper() #device ID en maj
sara_r5_DPD_setup = False
ser_sara = serial.Serial( ser_sara = serial.Serial(
port='/dev/ttyAMA2', port='/dev/ttyAMA2',
baudrate=baudrate, #115200 ou 9600 baudrate=baudrate,
parity=serial.PARITY_NONE, #PARITY_NONE, PARITY_EVEN or PARITY_ODD parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE, stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS, bytesize=serial.EIGHTBITS,
timeout = 2 timeout = 2
@@ -130,11 +86,10 @@ ser_sara = serial.Serial(
def read_complete_response(serial_connection, timeout=2, end_of_response_timeout=2, wait_for_lines=None, debug=True): def read_complete_response(serial_connection, timeout=2, end_of_response_timeout=2, wait_for_lines=None, debug=True):
''' '''
Fonction très importante !!!
Reads the complete response from a serial connection and waits for specific lines. Reads the complete response from a serial connection and waits for specific lines.
''' '''
if wait_for_lines is None: if wait_for_lines is None:
wait_for_lines = [] # Default to an empty list if not provided wait_for_lines = []
response = bytearray() response = bytearray()
serial_connection.timeout = timeout serial_connection.timeout = timeout
@@ -142,259 +97,72 @@ def read_complete_response(serial_connection, timeout=2, end_of_response_timeout
start_time = time.time() start_time = time.time()
while True: while True:
elapsed_time = time.time() - start_time # Time since function start elapsed_time = time.time() - start_time
if serial_connection.in_waiting > 0: if serial_connection.in_waiting > 0:
data = serial_connection.read(serial_connection.in_waiting) data = serial_connection.read(serial_connection.in_waiting)
response.extend(data) response.extend(data)
end_time = time.time() + end_of_response_timeout # Reset timeout on new data end_time = time.time() + end_of_response_timeout
# Decode and check for any target line
decoded_response = response.decode('utf-8', errors='replace') decoded_response = response.decode('utf-8', errors='replace')
for target_line in wait_for_lines: for target_line in wait_for_lines:
if target_line in decoded_response: if target_line in decoded_response:
if debug: if debug:
print(f"[DEBUG] 🔎 Found target line: {target_line} (in {elapsed_time:.2f}s)") print(f"[DEBUG] Found target line: {target_line} (in {elapsed_time:.2f}s)")
return decoded_response # Return response immediately if a target line is found return decoded_response
elif time.time() > end_time: elif time.time() > end_time:
if debug: if debug:
print(f"[DEBUG] Timeout reached. No more data received.") print(f"[DEBUG] Timeout reached. No more data received.")
break break
time.sleep(0.1) # Short sleep to prevent busy waiting time.sleep(0.1)
# Final response and debug output
total_elapsed_time = time.time() - start_time total_elapsed_time = time.time() - start_time
if debug: if debug:
print(f"[DEBUG] ⏱️ elapsed time: {total_elapsed_time:.2f}s. ⏱️") print(f"[DEBUG] elapsed time: {total_elapsed_time:.2f}s.")
# Check if the elapsed time exceeded 10 seconds
if total_elapsed_time > 10 and debug: if total_elapsed_time > 10 and debug:
print(f"[ALERT] 🚨 The operation took too long 🚨") print(f"[ALERT] The operation took too long ({total_elapsed_time:.2f}s)")
print(f'<span style="color: red;font-weight: bold;">[ALERT] ⚠️{total_elapsed_time:.2f}s⚠</span>')
return response.decode('utf-8', errors='replace') # Return the full response if no target line is found return response.decode('utf-8', errors='replace')
try: try:
print('<h3>Start reboot python script</h3>') print('<h3>Start reboot python script</h3>')
#First we need to power on the module (if connected to mosfet via gpio16) # 1. Reset modem_config_mode at boot to prevent capteur from staying stuck in config mode
cursor.execute("UPDATE config_table SET value = '0' WHERE key = 'modem_config_mode'")
conn.commit()
print("modem_config_mode reset to 0 (boot safety)")
# 2. Power on the module (MOSFET via GPIO 16)
GPIO.output(SARA_power_GPIO, GPIO.HIGH) GPIO.output(SARA_power_GPIO, GPIO.HIGH)
time.sleep(5) time.sleep(5)
#check modem status # 3. Detect modem model
#Attention:
# SARA R4 response: Manufacturer: u-blox Model: SARA-R410M-02B # SARA R4 response: Manufacturer: u-blox Model: SARA-R410M-02B
# SArA R5 response: SARA-R500S-01B-00 # SARA R5 response: SARA-R500S-01B-00
print("⚙️Check SARA Status") print("Check SARA Status")
command = f'ATI\r' command = f'ATI\r'
ser_sara.write(command.encode('utf-8')) ser_sara.write(command.encode('utf-8'))
response_SARA_ATI = read_complete_response(ser_sara, wait_for_lines=["IMEI"]) response_SARA_ATI = read_complete_response(ser_sara, wait_for_lines=["IMEI"])
print(response_SARA_ATI) print(response_SARA_ATI)
# Check for SARA model with more robust regex
model = "Unknown" model = "Unknown"
if "SARA-R410M" in response_SARA_ATI: if "SARA-R410M" in response_SARA_ATI:
model = "SARA-R410M" model = "SARA-R410M"
print("📱 Detected SARA R4 modem") print("Detected SARA R4 modem")
elif "SARA-R500" in response_SARA_ATI: elif "SARA-R500" in response_SARA_ATI:
model = "SARA-R500" model = "SARA-R500"
print("📱 Detected SARA R5 modem") print("Detected SARA R5 modem")
sara_r5_DPD_setup = True
else: else:
# Fallback to regex match if direct string match fails
match = re.search(r"Model:\s*([A-Za-z0-9\-]+)", response_SARA_ATI) match = re.search(r"Model:\s*([A-Za-z0-9\-]+)", response_SARA_ATI)
if match: if match:
model = match.group(1).strip() model = match.group(1).strip()
else: else:
model = "Unknown" print("Could not identify modem model")
print("⚠️ Could not identify modem model")
print(f"🔍 Model: {model}") print(f"Model: {model}")
update_sqlite_config("modem_version", model) update_sqlite_config("modem_version", model)
time.sleep(1)
''' print('<h3>Boot script complete. Modem ready for main loop.</h3>')
AIRCARTO
'''
# 1. Set AIRCARTO URL (profile id = 0)
print('Set aircarto URL')
aircarto_profile_id = 0
aircarto_url="data.nebuleair.fr"
command = f'AT+UHTTP={aircarto_profile_id},1,"{aircarto_url}"\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_1 = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_1)
time.sleep(1)
'''
uSpot
'''
print("Set uSpot URL with SSL")
security_profile_id = 1
uSpot_profile_id = 1
uSpot_url="api-prod.uspot.probesys.net"
#step 1: import the certificate
print("➡️ import certificate")
certificate_name = "e6"
with open("/var/www/nebuleair_pro_4g/SARA/SSL/certificate/e6.pem", "rb") as cert_file:
certificate = cert_file.read()
size_of_string = len(certificate)
# AT+USECMNG=0,<type>,<internal_name>,<data_size>
# type-> 0 -> trusted root CA
command = f'AT+USECMNG=0,0,"{certificate_name}",{size_of_string}\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_1 = read_complete_response(ser_sara)
print(response_SARA_1)
time.sleep(0.5)
print("➡️ add certificate")
ser_sara.write(certificate)
response_SARA_2 = read_complete_response(ser_sara)
print(response_SARA_2)
time.sleep(0.5)
# op_code: 0 -> certificate validation level
# param_val : 0 -> Level 0 No validation; 1-> Level 1 Root certificate validation
print("Set the security profile (params)")
certification_level=0
command = f'AT+USECPRF={security_profile_id},0,{certification_level}\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_5b = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_5b)
time.sleep(0.5)
# op_code: 1 -> minimum SSL/TLS version
# param_val : 0 -> any; server can use any version for the connection; 1-> LSv1.0; 2->TLSv1.1; 3->TLSv1.2;
print("Set the security profile (params)")
minimum_SSL_version = 0
command = f'AT+USECPRF={security_profile_id},1,{minimum_SSL_version}\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_5bb = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_5bb)
time.sleep(0.5)
#op_code: 2 -> legacy cipher suite selection
# 0 (factory-programmed value): a list of default cipher suites is proposed at the beginning of handshake process, and a cipher suite will be negotiated among the cipher suites proposed in the list.
print("Set cipher")
cipher_suite = 0
command = f'AT+USECPRF={security_profile_id},2,{cipher_suite}\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_5cc = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_5cc)
time.sleep(0.5)
# op_code: 3 -> trusted root certificate internal name
print("Set the security profile (choose cert)")
command = f'AT+USECPRF={security_profile_id},3,"{certificate_name}"\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_5c = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_5c)
time.sleep(0.5)
# op_code: 10 -> SNI (server name indication)
print("Set the SNI")
command = f'AT+USECPRF={security_profile_id},10,"{uSpot_url}"\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_5cf = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_5cf)
time.sleep(0.5)
#step 4: set url (op_code = 1)
print("SET URL")
command = f'AT+UHTTP={uSpot_profile_id},1,"{uSpot_url}"\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_5 = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_5)
time.sleep(1)
#step 4: set PORT (op_code = 5)
print("SET PORT")
port = 443
command = f'AT+UHTTP={uSpot_profile_id},5,{port}\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_55 = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_55)
time.sleep(1)
#step 4: set url to SSL (op_code = 6) (http_secure = 1 for HTTPS)(USECMNG_PROFILE = 2)
print("SET SSL")
http_secure = 1
command = f'AT+UHTTP={uSpot_profile_id},6,{http_secure},{security_profile_id}\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_5fg = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_5fg)
time.sleep(1)
'''
SARA R5
'''
if sara_r5_DPD_setup:
print("SARA R5 PDP SETUP")
# 2. Activate PDP context 1
print('Activate PDP context 1')
command = f'AT+CGACT=1,1\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_2, end="")
time.sleep(1)
# 2. Set the PDP type
print('Set the PDP type to IPv4 referring to the outputof the +CGDCONT read command')
command = f'AT+UPSD=0,0,0\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_3 = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_3, end="")
time.sleep(1)
# 2. Profile #0 is mapped on CID=1.
print('Profile #0 is mapped on CID=1.')
command = f'AT+UPSD=0,100,1\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_3 = read_complete_response(ser_sara, wait_for_lines=["OK"])
print(response_SARA_3, end="")
time.sleep(1)
# 2. Set the PDP type
print('Activate the PSD profile #0: the IPv4 address is already assigned by the network.')
command = f'AT+UPSDA=0,3\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_3 = read_complete_response(ser_sara, wait_for_lines=["OK","+UUPSDA"])
print(response_SARA_3, end="")
time.sleep(1)
#3. Get localisation (CellLocate)
mode = 2 #single shot position
sensor = 2 #use cellular CellLocate® location information
response_type = 0
timeout_s = 2
accuracy_m = 1
command = f'AT+ULOC={mode},{sensor},{response_type},{timeout_s},{accuracy_m}\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_3 = read_complete_response(ser_sara, wait_for_lines=["+UULOC"])
print(response_SARA_3)
match = re.search(r"\+UULOC: \d{2}/\d{2}/\d{4},\d{2}:\d{2}:\d{2}\.\d{3},([-+]?\d+\.\d+),([-+]?\d+\.\d+)", response_SARA_3)
if match:
latitude = match.group(1)
longitude = match.group(2)
print(f"📍 Latitude: {latitude}, Longitude: {longitude}")
#update sqlite table
update_sqlite_config("latitude_raw", float(latitude))
update_sqlite_config("longitude_raw", float(longitude))
else:
print("❌ Failed to extract coordinates.")
time.sleep(1)
except Exception as e: except Exception as e:
print("An error occurred:", e) print("An error occurred:", e)
traceback.print_exc() # This prints the full traceback traceback.print_exc()

View File

@@ -1 +1 @@
1.5.0 1.6.4

View File

@@ -1,5 +1,126 @@
{ {
"versions": [ "versions": [
{
"version": "1.6.4",
"date": "2026-04-02",
"changes": {
"features": [
"Page modem: boutons Activer/Desactiver LED status connexion PCB (AT+UGPIOC=16,2 / AT+UGPIOC=16,255)"
],
"improvements": [
"Page modem: messages de progression en 3 etapes pendant le reset hardware (coupure, redemarrage, test connexion)",
"Page modem: bouton reset hardware desactive pendant l'operation pour eviter les doubles clics"
],
"fixes": [],
"compatibility": []
},
"notes": "Le reset hardware affiche maintenant les etapes en temps reel (~20s). Deux nouveaux boutons permettent de controler la LED bleue du PCB qui indique l'etat de la connexion reseau du modem."
},
{
"version": "1.6.3",
"date": "2026-04-01",
"changes": {
"features": [
"Page logs: bouton Auto-refresh pour suivre les logs SARA en temps reel (polling 3s)"
],
"improvements": [
"Service SARA: ajout flag python3 -u (unbuffered) pour ecriture immediate des logs dans le fichier"
],
"fixes": [],
"compatibility": [
"Necessite re-execution de setup_services.sh pour activer le mode unbuffered (optionnel, pas d'impact si non fait)"
]
},
"notes": "Les logs SARA sont maintenant visibles en temps reel sur la page logs grace au mode unbuffered Python et au rafraichissement automatique. Aucun impact sur les anciennes installations qui ne relancent pas setup_services.sh."
},
{
"version": "1.6.2",
"date": "2026-03-27",
"changes": {
"features": [],
"improvements": [
"Simplification du script de boot SARA (start.py): suppression config AirCarto, uSpot/SSL, PDP et geolocalisation",
"La configuration modem est desormais entierement geree par le script principal (SARA_send_data_v2.py)"
],
"fixes": [],
"compatibility": []
},
"notes": "Le script de boot ne fait plus que 3 choses: reset modem_config_mode, alimentation modem GPIO 16, detection modele R4/R5. Toute la configuration (URLs, certificats, PDP, geolocalisation) est deja geree par le script principal qui tourne chaque minute avec gestion d'erreur et retry."
},
{
"version": "1.6.1",
"date": "2026-03-19",
"changes": {
"features": [
"Sonometre NSRT MK4: detection deconnexion avec message explicite (page capteurs + self-test)",
"Colonne noise_status dans data_NOISE (0x00=OK, 0xFF=deconnecte)",
"ERR_NOISE (bit 5, byte 66) dans error_flags UDP quand sonometre deconnecte"
],
"improvements": [
"Script NSRT_mk4_get_data.py ecrit en base meme si capteur deconnecte (valeurs a 0, noise_status=0xFF)",
"Script read.py: message d'erreur clair au lieu de l'exception Python brute",
"Self-test: affiche 'Capteur deconnecte — verifiez le cablage USB' au lieu de l'erreur technique"
],
"fixes": [],
"compatibility": [
"Migration automatique: colonne noise_status ajoutee via set_config.py lors du firmware update"
]
},
"notes": "Gestion de la deconnexion du sonometre NSRT MK4 alignee sur le modele NPM: ecriture en base avec status d'erreur, flag ERR_NOISE dans la payload UDP, et messages utilisateur explicites sur l'interface web."
},
{
"version": "1.6.0",
"date": "2026-03-18",
"changes": {
"features": [
"Payload UDP Miotiq: envoi npm_status (byte 67) — registre status NextPM en temps reel"
],
"improvements": [
"npm_status lu depuis la derniere mesure en base (rowid DESC, pas de moyenne ni de timestamp)"
],
"fixes": [],
"compatibility": [
"Necessite mise a jour du parser Miotiq pour decoder le byte 67 (npm_status)"
]
},
"notes": "Le capteur envoie maintenant le registre status du NextPM dans chaque trame UDP (byte 67). La valeur est prise de la derniere mesure sans moyenne (un code erreur ne se moyenne pas). Utilise rowid pour eviter toute dependance au RTC."
},
{
"version": "1.5.2",
"date": "2026-03-18",
"changes": {
"features": [
"Page capteurs: lecture NPM via get_data_modbus_v3.py --dry-run (meme script que le timer)",
"Page capteurs: affichage temperature et humidite interne du NPM",
"Page capteurs: decodage npm_status avec flags d'erreur individuels"
],
"improvements": [
"NPM get_data_modbus_v3.py: mode --dry-run (print JSON sans ecriture en base)",
"Page capteurs: status NPM affiche en vert (OK) ou orange/rouge (erreurs decodees)"
],
"fixes": [
"Page capteurs: suppression unite ug/m3 sur le champ message/status"
],
"compatibility": []
},
"notes": "La page capteurs utilise maintenant le meme script Modbus que le timer systemd, en mode dry-run pour eviter les conflits d'ecriture SQLite. Le status NPM est decode bit par bit."
},
{
"version": "1.5.1",
"date": "2026-03-18",
"changes": {
"features": [
"Payload UDP Miotiq: bytes 69-71 firmware version (major.minor.patch)",
"README: documentation complete de la structure des 100 bytes UDP"
],
"improvements": [],
"fixes": [],
"compatibility": [
"Necessite mise a jour du parser Miotiq pour decoder les bytes 69-71 (firmware version)"
]
},
"notes": "Le capteur envoie maintenant sa version firmware dans chaque trame UDP. Cote serveur, bytes 69/70/71 = major/minor/patch. Documentation payload complete ajoutee au README."
},
{ {
"version": "1.5.0", "version": "1.5.0",
"date": "2026-03-18", "date": "2026-03-18",

View File

@@ -320,10 +320,10 @@ async function selfTestSequence() {
try { try {
if (sensor.type === 'npm') { if (sensor.type === 'npm') {
// NPM sensor test // NPM sensor test (uses get_data_modbus_v3.py --dry-run)
const npmResult = await new Promise((resolve, reject) => { const npmResult = await new Promise((resolve, reject) => {
$.ajax({ $.ajax({
url: 'launcher.php?type=npm&port=' + sensor.port, url: 'launcher.php?type=npm',
dataType: 'json', dataType: 'json',
method: 'GET', method: 'GET',
timeout: 15000, timeout: 15000,
@@ -333,22 +333,42 @@ async function selfTestSequence() {
}); });
selfTestReport.rawResponses['NPM Sensor'] = JSON.stringify(npmResult, null, 2); selfTestReport.rawResponses['NPM Sensor'] = JSON.stringify(npmResult, null, 2);
addSelfTestLog(`NPM response: PM1=${npmResult.PM1}, PM2.5=${npmResult.PM25}, PM10=${npmResult.PM10}`); addSelfTestLog(`NPM response: PM1=${npmResult.PM1}, PM2.5=${npmResult.PM25}, PM10=${npmResult.PM10}, status=${npmResult.npm_status_hex}`);
// Check for errors // Decode npm_status flags
const npmErrors = ['notReady', 'fanError', 'laserError', 'heatError', 't_rhError', 'memoryError', 'degradedState']; const status = npmResult.npm_status !== undefined ? npmResult.npm_status : 0;
const activeErrors = npmErrors.filter(e => npmResult[e] === 1);
if (status === 0xFF) {
// 0xFF = no response = disconnected
updateTestStatus(sensor.id, 'Failed', 'Capteur déconnecté', 'bg-danger');
testsFailed++;
} else {
const statusFlags = {
0x01: "Sleep mode",
0x02: "Degraded mode",
0x04: "Not ready",
0x08: "Heater error",
0x10: "THP sensor error",
0x20: "Fan error",
0x40: "Memory error",
0x80: "Laser error"
};
const activeErrors = [];
Object.entries(statusFlags).forEach(([mask, label]) => {
if (status & mask) activeErrors.push(label);
});
if (activeErrors.length > 0) { if (activeErrors.length > 0) {
updateTestStatus(sensor.id, 'Warning', `Errors: ${activeErrors.join(', ')}`, 'bg-warning'); updateTestStatus(sensor.id, 'Warning', `Status ${npmResult.npm_status_hex}: ${activeErrors.join(', ')}`, 'bg-warning');
testsFailed++; testsFailed++;
} else if (npmResult.PM1 !== undefined && npmResult.PM25 !== undefined && npmResult.PM10 !== undefined) { } else if (npmResult.PM1 !== undefined && npmResult.PM25 !== undefined && npmResult.PM10 !== undefined) {
updateTestStatus(sensor.id, 'Passed', `PM1: ${npmResult.PM1} | PM2.5: ${npmResult.PM25} | PM10: ${npmResult.PM10} ug/m3`, 'bg-success'); updateTestStatus(sensor.id, 'Passed', `PM1: ${npmResult.PM1} | PM2.5: ${npmResult.PM25} | PM10: ${npmResult.PM10} µg/m³`, 'bg-success');
testsPassed++; testsPassed++;
} else { } else {
updateTestStatus(sensor.id, 'Warning', 'Incomplete data received', 'bg-warning'); updateTestStatus(sensor.id, 'Warning', 'Incomplete data received', 'bg-warning');
testsFailed++; testsFailed++;
} }
} // end else (not 0xFF)
} else if (sensor.type === 'BME280') { } else if (sensor.type === 'BME280') {
// BME280 sensor test // BME280 sensor test
@@ -392,7 +412,10 @@ async function selfTestSequence() {
addSelfTestLog(`Noise response: ${JSON.stringify(noiseResult)}`); addSelfTestLog(`Noise response: ${JSON.stringify(noiseResult)}`);
if (noiseResult.error) { if (noiseResult.error) {
updateTestStatus(sensor.id, 'Failed', noiseResult.error, 'bg-danger'); const noiseMsg = noiseResult.disconnected
? 'Capteur déconnecté — vérifiez le câblage USB'
: noiseResult.error;
updateTestStatus(sensor.id, 'Failed', noiseMsg, 'bg-danger');
testsFailed++; testsFailed++;
} else if (noiseResult.LEQ > 0 && noiseResult.dBA > 0) { } else if (noiseResult.LEQ > 0 && noiseResult.dBA > 0) {
updateTestStatus(sensor.id, 'Passed', `LEQ: ${noiseResult.LEQ} dB | dB(A): ${noiseResult.dBA}`, 'bg-success'); updateTestStatus(sensor.id, 'Passed', `LEQ: ${noiseResult.LEQ} dB | dB(A): ${noiseResult.dBA}`, 'bg-success');

View File

@@ -381,6 +381,7 @@ function get_data_sqlite(table, limit, download , startDate = "", endDate = "")
<th>Timestamp</th> <th>Timestamp</th>
<th>Curent LEQ</th> <th>Curent LEQ</th>
<th>DB_A_value</th> <th>DB_A_value</th>
<th>Status</th>
`; `;
}else if (table === "data_MHZ19") { }else if (table === "data_MHZ19") {
@@ -462,10 +463,13 @@ function get_data_sqlite(table, limit, download , startDate = "", endDate = "")
<td>${columns[5]}</td> <td>${columns[5]}</td>
`; `;
}else if (table === "data_NOISE") { }else if (table === "data_NOISE") {
const nStatus = parseInt(columns[3]) || 0;
const nStatusLabel = nStatus === 255 ? '❌ Déconnecté' : '✅ OK';
tableHTML += ` tableHTML += `
<td>${columns[0]}</td> <td>${columns[0]}</td>
<td>${columns[1]}</td> <td>${columns[1]}</td>
<td>${columns[2]}</td> <td>${columns[2]}</td>
<td>${nStatusLabel}</td>
`; `;
}else if (table === "data_MHZ19") { }else if (table === "data_MHZ19") {
@@ -532,6 +536,9 @@ function downloadCSV(response, table) {
else if (table === "data_NPM_5channels") { else if (table === "data_NPM_5channels") {
csvContent += "TimestampUTC,PM_ch1,PM_ch2,PM_ch3,PM_ch4,PM_ch5\n"; csvContent += "TimestampUTC,PM_ch1,PM_ch2,PM_ch3,PM_ch4,PM_ch5\n";
} }
else if (table === "data_NOISE") {
csvContent += "TimestampUTC,Current_LEQ,DB_A_value,noise_status\n";
}
else if (table === "data_MHZ19") { else if (table === "data_MHZ19") {
csvContent += "TimestampUTC,CO2_ppm\n"; csvContent += "TimestampUTC,CO2_ppm\n";
} }

View File

@@ -805,8 +805,7 @@ if ($type == "reboot") {
} }
if ($type == "npm") { if ($type == "npm") {
$port=$_GET['port']; $command = 'sudo /usr/bin/python3 /var/www/nebuleair_pro_4g/NPM/get_data_modbus_v3.py --dry-run';
$command = 'sudo /usr/bin/python3 /var/www/nebuleair_pro_4g/NPM/get_data.py ' . $port;
$output = shell_exec($command); $output = shell_exec($command);
echo $output; echo $output;
} }
@@ -881,6 +880,13 @@ if ($type == "sara") {
echo $output; echo $output;
} }
# SARA HARDWARE REBOOT (GPIO 16)
if ($type == "sara_hardware_reboot") {
$command = 'sudo /usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/reboot/hardware_reboot.py';
$output = shell_exec($command);
echo $output;
}
# SARA R4 COMMANDS (MQTT) # SARA R4 COMMANDS (MQTT)
if ($type == "sara_getMQTT_config") { if ($type == "sara_getMQTT_config") {
$port=$_GET['port']; $port=$_GET['port'];

View File

@@ -58,6 +58,9 @@
<div class="card-header"> <div class="card-header">
<span data-i18n="logs.saraLogs">Sara logs</span> <span data-i18n="logs.saraLogs">Sara logs</span>
<button type="submit" class="btn btn-secondary btn-sm" id="refresh-master-log" data-i18n="logs.refresh">Refresh</button> <button type="submit" class="btn btn-secondary btn-sm" id="refresh-master-log" data-i18n="logs.refresh">Refresh</button>
<button type="button" class="btn btn-sm" id="auto-refresh-toggle" onclick="toggleAutoRefresh()">
<span id="auto-refresh-icon"></span> Auto
</button>
<button type="submit" class="btn btn-secondary btn-sm" onclick="clear_loopLogs()" data-i18n="logs.clear">Clear</button> <button type="submit" class="btn btn-secondary btn-sm" onclick="clear_loopLogs()" data-i18n="logs.clear">Clear</button>
<span id="script_running"></span> <span id="script_running"></span>
@@ -274,6 +277,32 @@ function clear_loopLogs(){
}); });
} }
// Auto-refresh for SARA logs
let autoRefreshInterval = null;
const AUTO_REFRESH_MS = 3000;
function toggleAutoRefresh() {
const btn = document.getElementById('auto-refresh-toggle');
const icon = document.getElementById('auto-refresh-icon');
if (autoRefreshInterval) {
clearInterval(autoRefreshInterval);
autoRefreshInterval = null;
icon.textContent = '▶';
btn.classList.remove('btn-success');
btn.classList.add('btn-secondary');
} else {
// Refresh immediately, then every N seconds
displayLogFile('../logs/sara_service.log', document.getElementById('card_loop_content'), true, 1000);
autoRefreshInterval = setInterval(() => {
displayLogFile('../logs/sara_service.log', document.getElementById('card_loop_content'), true, 1000);
}, AUTO_REFRESH_MS);
icon.textContent = '⏸';
btn.classList.remove('btn-secondary');
btn.classList.add('btn-success');
}
}
function getModem_busy_status() { function getModem_busy_status() {
//console.log("Getting modem busy status"); //console.log("Getting modem busy status");

View File

@@ -58,6 +58,16 @@
<label class="form-check-label" for="check_modem_configMode">Mode configuration</label> <label class="form-check-label" for="check_modem_configMode">Mode configuration</label>
</div> </div>
<div id="configmode_alert" class="alert alert-danger align-items-center py-3 mb-3" role="alert" style="display:none;">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-exclamation-triangle-fill me-3 flex-shrink-0" viewBox="0 0 16 16">
<path d="M8.982 1.566a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767zM8 5c.535 0 .954.462.9.995l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995A.905.905 0 0 1 8 5m.002 6a1 1 0 1 1 0 2 1 1 0 0 1 0-2"/>
</svg>
<div>
<strong>Mode configuration actif — le capteur n'envoie aucune donnée !</strong><br>
<small>Le script de transmission (SARA) est désactivé tant que ce mode est actif. Pensez à le désactiver une fois la configuration terminée. Ce mode sera automatiquement désactivé au prochain redémarrage du capteur.</small>
</div>
</div>
<button class="btn btn-success mb-3 btn_selfTest" onclick="runSelfTest()"> <button class="btn btn-success mb-3 btn_selfTest" onclick="runSelfTest()">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check2-circle me-1" viewBox="0 0 16 16"> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check2-circle me-1" viewBox="0 0 16 16">
<path d="M2.5 8a5.5 5.5 0 1 1 11 0 5.5 5.5 0 0 1-11 0z"/> <path d="M2.5 8a5.5 5.5 0 1 1 11 0 5.5 5.5 0 0 1-11 0z"/>
@@ -151,14 +161,35 @@
</div> </div>
</div> </div>
<div class="col-sm-2"> <div class="col-sm-3">
<div class="card"> <div class="card">
<div class="card-body"> <div class="card-body">
<p class="card-text">Modem Reset </p> <p class="card-text"><strong>Modem Reset</strong></p>
<button class="btn btn-danger" onclick="getData_saraR4('ttyAMA2', 'AT+CFUN=15', 1)">Reset</button>
<p class="text-muted small mb-2">Reset software (AT+CFUN=15) : redémarre le firmware du modem.</p>
<button class="btn btn-warning mb-2" onclick="getData_saraR4('ttyAMA2', 'AT+CFUN=15', 1)">Reset Software</button>
<div id="loading_ttyAMA2_AT_CFUN_15" class="spinner-border spinner-border-sm" style="display: none;" role="status"></div> <div id="loading_ttyAMA2_AT_CFUN_15" class="spinner-border spinner-border-sm" style="display: none;" role="status"></div>
<div id="response_ttyAMA2_AT_CFUN_15"></div> <div id="response_ttyAMA2_AT_CFUN_15"></div>
</table>
<hr>
<p class="text-muted small mb-2">Reset hardware (GPIO 16) : coupe et rétablit l'alimentation du modem, puis teste la connexion avec le modem (~20 secondes).</p>
<button class="btn btn-danger mb-2" id="btn_hw_reboot" onclick="hardwareRebootSara()">Reset Hardware</button>
<div id="loading_hw_reboot" class="spinner-border spinner-border-sm" style="display: none;" role="status"></div>
<div id="response_hw_reboot"></div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="card">
<div class="card-body">
<p class="card-text"><strong>LED status connexion (PCB)</strong></p>
<p class="text-muted small mb-2">Active la LED bleue du PCB qui indique l'état de la connexion réseau (GPIO 16 du modem).</p>
<button class="btn btn-primary mb-2" onclick="sendLedCommand('AT+UGPIOC=16,2', 'Activer')">Activer LED</button>
<button class="btn btn-secondary mb-2" onclick="sendLedCommand('AT+UGPIOC=16,255', 'Désactiver')">Désactiver LED</button>
<div id="loading_led" class="spinner-border spinner-border-sm" style="display: none;" role="status"></div>
<div id="response_led"></div>
</div> </div>
</div> </div>
</div> </div>
@@ -446,6 +477,9 @@ document.addEventListener('DOMContentLoaded', function () {
if (check_modem_configMode) { if (check_modem_configMode) {
check_modem_configMode.checked = response.modem_config_mode; check_modem_configMode.checked = response.modem_config_mode;
console.log("Modem configuration: " + response.modem_config_mode); console.log("Modem configuration: " + response.modem_config_mode);
// Show/hide config mode alert banner
const alertEl = document.getElementById("configmode_alert");
if (alertEl) alertEl.style.display = response.modem_config_mode ? "flex" : "none";
} else { } else {
console.error("Checkbox element not found"); console.error("Checkbox element not found");
} }
@@ -1024,6 +1058,103 @@ function getSignalInfo(port, timeout) {
}); });
} }
function hardwareRebootSara() {
if (!confirm("Couper l'alimentation du modem SARA via GPIO 16 ?\nLe modem sera éteint pendant ~3 secondes puis redémarré.\nLa connexion avec le modem sera ensuite testée (~20 secondes au total).")) return;
console.log("Hardware reboot SARA via GPIO 16");
$("#btn_hw_reboot").prop("disabled", true);
$("#loading_hw_reboot").show();
$("#response_hw_reboot").html(`
<div class="alert alert-info py-2 mt-2" id="hw_reboot_progress">
<small><strong>Étape 1/3</strong> — Coupure alimentation du modem...</small>
</div>`);
// Progress messages
var step2Timeout = setTimeout(function() {
$("#hw_reboot_progress small").html('<strong>Étape 2/3</strong> — Attente du redémarrage du modem...');
}, 4000);
var step3Timeout = setTimeout(function() {
$("#hw_reboot_progress small").html('<strong>Étape 3/3</strong> — Test de la connexion avec le modem (ATI)...');
}, 9000);
$.ajax({
url: 'launcher.php?type=sara_hardware_reboot',
dataType: 'json',
method: 'GET',
timeout: 45000,
success: function(response) {
console.log(response);
clearTimeout(step2Timeout);
clearTimeout(step3Timeout);
$("#loading_hw_reboot").hide();
$("#btn_hw_reboot").prop("disabled", false);
if (response.reboot) {
$("#response_hw_reboot").html(`
<div class="alert alert-success py-2 mt-2">
<strong>Modem redémarré et connexion vérifiée</strong><br>
<small><code>${response.modem_response}</code></small>
</div>`);
} else {
$("#response_hw_reboot").html(`
<div class="alert alert-danger py-2 mt-2">
<strong>Echec</strong><br>
<small>${response.error || 'Modem ne répond pas après le redémarrage'}</small>
</div>`);
}
},
error: function(xhr, status, error) {
console.error('Hardware reboot failed:', status, error);
clearTimeout(step2Timeout);
clearTimeout(step3Timeout);
$("#loading_hw_reboot").hide();
$("#btn_hw_reboot").prop("disabled", false);
$("#response_hw_reboot").html(`
<div class="alert alert-danger py-2 mt-2">
<strong>Erreur de communication</strong><br>
<small>${error}</small>
</div>`);
}
});
}
function sendLedCommand(command, label) {
$("#loading_led").show();
$("#response_led").empty();
$.ajax({
url: 'launcher.php?type=sara&port=ttyAMA2&command=' + encodeURIComponent(command) + '&timeout=5',
dataType: 'text',
method: 'GET',
timeout: 10000,
success: function(response) {
$("#loading_led").hide();
if (response.indexOf("OK") !== -1) {
$("#response_led").html(`
<div class="alert alert-success py-2 mt-2">
<strong>${label} — OK</strong><br>
<small>Commande <code>${command}</code> exécutée avec succès.</small>
</div>`);
} else {
$("#response_led").html(`
<div class="alert alert-warning py-2 mt-2">
<strong>${label} — Réponse inattendue</strong><br>
<small><code>${response}</code></small>
</div>`);
}
},
error: function(xhr, status, error) {
$("#loading_led").hide();
$("#response_led").html(`
<div class="alert alert-danger py-2 mt-2">
<strong>Erreur de connexion avec le modem</strong><br>
<small>${error || 'Pas de réponse'}</small>
</div>`);
}
});
}
function getData_saraR4(port, command, timeout){ function getData_saraR4(port, command, timeout){
console.log("Data from SaraR4"); console.log("Data from SaraR4");
console.log("Port: " + port ); console.log("Port: " + port );
@@ -1429,6 +1560,10 @@ function update_modem_configMode(param, checked){
const toastBody = toastLiveExample.querySelector('.toast-body'); const toastBody = toastLiveExample.querySelector('.toast-body');
console.log("updating modem config mode to :" + checked); console.log("updating modem config mode to :" + checked);
// Toggle alert banner immediately
const alertEl = document.getElementById("configmode_alert");
if (alertEl) alertEl.style.display = checked ? "flex" : "none";
$.ajax({ $.ajax({
url: 'launcher.php?type=update_config_sqlite&param='+param+'&value='+checked, url: 'launcher.php?type=update_config_sqlite&param='+param+'&value='+checked,
dataType: 'json', // Specify that you expect a JSON response dataType: 'json', // Specify that you expect a JSON response

View File

@@ -117,54 +117,94 @@
$("#loading_" + port).show(); $("#loading_" + port).show();
$.ajax({ $.ajax({
url: 'launcher.php?type=npm&port=' + port, url: 'launcher.php?type=npm',
dataType: 'json', // Specify that you expect a JSON response dataType: 'json',
method: 'GET', // Use GET or POST depending on your needs method: 'GET',
success: function (response) { success: function (response) {
console.log(response); console.log(response);
const tableBody = document.getElementById("data-table-body_" + port); const tableBody = document.getElementById("data-table-body_" + port);
tableBody.innerHTML = ""; tableBody.innerHTML = "";
$("#loading_" + port).hide(); $("#loading_" + port).hide();
// Create an array of the desired keys
const keysToShow = ["PM1", "PM25", "PM10", "message"]; // PM values
// Error messages mapping const pmKeys = ["PM1", "PM25", "PM10"];
const errorMessages = { pmKeys.forEach(key => {
"notReady": "Sensor is not ready", if (response[key] !== undefined) {
"fanError": "Fan malfunction detected",
"laserError": "Laser malfunction detected",
"heatError": "Heating system error",
"t_rhError": "Temperature/Humidity sensor error",
"memoryError": "Memory failure detected",
"degradedState": "Sensor in degraded state"
};
// Add only the specified elements to the table
keysToShow.forEach(key => {
if (response[key] !== undefined) { // Check if the key exists in the response
const value = response[key];
$("#data-table-body_" + port).append(` $("#data-table-body_" + port).append(`
<tr> <tr>
<td>${key}</td> <td>${key}</td>
<td>${value} µg/m³</td> <td>${response[key]} µg/m³</td>
</tr> </tr>
`); `);
} }
}); });
// Check for errors and add them to the table // Temperature & humidity
Object.keys(errorMessages).forEach(errorKey => { if (response.temperature !== undefined) {
if (response[errorKey] === 1) { $("#data-table-body_" + port).append(`
<tr><td>Temperature</td><td>${response.temperature} °C</td></tr>
`);
}
if (response.humidity !== undefined) {
$("#data-table-body_" + port).append(`
<tr><td>Humidity</td><td>${response.humidity} %</td></tr>
`);
}
// NPM status decoded
if (response.npm_status !== undefined) {
const status = response.npm_status;
if (status === 0xFF) {
// 0xFF = no response from sensor = disconnected
$("#data-table-body_" + port).append(`
<tr>
<td>Status</td>
<td style="color: red; font-weight: bold;">Capteur déconnecté</td>
</tr>
`);
} else if (status === 0) {
$("#data-table-body_" + port).append(`
<tr>
<td>Status</td>
<td style="color: green; font-weight: bold;">OK</td>
</tr>
`);
} else {
$("#data-table-body_" + port).append(`
<tr>
<td>Status</td>
<td style="color: orange; font-weight: bold;">${response.npm_status_hex}</td>
</tr>
`);
// Decode individual error bits
const statusFlags = {
0x01: "Sleep mode",
0x02: "Degraded mode",
0x04: "Not ready",
0x08: "Heater error",
0x10: "THP sensor error",
0x20: "Fan error",
0x40: "Memory error",
0x80: "Laser error"
};
Object.entries(statusFlags).forEach(([mask, label]) => {
if (status & mask) {
$("#data-table-body_" + port).append(` $("#data-table-body_" + port).append(`
<tr class="error-row"> <tr class="error-row">
<td><b>${errorKey}</b></td> <td></td>
<td style="color: red;">⚠ ${errorMessages[errorKey]}</td> <td style="color: red;">⚠ ${label}</td>
</tr> </tr>
`); `);
} }
}); });
}
}
}, },
error: function (xhr, status, error) { error: function (xhr, status, error) {
console.error('AJAX request failed:', status, error); console.error('AJAX request failed:', status, error);
$("#loading_" + port).hide();
} }
}); });
} }

View File

@@ -386,6 +386,16 @@ class SensorPayload:
"""Set device status flags (byte 68)""" """Set device status flags (byte 68)"""
self.payload[68] = status & 0xFF self.payload[68] = status & 0xFF
def set_firmware_version(self, version_str):
"""Set firmware version bytes 69-71 (major.minor.patch)"""
try:
parts = version_str.strip().split('.')
self.payload[69] = int(parts[0]) & 0xFF
self.payload[70] = int(parts[1]) & 0xFF
self.payload[71] = int(parts[2]) & 0xFF
except (IndexError, ValueError):
pass # leave as 0xFF if VERSION file is malformed
def get_bytes(self): def get_bytes(self):
"""Get the complete 100-byte payload""" """Get the complete 100-byte payload"""
return bytes(self.payload) return bytes(self.payload)
@@ -836,6 +846,19 @@ try:
payload_csv[18] = npm_temp payload_csv[18] = npm_temp
payload_csv[19] = npm_hum payload_csv[19] = npm_hum
# npm_status: last value only (no average), use rowid (not timestamp)
npm_status_value = rows[0][7] if rows and rows[0][7] is not None else 0xFF
npm_disconnected = False
if npm_status_value == 0xFF:
# 0xFF = NPM disconnected/no response → will set ERR_NPM in error_flags
npm_disconnected = True
print("NPM status: 0xFF (disconnected)")
else:
# Valid status from NPM → send as byte 67
payload.set_npm_status(npm_status_value)
print(f"NPM status: 0x{npm_status_value:02X}")
#add data to payload UDP #add data to payload UDP
payload.set_npm_core(PM1, PM25, PM10) payload.set_npm_core(PM1, PM25, PM10)
payload.set_npm_internal(npm_temp, npm_hum) payload.set_npm_internal(npm_temp, npm_hum)
@@ -992,6 +1015,7 @@ try:
print("No data available in the database.") print("No data available in the database.")
# NOISE sensor # NOISE sensor
noise_disconnected = False
if NOISE_sensor: if NOISE_sensor:
print("Getting NOISE sensor values") print("Getting NOISE sensor values")
cursor.execute("SELECT * FROM data_NOISE ORDER BY rowid DESC LIMIT 1") cursor.execute("SELECT * FROM data_NOISE ORDER BY rowid DESC LIMIT 1")
@@ -1001,6 +1025,14 @@ try:
cur_LEQ = last_row[1] cur_LEQ = last_row[1]
cur_level = last_row[2] cur_level = last_row[2]
# noise_status: 0xFF = disconnected (column index 3)
noise_status_value = last_row[3] if len(last_row) > 3 and last_row[3] is not None else 0x00
if noise_status_value == 0xFF:
noise_disconnected = True
print("Noise status: 0xFF (disconnected)")
else:
print(f"Noise status: 0x{noise_status_value:02X}")
#Add data to payload CSV #Add data to payload CSV
payload_csv[6] = cur_level payload_csv[6] = cur_level
@@ -1124,8 +1156,19 @@ try:
error_flags |= ERR_RTC_DISCONNECTED error_flags |= ERR_RTC_DISCONNECTED
if rtc_status == "reset": if rtc_status == "reset":
error_flags |= ERR_RTC_RESET error_flags |= ERR_RTC_RESET
if npm_disconnected:
error_flags |= ERR_NPM
if noise_disconnected:
error_flags |= ERR_NOISE
payload.set_error_flags(error_flags) payload.set_error_flags(error_flags)
# ---- Firmware version (bytes 69-71) ----
try:
with open("/var/www/nebuleair_pro_4g/VERSION", "r") as f:
payload.set_firmware_version(f.read())
except FileNotFoundError:
pass
if send_miotiq: if send_miotiq:
print('<p class="fw-bold">➡SEND TO MIOTIQ</p>', end="") print('<p class="fw-bold">➡SEND TO MIOTIQ</p>', end="")

View File

@@ -247,7 +247,10 @@ def set_device_status(self, status):
2|error_flags|hex2dec||| 2|error_flags|hex2dec|||
2|npm_status|hex2dec||| 2|npm_status|hex2dec|||
2|device_status|hex2dec||| 2|device_status|hex2dec|||
28|reserved|skip||| 2|version_major|hex2dec|||
2|version_minor|hex2dec|||
2|version_patch|hex2dec|||
22|reserved|skip|||
``` ```
--- ---
@@ -310,9 +313,9 @@ if sara_rebooted:
## Notes ## Notes
- La payload est initialisee a 0xFF (tous bytes a 255). Le script doit explicitement - Les bytes 66-68 sont initialises a 0x00 dans le constructeur SensorPayload
ecrire 0x00 dans les bytes 66-67 quand tout va bien, sinon Miotiq interpretera (0x00 = aucune erreur/aucun flag). Les bytes 69-71 restent a 0xFF si le
255 = toutes les erreurs. fichier VERSION est absent ou malformed.
- Le NPM status n'est pas encore lu par `get_data_modbus_v3.py`. Il faut d'abord - Le NPM status n'est pas encore lu par `get_data_modbus_v3.py`. Il faut d'abord
ajouter la lecture du registre de status Modbus et le stocker en SQLite. ajouter la lecture du registre de status Modbus et le stocker en SQLite.
- Les flags du byte 66 sont determines par le script d'envoi en analysant les - Les flags du byte 66 sont determines par le script d'envoi en analysant les

View File

@@ -82,7 +82,7 @@ After=network.target
[Service] [Service]
Type=oneshot Type=oneshot
ExecStart=/usr/bin/python3 /var/www/nebuleair_pro_4g/loop/SARA_send_data_v2.py ExecStart=/usr/bin/python3 -u /var/www/nebuleair_pro_4g/loop/SARA_send_data_v2.py
User=root User=root
WorkingDirectory=/var/www/nebuleair_pro_4g WorkingDirectory=/var/www/nebuleair_pro_4g
StandardOutput=append:/var/www/nebuleair_pro_4g/logs/sara_service.log StandardOutput=append:/var/www/nebuleair_pro_4g/logs/sara_service.log

View File

@@ -28,8 +28,32 @@ write_fs(frequency: int) -> sampling freq
import nsrt_mk3_dev import nsrt_mk3_dev
import sqlite3 import sqlite3
# noise_status: 0x00 = OK, 0xFF = disconnected
noise_status = 0x00
leq_level_rounded = 0
weighted_level_rounded = 0
try:
nsrt = nsrt_mk3_dev.NsrtMk3Dev('/dev/ttyACM0') nsrt = nsrt_mk3_dev.NsrtMk3Dev('/dev/ttyACM0')
freq_level = nsrt.read_fs() #current sampling frequency
time_constant = nsrt.read_tau() #reads the current time constant
leq_level = nsrt.read_leq() #current running LEQ and starts the integration of a new LEQ.
weighting = nsrt.read_weighting() #weighting curve that is currently selected ( A ou C)
weighted_level = nsrt.read_level() #current running level in dB.
leq_level_rounded = round(leq_level, 2)
weighted_level_rounded = round(weighted_level, 2)
except Exception as e:
err_msg = str(e)
if "No such file or directory" in err_msg or "could not open port" in err_msg:
print(f"NSRT MK4 disconnected: {err_msg}")
noise_status = 0xFF
else:
print(f"NSRT MK4 error: {err_msg}")
noise_status = 0xFF
# Connect to the SQLite database # Connect to the SQLite database
conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db")
cursor = conn.cursor() cursor = conn.cursor()
@@ -39,34 +63,15 @@ cursor.execute("SELECT * FROM timestamp_table LIMIT 1")
row = cursor.fetchone() # Get the first (and only) row row = cursor.fetchone() # Get the first (and only) row
rtc_time_str = row[1] # '2025-02-07 12:30:45' rtc_time_str = row[1] # '2025-02-07 12:30:45'
freq_level = nsrt.read_fs() #current sampling frequency
time_constant = nsrt.read_tau() #reads the current time constant
leq_level = nsrt.read_leq() #current running LEQ and starts the integration of a new LEQ.
weighting = nsrt.read_weighting() #weighting curve that is currently selected ( A ou C)
weighted_level = nsrt.read_level() #current running level in dB.
#print(f'current sampling freq : {freq_level} Hz')
#print(f'current time constant : {time_constant} s')
#print(f'current LEQ level: {leq_level:0.2f} dB')
#print(f'{weighting} value: {weighted_level:0.2f} dBA')
# Round values to 2 decimal places before saving
leq_level_rounded = round(leq_level, 2)
weighted_level_rounded = round(weighted_level, 2)
#save to db
#save to sqlite database #save to sqlite database
try: try:
cursor.execute(''' cursor.execute('''
INSERT INTO data_NOISE (timestamp,current_LEQ, DB_A_value) VALUES (?,?,?)''' INSERT INTO data_NOISE (timestamp, current_LEQ, DB_A_value, noise_status) VALUES (?,?,?,?)'''
, (rtc_time_str,leq_level_rounded,weighted_level_rounded)) , (rtc_time_str, leq_level_rounded, weighted_level_rounded, noise_status))
# Commit and close the connection
conn.commit() conn.commit()
#print("Sensor data saved successfully!")
except Exception as e: except Exception as e:
print(f"Database error: {e}") print(f"Database error: {e}")
conn.close() conn.close()

View File

@@ -31,4 +31,10 @@ try:
print(json.dumps(data)) print(json.dumps(data))
except Exception as e: except Exception as e:
print(json.dumps({"error": str(e)})) err_msg = str(e)
if "No such file or directory" in err_msg or "could not open port" in err_msg:
print(json.dumps({"error": "Capteur déconnecté — vérifiez le câblage USB du sonomètre NSRT MK4 (/dev/ttyACM0)", "disconnected": True}))
elif "Permission denied" in err_msg:
print(json.dumps({"error": "Permission refusée sur /dev/ttyACM0 — exécutez: sudo chmod 777 /dev/ttyACM0", "disconnected": False}))
else:
print(json.dumps({"error": err_msg}))

View File

@@ -140,10 +140,18 @@ cursor.execute("""
CREATE TABLE IF NOT EXISTS data_NOISE ( CREATE TABLE IF NOT EXISTS data_NOISE (
timestamp TEXT, timestamp TEXT,
current_LEQ REAL, current_LEQ REAL,
DB_A_value REAL DB_A_value REAL,
noise_status INTEGER DEFAULT 0
) )
""") """)
# Add noise_status column to existing databases (migration)
try:
cursor.execute("ALTER TABLE data_NOISE ADD COLUMN noise_status INTEGER DEFAULT 0")
print("Added noise_status column to data_NOISE")
except:
pass # Column already exists
# Create a table MHZ19 (CO2 sensor) # Create a table MHZ19 (CO2 sensor)
cursor.execute(""" cursor.execute("""
CREATE TABLE IF NOT EXISTS data_MHZ19 ( CREATE TABLE IF NOT EXISTS data_MHZ19 (

View File

@@ -110,6 +110,7 @@ for connected, port, name, coefficient in envea_sondes:
# Database migrations (add columns to existing tables) # Database migrations (add columns to existing tables)
migrations = [ migrations = [
("data_NPM", "npm_status", "INTEGER DEFAULT 0"), ("data_NPM", "npm_status", "INTEGER DEFAULT 0"),
("data_NOISE", "noise_status", "INTEGER DEFAULT 0"),
] ]
for table, column, col_type in migrations: for table, column, col_type in migrations: