update
This commit is contained in:
@@ -501,107 +501,86 @@ try:
|
||||
'''
|
||||
if send_uSpot:
|
||||
print(">>>>>>>>")
|
||||
print("SEND TO MICRO SPOT:")
|
||||
print(">>>>>>>>")
|
||||
print(">>>>>>>>")
|
||||
print("SEND TO MICRO SPOT (HTTP):")
|
||||
profile_id = 1
|
||||
|
||||
#step 4: set url (op_code = 1)
|
||||
print("****")
|
||||
print("SET URL")
|
||||
command = f'AT+UHTTP={profile_id},1,"api-prod.uspot.probesys.net"\r'
|
||||
ser_sara.write((command + '\r').encode('utf-8'))
|
||||
response_SARA_5 = read_complete_response(ser_sara, wait_for_line="OK")
|
||||
print(response_SARA_5)
|
||||
time.sleep(1)
|
||||
|
||||
#step 4: set url to SSL (op_code = 6) (http_secure = 1 for HTTPS)(USECMNG_PROFILE = 2)
|
||||
print("****")
|
||||
print("SET SSL")
|
||||
command = f'AT+UHTTP={profile_id},6,0\r'
|
||||
ser_sara.write(command.encode('utf-8'))
|
||||
response_SARA_5 = read_complete_response(ser_sara, wait_for_line="OK")
|
||||
print(response_SARA_5)
|
||||
time.sleep(1)
|
||||
|
||||
#step 4: set PORT (op_code = 5)
|
||||
print("****")
|
||||
print("SET PORT")
|
||||
command = f'AT+UHTTP={profile_id},5,81\r'
|
||||
ser_sara.write((command + '\r').encode('utf-8'))
|
||||
response_SARA_55 = read_complete_response(ser_sara, wait_for_line="OK")
|
||||
print(response_SARA_55)
|
||||
time.sleep(1)
|
||||
|
||||
payload_string = json.dumps(payload_json) # Convert dict to JSON string
|
||||
|
||||
# Write Data to saraR4
|
||||
# 1. Open sensordata_csv.json (with correct data size)
|
||||
|
||||
|
||||
# 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_1 = read_complete_response(ser_sara)
|
||||
print("Open JSON:")
|
||||
response_SARA_1 = read_complete_response(ser_sara, wait_for_line=">")
|
||||
print(response_SARA_1)
|
||||
time.sleep(1)
|
||||
|
||||
#2. Write to shell
|
||||
print("Write to memory:")
|
||||
ser_sara.write(payload_string.encode())
|
||||
response_SARA_2 = read_complete_response(ser_sara)
|
||||
if need_to_log:
|
||||
print("Write to memory:")
|
||||
print('<p class="fw-bold">'+response_SARA_2+'</p>')
|
||||
response_SARA_2 = read_complete_response(ser_sara, wait_for_line="OK")
|
||||
print(response_SARA_2)
|
||||
|
||||
#3. Send to endpoint (with device ID)
|
||||
command= f'AT+UHTTPC=1,4,"nebuleair?token=2AFF6dQk68daFZ","server_response2.txt","sensordata_json.json",4\r'
|
||||
ser_sara.write((command + '\r').encode('utf-8'))
|
||||
response_SARA_3 = read_complete_response(ser_sara)
|
||||
|
||||
print("Send data:")
|
||||
print(response_SARA_3)
|
||||
#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={profile_id},4,"/nebuleair?token=2AFF6dQk68daFZ","http.resp","sensordata_json.json",4\r'
|
||||
ser_sara.write(command.encode('utf-8'))
|
||||
|
||||
lines = response_SARA_3.strip().splitlines()
|
||||
|
||||
response_SARA_3 = read_complete_response(ser_sara, timeout=5, end_of_response_timeout=30, wait_for_line="+UUHTTPCR")
|
||||
print('<p class="text-danger-emphasis">')
|
||||
print(response_SARA_3)
|
||||
print("</p>")
|
||||
|
||||
# 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")
|
||||
#READ REPLY
|
||||
print("****")
|
||||
print("Read reply from server")
|
||||
ser_sara.write(b'AT+URDFILE="http.resp"\r')
|
||||
response_SARA_7 = read_complete_response(ser_sara, wait_for_line="OK")
|
||||
print('<p class="text-success">')
|
||||
print(response_SARA_7)
|
||||
print('</p>')
|
||||
|
||||
# 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 + '\r').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
|
||||
|
||||
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>')
|
||||
print("*****")
|
||||
|
||||
|
||||
# 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("Turning on the blue LED...")
|
||||
for _ in range(4): # Faire clignoter 4 fois
|
||||
GPIO.output(23, GPIO.HIGH) # Allumer la LED
|
||||
time.sleep(0.1) # Attendre 100 ms
|
||||
GPIO.output(23, GPIO.LOW) # Éteindre la LED
|
||||
time.sleep(0.1) # Attendre 100 ms
|
||||
GPIO.output(23, GPIO.HIGH) # Turn on the LED
|
||||
#4. Read reply from server
|
||||
ser_sara.write(b'AT+URDFILE="server_response2.txt"\r')
|
||||
response_SARA_4 = read_complete_response(ser_sara)
|
||||
if need_to_log:
|
||||
print("Reply from server:")
|
||||
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_5 = read_complete_response(ser_sara)
|
||||
if need_to_log:
|
||||
print("Empty JSON:")
|
||||
print(response_SARA_5)
|
||||
response_SARA_8 = read_complete_response(ser_sara, wait_for_line="OK")
|
||||
print(response_SARA_8)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user