update
This commit is contained in:
@@ -5,9 +5,8 @@
|
||||
| |__| |_| / __/
|
||||
\____\___/_____|
|
||||
|
||||
Script to get CO2 values and write it to text file
|
||||
need parameter: CO2_port
|
||||
/usr/bin/python3 /var/www/moduleair_pro_4g/MH-Z19/write_data.py ttyAMA4
|
||||
Script to get CO2 values and write it to the database
|
||||
/usr/bin/python3 /var/www/moduleair_pro_4g/MH-Z19/write_data.py
|
||||
'''
|
||||
|
||||
import serial
|
||||
@@ -16,13 +15,32 @@ import json
|
||||
import sys
|
||||
import subprocess
|
||||
import time
|
||||
import sqlite3
|
||||
|
||||
parameter = sys.argv[1:] # Exclude the script name
|
||||
#print("Parameters received:")
|
||||
port='/dev/'+parameter[0]
|
||||
|
||||
|
||||
|
||||
# Connect to the SQLite database
|
||||
conn = sqlite3.connect("/var/www/moduleair_pro_4g/sqlite/sensors.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
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 {}
|
||||
|
||||
|
||||
# Load the configuration data
|
||||
config_file = '/var/www/moduleair_pro_4g/config.json'
|
||||
config = load_config(config_file)
|
||||
mh_z19_port = config.get('MH-Z19_port', '') #port du NPM solo
|
||||
|
||||
ser = serial.Serial(
|
||||
port=port,
|
||||
port=mh_z19_port,
|
||||
baudrate=9600,
|
||||
parity=serial.PARITY_NONE,
|
||||
stopbits=serial.STOPBITS_ONE,
|
||||
@@ -78,10 +96,23 @@ def main():
|
||||
co2 = read_co2()
|
||||
if co2 is not None:
|
||||
print(f"CO2 Concentration: {co2} ppm")
|
||||
#save to file
|
||||
output_file = "/var/www/moduleair_pro_4g/matrix/input_MHZ16.txt"
|
||||
with open(output_file, "w") as file:
|
||||
file.write(f"{co2} \n")
|
||||
print(f"Data written to {output_file}")
|
||||
#GET RTC TIME from SQlite
|
||||
cursor.execute("SELECT * FROM timestamp_table LIMIT 1")
|
||||
row = cursor.fetchone() # Get the first (and only) row
|
||||
rtc_time_str = row[1] # '2025-02-07 12:30:45'
|
||||
#save to sqlite
|
||||
cursor.execute('''
|
||||
INSERT INTO data_CO2 (timestamp,CO2) VALUES (?,?)'''
|
||||
, (rtc_time_str,co2))
|
||||
|
||||
# Commit and close the connection
|
||||
conn.commit()
|
||||
|
||||
else:
|
||||
print("Failed to get CO2 data.")
|
||||
|
||||
@@ -89,6 +120,7 @@ def main():
|
||||
print("Program terminated.")
|
||||
finally:
|
||||
ser.close()
|
||||
conn.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user