diff --git a/NPM/get_data_modbus_loop.py b/NPM/get_data_modbus_loop.py index 48a9570..d58258a 100755 --- a/NPM/get_data_modbus_loop.py +++ b/NPM/get_data_modbus_loop.py @@ -1,8 +1,10 @@ ''' Loop to run every minutes + * * * * * /usr/bin/python3 /var/www/nebuleair_pro_4g/NPM/get_data_modbus_loop.py ttyAMA5 -saves data to a json file /var/www/nebuleair_pro_4g/NPM/data/data.json +saves data (avaerage) to a json file /var/www/nebuleair_pro_4g/NPM/data/data.json +saves data (all) to a sqlite database first time running the script? sudo mkdir /var/www/nebuleair_pro_4g/NPM/data @@ -15,6 +17,10 @@ import crcmod import time import json import os +import sqlite3 +import smbus2 # For RTC DS3231 +from datetime import datetime + # Ensure a port argument is provided if len(sys.argv) < 2: @@ -54,14 +60,44 @@ request = data + bytes([crc_low, crc_high]) # Log request frame print(f"Request frame: {request.hex()}") +# Initialize SQLite database +conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") +cursor = conn.cursor() + +# RTC Module (DS3231) Setup +RTC_I2C_ADDR = 0x68 # DS3231 I2C Address +bus = smbus2.SMBus(1) + +def bcd_to_dec(bcd): + return (bcd // 16 * 10) + (bcd % 16) + +def get_rtc_time(): + """Reads time from RTC module (DS3231)""" + try: + data = bus.read_i2c_block_data(RTC_I2C_ADDR, 0x00, 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).strftime("%Y-%m-%d %H:%M:%S") + except Exception as e: + print(f"RTC Read Error: {e}") + return "N/A" + # Initialize storage for averaging num_samples = 6 channel_sums = [0] * 5 # 5 channels +#do not start immediately to prevent multiple write/read over NPM serial port +time.sleep(3) + # Loop 6 times to collect data every 10 seconds for i in range(num_samples): print(f"\nIteration {i+1}/{num_samples}") ser.write(request) + rtc_timestamp = get_rtc_time() try: byte_data = ser.readline() @@ -70,7 +106,7 @@ for i in range(num_samples): if len(byte_data) < 23: print("Incomplete response, skipping this sample.") - time.sleep(10) + time.sleep(9) continue # Extract and process the 5 channels @@ -86,9 +122,19 @@ for i in range(num_samples): channel_sums[j] += channels[j] # Print collected values + print(f"Timestamp (RTC): {rtc_timestamp}") for j in range(5): print(f"Channel {j+1}: {channels[j]}") + + # Save the individual reading to the database + cursor.execute(''' + INSERT INTO data (timestamp, PM_ch1, PM_ch2, PM_ch3, PM_ch4, PM_ch5) + VALUES (?, ?, ?, ?, ?, ?) + ''', (rtc_timestamp, channels[0], channels[1], channels[2], channels[3], channels[4])) + + conn.commit() + except Exception as e: print(f"Error reading data: {e}") diff --git a/RTC/read.py b/RTC/read.py old mode 100644 new mode 100755 diff --git a/RTC/set_with_NTP.py b/RTC/set_with_NTP.py old mode 100644 new mode 100755 diff --git a/RTC/set_with_browserTime.py b/RTC/set_with_browserTime.py old mode 100644 new mode 100755 diff --git a/envea/read_value_loop_json.py b/envea/read_value_loop_json.py old mode 100644 new mode 100755 diff --git a/sqlite/create_db.py b/sqlite/create_db.py old mode 100644 new mode 100755 index 3df266b..e844c46 --- a/sqlite/create_db.py +++ b/sqlite/create_db.py @@ -2,6 +2,8 @@ Script to create a sqlite database /usr/bin/python3 /var/www/nebuleair_pro_4g/sqlite/create_db.py + + ''' import sqlite3 @@ -13,10 +15,22 @@ cursor = conn.cursor() # Create a table for storing sensor data cursor.execute(""" CREATE TABLE IF NOT EXISTS data ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, - sensor TEXT, - value REAL + timestamp TEXT, + sensor_id TEXT, + PM1 REAL, + PM25 REAL, + PM10 REAL, + temp REAL, + hum REAL, + press REAL, + no2 REAL, + h2s REAL, + o3 REAL, + PM_ch1 INTEGER, + PM_ch2 INTEGER, + PM_ch3 INTEGER, + PM_ch4 INTEGER, + PM_ch5 INTEGER ) """) diff --git a/sqlite/read.py b/sqlite/read.py old mode 100644 new mode 100755 diff --git a/sqlite/write.py b/sqlite/write.py old mode 100644 new mode 100755 index cd5da62..e81baec --- a/sqlite/write.py +++ b/sqlite/write.py @@ -11,10 +11,15 @@ conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") cursor = conn.cursor() # Insert a sample temperature reading -sensor_name = "temperature" -sensor_value = 25.3 +timestamp = "2025-10-11" +sensor_name = "NebuleAir-pro020" +PM1 = 25.3 +PM25 = 18.3 +PM10 = 9.3 -cursor.execute("INSERT INTO data (sensor, value) VALUES (?, ?)", (sensor_name, sensor_value)) +cursor.execute(''' +INSERT INTO data (timestamp, sensor_id, PM1, PM25, PM10) VALUES (?,?,?,?,?)''' +, (timestamp, sensor_name,PM1,PM25,PM10)) # Commit and close the connection conn.commit()