diff --git a/loop/1_NPM/send_data.py b/loop/1_NPM/send_data.py index 8107bf1..c574041 100755 --- a/loop/1_NPM/send_data.py +++ b/loop/1_NPM/send_data.py @@ -74,7 +74,7 @@ import RPi.GPIO as GPIO from adafruit_bme280 import basic as adafruit_bme280 # Record the start time of the script -start_time = time.time() +start_time_script = time.time() # Check system uptime with open('/proc/uptime', 'r') as f: @@ -197,26 +197,38 @@ if connected_envea_sondes: -def read_complete_response(serial_connection, timeout=2, end_of_response_timeout=2): +def read_complete_response(serial_connection, timeout=2, end_of_response_timeout=2, wait_for_line=None): 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: data = serial_connection.read(serial_connection.in_waiting) 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: + print(f"[DEBUG] 🔎Found target line: {wait_for_line}") + break elif time.time() > end_time: + print(f"[DEBUG] Timeout reached. No more data received.") break time.sleep(0.1) # Short sleep to prevent busy waiting - - return response.decode('utf-8') + # Final response and debug output + total_elapsed_time = time.time() - start_time + print(f"[DEBUG] ⏱️ elapsed time: {total_elapsed_time:.2f}s. ⏱️") + return response.decode('utf-8', errors='replace') # Open and read the JSON file try: # Send the command to request data (e.g., data for 60 seconds) - print("SART LOOP") + print('

START LOOP

') print("Getting NPM values") ser_NPM.write(b'\x81\x12\x6D') @@ -320,9 +332,9 @@ try: print("-> Getting LTE signal <-") ser_sara.write(b'AT+CSQ\r') response2 = read_complete_response(ser_sara) - print("Response:") + print('

') print(response2) - print("<----") + print("

") match = re.search(r'\+CSQ:\s*(\d+),', response2) if match: signal_quality = match.group(1) @@ -360,9 +372,12 @@ try: 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')) - response_SARA_3 = read_complete_response(ser_sara, timeout=5) + response_SARA_3 = read_complete_response(ser_sara, timeout=5, end_of_response_timeout=10, wait_for_line="+UUHTTPCR") + + print('

') print(response_SARA_3) - + print("

") + # Wait for the +UUHTTPCR response #print("Waiting for +UUHTTPCR response...") #response_received = False @@ -373,7 +388,7 @@ try: # response_received = True if "+UUHTTPCR" in response_SARA_3: - print("Received +UUHTTPCR response.") + print("✅ Received +UUHTTPCR response.") # Les types de réponse @@ -475,6 +490,8 @@ try: print('

') print(response_SARA_4) print('

') + else: + print('No UUHTTPCR response') #5. empty json print("Empty SARA memory:") @@ -591,12 +608,10 @@ try: # Calculate and print the elapsed time - elapsed_time = time.time() - start_time + elapsed_time = time.time() - start_time_script if need_to_log: print(f"Elapsed time: {elapsed_time:.2f} seconds") - print("----------------------------------------") - print("----------------------------------------") - + print("
") except Exception as e: print("An error occurred:", e)