diff --git a/loop/SARA_send_data_v2.py b/loop/SARA_send_data_v2.py index e6ae4e5..a0fbf2c 100755 --- a/loop/SARA_send_data_v2.py +++ b/loop/SARA_send_data_v2.py @@ -304,6 +304,49 @@ def extract_error_code(response): # Return None if we couldn't find the error code return None +def send_error_notification(device_id, error_type, additional_info=None): + """ + Send an error notification to the server when issues with the SARA module occur. + Will silently fail if there's no internet connection. + + Parameters: + ----------- + device_id : str + The unique identifier of the device + error_type : str + Type of error encountered (e.g., 'serial_error', 'cme_error', 'http_error', 'timeout') + additional_info : str, optional + Any additional information about the error for logging purposes + + Returns: + -------- + bool + True if notification was sent successfully, False otherwise + """ + + # Create the alert URL with all relevant parameters + base_url = 'http://data.nebuleair.fr/pro_4G/alert.php' + alert_url = f'{base_url}?capteur_id={device_id}&error_type={error_type}' + + # Add additional info if provided + if additional_info: + # Make sure to URL encode the additional info + from urllib.parse import quote + alert_url += f'&details={quote(str(additional_info))}' + + # Try to send the notification, catch ALL exceptions + try: + response = requests.post(alert_url, timeout=3) + if response.status_code == 200: + print(f"✅ Alert notification sent successfully") + return True + else: + print(f"⚠️ Alert notification failed: Status code {response.status_code}") + except Exception as e: + print(f"⚠️ Alert notification couldn't be sent: {e}") + + return False + def modem_complete_reboot_and_reinitialize(modem_version, aircarto_profile_id): """ Performs a complete modem restart sequence: @@ -611,17 +654,9 @@ try: print('🛑STOP LOOP🛑') print("
') + print(command) + print("
", end="") 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", "ERROR"], debug=True) - + print("receiving:") print('') print(response_SARA_3) print("
", end="") @@ -898,7 +937,7 @@ try: #Si non ne recoit pas de réponse UHTTPCR - #on a peut etre une ERROR de type "+CME ERROR: No connection to phone" ou "Operation not allowed" + #on a peut etre une ERROR de type "+CME ERROR: No connection to phone" ou "Operation not allowed" ou "ERROR" else: print('No UUHTTPCR response') print("Blink red LED") @@ -946,6 +985,9 @@ try: if "ERROR" in line: print("⛔Attention ERROR!⛔") + #Send notification (WIFI) + send_error_notification(device_id, "sara_error") + #Software Reboot software_reboot_success = modem_complete_reboot_and_reinitialize(modem_version, aircarto_profile_id) if software_reboot_success: