- Scripts MH-Z19/get_data.py (lecture standalone) et write_data.py (écriture SQLite) - Table data_MHZ19, config MHZ19, cleanup et service systemd (120s) - Web UI : carte test sensors, checkbox admin, boutons database + CSV download - SARA_send_data_v2.py non modifié (sera fait dans un second temps) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
59 lines
1.2 KiB
Python
59 lines
1.2 KiB
Python
'''
|
|
Script to get CO2 values from MH-Z19 sensor
|
|
need parameter: CO2_port
|
|
/usr/bin/python3 /var/www/nebuleair_pro_4g/MH-Z19/get_data.py ttyAMA4
|
|
'''
|
|
|
|
import serial
|
|
import json
|
|
import sys
|
|
import time
|
|
|
|
parameter = sys.argv[1:]
|
|
port = '/dev/' + parameter[0]
|
|
|
|
ser = serial.Serial(
|
|
port=port,
|
|
baudrate=9600,
|
|
parity=serial.PARITY_NONE,
|
|
stopbits=serial.STOPBITS_ONE,
|
|
bytesize=serial.EIGHTBITS,
|
|
timeout=1
|
|
)
|
|
|
|
READ_CO2_COMMAND = b'\xFF\x01\x86\x00\x00\x00\x00\x00\x79'
|
|
|
|
|
|
def read_co2():
|
|
ser.write(READ_CO2_COMMAND)
|
|
time.sleep(2)
|
|
response = ser.read(9)
|
|
if len(response) < 9:
|
|
print("Error: No data or incomplete data received.")
|
|
return None
|
|
if response[0] == 0xFF:
|
|
co2_concentration = response[2] * 256 + response[3]
|
|
return co2_concentration
|
|
else:
|
|
print("Error reading data from sensor.")
|
|
return None
|
|
|
|
|
|
def main():
|
|
try:
|
|
co2 = read_co2()
|
|
if co2 is not None:
|
|
data = {"CO2": co2}
|
|
json_data = json.dumps(data)
|
|
print(json_data)
|
|
else:
|
|
print("Failed to get CO2 data.")
|
|
except KeyboardInterrupt:
|
|
print("Program terminated.")
|
|
finally:
|
|
ser.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|