diff --git a/SARA/SSL/full_test_HTTPS.py b/SARA/SSL/full_test_HTTPS.py index 9a9f5b3..22ac411 100644 --- a/SARA/SSL/full_test_HTTPS.py +++ b/SARA/SSL/full_test_HTTPS.py @@ -1,7 +1,7 @@ ''' 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 +/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/SSL/full_test_HTTPS.py ttyAMA2 api-prod.uspot.probesys.net To do: need to add profile id as parameter @@ -24,7 +24,7 @@ parameter = sys.argv[1:] # Exclude the script name port='/dev/'+parameter[0] # ex: ttyAMA2 url = parameter[1] # ex: data.mobileair.fr -profile_id = 0 +profile_id = 3 #get baudrate def load_config(config_file): @@ -113,16 +113,16 @@ try: print(response_SARA_55) time.sleep(1) - #step 4: set url to SSL (op_code = 6) + #step 4: set url to SSL (op_code = 6) (http_secure = 1 for HTTPS)(USECMNG_PROFILE = 2) print("****") print("SET SSL") - command = f'AT+UHTTP={profile_id},6,1,2\r' + command = f'AT+UHTTP={profile_id},6,1\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 + #step 4: trigger the request (http_command=1 for GET) print("****") print("Trigger POST REQUEST") command = f'AT+UHTTPC={profile_id},1,"/tests/test.php","https.resp"\r' @@ -130,10 +130,6 @@ try: 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...") diff --git a/SARA/SSL/full_test_HTTPS_POST.py b/SARA/SSL/full_test_HTTPS_POST.py new file mode 100644 index 0000000..e66eddb --- /dev/null +++ b/SARA/SSL/full_test_HTTPS_POST.py @@ -0,0 +1,221 @@ +''' +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_POST.py ttyAMA2 api-prod.uspot.probesys.net + + +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 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) (http_secure = 1 for HTTPS)(USECMNG_PROFILE = 2) + print("****") + print("SET SSL") + command = f'AT+UHTTP={profile_id},6,1\r' + ser_sara.write(command.encode('utf-8')) + response_SARA_5 = read_complete_response(ser_sara) + print(response_SARA_5) + time.sleep(1) + + # Write Data to saraR4 + payload_json = { + "nebuleairid": "C04F8B8D3A08", + "software_version": "ModuleAir-V1-012023", + "sensordatavalues": [ + { + "value_type": "NPM_P0", + "value": "2.3" + }, + { + "value_type": "NPM_P0", + "value": "3.30" + }, + { + "value_type": "NPM_P1", + "value": "9.05" + }, + { + "value_type": "NPM_P2", + "value": "20.60" + }, + { + "value_type": "NPM_N1", + "value": "49.00" + }, + { + "value_type": "NPM_N10", + "value": "49.00" + }, + { + "value_type": "NPM_N25", + "value": "49.00" + }, + { + "value_type": "BME280_temperature", + "value": "25.82" + } + ] +} + # 1. Open sensordata_csv.json (with correct data size) + payload_string = json.dumps(payload_json) # Convert dict to JSON string + size_of_string = len(payload_string) + 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) + print("Open JSON:") + print(response_SARA_1) + time.sleep(1) + + #2. Write to shell + ser_sara.write(payload_string.encode()) + response_SARA_2 = read_complete_response(ser_sara) + print("Write to memory:") + print(response_SARA_2) + + #step 4: trigger the request (http_command=1 for GET and http_command=1 for POST) + print("****") + print("Trigger POST REQUEST") + #command = f'AT+UHTTPC={profile_id},1,"/tests/test.php","https.resp"\r' + command = f'AT+UHTTPC={profile_id},4,"/nebuleair?token=2AFF6dQk68daFZ","https.resp","sensordata_json.json",4\r' + + ser_sara.write(command.encode('utf-8')) + + # 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) + + #5. empty json + print("Empty SARA memory:") + ser_sara.write(b'AT+UDELFILE="sensordata_json.json"\r') + response_SARA_8 = read_complete_response(ser_sara) + print(response_SARA_8) + + + + +except serial.SerialException as e: + print(f"Error: {e}") + +finally: + if ser_sara.is_open: + ser_sara.close() + print("****") + #print("Serial closed") + diff --git a/SARA/SSL/full_test_HTTP_POST.py b/SARA/SSL/full_test_HTTP_POST.py new file mode 100644 index 0000000..69c01cf --- /dev/null +++ b/SARA/SSL/full_test_HTTP_POST.py @@ -0,0 +1,208 @@ +''' +Script to set the URL for a HTTP request and trigger the POST Request +FONCTIONNE SUR data.nebuleair.fr +FONCTIONNE SUR uSpot +Ex: +/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/SSL/full_test_HTTP_POST.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 + +profile_id = 1 + +#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 url to SSL (op_code = 6) (http_secure = 1 for HTTPS)(USECMNG_PROFILE = 2) + print("****") + print("SET SSL") + command = f'AT+UHTTP={profile_id},6,0\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: set PORT (op_code = 5) + print("****") + print("SET PORT") + command = f'AT+UHTTP={profile_id},5,81\r' + ser_sara.write((command + '\r').encode('utf-8')) + response_SARA_55 = read_complete_response(ser_sara) + print(response_SARA_55) + time.sleep(1) + + + # Write Data to saraR4 + payload_json = { + "nebuleairid": "C04F8B8D3A08", + "software_version": "ModuleAir-V1-012023", + "sensordatavalues": [ + { + "value_type": "NPM_P0", + "value": "2.3" + }, + { + "value_type": "NPM_P0", + "value": "3.30" + }, + { + "value_type": "NPM_P1", + "value": "9.05" + }, + { + "value_type": "NPM_P2", + "value": "20.60" + }, + { + "value_type": "NPM_N1", + "value": "49.00" + }, + { + "value_type": "NPM_N10", + "value": "49.00" + }, + { + "value_type": "NPM_N25", + "value": "49.00" + } + ] + } + + payload_csv = [None] * 20 + payload_csv[0] = 1 + payload_csv[1] = 1 + payload_csv[2] = 1 + #csv_string = ','.join(str(value) if value is not None else '' for value in payload_csv) + #size_of_string = len(csv_string) + + # 1. Open sensordata_csv.json (with correct data size) + payload_string = json.dumps(payload_json) # Convert dict to JSON string + size_of_string = len(payload_string) + 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) + print("Open JSON:") + print(response_SARA_1) + time.sleep(1) + + #2. Write to shell + ser_sara.write(payload_string.encode()) + response_SARA_2 = read_complete_response(ser_sara) + print("Write to memory:") + print(response_SARA_2) + + #step 4: trigger the request (http_command=1 for GET and http_command=1 for POST) + print("****") + print("Trigger POST REQUEST") + #command = f'AT+UHTTPC={profile_id},1,"/tests/test.php","http.resp"\r' + command = f'AT+UHTTPC={profile_id},4,"/nebuleair?token=2AFF6dQk68daFZ","http.resp","sensordata_json.json",4\r' + #command = f'AT+UHTTPC={profile_id},4,"/wifi.php","http.resp","sensordata_json.json",4\r' + #command = f'AT+UHTTPC={profile_id},4,"/pro_4G/data.php?sensor_id=52E7573A","http.resp","sensordata_json.json",4\r' + + ser_sara.write(command.encode('utf-8')) + + # 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="http.resp"\r') + response_SARA_7 = read_complete_response(ser_sara) + print("Reply from server:") + print(response_SARA_7) + + #5. empty json + print("Empty SARA memory:") + ser_sara.write(b'AT+UDELFILE="sensordata_json.json"\r') + response_SARA_8 = read_complete_response(ser_sara) + print(response_SARA_8) + + + +except serial.SerialException as e: + print(f"Error: {e}") + +finally: + if ser_sara.is_open: + ser_sara.close() + print("****") + #print("Serial closed") +