update
This commit is contained in:
121
SARA/SSL/full_test_HTTP.py
Normal file
121
SARA/SSL/full_test_HTTP.py
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
'''
|
||||||
|
Script to set the URL for a HTTP request and trigger the POST Request
|
||||||
|
Ex:
|
||||||
|
/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/SSL/full_test_HTTP.py ttyAMA2 data.nebuleair.fr
|
||||||
|
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"
|
||||||
|
Third profile id:
|
||||||
|
AT+UHTTP=2,1,"aircarto.fr"
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
profile_id = 3
|
||||||
|
|
||||||
|
#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 4: set url (op_code = 1)
|
||||||
|
print("****")
|
||||||
|
print("SET URL")
|
||||||
|
command = f'AT+UHTTP={profile_id},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 PORT (op_code = 5)
|
||||||
|
print("****")
|
||||||
|
print("SET PORT")
|
||||||
|
command = f'AT+UHTTP={profile_id},5,80\r'
|
||||||
|
ser_sara.write((command + '\r').encode('utf-8'))
|
||||||
|
response_SARA_55 = read_complete_response(ser_sara)
|
||||||
|
print(response_SARA_55)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
#step 4: trigger the request
|
||||||
|
print("****")
|
||||||
|
print("Trigger POST REQUEST")
|
||||||
|
command = f'AT+UHTTPC={profile_id},1,"/pro_4G/test.php","http.resp"\r'
|
||||||
|
ser_sara.write((command + '\r').encode('utf-8'))
|
||||||
|
response_SARA_6 = read_complete_response(ser_sara)
|
||||||
|
print(response_SARA_6)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
#READ REPLY
|
||||||
|
print("****")
|
||||||
|
print("Read reply from server")
|
||||||
|
ser_sara.write(b'AT+URDFILE="http.resp"\r')
|
||||||
|
response_SARA_7 = read_complete_response(ser_sara)
|
||||||
|
print("Reply from server:")
|
||||||
|
print(response_SARA_7)
|
||||||
|
|
||||||
|
|
||||||
|
except serial.SerialException as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if ser_sara.is_open:
|
||||||
|
ser_sara.close()
|
||||||
|
print("****")
|
||||||
|
#print("Serial closed")
|
||||||
|
|
||||||
164
SARA/SSL/full_test_HTTPS.py
Normal file
164
SARA/SSL/full_test_HTTPS.py
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
'''
|
||||||
|
Script to set the URL for a HTTP request and trigger the POST Request
|
||||||
|
Ex:
|
||||||
|
/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/SSL/full_test_HTTPS.py ttyAMA2 aircarto.fr
|
||||||
|
|
||||||
|
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"
|
||||||
|
Third profile id:
|
||||||
|
AT+UHTTP=2,1,"aircarto.fr"
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
profile_id = 0
|
||||||
|
|
||||||
|
#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 4: set url (op_code = 1)
|
||||||
|
print("****")
|
||||||
|
print("SET URL")
|
||||||
|
command = f'AT+UHTTP={profile_id},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 PORT (op_code = 5)
|
||||||
|
print("****")
|
||||||
|
print("SET PORT")
|
||||||
|
command = f'AT+UHTTP={profile_id},5,443\r'
|
||||||
|
ser_sara.write((command + '\r').encode('utf-8'))
|
||||||
|
response_SARA_55 = read_complete_response(ser_sara)
|
||||||
|
print(response_SARA_55)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
#step 4: set url to SSL (op_code = 6)
|
||||||
|
print("****")
|
||||||
|
print("SET SSL")
|
||||||
|
command = f'AT+UHTTP={profile_id},6,1,2\r'
|
||||||
|
ser_sara.write(command.encode('utf-8'))
|
||||||
|
response_SARA_5 = read_complete_response(ser_sara)
|
||||||
|
print(response_SARA_5)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
#step 4: trigger the request
|
||||||
|
print("****")
|
||||||
|
print("Trigger POST REQUEST")
|
||||||
|
command = f'AT+UHTTPC={profile_id},1,"/tests/test.php","https.resp"\r'
|
||||||
|
#command = f'AT+UHTTPC={profile_id},1,"/nebuleair?token=2AFF6dQk68daFZ","https.resp"\r'
|
||||||
|
|
||||||
|
ser_sara.write(command.encode('utf-8'))
|
||||||
|
|
||||||
|
#response_SARA_6 = read_complete_response(ser_sara, timeout=5)
|
||||||
|
#print(response_SARA_6)
|
||||||
|
#time.sleep(1)
|
||||||
|
|
||||||
|
# Wait for the +UUHTTPCR response
|
||||||
|
print("Waiting for +UUHTTPCR response...")
|
||||||
|
|
||||||
|
response_received = False
|
||||||
|
while not response_received:
|
||||||
|
response = read_complete_response(ser_sara, timeout=5)
|
||||||
|
print(response)
|
||||||
|
if "+UUHTTPCR" in response:
|
||||||
|
response_received = True
|
||||||
|
|
||||||
|
#READ REPLY
|
||||||
|
print("****")
|
||||||
|
print("Read reply from server")
|
||||||
|
ser_sara.write(b'AT+URDFILE="https.resp"\r')
|
||||||
|
response_SARA_7 = read_complete_response(ser_sara)
|
||||||
|
print("Reply from server:")
|
||||||
|
print(response_SARA_7)
|
||||||
|
|
||||||
|
|
||||||
|
except serial.SerialException as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if ser_sara.is_open:
|
||||||
|
ser_sara.close()
|
||||||
|
print("****")
|
||||||
|
#print("Serial closed")
|
||||||
|
|
||||||
170
SARA/SSL/old/full_test_HTTPS.py
Normal file
170
SARA/SSL/old/full_test_HTTPS.py
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
'''
|
||||||
|
Script to set the URL for a HTTP request and trigger the POST Request
|
||||||
|
Ex:
|
||||||
|
/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/SSL/full_test_HTTPS.py ttyAMA2 aircarto.fr
|
||||||
|
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"
|
||||||
|
Third profile id:
|
||||||
|
AT+UHTTP=2,1,"aircarto.fr"
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
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
|
||||||
|
print("****")
|
||||||
|
print("SET PORT")
|
||||||
|
command = f'AT+UHTTP=1,5,443\r'
|
||||||
|
ser_sara.write((command + '\r').encode('utf-8'))
|
||||||
|
response_SARA_55 = read_complete_response(ser_sara)
|
||||||
|
print(response_SARA_55)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
#step 4: set url to SSL
|
||||||
|
print("****")
|
||||||
|
print("SET SSL")
|
||||||
|
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)
|
||||||
|
|
||||||
|
#step 4: trigger the request
|
||||||
|
print("****")
|
||||||
|
print("Trigger POST REQUEST")
|
||||||
|
command = f'AT+UHTTPC=1,1,"/tests/test.php","https.resp"\r'
|
||||||
|
ser_sara.write((command + '\r').encode('utf-8'))
|
||||||
|
response_SARA_6 = read_complete_response(ser_sara)
|
||||||
|
print(response_SARA_6)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
#READ REPLY
|
||||||
|
print("****")
|
||||||
|
print("Read reply from server")
|
||||||
|
ser_sara.write(b'AT+URDFILE="https.resp"\r')
|
||||||
|
response_SARA_7 = read_complete_response(ser_sara)
|
||||||
|
print("Reply from server:")
|
||||||
|
print(response_SARA_7)
|
||||||
|
|
||||||
|
|
||||||
|
except serial.SerialException as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if ser_sara.is_open:
|
||||||
|
ser_sara.close()
|
||||||
|
print("****")
|
||||||
|
#print("Serial closed")
|
||||||
|
|
||||||
133
SARA/SSL/prepareUspotProfile_V2.py
Normal file
133
SARA/SSL/prepareUspotProfile_V2.py
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
'''
|
||||||
|
Script to set the URL for a HTTP request
|
||||||
|
Ex:
|
||||||
|
/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/SSL/prepareUspotProfile_V2.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"
|
||||||
|
Third profile id:
|
||||||
|
AT+UHTTP=2,1,"aircarto.fr"
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
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 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 PORT
|
||||||
|
print("****")
|
||||||
|
print("SET PORT")
|
||||||
|
command = f'AT+UHTTP=1,5,443\r'
|
||||||
|
ser_sara.write((command + '\r').encode('utf-8'))
|
||||||
|
response_SARA_55 = read_complete_response(ser_sara)
|
||||||
|
print(response_SARA_55)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
#step 4: set url to SSL
|
||||||
|
print("****")
|
||||||
|
print("SET SSL")
|
||||||
|
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")
|
||||||
|
|
||||||
@@ -338,7 +338,7 @@ try:
|
|||||||
csv_string = ','.join(str(value) if value is not None else '' for value in payload_csv)
|
csv_string = ','.join(str(value) if value is not None else '' for value in payload_csv)
|
||||||
size_of_string = len(csv_string)
|
size_of_string = len(csv_string)
|
||||||
command = f'AT+UDWNFILE="sensordata_csv.json",{size_of_string}\r'
|
command = f'AT+UDWNFILE="sensordata_csv.json",{size_of_string}\r'
|
||||||
ser_sara.write((command + '\r').encode('utf-8'))
|
ser_sara.write(command.encode('utf-8'))
|
||||||
response_SARA_1 = read_complete_response(ser_sara)
|
response_SARA_1 = read_complete_response(ser_sara)
|
||||||
#if need_to_log:
|
#if need_to_log:
|
||||||
#print("Open JSON:")
|
#print("Open JSON:")
|
||||||
@@ -353,113 +353,118 @@ try:
|
|||||||
print(response_SARA_2)
|
print(response_SARA_2)
|
||||||
|
|
||||||
#3. Send to endpoint (with device ID)
|
#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_csv.json",4\r'
|
|
||||||
ser_sara.write((command + '\r').encode('utf-8'))
|
|
||||||
response_SARA_3 = read_complete_response(ser_sara)
|
|
||||||
|
|
||||||
print("Send data:")
|
print("Send data:")
|
||||||
print(response_SARA_3)
|
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'))
|
||||||
# Les types de réponse
|
|
||||||
|
|
||||||
# 1.La commande n'a pas fonctionné
|
print("Waiting for +UUHTTPCR response...")
|
||||||
# +CME ERROR: No connection to phone
|
response_received = False
|
||||||
# +CME ERROR: Operation not allowed
|
while not response_received:
|
||||||
|
response_SARA_3 = read_complete_response(ser_sara, timeout=5)
|
||||||
|
print(response_SARA_3)
|
||||||
|
if "+UUHTTPCR" in response_SARA_3:
|
||||||
|
response_received = True
|
||||||
|
|
||||||
|
# Les types de réponse
|
||||||
|
|
||||||
# 2.La commande fonctionne: elle renvoie un code
|
# 1.La commande n'a pas fonctionné
|
||||||
# +UUHTTPCR: <profile_id>,<http_command>,<http_result>
|
# +CME ERROR: No connection to phone
|
||||||
# <http_result>: 1 pour sucess et 0 pour fail
|
# +CME ERROR: Operation not allowed
|
||||||
# +UUHTTPCR: 0,4,1 -> OK
|
|
||||||
# +UUHTTPCR: 0,4,0 -> error
|
|
||||||
|
|
||||||
# Split response into lines
|
# 2.La commande fonctionne: elle renvoie un code
|
||||||
lines = response_SARA_3.strip().splitlines()
|
# +UUHTTPCR: <profile_id>,<http_command>,<http_result>
|
||||||
|
# <http_result>: 1 pour sucess et 0 pour fail
|
||||||
|
# +UUHTTPCR: 0,4,1 -> OK
|
||||||
|
# +UUHTTPCR: 0,4,0 -> error
|
||||||
|
|
||||||
# 1.Vérifier si la réponse contient un message d'erreur CME
|
# Split response into lines
|
||||||
if "+CME ERROR" in lines[-1]:
|
lines = response_SARA_3.strip().splitlines()
|
||||||
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
|
# 1.Vérifier si la réponse contient un message d'erreur CME
|
||||||
if "No connection to phone" in lines[-1]:
|
if "+CME ERROR" in lines[-1]:
|
||||||
print("No connection to the phone. Retrying or reset may be required.")
|
print("*****")
|
||||||
# Actions spécifiques pour ce type d'erreur (par exemple, réinitialiser ou tenter de reconnecter)
|
print('<span style="color: red;font-weight: bold;">ATTENTION: CME ERROR</span>')
|
||||||
# need to reconnect to network
|
print("error:", lines[-1])
|
||||||
# and reset HTTP profile (AT+UHTTP=0) -> ne fonctionne pas..
|
print("*****")
|
||||||
# tester un reset avec CFUN 15
|
#update status
|
||||||
# 1.Reconnexion au réseau (AT+COPS)
|
update_json_key(config_file, "SARA_R4_network_status", "disconnected")
|
||||||
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]:
|
# Gestion de l'erreur spécifique
|
||||||
print("Operation not allowed. This may require a different configuration.")
|
if "No connection to phone" in lines[-1]:
|
||||||
# Actions spécifiques pour ce type d'erreur
|
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.encode('utf-8'))
|
||||||
|
responseReconnect = read_complete_response(ser_sara)
|
||||||
|
print("Response reconnect:")
|
||||||
|
print(responseReconnect)
|
||||||
|
print("End response reconnect")
|
||||||
|
|
||||||
# Clignotement LED en cas d'erreur
|
elif "Operation not allowed" in lines[-1]:
|
||||||
GPIO.output(23, GPIO.LOW) # Éteindre la LED définitivement
|
print("Operation not allowed. This may require a different configuration.")
|
||||||
for _ in range(4):
|
# Actions spécifiques pour ce type d'erreur
|
||||||
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:
|
# Clignotement LED en cas d'erreur
|
||||||
# 2.Si la réponse contient une réponse HTTP valide
|
GPIO.output(23, GPIO.LOW) # Éteindre la LED définitivement
|
||||||
# Extract HTTP response code from the last line
|
for _ in range(4):
|
||||||
# ATTENTION: lines[-1] renvoie l'avant dernière ligne et il peut y avoir un soucis avec le OK
|
GPIO.output(23, GPIO.HIGH) # Allumer la LED
|
||||||
# rechercher plutot
|
time.sleep(0.1)
|
||||||
http_response = lines[-1] # "+UUHTTPCR: 0,4,0"
|
GPIO.output(23, GPIO.LOW) # Éteindre la LED
|
||||||
parts = http_response.split(',')
|
time.sleep(0.1)
|
||||||
|
GPIO.output(23, GPIO.LOW) # Turn off the LED
|
||||||
|
|
||||||
# 2.1 code 0 (HTTP failed)
|
else:
|
||||||
if len(parts) == 3 and parts[-1] == '0': # The third value indicates success
|
# 2.Si la réponse contient une réponse HTTP valide
|
||||||
print("*****")
|
# Extract HTTP response code from the last line
|
||||||
print('<span style="color: red;font-weight: bold;">ATTENTION: HTTP operation failed</span>')
|
# ATTENTION: lines[-1] renvoie l'avant dernière ligne et il peut y avoir un soucis avec le OK
|
||||||
update_json_key(config_file, "SARA_R4_network_status", "disconnected")
|
# rechercher plutot
|
||||||
print("*****")
|
http_response = lines[-1] # "+UUHTTPCR: 0,4,0"
|
||||||
print("resetting the URL (domain name):")
|
parts = http_response.split(',')
|
||||||
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)
|
# 2.1 code 0 (HTTP failed)
|
||||||
else:
|
if len(parts) == 3 and parts[-1] == '0': # The third value indicates success
|
||||||
# Si la commande HTTP a réussi
|
print("*****")
|
||||||
print('<span class="badge text-bg-success">HTTP operation successful.</span>')
|
print('<span style="color: red;font-weight: bold;">ATTENTION: HTTP operation failed</span>')
|
||||||
update_json_key(config_file, "SARA_R4_network_status", "connected")
|
update_json_key(config_file, "SARA_R4_network_status", "disconnected")
|
||||||
print("Turning on the blue LED...")
|
print("*****")
|
||||||
for _ in range(4): # Faire clignoter 4 fois
|
print("resetting the URL (domain name):")
|
||||||
GPIO.output(23, GPIO.HIGH) # Allumer la LED
|
print("Turning off the blue LED...")
|
||||||
time.sleep(0.1) # Attendre 100 ms
|
for _ in range(4): # Faire clignoter 4 fois
|
||||||
GPIO.output(23, GPIO.LOW) # Éteindre la LED
|
GPIO.output(23, GPIO.HIGH) # Allumer la LED
|
||||||
time.sleep(0.1) # Attendre 100 ms
|
time.sleep(0.1) # Attendre 100 ms
|
||||||
GPIO.output(23, GPIO.HIGH) # Turn on the LED
|
GPIO.output(23, GPIO.LOW) # Éteindre la LED
|
||||||
#4. Read reply from server
|
time.sleep(0.1) # Attendre 100 ms
|
||||||
ser_sara.write(b'AT+URDFILE="server_response.txt"\r')
|
GPIO.output(23, GPIO.LOW) # Turn off the LED
|
||||||
response_SARA_4 = read_complete_response(ser_sara)
|
command = f'AT+UHTTP=0,1,"{url_nebuleair}"\r'
|
||||||
if need_to_log:
|
ser_sara.write(command.encode('utf-8'))
|
||||||
print("Reply from server:")
|
response_SARA_31 = read_complete_response(ser_sara)
|
||||||
print('<p class="text-success">')
|
if need_to_log:
|
||||||
print(response_SARA_4)
|
print(response_SARA_31)
|
||||||
print('</p>')
|
|
||||||
|
# 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_response.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
|
#5. empty json
|
||||||
ser_sara.write(b'AT+UDELFILE="sensordata_csv.json"\r')
|
ser_sara.write(b'AT+UDELFILE="sensordata_csv.json"\r')
|
||||||
@@ -489,8 +494,6 @@ try:
|
|||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
#2. Write to shell
|
#2. Write to shell
|
||||||
|
|
||||||
|
|
||||||
ser_sara.write(payload_string.encode())
|
ser_sara.write(payload_string.encode())
|
||||||
response_SARA_2 = read_complete_response(ser_sara)
|
response_SARA_2 = read_complete_response(ser_sara)
|
||||||
if need_to_log:
|
if need_to_log:
|
||||||
|
|||||||
Reference in New Issue
Block a user