From 10c4348e5496735ad9a4f003a1d8908be0f29f38 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 10 Mar 2025 13:38:02 +0100 Subject: [PATCH] update --- RTC/set_with_NTP.py | 8 ++- RTC/set_with_browserTime.py | 8 ++- loop/SARA_send_data_v2.py | 128 ++++++++++++++++++++++++++++++------ 3 files changed, 123 insertions(+), 21 deletions(-) diff --git a/RTC/set_with_NTP.py b/RTC/set_with_NTP.py index 51fd756..811a110 100755 --- a/RTC/set_with_NTP.py +++ b/RTC/set_with_NTP.py @@ -1,6 +1,12 @@ #!/usr/bin/python3 """ -Script to set the RTC using an NTP server. + ____ _____ ____ + | _ \_ _/ ___| + | |_) || || | + | _ < | || |___ + |_| \_\|_| \____| + +Script to set the RTC using an NTP server (script used by web UI) RPI needs to be connected to the internet (WIFI). Requires ntplib and pytz: sudo pip3 install ntplib pytz --break-system-packages diff --git a/RTC/set_with_browserTime.py b/RTC/set_with_browserTime.py index e0c8e80..588ffb4 100755 --- a/RTC/set_with_browserTime.py +++ b/RTC/set_with_browserTime.py @@ -1,5 +1,11 @@ """ -Script to set the RTC using the browser time. + ____ _____ ____ + | _ \_ _/ ___| + | |_) || || | + | _ < | || |___ + |_| \_\|_| \____| + +Script to set the RTC using the browser time (script used by the web UI). /usr/bin/python3 /var/www/nebuleair_pro_4g/RTC/set_with_browserTime.py '2024-01-30 12:48:39' diff --git a/loop/SARA_send_data_v2.py b/loop/SARA_send_data_v2.py index 845be4d..408831b 100755 --- a/loop/SARA_send_data_v2.py +++ b/loop/SARA_send_data_v2.py @@ -290,23 +290,45 @@ try: print('

START LOOP

') #Local timestamp + #ATTENTION: + # -> RTC module can be deconnected "" + # -> RTC module can be out of time like "2000-01-01T00:55:21Z" print("➡️Getting local timestamp") cursor.execute("SELECT * FROM timestamp_table LIMIT 1") row = cursor.fetchone() # Get the first (and only) row - rtc_time_str = row[1] # '2025-02-07 12:30:45' - # Convert to a datetime object - dt_object = datetime.strptime(rtc_time_str, '%Y-%m-%d %H:%M:%S') - # Convert to InfluxDB RFC3339 format with UTC 'Z' suffix - influx_timestamp = dt_object.strftime('%Y-%m-%dT%H:%M:%SZ') - print(influx_timestamp) + rtc_time_str = row[1] # '2025-02-07 12:30:45' ou '2000-01-01 00:55:21' ou 'not connected' + print(rtc_time_str) + if rtc_time_str == 'not connected': + print("⛔ Atttention RTC module not connected⛔") + rtc_status = "disconnected" + influx_timestamp="rtc_disconnected" + else : + # Convert to a datetime object + dt_object = datetime.strptime(rtc_time_str, '%Y-%m-%d %H:%M:%S') + # Check if timestamp is reset (year 2000) + if dt_object.year == 2000: + print("⛔ Attention: RTC has been reset to default date ⛔") + rtc_status = "reset" + else: + print("✅ RTC timestamp is valid") + rtc_status = "valid" + + # Always convert to InfluxDB format + # Convert to InfluxDB RFC3339 format with UTC 'Z' suffix + influx_timestamp = dt_object.strftime('%Y-%m-%dT%H:%M:%SZ') + rtc_status = "valid" + print(influx_timestamp) #NEXTPM + # We take the last measures (order by rowid and not by timestamp) print("➡️Getting NPM values (last 6 measures)") #cursor.execute("SELECT * FROM data_NPM ORDER BY timestamp DESC LIMIT 1") - cursor.execute("SELECT * FROM data_NPM ORDER BY timestamp DESC LIMIT 6") + #cursor.execute("SELECT * FROM data_NPM ORDER BY timestamp DESC LIMIT 6") + cursor.execute("SELECT rowid, * FROM data_NPM ORDER BY rowid DESC LIMIT 6") + rows = cursor.fetchall() # Exclude the timestamp column (assuming first column is timestamp) - data_values = [row[1:] for row in rows] # Exclude timestamp + data_values = [row[2:] for row in rows] # Exclude timestamp # Compute column-wise average num_columns = len(data_values[0]) averages = [round(sum(col) / len(col),1) for col in zip(*data_values)] @@ -407,7 +429,7 @@ try: response2 = read_complete_response(ser_sara, wait_for_lines=["OK"]) print('

