From fb44b57ac122fcfa6abd6ba59bd75dc7c1d40c7e Mon Sep 17 00:00:00 2001 From: PaulVua Date: Fri, 7 Feb 2025 13:31:07 +0100 Subject: [PATCH] update --- README.md | 2 +- SARA/check_running.py | 20 ++++++++++ SARA/sara.py | 2 + html/saraR4.html | 1 + loop/SARA_send_data_v2.py | 84 +++++++++++++++++++++++++++++---------- 5 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 SARA/check_running.py diff --git a/README.md b/README.md index ef9d04c..ab69621 100755 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ See `installation.sh` ``` sudo apt update sudo apt install git gh apache2 php php-sqlite3 python3 python3-pip jq autossh i2c-tools python3-smbus -y -sudo pip3 install pyserial requests RPi.GPIO adafruit-circuitpython-bme280 crcmod --break-system-packages +sudo pip3 install pyserial requests RPi.GPIO adafruit-circuitpython-bme280 crcmod psutil --break-system-packages sudo mkdir -p /var/www/.ssh sudo ssh-keygen -t rsa -b 4096 -f /var/www/.ssh/id_rsa -N "" sudo ssh-copy-id -i /var/www/.ssh/id_rsa.pub -p 50221 airlab_server1@aircarto.fr diff --git a/SARA/check_running.py b/SARA/check_running.py new file mode 100644 index 0000000..fcda7e0 --- /dev/null +++ b/SARA/check_running.py @@ -0,0 +1,20 @@ +''' +Check if the main loop is running +/usr/bin/python3 /var/www/nebuleair_pro_4g/tests/check_running.py +''' +import psutil +import subprocess + +def is_script_running(script_name): + """Check if a given Python script is running.""" + for process in psutil.process_iter(['pid', 'cmdline']): + if process.info['cmdline'] and script_name in " ".join(process.info['cmdline']): + return True # Script is running + return False # Script is not running + +script_to_check = "/var/www/nebuleair_pro_4g/loop/SARA_send_data_v2.py" + +if is_script_running(script_to_check): + print(f"{script_to_check} is still running.❌❌❌") +else: + print(f"{script_to_check} is NOT running.✅✅✅") diff --git a/SARA/sara.py b/SARA/sara.py index 06a0f43..d124e07 100755 --- a/SARA/sara.py +++ b/SARA/sara.py @@ -6,6 +6,8 @@ ex 2 (turn on blue light): python3 /var/www/nebuleair_pro_4g/SARA/sara.py ttyAMA2 AT+UGPIOC=16,2 2 ex 3 (reconnect network) python3 /var/www/nebuleair_pro_4g/SARA/sara.py ttyAMA2 AT+COPS=1,2,20801 20 +ex 4 (get HTTP Profiles) +python3 /var/www/nebuleair_pro_4g/SARA/sara.py ttyAMA2 AT+UHTTP? 2 ''' diff --git a/html/saraR4.html b/html/saraR4.html index 75afe68..f358748 100755 --- a/html/saraR4.html +++ b/html/saraR4.html @@ -51,6 +51,7 @@

Modem 4G

Votre capteur est équipé d'un modem 4G et d'une carte SIM afin d'envoyer les mesures sur internet.

+

Verification utilisation du Modem:

Status Loading... diff --git a/loop/SARA_send_data_v2.py b/loop/SARA_send_data_v2.py index e7f2e2b..8e3c38e 100644 --- a/loop/SARA_send_data_v2.py +++ b/loop/SARA_send_data_v2.py @@ -218,15 +218,19 @@ ser_sara = serial.Serial( timeout = 2 ) -def read_complete_response(serial_connection, timeout=2, end_of_response_timeout=2, wait_for_line=None, debug=True): +def read_complete_response(serial_connection, timeout=2, end_of_response_timeout=2, wait_for_lines=None, debug=True): ''' Fonction très importante !!! + Reads the complete response from a serial connection and waits for specific lines. ''' + if wait_for_lines is None: + wait_for_lines = [] # Default to an empty list if not provided + response = bytearray() serial_connection.timeout = timeout end_time = time.time() + end_of_response_timeout start_time = time.time() - + while True: elapsed_time = time.time() - start_time # Time since function start if serial_connection.in_waiting > 0: @@ -234,25 +238,29 @@ def read_complete_response(serial_connection, timeout=2, end_of_response_timeout response.extend(data) end_time = time.time() + end_of_response_timeout # Reset timeout on new data - # Decode and check for the specific line - if wait_for_line: - decoded_response = response.decode('utf-8', errors='replace') - if wait_for_line in decoded_response: - if debug: print(f"[DEBUG] 🔎Found target line: {wait_for_line}") - break + # Decode and check for any target line + decoded_response = response.decode('utf-8', errors='replace') + for target_line in wait_for_lines: + if target_line in decoded_response: + if debug: + print(f"[DEBUG] 🔎 Found target line: {target_line}") + return decoded_response # Return response immediately if a target line is found elif time.time() > end_time: - if debug: print(f"[DEBUG] Timeout reached. No more data received.") + if debug: + print(f"[DEBUG] Timeout reached. No more data received.") break time.sleep(0.1) # Short sleep to prevent busy waiting + # Final response and debug output total_elapsed_time = time.time() - start_time - if debug: print(f"[DEBUG] ⏱️ elapsed time: {total_elapsed_time:.2f}s. ⏱️") + if debug: + print(f"[DEBUG] ⏱️ elapsed time: {total_elapsed_time:.2f}s. ⏱️") # Check if the elapsed time exceeded 10 seconds if total_elapsed_time > 10 and debug: - print(f"[ALERT] 🚨 The operation took too long🚨") + print(f"[ALERT] 🚨 The operation took too long 🚨") print(f'[ALERT] ⚠️{total_elapsed_time:.2f}s⚠️') - return response.decode('utf-8', errors='replace') + return response.decode('utf-8', errors='replace') # Return the full response if no target line is found try: ''' @@ -297,7 +305,7 @@ try: # Getting the LTE Signal print("-> Getting LTE signal <-") ser_sara.write(b'AT+CSQ\r') - response2 = read_complete_response(ser_sara, wait_for_line="OK") + response2 = read_complete_response(ser_sara, wait_for_lines=["OK"]) print('

') print(response2) print("

") @@ -338,22 +346,22 @@ try: print("Open JSON:") command = f'AT+UDWNFILE="sensordata_csv.json",{size_of_string}\r' ser_sara.write(command.encode('utf-8')) - response_SARA_1 = read_complete_response(ser_sara, wait_for_line=">", debug=False) + response_SARA_1 = read_complete_response(ser_sara, wait_for_lines=[">"], debug=False) print(response_SARA_1) time.sleep(1) #2. Write to shell print("Write data to memory:") ser_sara.write(csv_string.encode()) - response_SARA_2 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) + response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False) print(response_SARA_2) #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}?timestamp=000","server_response.txt","sensordata_csv.json",4\r' + command= f'AT+UHTTPC={aircarto_profile_id},4,"/pro_4G/data.php?sensor_id={device_id}&datetime=000","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_line="+UUHTTPCR") + response_SARA_3 = read_complete_response(ser_sara, timeout=5, end_of_response_timeout=120, wait_for_lines=["+UUHTTPCR", "+CME ERROR"], debug=True) print('

') print(response_SARA_3) @@ -433,7 +441,7 @@ try: 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_line="OK", debug=False) + response_SARA_9 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False) print('

') print(response_SARA_9) print("

") @@ -470,22 +478,58 @@ try: #4. Read reply from server print("Reply from server:") ser_sara.write(b'AT+URDFILE="server_response.txt"\r') - response_SARA_4 = read_complete_response(ser_sara, wait_for_line="OK", debug=False) + response_SARA_4 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False) print('

') print(response_SARA_4) print('

') + + #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('No UUHTTPCR response') 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('⚠️ATTENTION: CME ERROR⚠️') + print(f"Error type: {error_message}") + print("*****") + # Handle "No connection to phone" error + if error_message == "No connection to phone": + print('📞Try reconnect to network📞') + #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('

') + print(responseReconnect) + print("

") + # Handle "Operation not allowed" error + if error_message == "Operation not allowed": + print('❓Try Resetting the HTTP Profile❓') + 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('

') + print(responseResetHTTP_profile) + print("

") + #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_line="OK", debug=False) + response_SARA_5 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False) print(response_SARA_5)