first commit

This commit is contained in:
PaulVua
2025-01-09 14:09:21 +01:00
parent 531f0ef740
commit 3081e43a1a
96 changed files with 65961 additions and 1 deletions

6
loop/3_NPM/data.json Executable file
View File

@@ -0,0 +1,6 @@
{
"sondeID": "Average_USB2_USB3",
"PM1": 0.0,
"PM25": 0.0,
"PM10": 0.0
}

101
loop/3_NPM/get_data.py Executable file
View File

@@ -0,0 +1,101 @@
import serial
import json
import time
# Initialize serial ports for the three sensors
ser3 = serial.Serial(
port='/dev/ttyAMA3',
baudrate=115200,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = 1
)
ser4 = serial.Serial(
port='/dev/ttyAMA4',
baudrate=115200,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = 1
)
ser5 = serial.Serial(
port='/dev/ttyAMA5',
baudrate=115200,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = 1
)
# Function to read and parse sensor data
def read_sensor_data(ser, sonde_id):
try:
# Send the command to request data (e.g., data for 60 seconds)
ser.write(b'\x81\x12\x6D')
# Read the response
byte_data = ser.readline()
# Extract the state byte and PM data from the response
state_byte = int.from_bytes(byte_data[2:3], byteorder='big')
state_bits = [int(bit) for bit in bin(state_byte)[2:].zfill(8)]
PM1 = int.from_bytes(byte_data[9:11], byteorder='big') / 10
PM25 = int.from_bytes(byte_data[11:13], byteorder='big') / 10
PM10 = int.from_bytes(byte_data[13:15], byteorder='big') / 10
# Create a dictionary with the parsed data
data = {
'sondeID': sonde_id,
'PM1': PM1,
'PM25': PM25,
'PM10': PM10
}
return data
except Exception as e:
print(f"Error reading from sensor {sonde_id}: {e}")
return None
# Function to create a JSON object with all sensor data
def collect_all_sensor_data():
all_data = {}
# Read data from each sensor and add to the all_data dictionary
sensor_data_3 = read_sensor_data(ser3, 'USB2')
sensor_data_4 = read_sensor_data(ser4, 'USB3')
sensor_data_5 = read_sensor_data(ser5, 'USB4')
# Store the data for each sensor in the dictionary
if sensor_data_3:
all_data['sensor_3'] = sensor_data_3
if sensor_data_4:
all_data['sensor_4'] = sensor_data_4
if sensor_data_5:
all_data['sensor_5'] = sensor_data_5
return all_data
# Main script to run once
if __name__ == "__main__":
try:
# Collect data from all sensors
data = collect_all_sensor_data()
# Convert data to JSON
json_data = json.dumps(data, indent=4)
# Define the output file path
output_file = "/var/www/nebuleair_pro_4g/loop/data.json" # Change this to your desired file path
# Write the JSON data to the file
with open(output_file, 'w') as file:
file.write(json_data)
print(f"Data successfully written to {output_file}")
except Exception as e:
print(f"Error: {e}")

View File

@@ -0,0 +1,182 @@
import serial
import json
import time
import math
# Record the start time of the script
start_time = time.time()
#get config
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
need_to_log = config.get('loop_log', False)
# Initialize serial ports for the three sensors
ser3 = serial.Serial(
port='/dev/ttyAMA3',
baudrate=115200,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
ser4 = serial.Serial(
port='/dev/ttyAMA4',
baudrate=115200,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
ser5 = serial.Serial(
port='/dev/ttyAMA5',
baudrate=115200,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
# Function to read and parse sensor data
def read_sensor_data(ser, sonde_id):
try:
# Send the command to request data (e.g., data for 60 seconds)
ser.write(b'\x81\x12\x6D')
# Read the response
byte_data = ser.readline()
# Extract the state byte and PM data from the response
PM1 = int.from_bytes(byte_data[9:11], byteorder='big') / 10
PM25 = int.from_bytes(byte_data[11:13], byteorder='big') / 10
PM10 = int.from_bytes(byte_data[13:15], byteorder='big') / 10
# Create a dictionary with the parsed data
data = {
'sondeID': sonde_id,
'PM1': PM1,
'PM25': PM25,
'PM10': PM10
}
return data
except Exception as e:
print(f"Error reading from sensor {sonde_id}: {e}")
return None
# Function to calculate the Euclidean distance between two sensor readings
def calculate_distance(sensor1, sensor2):
PM1_diff = sensor1['PM1'] - sensor2['PM1']
PM25_diff = sensor1['PM25'] - sensor2['PM25']
PM10_diff = sensor1['PM10'] - sensor2['PM10']
return math.sqrt(PM1_diff**2 + PM25_diff**2 + PM10_diff**2)
# Function to select the closest pair of sensors and average their data
def average_closest_pair(data):
# List of sensor names and their data
sensors = list(data.items())
# Variable to keep track of the smallest distance and corresponding pair
min_distance = float('inf')
closest_pair = None
# Compare each pair of sensors to find the closest one
for i in range(len(sensors)):
for j in range(i + 1, len(sensors)):
sensor1 = sensors[i][1]
sensor2 = sensors[j][1]
# Calculate the distance between the two sensors
distance = calculate_distance(sensor1, sensor2)
# Update the closest pair if a smaller distance is found
if distance < min_distance:
min_distance = distance
closest_pair = (sensor1, sensor2)
# If a closest pair is found, average their values
if closest_pair:
sensor1, sensor2 = closest_pair
averaged_data = {
'sondeID': f"Average_{sensor1['sondeID']}_{sensor2['sondeID']}",
'PM1': round((sensor1['PM1'] + sensor2['PM1']) / 2, 2),
'PM25': round((sensor1['PM25'] + sensor2['PM25']) / 2, 2),
'PM10': round((sensor1['PM10'] + sensor2['PM10']) / 2, 2)
}
return averaged_data
else:
return None
# Function to create a JSON object with all sensor data
def collect_all_sensor_data():
all_data = {}
# Read data from each sensor and add to the all_data dictionary
sensor_data_3 = read_sensor_data(ser3, 'USB2')
sensor_data_4 = read_sensor_data(ser4, 'USB3')
sensor_data_5 = read_sensor_data(ser5, 'USB4')
# Store the data for each sensor in the dictionary
if sensor_data_3:
all_data['sensor_3'] = sensor_data_3
if sensor_data_4:
all_data['sensor_4'] = sensor_data_4
if sensor_data_5:
all_data['sensor_5'] = sensor_data_5
return all_data
# Main script to run once and average data for the closest sensors
if __name__ == "__main__":
try:
# Collect data from all sensors
data = collect_all_sensor_data()
if need_to_log:
print("Getting Data from all sensors:")
print(data)
# Average the closest pair of sensors
averaged_data = average_closest_pair(data)
if need_to_log:
print("Average the closest pair of sensors:")
print(averaged_data)
if averaged_data:
# Convert the averaged data to JSON
json_data = json.dumps(averaged_data, indent=4)
# Define the output file path
output_file = "/var/www/nebuleair_pro_4g/loop/data.json" # Change this to your desired file path
# Write the JSON data to the file
with open(output_file, 'w') as file:
file.write(json_data)
if need_to_log:
print(f"Data successfully written to {output_file}")
else:
print("No closest pair found to average.")
# Calculate and print the elapsed time
elapsed_time = time.time() - start_time
if need_to_log:
print(f"Elapsed time: {elapsed_time:.2f} seconds")
print("-----------------")
except Exception as e:
print(f"Error: {e}")

142
loop/3_NPM/send_data.py Executable file
View File

@@ -0,0 +1,142 @@
import json
import serial
import time
# Record the start time of the script
start_time = time.time()
# Define the path to the JSON file
file_path = "/var/www/nebuleair_pro_4g/loop/data.json" # Replace with your actual file path
url="data.nebuleair.fr"
#get config
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)
device_id = config.get('deviceID', '').upper()
need_to_log = config.get('loop_log', False)
ser = serial.Serial(
port='/dev/ttyAMA2',
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
)
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')
# Open and read the JSON file
try:
with open(file_path, 'r') as file:
# Load the data from the file
data = json.load(file)
# Print the content of the JSON file
if need_to_log:
print("Data from JSON file:")
print(json.dumps(data, indent=4)) # Pretty print the JSON data
message = f"{data['PM1']},{data['PM25']},{data['PM10']}"
#Write Data to saraR4
#1. Open sensordata.json (with correct data size)
size_of_string = len(message)
command = f'AT+UDWNFILE="sensordata.json",{size_of_string}\r'
ser.write((command + '\r').encode('utf-8'))
response_SARA_1 = read_complete_response(ser)
if need_to_log:
print("Open JSON:")
print(response_SARA_1)
time.sleep(1)
#2. Write to shell
ser.write(message.encode())
response_SARA_2 = read_complete_response(ser)
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'
ser.write((command + '\r').encode('utf-8'))
response_SARA_3 = read_complete_response(ser)
if need_to_log:
print("Send data:")
print(response_SARA_3)
# Split response into lines
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
# Extract HTTP response code from the last line
http_response = lines[-1] # "+UUHTTPCR: 0,4,0"
parts = http_response.split(',')
# Check HTTP result
if len(parts) == 3 and parts[-1] == '0': # The third value indicates success
print("*****")
print("!ATTENTION!")
print("error: HTTP operation failed.")
print("*****")
print("resetting the URL (domain name):")
command = f'AT+UHTTP=0,1,"{url}"\r'
ser.write((command + '\r').encode('utf-8'))
response_SARA_31 = read_complete_response(ser)
if need_to_log:
print(response_SARA_31)
else:
print("HTTP operation successful.")
#4. Read reply from server
ser.write(b'AT+URDFILE="server_response.txt"\r')
response_SARA_4 = read_complete_response(ser)
if need_to_log:
print("Reply from server:")
print(response_SARA_4)
#5. empty json
ser.write(b'AT+UDELFILE="sensordata.json"\r')
response_SARA_5 = read_complete_response(ser)
if need_to_log:
print("Empty JSON:")
print(response_SARA_5)
# Calculate and print the elapsed time
elapsed_time = time.time() - start_time
if need_to_log:
print(f"Elapsed time: {elapsed_time:.2f} seconds")
print("-----------------")
except Exception as e:
print(f"Error reading the JSON file: {e}")