') print(response2) - print("

") + print("

", end="") match = re.search(r'\+CSQ:\s*(\d+),', response2) if match: signal_quality = int(match.group(1)) @@ -425,7 +447,7 @@ try: responseReconnect = read_complete_response(ser_sara, timeout=20, end_of_response_timeout=20) print('

') print(responseReconnect) - print("

") + print("

", end="") print('🛑STOP LOOP🛑') print("
") @@ -467,7 +489,7 @@ try: print('

') print(response_SARA_3) - print("

") + print("

", end="") # si on recoit la réponse UHTTPCR if "+UUHTTPCR" in response_SARA_3: @@ -547,7 +569,7 @@ try: response_SARA_9 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False) print('

') print(response_SARA_9) - print("

") + print("

", end="") ''' +UHTTPER: profile_id,error_class,error_code @@ -580,7 +602,7 @@ try: responseResetHTTP2_profile = read_complete_response(ser_sara, timeout=5, end_of_response_timeout=5, wait_for_lines=["OK", "+CME ERROR"], debug=True) print('

') print(responseResetHTTP2_profile) - print("

") + print("

", end="") # 2.2 code 1 (HHTP succeded) @@ -597,7 +619,75 @@ try: response_SARA_4 = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False) print('

') print(response_SARA_4) - print('

') + print("

", end="") + + #Parse the server datetime + # Extract just the date from the response + date_string = None + date_start = response_SARA_4.find("Date: ") + if date_start != -1: + date_end = response_SARA_4.find("\n", date_start) + date_string = response_SARA_4[date_start + 6:date_end].strip() + print(f'
Server date: {date_string}
', end="") + + # Optionally convert to datetime object + try: + from datetime import datetime + server_datetime = datetime.strptime( + date_string, + "%a, %d %b %Y %H:%M:%S %Z" + ) + #print(f'

Parsed datetime: {server_datetime}

') + except Exception as e: + print(f'

Error parsing date: {e}

') + + # Get RTC time from SQLite + cursor.execute("SELECT * FROM timestamp_table LIMIT 1") + row = cursor.fetchone() + rtc_time_str = row[1] # '2025-02-07 12:30:45' or '2000-01-01 00:55:21' or 'not connected' + print(f'
RTC time: {rtc_time_str}
', end="") + + # Compare times if both are available + if server_datetime and rtc_time_str != 'not connected': + try: + # Convert RTC time string to datetime + rtc_datetime = datetime.strptime(rtc_time_str, '%Y-%m-%d %H:%M:%S') + + # Calculate time difference in seconds + time_diff = abs((server_datetime - rtc_datetime).total_seconds()) + + print(f'
Time difference: {time_diff:.2f} seconds
', end="") + + # Check if difference is more than 10 seconds + # and update the RTC clock + if time_diff > 10: + print(f'
⚠️ RTC time differs from server time by {time_diff:.2f} seconds!
', end="") + # Format server time for RTC update + server_time_formatted = server_datetime.strftime('%Y-%m-%d %H:%M:%S') + + #update RTC module do not wait for answer, non blocking + #/usr/bin/python3 /var/www/nebuleair_pro_4g/RTC/set_with_browserTime.py '2024-01-30 12:48:39' + # Launch RTC update script as non-blocking subprocess + import subprocess + update_command = [ + "/usr/bin/python3", + "/var/www/nebuleair_pro_4g/RTC/set_with_browserTime.py", + server_time_formatted + ] + + # Execute the command without waiting for result + subprocess.Popen(update_command, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + + print(f'
➡️ Updating RTC with server time: {server_time_formatted}
', end="") + + else: + print(f'
✅ RTC time is synchronized with server time (within 10 seconds)
') + + except Exception as e: + print(f'

Error comparing times: {e}

') + #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" @@ -629,7 +719,7 @@ try: 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("

") + print("

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

") + print("

", end="") check_lines = responseResetHTTP_profile.strip().splitlines() for line in check_lines: if "+CME ERROR: Operation not allowed" in line: @@ -687,7 +777,7 @@ try: print('

') print(response_SARA_8) - print("

") + print("

", end="") # si on recoit la réponse UHTTPCR if "+UUHTTPCR" in response_SARA_8: @@ -740,7 +830,7 @@ try: response_SARA_9b = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False) print('

') print(response_SARA_9b) - print("

") + print("

", end="") ''' +UHTTPER: profile_id,error_class,error_code @@ -773,7 +863,7 @@ try: response_SARA_4b = read_complete_response(ser_sara, wait_for_lines=["OK"], debug=False) print('

') print(response_SARA_4b) - print('

') + print("

", end="")