update
This commit is contained in:
@@ -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('<h3>START LOOP</h3>')
|
||||
print("Getting NPM values")
|
||||
# Retrieve the last sensor readings
|
||||
cursor.execute("SELECT * FROM data ORDER BY timestamp DESC LIMIT 1")
|
||||
cursor.execute("SELECT * FROM data_NPM ORDER BY timestamp DESC LIMIT 1")
|
||||
last_row = cursor.fetchone()
|
||||
# Display the result
|
||||
if last_row:
|
||||
pm1_value = last_row[1] # Adjust the index based on the column order in your table
|
||||
print("Last available row:", last_row)
|
||||
print("SQLite DB last available row:", last_row)
|
||||
PM1 = last_row[1]
|
||||
PM25 = last_row[2]
|
||||
PM10 = last_row[3]
|
||||
npm_temp = last_row[4]
|
||||
npm_hum = last_row[5]
|
||||
|
||||
#Add data to payload CSV
|
||||
payload_csv[0] = PM1
|
||||
payload_csv[1] = PM25
|
||||
payload_csv[2] = PM10
|
||||
payload_csv[18] = npm_temp
|
||||
payload_csv[19] = npm_hum
|
||||
|
||||
#Add data to payload JSON
|
||||
payload_json["sensordatavalues"].append({"value_type": "NPM_P0", "value": str(PM1)})
|
||||
payload_json["sensordatavalues"].append({"value_type": "NPM_P1", "value": str(PM10)})
|
||||
payload_json["sensordatavalues"].append({"value_type": "NPM_P2", "value": str(PM25)})
|
||||
else:
|
||||
print("No data available in the database.")
|
||||
|
||||
@@ -305,6 +331,162 @@ try:
|
||||
'''
|
||||
SEND TO AIRCARTO
|
||||
'''
|
||||
# Write Data to saraR4
|
||||
# 1. Open sensordata_csv.json (with correct data size)
|
||||
csv_string = ','.join(str(value) if value is not None else '' for value in payload_csv)
|
||||
size_of_string = len(csv_string)
|
||||
print("Open JSON:")
|
||||
command = f'AT+UDWNFILE="sensordata_csv.json",{size_of_string}\r'
|
||||
ser_sara.write(command.encode('utf-8'))
|
||||
response_SARA_1 = read_complete_response(ser_sara, wait_for_line=">", debug=False)
|
||||
print(response_SARA_1)
|
||||
time.sleep(1)
|
||||
|
||||
#2. Write to shell
|
||||
print("Write data to memory:")
|
||||
ser_sara.write(csv_string.encode())
|
||||
response_SARA_2 = read_complete_response(ser_sara, wait_for_line="OK", debug=False)
|
||||
print(response_SARA_2)
|
||||
|
||||
#3. Send to endpoint (with device ID)
|
||||
print("Send data (POST REQUEST):")
|
||||
command= f'AT+UHTTPC={aircarto_profile_id},4,"/pro_4G/data.php?sensor_id={device_id}?timestamp=000","server_response.txt","sensordata_csv.json",4\r'
|
||||
ser_sara.write(command.encode('utf-8'))
|
||||
|
||||
response_SARA_3 = read_complete_response(ser_sara, timeout=5, end_of_response_timeout=120, wait_for_line="+UUHTTPCR")
|
||||
|
||||
print('<p class="text-danger-emphasis">')
|
||||
print(response_SARA_3)
|
||||
print("</p>")
|
||||
|
||||
# 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: <profile_id>,<http_command>,<http_result>
|
||||
# <http_result>: 1 pour sucess et 0 pour fail
|
||||
# +UUHTTPCR: 0,4,1 -> OK
|
||||
# +UUHTTPCR: 0,4,0 -> error
|
||||
|
||||
# Split response into lines
|
||||
lines = response_SARA_3.strip().splitlines()
|
||||
|
||||
# 1.Vérifier si la réponse contient un message d'erreur CME
|
||||
if "+CME ERROR" in lines[-1]:
|
||||
print("*****")
|
||||
print('<span style="color: red;font-weight: bold;">ATTENTION: CME ERROR</span>')
|
||||
print("error:", lines[-1])
|
||||
print("*****")
|
||||
#update status
|
||||
update_json_key(config_file, "SARA_R4_network_status", "disconnected")
|
||||
|
||||
# Gestion de l'erreur spécifique
|
||||
if "No connection to phone" in lines[-1]:
|
||||
print("No connection to the phone. Retrying or reset may be required.")
|
||||
# Actions spécifiques pour ce type d'erreur (par exemple, réinitialiser ou tenter de reconnecter)
|
||||
# need to reconnect to network
|
||||
# and reset HTTP profile (AT+UHTTP=0) -> ne fonctionne pas..
|
||||
# tester un reset avec CFUN 15
|
||||
# 1.Reconnexion au réseau (AT+COPS)
|
||||
command = f'AT+COPS=1,2,"{selected_networkID}"\r'
|
||||
ser_sara.write(command.encode('utf-8'))
|
||||
responseReconnect = read_complete_response(ser_sara)
|
||||
print("Response reconnect:")
|
||||
print(responseReconnect)
|
||||
print("End response reconnect")
|
||||
|
||||
elif "Operation not allowed" in lines[-1]:
|
||||
print("Operation not allowed. This may require a different configuration.")
|
||||
# Actions spécifiques pour ce type d'erreur
|
||||
|
||||
# Clignotement LED rouge en cas d'erreur
|
||||
led_thread = Thread(target=blink_led, args=(24, 5, 0.5))
|
||||
led_thread.start()
|
||||
|
||||
else:
|
||||
# 2.Si la réponse contient une réponse HTTP valide
|
||||
# Extract HTTP response code from the last line
|
||||
# ATTENTION: lines[-1] renvoie l'avant dernière ligne et il peut y avoir un soucis avec le OK
|
||||
# rechercher plutot
|
||||
http_response = lines[-1] # "+UUHTTPCR: 0,4,0"
|
||||
parts = http_response.split(',')
|
||||
|
||||
# 2.1 code 0 (HTTP failed)
|
||||
if len(parts) == 3 and parts[-1] == '0': # The third value indicates success
|
||||
print("*****")
|
||||
print('<span style="color: red;font-weight: bold;">ATTENTION: HTTP operation failed</span>')
|
||||
update_json_key(config_file, "SARA_R4_network_status", "disconnected")
|
||||
print("*****")
|
||||
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()
|
||||
|
||||
# Get error code
|
||||
print("Getting error code (11->Server connection error, 73->Secure socket connect error)")
|
||||
command = f'AT+UHTTPER={aircarto_profile_id}\r'
|
||||
ser_sara.write(command.encode('utf-8'))
|
||||
response_SARA_9 = read_complete_response(ser_sara, wait_for_line="OK", debug=False)
|
||||
print('<p class="text-danger-emphasis">')
|
||||
print(response_SARA_9)
|
||||
print("</p>")
|
||||
|
||||
'''
|
||||
+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('<span class="badge text-bg-success">HTTP operation successful.</span>')
|
||||
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('<p class="text-success">')
|
||||
print(response_SARA_4)
|
||||
print('</p>')
|
||||
else:
|
||||
print('<span style="color: red;font-weight: bold;">No UUHTTPCR response</span>')
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user