update
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,6 +9,7 @@ config.json
|
|||||||
sound_meter/moving_avg_minute.txt
|
sound_meter/moving_avg_minute.txt
|
||||||
wifi_list.csv
|
wifi_list.csv
|
||||||
envea/data/*.txt
|
envea/data/*.txt
|
||||||
|
envea/data/*.json
|
||||||
NPM/data/*.txt
|
NPM/data/*.txt
|
||||||
NPM/data/*.json
|
NPM/data/*.json
|
||||||
*.lock
|
*.lock
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
"""
|
"""
|
||||||
Main loop to gather data from envea Sensors
|
Main loop to gather data from envea Sensors
|
||||||
/usr/bin/python3 /var/www/nebuleair_pro_4g/envea/read_value_loop.py
|
Need to run every minutes
|
||||||
|
|
||||||
|
* * * * * /usr/bin/python3 /var/www/nebuleair_pro_4g/envea/read_value_loop.py
|
||||||
|
|
||||||
Save data to .txt file inside /var/www/nebuleair_pro_4g/envea/data/
|
Save data to .txt file inside /var/www/nebuleair_pro_4g/envea/data/
|
||||||
"""
|
"""
|
||||||
|
|||||||
127
envea/read_value_loop_json.py
Normal file
127
envea/read_value_loop_json.py
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
"""
|
||||||
|
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)
|
||||||
@@ -375,50 +375,42 @@ try:
|
|||||||
|
|
||||||
# Sondes Envea
|
# Sondes Envea
|
||||||
if connected_envea_sondes:
|
if connected_envea_sondes:
|
||||||
# Pour chacune des sondes
|
print("Getting Envea values")
|
||||||
for device in connected_envea_sondes:
|
# Define the path to the JSON file
|
||||||
port = device.get('port', 'Unknown')
|
json_file_path_envea = "/var/www/nebuleair_pro_4g/envea/data/data.json"
|
||||||
name = device.get('name', 'Unknown')
|
# Read the JSON file
|
||||||
coefficient = device.get('coefficient', 'Unknown')
|
try:
|
||||||
|
with open(json_file_path_envea, "r") as file:
|
||||||
|
data = json.load(file) # Load JSON into a dictionary
|
||||||
|
|
||||||
print(f"Connected envea Sonde: {name} on port {port} and coefficient {coefficient} ")
|
# Extract values
|
||||||
|
h2s = data.get("h2s", 0)
|
||||||
if name in serial_connections:
|
no2 = data.get("no2", 0)
|
||||||
serial_connection = serial_connections[name]
|
o3 = data.get("o3", 0)
|
||||||
|
|
||||||
try:
|
# Print extracted values
|
||||||
# Write data to the device
|
print(f"h2s : {h2s}")
|
||||||
serial_connection.write(
|
print(f"no2 : {no2}")
|
||||||
b"\xFF\x02\x13\x30\x01\x02\x03\x04\x05\x06\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x12\xAF\x88\x03"
|
print(f"o3: {o3}")
|
||||||
)
|
|
||||||
|
|
||||||
# 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
|
|
||||||
payload_json["sensordatavalues"].append({"value_type": "CAIRSENS_H2S", "value": str(byte_20)})
|
|
||||||
if name == "no2":
|
|
||||||
payload_csv[9] = byte_20
|
|
||||||
payload_json["sensordatavalues"].append({"value_type": "CAIRSENS_NO2", "value": str(byte_20)})
|
|
||||||
if name == "o3":
|
|
||||||
payload_csv[11] = byte_20
|
|
||||||
payload_json["sensordatavalues"].append({"value_type": "CAIRSENS_O3", "value": str(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(f"No serial connection for {name}")
|
|
||||||
|
|
||||||
|
#add to CSV
|
||||||
|
payload_csv[10] = h2s
|
||||||
|
payload_csv[9] = no2
|
||||||
|
payload_csv[11] = o3
|
||||||
|
|
||||||
|
#add to JSON
|
||||||
|
payload_json["sensordatavalues"].append({"value_type": "CAIRSENS_H2S", "value": str(h2s)})
|
||||||
|
payload_json["sensordatavalues"].append({"value_type": "CAIRSENS_NO2", "value": str(no2)})
|
||||||
|
payload_json["sensordatavalues"].append({"value_type": "CAIRSENS_O3", "value": str(o3)})
|
||||||
|
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"Error: JSON file not found at {json_file_path_envea}")
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
print("Error: JSON file is not formatted correctly")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Unexpected error: {e}")
|
||||||
|
|
||||||
# Getting the LTE Signal
|
# Getting the LTE Signal
|
||||||
print("-> Getting LTE signal <-")
|
print("-> Getting LTE signal <-")
|
||||||
ser_sara.write(b'AT+CSQ\r')
|
ser_sara.write(b'AT+CSQ\r')
|
||||||
|
|||||||
Reference in New Issue
Block a user