-
Notifications
You must be signed in to change notification settings - Fork 4
custom device 'PM detector': case study
This device measures PM1.0, PM2.5, PM10 (μg/m³), T (°C), RH (%), AQI (0..500) and n@0.3, n@0.5, n@1.0, n@2.5, n@5.0, n@10 (number of particles). About atmospheric particulate matter see Wikipedia for info and limits and aqicn or sensor.community for actual values.
This device has a double-colored screen and can exchange data via USB. It shows, sends, and also stores the measurements.
The protocol is USB-serial, 115200:8N1.
The basic application is 'pm2.5'. On Windows is required a driver for CH340.
- Download the 'PM2.5' software
- Download the 'driver for CH340'
- Connect the device to a USB port
- Select START > Device Manager - you will see an "unknown USB device" with a question mark under the USB devices
- Click > Update Driver - browse to the downloaded driver file
- Check the Port assignment after updating the driver - COM port number
- Launch PM2.5.EXE from the download
- Click the last option in the top menu to change language (English)
- Click the Equipment > Config menu and set the COM Port from that in the device manager and the baud rate to 115200
- SAVE.
Using the basic application 'pm2.5' and 'SerialMon', (by DuNovo) I captured the following 'functions':
send to device receive from device
{"fun":"01","sendtime":"002"}} {"res":"1"}
{"fun":"02","storetime":"004"}} {"res":"2"}
{"fun":"03","clock":"21-03-05 20:57:24"}} {"res":"3"} (YY-MM-DD hh:mm:ss)
{"fun":"04"}} {"res":"10","total":"004354"} (start dump historical data)
(push RT/dump historical data) {"res":"4","y":"2021","m":"03","d":"05","h":"11","min":"53","sec":"03",
"cpm2.5":"0033","cpm1.0":"0022","cpm10":"0037", (cpm: Condensable Particulate Matter)
"apm2.5":"0036","apm1.0":"0023","apm10":"0037", (apm: Ambient Particulate Matter)
"aqi":"033","t":"19.6","r":"45"} ... (aqi: Air Quality Index)
{"res":"0"} (end historical data)
{"fun":"05","flag":"1"}} {"res":"5"} (start(1)/stop(0) push RT data)
{"fun":"06"}} {"res":"6"} (clear internal memory)
{"fun":"80"}} {"res":"80","SendInteralTime":"000002","StoreInteralTime":"000005",
"WritePoint":"000046","ReadPoint":"000000","SendInteralFlag":"000000",}
{"res":"-4"} (error condition, e.g. no data)
notes
- Data is like JSON data, but with some exceptions. The extra ending '}' is not required.
- The display is updated every second, the data is sent every "sendtime" sec, and stored every "storetime" sec.
- The "particle number" is shown on the display but not sent.
- The data shown on the display as PM is APM (total). Sent also CPM (Condensable PM).
In a custom device, we can choose the DPs as we want. I used the following criteria:
- This custom device, like many tuya devices, will use atomic data points.
- Any SET will echo the update data, the response messages
{"res":"xx"}
are used as ack. - Internal memory can be used in many ways. I have chosen to produce daily stats every hour, as well as TVR Thermostatic Valve, so I can use the same interface.
- The complete definition of all 27 DPs is on the standard documentation.
pick and execute
- Format standard input commands from tuyaDAEMON
device/capability/value
to required serial command. note: The input 'fake' commands have 2 extra fields:msg.to:<deviceId>
andmsg.payload.dp:<dp>
added by CORE to speedup the process. - Setup: inizialize
'tuyastatus'
structure.
RT data processing
- Convert serial responses to standard tuya answer object with multiple DPs.
- The answer {"res":"10"...} enter the 'skipdata' mode (historical processing), stop RT
start dump
- Inizialize required data structures
- Sends the command {"fun":"04"} (start dump data)
historical data processing (only in the 'skipdata' mode)
- Cumulates data to do averages.
- The answer {"res":"0"} (end data):
- produces the output
- terminates the 'skipdata' mode
- restarts RT
timers
- At 00:00: clear data in device memory
- Every hour: create historical data
startup (init)
- Initializations with defaults: sendtime, storetime, clock, RT on
Manual commands for test.