diff --git a/loop/1_NPM/send_data.py b/loop/1_NPM/send_data.py index fa616f2..9ed00b0 100755 --- a/loop/1_NPM/send_data.py +++ b/loop/1_NPM/send_data.py @@ -216,6 +216,8 @@ def read_complete_response(serial_connection, timeout=2, end_of_response_timeout # Open and read the JSON file try: # Send the command to request data (e.g., data for 60 seconds) + print("SART LOOP") + print("Getting NPM values") ser_NPM.write(b'\x81\x12\x6D') # Read the response @@ -242,6 +244,7 @@ try: # Sonde BME280 connected if bme_280_config: + print("Getting BME280 values") #on récupère les infos du BME280 et on les ajoute au payload_csv i2c = busio.I2C(board.SCL, board.SDA) bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76) @@ -314,7 +317,7 @@ try: print(f"No serial connection for {name}") # Getting the LTE Signal - print("-> Getting signal <-") + print("-> Getting LTE signal <-") ser_sara.write(b'AT+CSQ\r') response2 = read_complete_response(ser_sara) print("Response:") @@ -349,122 +352,120 @@ try: ser_sara.write(csv_string.encode()) response_SARA_2 = read_complete_response(ser_sara) if need_to_log: - print("Write to memory:") + print("Write data to memory:") print(response_SARA_2) #3. Send to endpoint (with device ID) print("Send data:") command= f'AT+UHTTPC=0,4,"/pro_4G/data.php?sensor_id={device_id}","server_response.txt","sensordata_csv.json",4\r' ser_sara.write(command.encode('utf-8')) - - print("Waiting for +UUHTTPCR response...") - response_received = False - while not response_received: - response_SARA_3 = read_complete_response(ser_sara, timeout=5) - print(response_SARA_3) - if "+UUHTTPCR" in response_SARA_3: - response_received = True - # Les types de réponse + response_SARA_3 = read_complete_response(ser_sara, timeout=5) + print(response_SARA_3) + if "+UUHTTPCR" in response_SARA_3: + print("Received +UUHTTPCR response.") + response_received = True - # 1.La commande n'a pas fonctionné - # +CME ERROR: No connection to phone - # +CME ERROR: Operation not allowed + # Les types de réponse - # 2.La commande fonctionne: elle renvoie un code - # +UUHTTPCR: ,, - # : 1 pour sucess et 0 pour fail - # +UUHTTPCR: 0,4,1 -> OK - # +UUHTTPCR: 0,4,0 -> error + # 1.La commande n'a pas fonctionné + # +CME ERROR: No connection to phone + # +CME ERROR: Operation not allowed - # Split response into lines - lines = response_SARA_3.strip().splitlines() + # 2.La commande fonctionne: elle renvoie un code + # +UUHTTPCR: ,, + # : 1 pour sucess et 0 pour fail + # +UUHTTPCR: 0,4,1 -> OK + # +UUHTTPCR: 0,4,0 -> error - # 1.Vérifier si la réponse contient un message d'erreur CME - if "+CME ERROR" in lines[-1]: + # 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('ATTENTION: CME ERROR') + 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 en cas d'erreur + GPIO.output(23, GPIO.LOW) # Éteindre la LED définitivement + for _ in range(4): + GPIO.output(23, GPIO.HIGH) # Allumer la LED + time.sleep(0.1) + GPIO.output(23, GPIO.LOW) # Éteindre la LED + time.sleep(0.1) + GPIO.output(23, GPIO.LOW) # Turn off the LED + + 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('ATTENTION: CME ERROR') - print("error:", lines[-1]) - print("*****") - #update status + print('ATTENTION: HTTP operation failed') 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 en cas d'erreur - GPIO.output(23, GPIO.LOW) # Éteindre la LED définitivement - for _ in range(4): + print("*****") + print("resetting the URL (domain name):") + print("Turning off the blue LED...") + for _ in range(4): # Faire clignoter 4 fois GPIO.output(23, GPIO.HIGH) # Allumer la LED - time.sleep(0.1) + time.sleep(0.1) # Attendre 100 ms GPIO.output(23, GPIO.LOW) # Éteindre la LED - time.sleep(0.1) + time.sleep(0.1) # Attendre 100 ms GPIO.output(23, GPIO.LOW) # Turn off the LED + command = f'AT+UHTTP=0,1,"{url_nebuleair}"\r' + ser_sara.write(command.encode('utf-8')) + response_SARA_31 = read_complete_response(ser_sara) + if need_to_log: + print(response_SARA_31) + # 2.2 code 1 (HHTP succeded) 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('ATTENTION: HTTP operation failed') - update_json_key(config_file, "SARA_R4_network_status", "disconnected") - print("*****") - print("resetting the URL (domain name):") - print("Turning off 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.LOW) # Turn off the LED - command = f'AT+UHTTP=0,1,"{url_nebuleair}"\r' - ser_sara.write(command.encode('utf-8')) - response_SARA_31 = read_complete_response(ser_sara) - if need_to_log: - print(response_SARA_31) - - # 2.2 code 1 (HHTP succeded) - else: - # Si la commande HTTP a réussi - print('HTTP operation successful.') - 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_response.txt"\r') - response_SARA_4 = read_complete_response(ser_sara) - if need_to_log: - print("Reply from server:") - print('

') - print(response_SARA_4) - print('

') + # Si la commande HTTP a réussi + print('HTTP operation successful.') + 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_response.txt"\r') + response_SARA_4 = read_complete_response(ser_sara) + if need_to_log: + print("Reply from server:") + print('

') + print(response_SARA_4) + print('

') #5. empty json ser_sara.write(b'AT+UDELFILE="sensordata_csv.json"\r')