update
This commit is contained in:
64
NPM/firmware_version.py
Normal file
64
NPM/firmware_version.py
Normal file
@@ -0,0 +1,64 @@
|
||||
'''
|
||||
Script to get NPM firmware version
|
||||
need parameter: port
|
||||
/usr/bin/python3 /var/www/nebuleair_pro_4g/NPM/firmware_version.py ttyAMA5
|
||||
'''
|
||||
|
||||
import serial
|
||||
import requests
|
||||
import json
|
||||
import sys
|
||||
|
||||
parameter = sys.argv[1:] # Exclude the script name
|
||||
#print("Parameters received:")
|
||||
port='/dev/'+parameter[0]
|
||||
|
||||
ser = serial.Serial(
|
||||
port=port,
|
||||
baudrate=115200,
|
||||
parity=serial.PARITY_EVEN,
|
||||
stopbits=serial.STOPBITS_ONE,
|
||||
bytesize=serial.EIGHTBITS,
|
||||
timeout = 1
|
||||
)
|
||||
|
||||
ser.write(b'\x81\x17\x68') #firmware version
|
||||
|
||||
while True:
|
||||
try:
|
||||
byte_data = ser.readline()
|
||||
formatted = ''.join(f'\\x{byte:02x}' for byte in byte_data)
|
||||
#print(formatted)
|
||||
|
||||
'''
|
||||
la réponse est de type
|
||||
\x81\x17\x00\x10\x46\x12
|
||||
avec
|
||||
\x81 address
|
||||
\x17 command code
|
||||
\x00 state
|
||||
\x10\x46 firmware version
|
||||
\x12 checksum
|
||||
'''
|
||||
# Extract byte 4 and byte 5
|
||||
byte4 = byte_data[3] # 4th byte (index 3)
|
||||
byte5 = byte_data[4] # 5th byte (index 4)
|
||||
firmware_version = int(f"{byte4:02x}{byte5:02x}")
|
||||
|
||||
|
||||
data = {
|
||||
'firmware_version': firmware_version,
|
||||
}
|
||||
json_data = json.dumps(data)
|
||||
print(json_data)
|
||||
break
|
||||
except KeyboardInterrupt:
|
||||
print("User interrupt encountered. Exiting...")
|
||||
time.sleep(3)
|
||||
exit()
|
||||
except:
|
||||
# for all other kinds of error, but not specifying which one
|
||||
print("Unknown error...")
|
||||
time.sleep(3)
|
||||
exit()
|
||||
|
||||
69
NPM/get_data.py
Executable file
69
NPM/get_data.py
Executable file
@@ -0,0 +1,69 @@
|
||||
'''
|
||||
Script to get NPM values
|
||||
need parameter: port
|
||||
/usr/bin/python3 /var/www/nebuleair_pro_4g/NPM/get_data.py ttyAMA5
|
||||
'''
|
||||
|
||||
import serial
|
||||
import requests
|
||||
import json
|
||||
import sys
|
||||
|
||||
parameter = sys.argv[1:] # Exclude the script name
|
||||
#print("Parameters received:")
|
||||
port='/dev/'+parameter[0]
|
||||
|
||||
ser = serial.Serial(
|
||||
port=port,
|
||||
baudrate=115200,
|
||||
parity=serial.PARITY_EVEN,
|
||||
stopbits=serial.STOPBITS_ONE,
|
||||
bytesize=serial.EIGHTBITS,
|
||||
timeout = 1
|
||||
)
|
||||
|
||||
ser.write(b'\x81\x11\x6E') #data10s
|
||||
#ser.write(b'\x81\x12\x6D') #data60s
|
||||
|
||||
while True:
|
||||
try:
|
||||
byte_data = ser.readline()
|
||||
#print(byte_data)
|
||||
stateByte = int.from_bytes(byte_data[2:3], byteorder='big')
|
||||
Statebits = [int(bit) for bit in bin(stateByte)[2:].zfill(8)]
|
||||
PM1 = int.from_bytes(byte_data[9:11], byteorder='big')/10
|
||||
PM25 = int.from_bytes(byte_data[11:13], byteorder='big')/10
|
||||
PM10 = int.from_bytes(byte_data[13:15], byteorder='big')/10
|
||||
#print(f"State: {Statebits}")
|
||||
#print(f"PM1: {PM1}")
|
||||
#print(f"PM25: {PM25}")
|
||||
#print(f"PM10: {PM10}")
|
||||
#create JSON
|
||||
data = {
|
||||
'capteurID': 'nebuleairpro1',
|
||||
'sondeID':'USB2',
|
||||
'PM1': PM1,
|
||||
'PM25': PM25,
|
||||
'PM10': PM10,
|
||||
'sleep' : Statebits[0],
|
||||
'degradedState' : Statebits[1],
|
||||
'notReady' : Statebits[2],
|
||||
'heatError' : Statebits[3],
|
||||
't_rhError' : Statebits[4],
|
||||
'fanError' : Statebits[5],
|
||||
'memoryError' : Statebits[6],
|
||||
'laserError' : Statebits[7]
|
||||
}
|
||||
json_data = json.dumps(data)
|
||||
print(json_data)
|
||||
break
|
||||
except KeyboardInterrupt:
|
||||
print("User interrupt encountered. Exiting...")
|
||||
time.sleep(3)
|
||||
exit()
|
||||
except:
|
||||
# for all other kinds of error, but not specifying which one
|
||||
print("Unknown error...")
|
||||
time.sleep(3)
|
||||
exit()
|
||||
|
||||
103
NPM/get_data_modbus.py
Normal file
103
NPM/get_data_modbus.py
Normal file
@@ -0,0 +1,103 @@
|
||||
'''
|
||||
Script to get NPM data via Modbus
|
||||
need parameter: port
|
||||
/usr/bin/python3 /var/www/nebuleair_pro_4g/NPM/get_data_modbus.py ttyAMA5
|
||||
|
||||
Modbus RTU
|
||||
[Slave Address][Function Code][Starting Address][Quantity of Registers][CRC]
|
||||
|
||||
Pour récupérer les 5 cannaux (a partir du registre 0x80)
|
||||
|
||||
Request
|
||||
\x01\x03\x00\x80\x00\x0A\xE4\x1E
|
||||
|
||||
\x01 Slave Address (slave device address)
|
||||
\x03 Function code (read multiple holding registers)
|
||||
\x00\x80 Starting Address (The request starts reading from holding register address 0x80 or 128)
|
||||
\x00\x0A Quantity of Registers (Requests to read 0x0A or 10 consecutive registers starting from address 50)
|
||||
\xE4\x1E Cyclic Redundancy Check (checksum )
|
||||
|
||||
'''
|
||||
|
||||
import serial
|
||||
import requests
|
||||
import json
|
||||
import sys
|
||||
import crcmod
|
||||
|
||||
parameter = sys.argv[1:] # Exclude the script name
|
||||
#print("Parameters received:")
|
||||
port='/dev/'+parameter[0]
|
||||
|
||||
ser = serial.Serial(
|
||||
port=port,
|
||||
baudrate=115200,
|
||||
parity=serial.PARITY_EVEN,
|
||||
stopbits=serial.STOPBITS_ONE,
|
||||
bytesize=serial.EIGHTBITS,
|
||||
timeout = 0.5
|
||||
)
|
||||
|
||||
# Define Modbus CRC-16 function
|
||||
crc16 = crcmod.predefined.mkPredefinedCrcFun('modbus')
|
||||
|
||||
# Request frame without CRC
|
||||
data = b'\x01\x03\x00\x80\x00\x0A'
|
||||
|
||||
# Calculate CRC
|
||||
crc = crc16(data)
|
||||
crc_low = crc & 0xFF
|
||||
crc_high = (crc >> 8) & 0xFF
|
||||
|
||||
# Append CRC to the frame
|
||||
request = data + bytes([crc_low, crc_high])
|
||||
print(f"Request frame: {request.hex()}")
|
||||
|
||||
ser.write(request)
|
||||
|
||||
while True:
|
||||
try:
|
||||
byte_data = ser.readline()
|
||||
formatted = ''.join(f'\\x{byte:02x}' for byte in byte_data)
|
||||
print(formatted)
|
||||
|
||||
# Extract LSW (first 2 bytes) and MSW (last 2 bytes)
|
||||
lsw_channel1 = int.from_bytes(byte_data[3:5], byteorder='little')
|
||||
msw_chanel1 = int.from_bytes(byte_data[5:7], byteorder='little')
|
||||
raw_value_channel1 = (msw_chanel1 << 16) | lsw_channel1
|
||||
|
||||
lsw_channel2 = int.from_bytes(byte_data[7:9], byteorder='little')
|
||||
msw_chanel2 = int.from_bytes(byte_data[9:11], byteorder='little')
|
||||
raw_value_channel2 = (msw_chanel2 << 16) | lsw_channel2
|
||||
|
||||
lsw_channel3 = int.from_bytes(byte_data[11:13], byteorder='little')
|
||||
msw_chanel3 = int.from_bytes(byte_data[13:15], byteorder='little')
|
||||
raw_value_channel3 = (msw_chanel3 << 16) | lsw_channel3
|
||||
|
||||
lsw_channel4 = int.from_bytes(byte_data[15:17], byteorder='little')
|
||||
msw_chanel4 = int.from_bytes(byte_data[17:19], byteorder='little')
|
||||
raw_value_channel4 = (msw_chanel1 << 16) | lsw_channel4
|
||||
|
||||
lsw_channel5 = int.from_bytes(byte_data[19:21], byteorder='little')
|
||||
msw_chanel5 = int.from_bytes(byte_data[21:23], byteorder='little')
|
||||
raw_value_channel5 = (msw_chanel5 << 16) | lsw_channel5
|
||||
|
||||
print(f"Channel 1 (0.2->0.5): {raw_value_channel1}")
|
||||
print(f"Channel 2 (0.5->1.0): {raw_value_channel2}")
|
||||
print(f"Channel 3 (1.0->2.5): {raw_value_channel3}")
|
||||
print(f"Channel 4 (2.5->5.0): {raw_value_channel4}")
|
||||
print(f"Channel 5 (5.0->10.): {raw_value_channel5}")
|
||||
|
||||
|
||||
|
||||
break
|
||||
except KeyboardInterrupt:
|
||||
print("User interrupt encountered. Exiting...")
|
||||
time.sleep(3)
|
||||
exit()
|
||||
except:
|
||||
# for all other kinds of error, but not specifying which one
|
||||
print("Unknown error...")
|
||||
time.sleep(3)
|
||||
exit()
|
||||
|
||||
Reference in New Issue
Block a user