This commit is contained in:
PaulVua
2025-01-14 16:24:02 +01:00
parent 96524bca20
commit b9c7caf624
3 changed files with 350 additions and 25 deletions

View File

@@ -0,0 +1,139 @@
'''
Script to set the URL for a HTTP request
Ex:
/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/SSL/prepareUspotProfile.py ttyAMA2 api-prod.uspot.probesys.net
To do: need to add profile id as parameter
First profile id:
AT+UHTTP=0,1,"data.nebuleair.fr"
Second profile id:
AT+UHTTP=1,1,"api-prod.uspot.probesys.net"
'''
import serial
import time
import sys
import json
parameter = sys.argv[1:] # Exclude the script name
#print("Parameters received:")
port='/dev/'+parameter[0] # ex: ttyAMA2
url = parameter[1] # ex: data.mobileair.fr
#get baudrate
def load_config(config_file):
try:
with open(config_file, 'r') as file:
config_data = json.load(file)
return config_data
except Exception as e:
print(f"Error loading config file: {e}")
return {}
# Define the config file path
config_file = '/var/www/nebuleair_pro_4g/config.json'
# Load the configuration data
config = load_config(config_file)
# Access the shared variables
baudrate = config.get('SaraR4_baudrate', 115200)
send_uSpot = config.get('send_uSpot', False)
def read_complete_response(serial_connection, timeout=2, end_of_response_timeout=2):
response = bytearray()
serial_connection.timeout = timeout
end_time = time.time() + end_of_response_timeout
while True:
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
elif time.time() > end_time:
break
time.sleep(0.1) # Short sleep to prevent busy waiting
return response.decode('utf-8', errors='replace')
ser_sara = serial.Serial(
port=port, #USB0 or ttyS0
baudrate=baudrate, #115200 ou 9600
parity=serial.PARITY_NONE, #PARITY_NONE, PARITY_EVEN or PARITY_ODD
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = 2
)
try:
#step 1: import the certificate
print("****")
print("Add certificate")
with open("/var/www/nebuleair_pro_4g/SARA/SSL/isrgrootx1.der", "rb") as cert_file:
certificate = cert_file.read()
size_of_string = len(certificate)
command = f'AT+USECMNG=0,0,"myCertificate2",{size_of_string}\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_1 = read_complete_response(ser_sara)
print("Write certificate metadata")
print(response_SARA_1)
time.sleep(1)
ser_sara.write(certificate)
response_SARA_2 = read_complete_response(ser_sara)
print("Write certificate data")
print(response_SARA_2)
time.sleep(1)
#step 2: set the security profile 2 to trusted root
print("****")
print("SET security profile")
command = f'AT+USECPRF=2,0,1\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_3 = read_complete_response(ser_sara)
print(response_SARA_3)
time.sleep(1)
#step 3: set the security profile 2 to the imported certificate
print("****")
print("SET certificate")
command = f'AT+USECPRF=2,3,"myCertificate2"\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_4 = read_complete_response(ser_sara)
print(response_SARA_4)
time.sleep(1)
#step 4: set url
print("****")
print("SET URL")
command = f'AT+UHTTP=1,1,"{url}"\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_5 = read_complete_response(ser_sara)
print(response_SARA_5)
time.sleep(1)
#step 4: set url to SSL
print("****")
print("SET URL")
command = f'AT+UHTTP=1,6,1,2\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_5 = read_complete_response(ser_sara)
print(response_SARA_5)
time.sleep(1)
except serial.SerialException as e:
print(f"Error: {e}")
finally:
if ser_sara.is_open:
ser_sara.close()
print("****")
#print("Serial closed")

View File

@@ -4,6 +4,9 @@
@reboot sleep 30 && /usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/sara_setURL.py ttyAMA2 data.nebuleair.fr >> /var/www/nebuleair_pro_4g/logs/app.log 2>&1
@reboot sleep 45 && /usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/SSL/prepareUspotProfile.py ttyAMA2 api-prod.uspot.probesys.net >> /var/www/nebuleair_pro_4g/logs/app.log 2>&1
* * * * * /usr/bin/python3 /var/www/nebuleair_pro_4g/loop/1_NPM/send_data.py >> /var/www/nebuleair_pro_4g/logs/loop.log 2>&1
0 0 */2 * * > /var/www/nebuleair_pro_4g/logs/loop.log

View File

