update
This commit is contained in:
@@ -129,6 +129,7 @@ try:
|
||||
time.sleep(1)
|
||||
|
||||
#2. Set uSpot URL
|
||||
print('Set uSpot URL')
|
||||
uSpot_profile_id = 1
|
||||
uSpot_url="api-prod.uspot.probesys.net"
|
||||
command = f'AT+UHTTP={uSpot_profile_id},1,"{uSpot_url}"\r'
|
||||
@@ -137,6 +138,13 @@ try:
|
||||
print(response_SARA_2)
|
||||
time.sleep(1)
|
||||
|
||||
print("set port 81")
|
||||
command = f'AT+UHTTP={uSpot_profile_id},5,81\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)
|
||||
|
||||
#3. Get localisation (CellLocate)
|
||||
mode = 2
|
||||
sensor = 2
|
||||
|
||||
@@ -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()
|
||||
@@ -514,6 +516,179 @@ try:
|
||||
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
|
||||
#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="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)
|
||||
print('</p>')
|
||||
|
||||
#Si non ne recoit pas de réponse UHTTPCR
|
||||
#on a peut etre une ERROR de type "+CME ERROR: No connection to phone"
|
||||
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()
|
||||
#Vérification de l'erreur
|
||||
print("Getting type of error")
|
||||
# Split the response into lines and search for "+CME ERROR:"
|
||||
lines2 = response_SARA_3.strip().splitlines()
|
||||
for line in lines2:
|
||||
if "+CME ERROR" in line:
|
||||
error_message = line.split("+CME ERROR:")[1].strip()
|
||||
print("*****")
|
||||
print('<span style="color: red;font-weight: bold;">⚠️ATTENTION: CME ERROR⚠️</span>')
|
||||
print(f"Error type: {error_message}")
|
||||
print("*****")
|
||||
# Handle "No connection to phone" error
|
||||
if error_message == "No connection to phone":
|
||||
print('<span style="color: orange;font-weight: bold;">📞Try reconnect to network📞</span>')
|
||||
#IMPORTANT!
|
||||
# Reconnexion au réseau (AT+COPS)
|
||||
#command = f'AT+COPS=1,2,{selected_networkID}\r'
|
||||
command = f'AT+COPS=0\r'
|
||||
ser_sara.write(command.encode('utf-8'))
|
||||
responseReconnect = read_complete_response(ser_sara, timeout=5, end_of_response_timeout=120, wait_for_lines=["OK", "+CME ERROR"], debug=True)
|
||||
print('<p class="text-danger-emphasis">')
|
||||
print(responseReconnect)
|
||||
print("</p>")
|
||||
# Handle "Operation not allowed" error
|
||||
if error_message == "Operation not allowed":
|
||||
print('<span style="color: orange;font-weight: bold;">❓Try Resetting the HTTP Profile❓</span>')
|
||||
command = f'AT+UHTTP={aircarto_profile_id},1,"data.nebuleair.fr"\r'
|
||||
ser_sara.write(command.encode('utf-8'))
|
||||
responseResetHTTP_profile = read_complete_response(ser_sara, timeout=5, end_of_response_timeout=5, wait_for_lines=["OK", "+CME ERROR"], debug=True)
|
||||
print('<p class="text-danger-emphasis">')
|
||||
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)
|
||||
|
||||
|
||||
'''
|
||||
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
|
||||
@@ -557,15 +732,10 @@ try:
|
||||
'''
|
||||
|
||||
#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
|
||||
# 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,60 +743,20 @@ 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="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>')
|
||||
|
||||
#Si non ne recoit pas de réponse UHTTPCR
|
||||
#on a peut etre une ERROR de type "+CME ERROR: No connection to phone"
|
||||
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()
|
||||
#Vérification de l'erreur
|
||||
print("Getting type of error")
|
||||
# Split the response into lines and search for "+CME ERROR:"
|
||||
lines2 = response_SARA_3.strip().splitlines()
|
||||
for line in lines2:
|
||||
if "+CME ERROR" in line:
|
||||
error_message = line.split("+CME ERROR:")[1].strip()
|
||||
print("*****")
|
||||
print('<span style="color: red;font-weight: bold;">⚠️ATTENTION: CME ERROR⚠️</span>')
|
||||
print(f"Error type: {error_message}")
|
||||
print("*****")
|
||||
# Handle "No connection to phone" error
|
||||
if error_message == "No connection to phone":
|
||||
print('<span style="color: orange;font-weight: bold;">📞Try reconnect to network📞</span>')
|
||||
#IMPORTANT!
|
||||
# Reconnexion au réseau (AT+COPS)
|
||||
#command = f'AT+COPS=1,2,{selected_networkID}\r'
|
||||
command = f'AT+COPS=0\r'
|
||||
ser_sara.write(command.encode('utf-8'))
|
||||
responseReconnect = read_complete_response(ser_sara, timeout=5, end_of_response_timeout=120, wait_for_lines=["OK", "+CME ERROR"], debug=True)
|
||||
print('<p class="text-danger-emphasis">')
|
||||
print(responseReconnect)
|
||||
print("</p>")
|
||||
# Handle "Operation not allowed" error
|
||||
if error_message == "Operation not allowed":
|
||||
print('<span style="color: orange;font-weight: bold;">❓Try Resetting the HTTP Profile❓</span>')
|
||||
command = f'AT+UHTTP={aircarto_profile_id},1,"data.nebuleair.fr"\r'
|
||||
ser_sara.write(command.encode('utf-8'))
|
||||
responseResetHTTP_profile = read_complete_response(ser_sara, timeout=5, end_of_response_timeout=5, wait_for_lines=["OK", "+CME ERROR"], debug=True)
|
||||
print('<p class="text-danger-emphasis">')
|
||||
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)
|
||||
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