This commit is contained in:
root
2025-07-21 11:11:09 +01:00
parent 0539cb67af
commit 74fc3baece
4 changed files with 552 additions and 301 deletions

130
SARA/UDP/sendUDP_message.py Normal file
View File

@@ -0,0 +1,130 @@
'''
____ _ ____ _
/ ___| / \ | _ \ / \
\___ \ / _ \ | |_) | / _ \
___) / ___ \| _ < / ___ \
|____/_/ \_\_| \_\/_/ \_\
Script to set the PDP context for the SARA R5
/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/UDP/sendUDP_message.py
'''
import serial
import time
import sys
import json
import re
ser_sara = serial.Serial(
port='/dev/ttyAMA2',
baudrate=115200, #115200 ou 9600
parity=serial.PARITY_NONE, #PARITY_NONE, PARITY_EVEN or PARITY_ODD
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = 2
)
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:
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 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} (in {elapsed_time:.2f}s)")
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.")
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. ⏱️")
# 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'<span style="color: red;font-weight: bold;">[ALERT] ⚠️{total_elapsed_time:.2f}s⚠</span>')
return response.decode('utf-8', errors='replace') # Return the full response if no target line is found
try:
print('Start script')
# Increase verbosity
command = f'AT+CMEE=2\r'
ser_sara.write(command.encode('utf-8'))
response_SARA_1 = read_complete_response(ser_sara, wait_for_lines=["OK", "ERROR"])
print(response_SARA_1, end="")
time.sleep(1)
# 1. Create SOCKET
print('Create SOCKET')
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", "ERROR"])
print(response_SARA_1, end="")
time.sleep(1)
# 2. Retreive Socket ID
match = re.search(r'\+USOCR:\s*(\d+)', response_SARA_1)
if match:
socket_id = match.group(1)
print(f"Socket ID: {socket_id}")
else:
print("Failed to extract socket ID")
#3. Connect to UDP server
print("Connect to server:")
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(response_SARA_2)
# 4. Write data and send
print("Write data:")
command = f'AT+USOWR={socket_id},10\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(response_SARA_2)
ser_sara.write("1234567890".encode())
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["@","OK", "+CME ERROR", "ERROR"], debug=False)
print(response_SARA_2)
#Close socket
print("Close socket:")
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)
print(response_SARA_2)
except Exception as e:
print("An error occurred:", e)
traceback.print_exc() # This prints the full traceback

View File

@@ -118,10 +118,25 @@
</label>
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" value="" id="check_aircarto" onchange="update_config_sqlite('send_aircarto', this.checked)" disabled>
<label class="form-check-label" for="check_aircarto">
Send to AirCarto (HTTP)
</label>
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" value="" id="check_uSpot" onchange="update_config_sqlite('send_uSpot', this.checked)" disabled>
<label class="form-check-label" for="check_uSpot">
Send to uSpot
Send to uSpot (HTTPS)
</label>
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" value="" id="check_miotiq" onchange="update_config_sqlite('send_miotiq', this.checked)" disabled>
<label class="form-check-label" for="check_miotiq">
Send to miotiq (UDP)
</label>
</div>
@@ -365,6 +380,9 @@ window.onload = function() {
const checkbox_nmp5channels = document.getElementById("check_NPM_5channels");
const checkbox_wind = document.getElementById("check_WindMeter");
const checkbox_uSpot = document.getElementById("check_uSpot");
const checkbox_aircarto = document.getElementById("check_aircarto");
const checkbox_miotiq = document.getElementById("check_miotiq");
const checkbox_bme = document.getElementById("check_bme280");
const checkbox_envea = document.getElementById("check_envea");
const checkbox_solar = document.getElementById("check_solarBattery");
@@ -378,6 +396,8 @@ window.onload = function() {
checkbox_noise.checked = response["NOISE"];
checkbox_uSpot.checked = response["send_uSpot"];
checkbox_aircarto.checked = response["send_aircarto"];
checkbox_miotiq.checked = response["send_miotiq"];
// If envea is enabled, show the envea sondes container
if (response["envea"]) {

View File

@@ -57,6 +57,18 @@ CSV PAYLOAD (AirCarto Servers)
25 -> Wind speed
26 -> Wind direction
CSV FOR UDP (miotiq)
{device_id},{timestamp},{PM1},{PM25},{PM10},{temp},{hum},{press},{avg_noise},{max_noise},{min_noise},{envea_no2},{envea_h2s},{envea_o3},{4g_signal_quality}
0 -> device ID
1 -> timestamp
2 -> PM1
3 -> PM2.5
4 -> PM10
5 -> temp
6 -> hum
7 -> press
JSON PAYLOAD (Micro-Spot Servers)
Same as NebuleAir wifi
Endpoint:
@@ -124,6 +136,8 @@ if uptime_seconds < 120:
#Payload CSV to be sent to data.nebuleair.fr
payload_csv = [None] * 30
#Payload UPD to be sent to miotiq
payload_udp = [None] * 30
#Payload JSON to be sent to uSpot
payload_json = {
"nebuleairid": "XXX",
@@ -211,6 +225,8 @@ device_longitude_raw = config.get('longitude_raw', 0)
modem_version=config.get('modem_version', "")
Sara_baudrate = config.get('SaraR4_baudrate', 115200)
selected_networkID = int(config.get('SARA_R4_neworkID', 0))
send_miotiq = config.get('send_miotiq', True)
send_aircarto = config.get('send_aircarto', True)
send_uSpot = config.get('send_uSpot', False) #envoi sur MicroSpot ()
npm_5channel = config.get('npm_5channel', False) #5 canaux du NPM
envea_cairsens= config.get('envea', False)
@@ -221,6 +237,7 @@ NOISE_sensor = config.get('NOISE', False)
#update device id in the payload json
payload_json["nebuleairid"] = device_id
payload_udp[0] = device_id
# Skip execution if modem_config_mode is true
if modem_config_mode:
@@ -648,6 +665,11 @@ try:
payload_csv[18] = npm_temp
payload_csv[19] = npm_hum
#add data to payload UDP
payload_udp[2] = PM1
payload_udp[3] = PM25
payload_udp[4] = PM10
#Add data to payload JSON
payload_json["sensordatavalues"].append({"value_type": "NPM_P0", "value": str(PM1)})
payload_json["sensordatavalues"].append({"value_type": "NPM_P1", "value": str(PM10)})
@@ -863,16 +885,91 @@ try:
print('<p class="text-danger-emphasis">')
print(responseReconnect)
print("</p>", end="")
print('🛑STOP LOOP🛑')
print("<hr>")
#on arrete le script pas besoin de continuer
sys.exit()
else:
print("Signal Quality:", signal_quality)
'''
____ _____ _ _ ____ _ _ ____ ____
/ ___|| ____| \ | | _ \ | | | | _ \| _ \
\___ \| _| | \| | | | | | | | | | | | |_) |
___) | |___| |\ | |_| | | |_| | |_| | __/
|____/|_____|_| \_|____/ \___/|____/|_|
'''
if send_miotiq:
print('<p class="fw-bold">➡SEND TO MIOTIQ</p>', end="")
#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:")
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('<p class="text-danger-emphasis">')
print(response_SARA_1)
print("</p>", end="")
if "+CME ERROR" in response_SARA_1:
print('<span style="color: red;font-weight: bold;">⚠ATTENTION: need to reset PDP connection⚠</span>')
psd_csd_resets = reset_PSD_CSD_connection()
if psd_csd_resets:
print("✅PSD CSD connection reset successfully")
else:
print("⛔There were issues with the modem CSD PSD reinitialize process")
#Retreive Socket ID
match = re.search(r'\+USOCR:\s*(\d+)', response_SARA_1)
if match:
socket_id = match.group(1)
print(f"Socket ID: {socket_id}")
else:
print("Failed to extract socket ID")
#Connect to UDP server
print("Connect to server:")
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">')
print(response_SARA_2)
print("</p>", end="")
#prepare data
csv_udp_string = ','.join(str(value) if value is not None else '' for value in payload_udp)
size_of_udp_string = len(csv_udp_string)
# 4. Write data and send
print("Write data:")
command = f'AT+USOWR={socket_id},{size_of_udp_string}\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(response_SARA_2)
ser_sara.write(csv_udp_string.encode())
response_SARA_2 = read_complete_response(ser_sara, wait_for_lines=["@","OK", "+CME ERROR", "ERROR"], debug=False)
print(response_SARA_2)
#Close socket
print("Close socket:")
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)
print('<p class="text-danger-emphasis">')
print(response_SARA_2)
print("</p>", end="")
'''
____ _____ _ _ ____ _ ___ ____ ____ _ ____ _____ ___
/ ___|| ____| \ | | _ \ / \ |_ _| _ \ / ___| / \ | _ \_ _/ _ \
@@ -882,6 +979,8 @@ try:
'''
if send_aircarto:
print('<p class="fw-bold">➡SEND TO AIRCARTO SERVERS</p>', end="")
# Write Data to saraR4
# 1. Open sensordata_csv.json (with correct data size)

View File

@@ -41,7 +41,9 @@ config_entries = [
("SARA_R4_network_status", "connected", "str"),
("SARA_R4_neworkID", "20810", "int"),
("WIFI_status", "connected", "str"),
("send_aircarto", "1", "bool"),
("send_uSpot", "0", "bool"),
("send_miotiq", "0", "bool"),
("npm_5channel", "0", "bool"),
("envea", "0", "bool"),
("windMeter", "0", "bool"),