This commit is contained in:
Your Name
2025-01-31 13:38:26 +01:00
parent c15838fc47
commit 456db5da98
8 changed files with 74 additions and 9 deletions

View File

@@ -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}")

0
RTC/read.py Normal file → Executable file
View File

0
RTC/set_with_NTP.py Normal file → Executable file
View File

0
RTC/set_with_browserTime.py Normal file → Executable file
View File

0
envea/read_value_loop_json.py Normal file → Executable file
View File

22
sqlite/create_db.py Normal file → Executable file
View File

@@ -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
)
""")

0
sqlite/read.py Normal file → Executable file
View File

11
sqlite/write.py Normal file → Executable file
View File

@@ -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()