128 lines
4.1 KiB
Python
Executable File
128 lines
4.1 KiB
Python
Executable File
"""
|
|
Main loop to gather data from Envea Sensors
|
|
|
|
Runs every minute via cron:
|
|
|
|
* * * * * /usr/bin/python3 /var/www/nebuleair_pro_4g/envea/read_value_loop_json.py
|
|
|
|
Saves data as JSON inside: /var/www/nebuleair_pro_4g/envea/data/data.json
|
|
"""
|
|
|
|
import json
|
|
import serial
|
|
import time
|
|
import traceback
|
|
from datetime import datetime
|
|
|
|
# Function to load config data
|
|
def load_config(config_file):
|
|
try:
|
|
with open(config_file, 'r') as file:
|
|
return json.load(file)
|
|
except Exception as e:
|
|
print(f"Error loading config file: {e}")
|
|
return {}
|
|
|
|
# Function to save data to a JSON file
|
|
def save_data_to_json(filename, data):
|
|
try:
|
|
with open(filename, 'w') as file:
|
|
json.dump(data, file, indent=4)
|
|
print(f"Data saved to {filename}")
|
|
except Exception as e:
|
|
print(f"Error saving to file {filename}: {e}")
|
|
|
|
# Define the config file path
|
|
config_file = '/var/www/nebuleair_pro_4g/config.json'
|
|
|
|
# Load configuration data
|
|
config = load_config(config_file)
|
|
|
|
# Initialize sensors and serial connections
|
|
envea_sondes = config.get('envea_sondes', [])
|
|
connected_envea_sondes = [sonde for sonde in envea_sondes if sonde.get('connected', False)]
|
|
serial_connections = {}
|
|
|
|
if connected_envea_sondes:
|
|
for device in connected_envea_sondes:
|
|
port = device.get('port', 'Unknown')
|
|
name = device.get('name', 'Unknown')
|
|
try:
|
|
serial_connections[name] = serial.Serial(
|
|
port=f'/dev/{port}',
|
|
baudrate=9600,
|
|
parity=serial.PARITY_NONE,
|
|
stopbits=serial.STOPBITS_ONE,
|
|
bytesize=serial.EIGHTBITS,
|
|
timeout=1
|
|
)
|
|
except serial.SerialException as e:
|
|
print(f"Error opening serial port for {name}: {e}")
|
|
|
|
# Function to gather data from sensors
|
|
def gather_data():
|
|
global data_h2s, data_no2, data_o3
|
|
data_h2s = 0
|
|
data_no2 = 0
|
|
data_o3 = 0
|
|
|
|
try:
|
|
if connected_envea_sondes:
|
|
for device in connected_envea_sondes:
|
|
name = device.get('name', 'Unknown')
|
|
coefficient = device.get('coefficient', 1)
|
|
if name in serial_connections:
|
|
serial_connection = serial_connections[name]
|
|
try:
|
|
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"
|
|
)
|
|
data_envea = serial_connection.readline()
|
|
if len(data_envea) >= 20:
|
|
byte_20 = data_envea[19] * coefficient
|
|
if name == "h2s":
|
|
data_h2s = byte_20
|
|
elif name == "no2":
|
|
data_no2 = byte_20
|
|
elif name == "o3":
|
|
data_o3 = byte_20
|
|
except serial.SerialException as e:
|
|
print(f"Error communicating with {name}: {e}")
|
|
except Exception as e:
|
|
print("An error occurred while gathering data:", e)
|
|
traceback.print_exc()
|
|
|
|
# Main loop
|
|
if __name__ == "__main__":
|
|
h2s_values = []
|
|
no2_values = []
|
|
o3_values = []
|
|
|
|
for cycle in range(6): # Run 6 times
|
|
gather_data()
|
|
h2s_values.append(data_h2s)
|
|
no2_values.append(data_no2)
|
|
o3_values.append(data_o3)
|
|
|
|
print(f"Cycle {cycle + 1}:")
|
|
print(f" H2S: {data_h2s}, NO2: {data_no2}, O3: {data_o3}")
|
|
time.sleep(9) # Wait 9 seconds
|
|
|
|
# Compute the mean values (as integers)
|
|
mean_h2s = int(sum(h2s_values) / len(h2s_values)) if h2s_values else 0
|
|
mean_no2 = int(sum(no2_values) / len(no2_values)) if no2_values else 0
|
|
mean_o3 = int(sum(o3_values) / len(o3_values)) if o3_values else 0
|
|
|
|
# Create JSON structure
|
|
data_json = {
|
|
"h2s": mean_h2s,
|
|
"no2": mean_no2,
|
|
"o3": mean_o3
|
|
}
|
|
|
|
# Define JSON file path
|
|
output_file = "/var/www/nebuleair_pro_4g/envea/data/data.json"
|
|
|
|
# Save to JSON file
|
|
save_data_to_json(output_file, data_json)
|