This commit is contained in:
PaulVua
2025-02-05 17:37:27 +01:00
parent 46303b9c19
commit d98eb48535
5 changed files with 284 additions and 71 deletions

View File

@@ -1,12 +1,12 @@
'''
____ _____ _ _ ____ ___ ____ ____
/ ___|| ____| \ | / ___| / _ \| _ \/ ___|
\___ \| _| | \| \___ \| | | | |_) \___ \
___) | |___| |\ |___) | |_| | _ < ___) |
|____/|_____|_| \_|____/ \___/|_| \_\____/
Script to get SENSORS values
_ _ ____ __ __
| \ | | _ \| \/ |
| \| | |_) | |\/| |
| |\ | __/| | | |
|_| \_|_| |_| |_|
Script to get NPM values
PM and the sensor temp/hum
And store them inside sqlite database
Uses RTC module for timing
/usr/bin/python3 /var/www/nebuleair_pro_4g/NPM/get_data_v2.py
@@ -72,70 +72,58 @@ ser = serial.Serial(
#ser.write(b'\x81\x11\x6E') #data10s
ser.write(b'\x81\x12\x6D') #data60s
while True:
try:
#print("Start get_data_v2.py script")
byte_data = ser.readline()
#print(byte_data)
stateByte = int.from_bytes(byte_data[2:3], byteorder='big')
Statebits = [int(bit) for bit in bin(stateByte)[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
#print(f"State: {Statebits}")
#print(f"PM1: {PM1}")
#print(f"PM25: {PM25}")
#print(f"PM10: {PM10}")
#create JSON
data = {
'PM1': PM1,
'PM25': PM25,
'PM10': PM10,
'sleep' : Statebits[0],
'degradedState' : Statebits[1],
'notReady' : Statebits[2],
'heatError' : Statebits[3],
't_rhError' : Statebits[4],
'fanError' : Statebits[5],
'memoryError' : Statebits[6],
'laserError' : Statebits[7]
}
json_data = json.dumps(data)
#print(json_data)
#GET RTC TIME
# Read RTC time
bus = smbus2.SMBus(1)
# Try to read RTC time
rtc_time = read_time(bus)
#print("Start get_data_v2.py script")
byte_data = ser.readline()
#print(byte_data)
stateByte = int.from_bytes(byte_data[2:3], byteorder='big')
Statebits = [int(bit) for bit in bin(stateByte)[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
if rtc_time:
rtc_time_str = rtc_time.strftime('%Y-%m-%d %H:%M:%S')
#print(rtc_time_str)
else:
print("Error! RTC module not connected")
rtc_time_str = "1970-01-01 00:00:00" # Default fallback time
# Write command to retrieve temperature and humidity data
ser.write(b'\x81\x14\x6B') # Temp and humidity command
byte_data_temp_hum = ser.readline()
# Decode temperature and humidity values
temperature = int.from_bytes(byte_data_temp_hum[3:5], byteorder='big') / 100.0
humidity = int.from_bytes(byte_data_temp_hum[5:7], byteorder='big') / 100.0
#print(f"State: {Statebits}")
#print(f"PM1: {PM1}")
#print(f"PM25: {PM25}")
#print(f"PM10: {PM10}")
#print(f"temp: {temperature}")
#print(f"hum: {humidity}")
#GET RTC TIME
# Read RTC time
bus = smbus2.SMBus(1)
# Try to read RTC time
rtc_time = read_time(bus)
if rtc_time:
rtc_time_str = rtc_time.strftime('%Y-%m-%d %H:%M:%S')
#print(rtc_time_str)
else:
print("Error! RTC module not connected")
rtc_time_str = "1970-01-01 00:00:00" # Default fallback time
#save to sqlite database
cursor.execute('''
INSERT INTO data (timestamp,PM1, PM25, PM10) VALUES (?,?,?,?)'''
, (rtc_time_str,PM1,PM25,PM10))
#save to sqlite database
try:
cursor.execute('''
INSERT INTO data_NPM (timestamp,PM1, PM25, PM10, temp_npm, hum_npm) VALUES (?,?,?,?,?,?)'''
, (rtc_time_str,PM1,PM25,PM10,temperature,humidity ))
# Commit and close the connection
conn.commit()
# Commit and close the connection
conn.commit()
#print("Sensor data saved successfully!")
#print("Sensor data saved successfully!")
except Exception as e:
print(f"Database error: {e}")
break # Exit loop after successful execution
except KeyboardInterrupt:
print("User interrupt encountered. Exiting...")
time.sleep(3)
exit()
except Exception as e:
print(f"Error: {e}") # Show actual error
time.sleep(3)
exit()
conn.close()