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