This commit is contained in:
PaulVua
2025-01-13 17:51:12 +01:00
parent b411d37d01
commit 3414a0db1a
2 changed files with 38 additions and 39 deletions

View File

@@ -1,5 +1,13 @@
'''
Script to set the URL for a HTTP request
Ex:
/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/sara_setURL.py ttyAMA2 data.nebuleair.fr
To do: need to add profile id as parameter
First profile id:
AT+UHTTP=0,1,"data.nebuleair.fr"
Second profile id:
AT+UHTTP=1,1,"api-prod.uspot.probesys.net"
'''
import serial

View File

@@ -6,7 +6,11 @@ Main loop to gather data from sensor:
* Noise sensor
and send it to AirCarto servers via SARA R4 HTTP post requests
CSV PAYLOAD
CSV PAYLOAD (AirCarto Servers)
Endpoint:
data.nebuleair.fr
/pro_4G/data.php?sensor_id={device_id}
ATTENTION : do not change order !
{PM1},{PM25},{PM10},{temp},{hum},{press},{avg_noise},{max_noise},{min_noise},{envea_no2},{envea_h2s},{envea_o3},{4g_signal_quality}
0 -> PM1
@@ -23,8 +27,13 @@ CSV PAYLOAD
11 -> envea_o3
12 -> 4G signal quality
JSON PAYLOAD
JSON PAYLOAD (Micro-Spot Servers)
Same as NebuleAir wifi
Endpoint:
api-prod.uspot.probesys.net
nebuleair?token=2AFF6dQk68daFZ
port 443
{"nebuleairid": "82D25549434",
"software_version": "ModuleAirV2-V1-042022",
"sensordatavalues":
@@ -46,9 +55,8 @@ JSON PAYLOAD
{"value_type":"longitude","value":"5.36978"},
{"value_type":"state_npm","value":"State: 00000000"},
{"value_type":"th_npm","value":"28.47 / 37.54"}
]}
]
}
"""
import board
import json
@@ -82,6 +90,7 @@ def load_config(config_file):
print(f"Error loading config file: {e}")
return {}
#Fonction pour mettre à jour le JSON de configuration
def update_json_key(file_path, key, value):
"""
Updates a specific key in a JSON file with a new value.
@@ -115,17 +124,19 @@ config_file = '/var/www/nebuleair_pro_4g/config.json'
# Load the configuration data
config = load_config(config_file)
# Access the shared variables
baudrate = config.get('SaraR4_baudrate', 115200) #baudrate du sara R4
device_id = config.get('deviceID', '').upper() #device ID en maj
need_to_log = config.get('loop_log', False) #inscription des logs
bme_280_config = config.get('i2c_BME', False) #présence du BME280
i2C_sound_config = config.get('i2C_sound', False) #présence du capteur son
send_aircarto = config.get('send_aircarto', True) #envoi sur AirCarto (data.nebuleair.fr)
send_uSpot = config.get('send_uSpot', False) #envoi sur MicroSpot ()
envea_sondes = config.get('envea_sondes', [])
connected_envea_sondes = [sonde for sonde in envea_sondes if sonde.get('connected', False)]
selected_networkID = config.get('SARA_R4_neworkID', '')
ser_sara = serial.Serial(
port='/dev/ttyAMA2',
baudrate=baudrate, #115200 ou 9600
@@ -188,18 +199,9 @@ try:
PM25 = int.from_bytes(byte_data[11:13], byteorder='big') / 10
PM10 = int.from_bytes(byte_data[13:15], byteorder='big') / 10
# Create a dictionary with the parsed data
data = {
'sondeID': device_id,
'PM1': PM1,
'PM25': PM25,
'PM10': PM10
}
message = f"{data['PM1']},{data['PM25']},{data['PM10']}"
payload[0] = data['PM1']
payload[1] = data['PM25']
payload[2] = data['PM10']
payload[0] = PM1
payload[1] = PM25
payload[2] = PM10
# Sonde BME280 connected
if bme_280_config:
@@ -207,13 +209,10 @@ try:
i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76)
bme280.sea_level_pressure = 1013.25 # Update this value for your location
data['temp'] = round(bme280.temperature, 2)
data['hum'] = round(bme280.humidity, 2)
data['press'] = round(bme280.pressure, 2)
message += f",{data['temp']},{data['hum']},{data['press']}"
payload[3] = data['temp']
payload[4] = data['hum']
payload[5] = data['press']
payload[3] = round(bme280.temperature, 2)
payload[4] = round(bme280.humidity, 2)
payload[5] = round(bme280.pressure, 2)
# Sonde Bruit connected
if i2C_sound_config:
@@ -225,18 +224,10 @@ try:
content = file.read().strip()
avg_noise, max_noise, min_noise = map(int, content.split())
# Append the variables to the JSON and to the message
data['avg_noise'] = avg_noise
data['max_noise'] = max_noise
data['min_noise'] = min_noise
#get BME280 data (SAFE: it returns none if the key do not exist)
message = f"{data.get('PM1', '')},{data.get('PM25', '')},{data.get('PM10', '')},{data.get('temp', '')},{data.get('hum', '')},{data.get('press', '')},{avg_noise},{max_noise},{min_noise}"
payload[6] = data['avg_noise']
payload[7] = data['max_noise']
payload[8] = data['min_noise']
print(message) # Display the message or send it further
# Append the variables to the payload
payload[6] = avg_noise
payload[7] = max_noise
payload[8] = min_noise
except FileNotFoundError:
print(f"Error: File {file_path} not found.")