This commit is contained in:
PaulVua
2025-01-20 16:21:26 +01:00
parent ebc58a6c24
commit a2cc6677ff

View File

@@ -74,7 +74,7 @@ import RPi.GPIO as GPIO
from adafruit_bme280 import basic as adafruit_bme280 from adafruit_bme280 import basic as adafruit_bme280
# Record the start time of the script # Record the start time of the script
start_time = time.time() start_time_script = time.time()
# Check system uptime # Check system uptime
with open('/proc/uptime', 'r') as f: 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() response = bytearray()
serial_connection.timeout = timeout serial_connection.timeout = timeout
end_time = time.time() + end_of_response_timeout end_time = time.time() + end_of_response_timeout
start_time = time.time()
while True: while True:
elapsed_time = time.time() - start_time # Time since function start
if serial_connection.in_waiting > 0: if serial_connection.in_waiting > 0:
data = serial_connection.read(serial_connection.in_waiting) data = serial_connection.read(serial_connection.in_waiting)
response.extend(data) response.extend(data)
end_time = time.time() + end_of_response_timeout # Reset timeout on new 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: elif time.time() > end_time:
print(f"[DEBUG] Timeout reached. No more data received.")
break break
time.sleep(0.1) # Short sleep to prevent busy waiting time.sleep(0.1) # Short sleep to prevent busy waiting
# Final response and debug output
return response.decode('utf-8') 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 # Open and read the JSON file
try: try:
# Send the command to request data (e.g., data for 60 seconds) # Send the command to request data (e.g., data for 60 seconds)
print("SART LOOP") print('<h3>START LOOP</h3>')
print("Getting NPM values") print("Getting NPM values")
ser_NPM.write(b'\x81\x12\x6D') ser_NPM.write(b'\x81\x12\x6D')
@@ -320,9 +332,9 @@ try:
print("-> Getting LTE signal <-") print("-> Getting LTE signal <-")
ser_sara.write(b'AT+CSQ\r') ser_sara.write(b'AT+CSQ\r')
response2 = read_complete_response(ser_sara) response2 = read_complete_response(ser_sara)
print("Response:") print('<p class="text-danger-emphasis">')
print(response2) print(response2)
print("<----") print("</p>")
match = re.search(r'\+CSQ:\s*(\d+),', response2) match = re.search(r'\+CSQ:\s*(\d+),', response2)
if match: if match:
signal_quality = match.group(1) signal_quality = match.group(1)
@@ -360,8 +372,11 @@ try:
command= f'AT+UHTTPC=0,4,"/pro_4G/data.php?sensor_id={device_id}","server_response.txt","sensordata_csv.json",4\r' 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')) 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('<p class="text-danger-emphasis">')
print(response_SARA_3) print(response_SARA_3)
print("</p>")
# Wait for the +UUHTTPCR response # Wait for the +UUHTTPCR response
#print("Waiting for +UUHTTPCR response...") #print("Waiting for +UUHTTPCR response...")
@@ -373,7 +388,7 @@ try:
# response_received = True # response_received = True
if "+UUHTTPCR" in response_SARA_3: if "+UUHTTPCR" in response_SARA_3:
print("Received +UUHTTPCR response.") print("Received +UUHTTPCR response.")
# Les types de réponse # Les types de réponse
@@ -475,6 +490,8 @@ try:
print('<p class="text-success">') print('<p class="text-success">')
print(response_SARA_4) print(response_SARA_4)
print('</p>') print('</p>')
else:
print('<span style="color: red;font-weight: bold;">No UUHTTPCR response</span>')
#5. empty json #5. empty json
print("Empty SARA memory:") print("Empty SARA memory:")
@@ -591,12 +608,10 @@ try:
# Calculate and print the elapsed time # Calculate and print the elapsed time
elapsed_time = time.time() - start_time elapsed_time = time.time() - start_time_script
if need_to_log: if need_to_log:
print(f"Elapsed time: {elapsed_time:.2f} seconds") print(f"Elapsed time: {elapsed_time:.2f} seconds")
print("----------------------------------------") print("<hr>")
print("----------------------------------------")
except Exception as e: except Exception as e:
print("An error occurred:", e) print("An error occurred:", e)