This commit is contained in:
PaulVua
2025-02-10 10:06:16 +01:00
parent 7cac769795
commit 1cb1b05b51
10 changed files with 200 additions and 77 deletions

View File

@@ -8,7 +8,7 @@
Script to get NPM values
PM and the sensor temp/hum
And store them inside sqlite database
Uses RTC module for timing
Uses RTC module for timing (from SQLite db)
/usr/bin/python3 /var/www/nebuleair_pro_4g/NPM/get_data_v2.py
'''
@@ -25,27 +25,6 @@ from datetime import datetime
conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db")
cursor = conn.cursor()
#RTC module
DS3231_ADDR = 0x68
REG_TIME = 0x00
def bcd_to_dec(bcd):
return (bcd // 16 * 10) + (bcd % 16)
def read_time(bus):
"""Try to read and decode time from the RTC module (DS3231)."""
try:
data = bus.read_i2c_block_data(DS3231_ADDR, REG_TIME, 7)
seconds = bcd_to_dec(data[0] & 0x7F)
minutes = bcd_to_dec(data[1])
hours = bcd_to_dec(data[2] & 0x3F)
day = bcd_to_dec(data[4])
month = bcd_to_dec(data[5])
year = bcd_to_dec(data[6]) + 2000
return datetime(year, month, day, hours, minutes, seconds)
except OSError:
return None # RTC module not connected
def load_config(config_file):
try:
with open(config_file, 'r') as file:
@@ -96,19 +75,11 @@ humidity = int.from_bytes(byte_data_temp_hum[5:7], byteorder='big') / 100.0
#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
#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 database
try: