NPM: lecture registre status Modbus (reg 19) + colonne npm_status
- get_data_modbus_v3.py: requete Modbus separee pour lire le registre status (0x13) du NextPM apres les donnees. Stocke dans npm_status. - create_db.py: ajout colonne npm_status (INTEGER DEFAULT 0) dans data_NPM + migration ALTER TABLE pour bases existantes. - En cas d'erreur de lecture status, garde 0xFF (toutes erreurs). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -72,6 +72,7 @@ channel_4 = 0
|
|||||||
channel_5 = 0
|
channel_5 = 0
|
||||||
relative_humidity = 0
|
relative_humidity = 0
|
||||||
temperature = 0
|
temperature = 0
|
||||||
|
npm_status = 0xFF # Default: all errors (will be overwritten if read succeeds)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Initialize serial port
|
# Initialize serial port
|
||||||
@@ -176,6 +177,29 @@ try:
|
|||||||
#print(f"Internal Relative Humidity: {relative_humidity} %")
|
#print(f"Internal Relative Humidity: {relative_humidity} %")
|
||||||
#print(f"Internal temperature: {temperature} °C")
|
#print(f"Internal temperature: {temperature} °C")
|
||||||
|
|
||||||
|
# Read NPM status register (register 19 = 0x13, 1 register)
|
||||||
|
# Modbus request: slave=0x01, func=0x03, addr=0x0013, qty=0x0001
|
||||||
|
status_request = b'\x01\x03\x00\x13\x00\x01'
|
||||||
|
status_crc = crc16(status_request)
|
||||||
|
status_request += bytes([status_crc & 0xFF, (status_crc >> 8) & 0xFF])
|
||||||
|
|
||||||
|
ser.flushInput()
|
||||||
|
ser.write(status_request)
|
||||||
|
time.sleep(0.2)
|
||||||
|
|
||||||
|
# Response: addr(1) + func(1) + byte_count(1) + data(2) + crc(2) = 7 bytes
|
||||||
|
status_response = ser.read(7)
|
||||||
|
if len(status_response) == 7:
|
||||||
|
status_recv_crc = int.from_bytes(status_response[-2:], byteorder='little')
|
||||||
|
status_calc_crc = crc16(status_response[:-2])
|
||||||
|
if status_recv_crc == status_calc_crc:
|
||||||
|
npm_status = int.from_bytes(status_response[3:5], byteorder='big') & 0xFF
|
||||||
|
print(f"NPM status: 0x{npm_status:02X} ({npm_status})")
|
||||||
|
else:
|
||||||
|
print("[WARNING] NPM status CRC check failed, keeping default")
|
||||||
|
else:
|
||||||
|
print(f"[WARNING] NPM status incomplete response ({len(status_response)} bytes)")
|
||||||
|
|
||||||
ser.close()
|
ser.close()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -189,8 +213,8 @@ finally:
|
|||||||
, (rtc_time_str, channel_1, channel_2, channel_3, channel_4, channel_5))
|
, (rtc_time_str, channel_1, channel_2, channel_3, channel_4, channel_5))
|
||||||
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
INSERT INTO data_NPM (timestamp,PM1, PM25, PM10, temp_npm, hum_npm) VALUES (?,?,?,?,?,?)'''
|
INSERT INTO data_NPM (timestamp,PM1, PM25, PM10, temp_npm, hum_npm, npm_status) VALUES (?,?,?,?,?,?,?)'''
|
||||||
, (rtc_time_str, pm1_10s, pm25_10s, pm10_10s, temperature, relative_humidity))
|
, (rtc_time_str, pm1_10s, pm25_10s, pm10_10s, temperature, relative_humidity, npm_status))
|
||||||
|
|
||||||
# Commit and close the connection
|
# Commit and close the connection
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|||||||
@@ -67,10 +67,18 @@ CREATE TABLE IF NOT EXISTS data_NPM (
|
|||||||
PM25 REAL,
|
PM25 REAL,
|
||||||
PM10 REAL,
|
PM10 REAL,
|
||||||
temp_npm REAL,
|
temp_npm REAL,
|
||||||
hum_npm REAL
|
hum_npm REAL,
|
||||||
|
npm_status INTEGER DEFAULT 0
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
# Add npm_status column to existing databases (migration)
|
||||||
|
try:
|
||||||
|
cursor.execute("ALTER TABLE data_NPM ADD COLUMN npm_status INTEGER DEFAULT 0")
|
||||||
|
print("Added npm_status column to data_NPM")
|
||||||
|
except:
|
||||||
|
pass # Column already exists
|
||||||
|
|
||||||
# Create a table BME280
|
# Create a table BME280
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
CREATE TABLE IF NOT EXISTS data_BME280 (
|
CREATE TABLE IF NOT EXISTS data_BME280 (
|
||||||
|
|||||||
Reference in New Issue
Block a user