@@ -66,7 +66,7 @@ import serial
import time
import busio
import re
import os
import RPi.GPIO as GPIO
from adafruit_bme280 import basic as adafruit_bme280
@@ -74,6 +74,15 @@ from adafruit_bme280 import basic as adafruit_bme280
# Record the start time of the script
start_time = time.time()
# Check system uptime
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])
# Skip execution if uptime is less than 2 minutes (120 seconds)
if uptime_seconds < 120:
print("System just booted, skipping execution.")
exit()
url_nebuleair="data.nebuleair.fr"
payload_csv = [None] * 20
payload_json = {
@@ -156,7 +165,6 @@ ser_sara = serial.Serial(
timeout = 2
)
ser_NPM = serial.Serial(
port='/dev/ttyAMA5',
baudrate=115200,
@@ -166,15 +174,27 @@ ser_NPM = serial.Serial(
timeout = 1
)
ser_envea = serial.Serial(
port='/dev/ttyAMA4',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = 1
serial_connections = {}
# Sondes Envea
if connected_envea_sondes:
# Pour chacune des sondes
for device in connected_envea_sondes:
port = device.get('port', 'Unknown')
name = device.get('name', 'Unknown')
connected = device.get('connected', False)
serial_connections[name] = serial.Serial(
port=f'/dev/{port}', # Format the port string
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = 1
)
def read_complete_response(serial_connection, timeout=2, end_of_response_timeout=2):
response = bytearray()
serial_connection.timeout = timeout
@@ -253,17 +273,43 @@ try:
if connected_envea_sondes:
# Pour chacune des sondes
for device in connected_envea_sondes:
print(f"Connected envea Sonde: {device.get('name', 'Unknown')} on port {device.get('port', 'Unknown')} and coefficient {device.get('coefficient', 'Unknown')} ")
ser_envea.write(b'\xFF\x02\x13\x30\x01\x02\x03\x04\x05\x06\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x12\xAF\x88\x03')
data_envea = ser_envea.readline()
port = device.get('port', 'Unknown')
name = device.get('name', 'Unknown')
coefficient = device.get('coefficient', 'Unknown')
if len(data_envea) >= 20:
byte_20 = data_envea[19]
byte_20 = byte_20 * coefficient
payload_csv[10] = byte_20
print(f"Data from envea {byte_20}")
print(f"Connected envea Sonde: {name} on port {port} and coefficient {coefficient} ")
if name in serial_connections:
serial_connection = serial_connections[name]
try:
# Write data to the device
serial_connection.write(
b"\xFF\x02\x13\x30\x01\x02\x03\x04\x05\x06\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x12\xAF\x88\x03"
)
# Read data from the device
data_envea = serial_connection.readline()
if len(data_envea) >= 20:
byte_20 = data_envea[19]
byte_20 = byte_20 * coefficient
# Update payload CSV based on device type
if name == "h2s":
payload_csv[10] = byte_20
if name == "no2":
payload_csv[9] = byte_20
if name == "o3":
payload_csv[11] = byte_20
print(f"Data from envea {name}: {byte_20}")
else:
print(f"Données reçues insuffisantes pour {name} pour extraire le 20ème octet.")
except serial.SerialException as e:
print(f"Error communicating with {name}: {e}")
else:
print("Données reçues insuffisantes pour extraire le 20ème octet.")
print(f"No serial connection for {name}")
# Getting the LTE Signal
print("-> Getting signal <-")
@@ -279,26 +325,33 @@ try:
payload_csv[12]=signal_quality
time.sleep(1)
#print(payload_json)
#Write Data to saraR4
#1. Open sensordata.json (with correct data size)
'''
SEND TO AIRCARTO
'''
# Write Data to saraR4
# 1. Open sensordata_csv.json (with correct data size)
csv_string = ','.join(str(value) if value is not None else '' for value in payload_csv)
size_of_string = len(csv_string)
command = f'AT+UDWNFILE="sensordata.json",{size_of_string}\r'
command = f'AT+UDWNFILE="sensordata_csv.json",{size_of_string}\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_1 = read_complete_response(ser_sara)
#if need_to_log:
#print("Open JSON:")
#print(response_SARA_1)
time.sleep(1)
#2. Write to shell
ser_sara.write(csv_string.encode())
response_SARA_2 = read_complete_response(ser_sara)
if need_to_log:
print("Write to memory:")
print(response_SARA_2)
#3. Send to endpoint (with device ID)
command= f'AT+UHTTPC=0,4,"/pro_4G/data.php?sensor_id={device_id}","server_response.txt","sensordata.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 + '\r').encode('utf-8'))
response_SARA_3 = read_complete_response(ser_sara)
@@ -407,13 +460,143 @@ try:
print('</p>')
#5. empty json
ser_sara.write(b'AT+UDELFILE="sensordata.json"\r')
ser_sara.write(b'AT+UDELFILE="sensordata_csv.json"\r')
response_SARA_5 = read_complete_response(ser_sara)
if need_to_log:
print("Empty JSON:")
print(response_SARA_5)
'''
SEND TO MICRO SPOT
'''
if send_uSpot:
print(">>>>>>>>")
print("SEND TO MICRO SPOT:")
print(payload_json)
# Write Data to saraR4
# 1. Open sensordata_csv.json (with correct data size)
size_of_string = len(payload_json)
command = f'AT+UDWNFILE="sensordata_json.json",{size_of_string}\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_1 = read_complete_response(ser_sara)
#if need_to_log:
#print("Open JSON:")
#print(response_SARA_1)
time.sleep(1)
#2. Write to shell
ser_sara.write(payload_json.encode())
response_SARA_2 = read_complete_response(ser_sara)
if need_to_log:
print("Write to memory:")
print(response_SARA_2)
#3. Send to endpoint (with device ID)
command= f'AT+UHTTPC=1,4,"nebuleair?token=2AFF6dQk68daFZ","server_response2.txt","sensordata_json.json",4\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_3 = read_complete_response(ser_sara)
print("Send data:")
print(response_SARA_3)
lines = response_SARA_3.strip().splitlines()
# 1.Vérifier si la réponse contient un message d'erreur CME
if "+CME ERROR" in lines[-1]:
print("*****")
print('<span style="color: red;font-weight: bold;">ATTENTION: CME ERROR</span>')
print("error:", lines[-1])
print("*****")
#update status
update_json_key(config_file, "SARA_R4_network_status", "disconnected")
# Gestion de l'erreur spécifique
if "No connection to phone" in lines[-1]:
print("No connection to the phone. Retrying or reset may be required.")
# Actions spécifiques pour ce type d'erreur (par exemple, réinitialiser ou tenter de reconnecter)
# need to reconnect to network
# and reset HTTP profile (AT+UHTTP=0) -> ne fonctionne pas..
# tester un reset avec CFUN 15
# 1.Reconnexion au réseau (AT+COPS)
command = f'AT+COPS=1,2,"{selected_networkID}"\r'
ser_sara.write((command + '\r').encode('utf-8'))
responseReconnect = read_complete_response(ser_sara)
print("Response reconnect:")
print(responseReconnect)
print("End response reconnect")
elif "Operation not allowed" in lines[-1]:
print("Operation not allowed. This may require a different configuration.")
# Actions spécifiques pour ce type d'erreur
# Clignotement LED en cas d'erreur
GPIO.output(23, GPIO.LOW) # Éteindre la LED définitivement
for _ in range(4):
GPIO.output(23, GPIO.HIGH) # Allumer la LED
time.sleep(0.1)
GPIO.output(23, GPIO.LOW) # Éteindre la LED
time.sleep(0.1)
GPIO.output(23, GPIO.LOW) # Turn off the LED
else:
# 2.Si la réponse contient une réponse HTTP valide
# Extract HTTP response code from the last line
# ATTENTION: lines[-1] renvoie l'avant dernière ligne et il peut y avoir un soucis avec le OK
# rechercher plutot
http_response = lines[-1] # "+UUHTTPCR: 0,4,0"
parts = http_response.split(',')
# 2.1 code 0 (HTTP failed)
if len(parts) == 3 and parts[-1] == '0': # The third value indicates success
print("*****")
print('<span style="color: red;font-weight: bold;">ATTENTION: HTTP operation failed</span>')
update_json_key(config_file, "SARA_R4_network_status", "disconnected")
print("*****")
print("resetting the URL (domain name):")
print("Turning off the blue LED...")
for _ in range(4): # Faire clignoter 4 fois
GPIO.output(23, GPIO.HIGH) # Allumer la LED
time.sleep(0.1) # Attendre 100 ms
GPIO.output(23, GPIO.LOW) # Éteindre la LED
time.sleep(0.1) # Attendre 100 ms
GPIO.output(23, GPIO.LOW) # Turn off the LED
command = f'AT+UHTTP=0,1,"{url_nebuleair}"\r'
ser_sara.write((command + '\r').encode('utf-8'))
response_SARA_31 = read_complete_response(ser_sara)
if need_to_log:
print(response_SARA_31)
# 2.2 code 1 (HHTP succeded)
else:
# Si la commande HTTP a réussi
print('<span class="badge text-bg-success">HTTP operation successful.</span>')
update_json_key(config_file, "SARA_R4_network_status", "connected")
print("Turning on the blue LED...")
for _ in range(4): # Faire clignoter 4 fois
GPIO.output(23, GPIO.HIGH) # Allumer la LED
time.sleep(0.1) # Attendre 100 ms
GPIO.output(23, GPIO.LOW) # Éteindre la LED
time.sleep(0.1) # Attendre 100 ms
GPIO.output(23, GPIO.HIGH) # Turn on the LED
#4. Read reply from server
ser_sara.write(b'AT+URDFILE="server_response2.txt"\r')
response_SARA_4 = read_complete_response(ser_sara)
if need_to_log:
print("Reply from server:")
print('<p class="text-success">')
print(response_SARA_4)
print('</p>')
#5. empty json
ser_sara.write(b'AT+UDELFILE="sensordata_json.json"\r')
response_SARA_5 = read_complete_response(ser_sara)
if need_to_log:
print("Empty JSON:")
print(response_SARA_5)
# Calculate and print the elapsed time
elapsed_time = time.time() - start_time