v1.4.2 — Fix bug AT+USOWR leak dans payload UDP Miotiq

Corrige une desynchronisation serie qui causait l'envoi de la commande
AT+USOWR comme donnees UDP au lieu du payload capteurs. Ajout de flush
buffer serie, verification du prompt @, et abort propre a chaque etape.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
paul_vua
2026-03-14 22:53:59 +01:00
parent 7ab06f3413
commit d5b2e9c6c3
4 changed files with 203 additions and 61 deletions

View File

@@ -1091,10 +1091,13 @@ try:
print('<p class="fw-bold">➡SEND TO MIOTIQ</p>', end="")
binary_data = payload.get_bytes()
print(f"Binary payload: {len(binary_data)} bytes")
#print(f"Binary payload: {binary_data}")
# Flush serial buffer to avoid stale data from previous operations
ser_sara.reset_input_buffer()
#create UDP socket (will return socket number) -> 17 is UDP protocol and 6 is TCP protocol
# IF ERROR -> need to create the PDP connection
print("Create Socket:", end="")
@@ -1110,88 +1113,103 @@ try:
psd_csd_resets = reset_PSD_CSD_connection()
if psd_csd_resets:
print("✅PSD CSD connection reset successfully")
# Retry socket creation after PDP reset
ser_sara.reset_input_buffer()
command = f'AT+USOCR=17\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_1 = read_complete_response(ser_sara, wait_for_lines=["OK", "+CME ERROR", "ERROR"], debug=False)
print("Retry create socket:", end="")
print(response_SARA_1)
else:
print("⛔There were issues with the modem CSD PSD reinitialize process")
# Clignotement LED rouge en cas d'erreur
led_thread = Thread(target=blink_led, args=(24, 5, 0.5))
led_thread.start()
#Retreive Socket ID
socket_id = None
match = re.search(r'\+USOCR:\s*(\d+)', response_SARA_1)
if match:
socket_id = match.group(1)
print(f"Socket ID: {socket_id}", end="")
else:
print("Failed to extract socket ID")
print('<span style="color: red;font-weight: bold;">⚠Failed to extract socket ID - skip UDP send⚠</span>')
#Connect to UDP server (USOCO)
print("Connect to server:", end="")
command = f'AT+USOCO={socket_id},"192.168.0.20",4242\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["OK", "+CME ERROR", "ERROR"], debug=False)
print('<p class="text-danger-emphasis">', end="")
print(response_SARA_2)
print("</p>", end="")
if socket_id is not None:
print("Connect to server:", end="")
ser_sara.reset_input_buffer()
command = f'AT+USOCO={socket_id},"192.168.0.20",4242\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["OK", "+CME ERROR", "ERROR"], debug=False)
print('<p class="text-danger-emphasis">', end="")
print(response_SARA_2)
print("</p>", end="")
# Write data and send
if "+CME ERROR" in response_SARA_2 or "ERROR" in response_SARA_2:
print('<span style="color: red;font-weight: bold;">⚠ATTENTION: Error connecting socket - skip UDP send⚠</span>')
ser_sara.write(f'AT+USOCL={socket_id}\r'.encode('utf-8'))
read_complete_response(ser_sara, wait_for_lines=["OK", "+CME ERROR", "ERROR"], timeout=1, end_of_response_timeout=1, debug=False)
socket_id = None
print(f"Write data: {len(binary_data)} bytes", end="")
command = f'AT+USOWR={socket_id},{len(binary_data)}\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["@","OK", "+CME ERROR", "ERROR"], debug=False)
print('<p class="text-danger-emphasis">', end="")
print(response_SARA_2)
print("</p>", end="")
# Write data and send
if socket_id is not None:
print(f"Write data: {len(binary_data)} bytes", end="")
ser_sara.reset_input_buffer()
command = f'AT+USOWR={socket_id},{len(binary_data)}\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["@","OK", "+CME ERROR", "ERROR"], debug=False)
print('<p class="text-danger-emphasis">', end="")
print(response_SARA_2)
print("</p>", end="")
# Send the raw payload bytes (already prepared)
ser_sara.write(binary_data)
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["@","OK", "+CME ERROR", "ERROR"], debug=False)
print('<p class="text-danger-emphasis">', end="")
print(response_SARA_2)
print("</p>", end="")
# Verify modem sent @ prompt (ready for binary data)
if "@" not in response_SARA_2:
print('<span style="color: red;font-weight: bold;">⚠Modem did not send @ prompt - skip data send to avoid AT+USOWR leak⚠</span>')
ser_sara.reset_input_buffer()
ser_sara.write(f'AT+USOCL={socket_id}\r'.encode('utf-8'))
read_complete_response(ser_sara, wait_for_lines=["OK", "+CME ERROR", "ERROR"], timeout=1, end_of_response_timeout=1, debug=False)
socket_id = None
#parfois ici on peut avoir une erreur ERROR
if "+CME ERROR" in response_SARA_2 or "ERROR" in response_SARA_2:
print('<span style="color: red;font-weight: bold;">⚠ATTENTION: Error while sending data⚠</span>')
print('🛑STOP LOOP🛑')
print("<hr>")
if socket_id is not None:
# Send the raw payload bytes (already prepared)
ser_sara.write(binary_data)
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["OK", "+CME ERROR", "ERROR"], debug=False)
print('<p class="text-danger-emphasis">', end="")
print(response_SARA_2)
print("</p>", end="")
#Send notification (WIFI)
send_error_notification(device_id, "UDP sending issue")
#Hardware Reboot
hardware_reboot_success = modem_hardware_reboot()
if hardware_reboot_success:
print("✅Modem successfully rebooted and reinitialized")
else:
print("⛔There were issues with the modem reboot/reinitialize process")
#end loop
sys.exit()
#parfois ici on peut avoir une erreur ERROR
if "+CME ERROR" in response_SARA_2 or "ERROR" in response_SARA_2:
print('<span style="color: red;font-weight: bold;">⚠ATTENTION: Error while sending data⚠</span>')
print('🛑STOP LOOP🛑')
print("<hr>")
#Send notification (WIFI)
send_error_notification(device_id, "UDP sending issue")
#Hardware Reboot
hardware_reboot_success = modem_hardware_reboot()
if hardware_reboot_success:
print("✅Modem successfully rebooted and reinitialized")
else:
print("⛔There were issues with the modem reboot/reinitialize process")
#Read reply from server (USORD)
#print("Read reply:", end="")
#command = f'AT+USORD=0,100\r'
#ser_sara.write(command.encode('utf-8'))
#response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["OK", "+CME ERROR", "ERROR"], debug=False)
#print('<p class="text-danger-emphasis">')
#print(response_SARA_2)
#print("</p>", end="")
#end loop
sys.exit()
#Close socket
print("Close socket:", end="")
command = f'AT+USOCL={socket_id}\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["OK", "+CME ERROR", "ERROR"], debug=False)
#Close socket
print("Close socket:", end="")
command = f'AT+USOCL={socket_id}\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["OK", "+CME ERROR", "ERROR"], debug=False)
#blink green LEDs
led_thread = Thread(target=blink_led, args=(23, 5, 0.5))
led_thread.start()
#blink green LEDs
led_thread = Thread(target=blink_led, args=(23, 5, 0.5))
led_thread.start()
print('<p class="text-danger-emphasis">', end="")
print(response_SARA_2)
print("</p>", end="")
print('<p class="text-danger-emphasis">', end="")
print(response_SARA_2)
print("</p>", end="")