update
This commit is contained in:
@@ -435,6 +435,8 @@ try:
|
||||
'''
|
||||
SEND TO AIRCARTO
|
||||
'''
|
||||
|
||||
print('➡️<p class="fw-bold">SEND TO AIRCARTO SERVERS</p>')
|
||||
# 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)
|
||||
@@ -454,7 +456,7 @@ try:
|
||||
|
||||
#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}&lat{device_latitude_raw}=&long={device_longitude_raw}&datetime={influx_timestamp}","server_response.txt","sensordata_csv.json",4\r'
|
||||
command= f'AT+UHTTPC={aircarto_profile_id},4,"/pro_4G/data.php?sensor_id={device_id}&lat{device_latitude_raw}=&long={device_longitude_raw}&datetime={influx_timestamp}","aircarto_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_lines=["+UUHTTPCR", "+CME ERROR"], debug=True)
|
||||
@@ -476,8 +478,8 @@ try:
|
||||
# 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
|
||||
# +UUHTTPCR: 0,4,1 -> OK ✅
|
||||
# +UUHTTPCR: 0,4,0 -> error ⛔
|
||||
|
||||
# Split response into lines
|
||||
lines = response_SARA_3.strip().splitlines()
|
||||
@@ -522,7 +524,7 @@ try:
|
||||
http_response = lines[-1] # "+UUHTTPCR: 0,4,0"
|
||||
parts = http_response.split(',')
|
||||
|
||||
# 2.1 code 0 (HTTP failed)
|
||||
# 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>')
|
||||
@@ -565,7 +567,7 @@ try:
|
||||
|
||||
# 2.2 code 1 (HHTP succeded)
|
||||
else:
|
||||
# Si la commande HTTP a réussi
|
||||
# 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")
|
||||
@@ -573,7 +575,7 @@ try:
|
||||
led_thread.start()
|
||||
#4. Read reply from server
|
||||
print("Reply from server:")
|
||||
ser_sara.write(b'AT+URDFILE="server_response.txt"\r')
|
||||
ser_sara.write(b'AT+URDFILE="aircarto_server_response.txt"\r')
|
||||
response_SARA_4 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False)
|
||||
print('<p class="text-success">')
|
||||
print(response_SARA_4)
|
||||
@@ -620,13 +622,141 @@ try:
|
||||
print(responseResetHTTP_profile)
|
||||
print("</p>")
|
||||
|
||||
|
||||
|
||||
#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_lines=["OK"], debug=False)
|
||||
print(response_SARA_5)
|
||||
print(response_SARA_5)
|
||||
|
||||
|
||||
'''
|
||||
SEND TO uSPOT
|
||||
'''
|
||||
|
||||
if send_uSpot:
|
||||
print('➡️<p class="fw-bold">SEND TO uSPOT SERVERS</p>')
|
||||
|
||||
# 1. Open sensordata_json.json (with correct data size)
|
||||
print("Open JSON:")
|
||||
payload_string = json.dumps(payload_json) # Convert dict to JSON string
|
||||
size_of_string = len(payload_string)
|
||||
command = f'AT+UDWNFILE="sensordata_json.json",{size_of_string}\r'
|
||||
ser_sara.write((command + '\r').encode('utf-8'))
|
||||
response_SARA_6 = read_complete_response(ser_sara, wait_for_lines=[">"], debug=False)
|
||||
print(response_SARA_6)
|
||||
time.sleep(1)
|
||||
|
||||
#2. Write to shell
|
||||
print("Write to memory:")
|
||||
ser_sara.write(payload_string.encode())
|
||||
response_SARA_7 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False)
|
||||
print(response_SARA_7)
|
||||
|
||||
#step 4: trigger the request (http_command=1 for GET and http_command=1 for POST)
|
||||
print("****")
|
||||
print("Trigger POST REQUEST")
|
||||
command = f'AT+UHTTPC={uSpot_profile_id},4,"/nebuleair?token=2AFF6dQk68daFZ","uSpot_server_response.txt","sensordata_json.json",4\r'
|
||||
ser_sara.write(command.encode('utf-8'))
|
||||
|
||||
response_SARA_8 = read_complete_response(ser_sara, timeout=5, end_of_response_timeout=120, wait_for_lines=["+UUHTTPCR", "+CME ERROR"], debug=True)
|
||||
|
||||
print('<p class="text-danger-emphasis">')
|
||||
print(response_SARA_8)
|
||||
print("</p>")
|
||||
|
||||
# si on recoit la réponse UHTTPCR
|
||||
if "+UUHTTPCR" in response_SARA_8:
|
||||
print("✅ Received +UUHTTPCR response.")
|
||||
lines = response_SARA_8.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.")
|
||||
|
||||
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_lines=["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
|
||||
|
||||
# 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="uSpot_server_response.txt"\r')
|
||||
response_SARA_4 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False)
|
||||
print('<p class="text-success">')
|
||||
print(response_SARA_4)
|
||||
print('</p>')
|
||||
|
||||
|
||||
|
||||
#5. empty json
|
||||
print("Empty SARA memory:")
|
||||
ser_sara.write(b'AT+UDELFILE="sensordata_json.json"\r')
|
||||
response_SARA_9 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False)
|
||||
print(response_SARA_9)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user