Plugin timing/architecture questions [TE AmbiMate MS4 multisensor)
Posted: 04 Sep 2019, 04:16
Hey all - I'm working again on my ESPEasy plugin for the TE AmbiMate MS4 multisensor (temp/humidity/light/audio/co2/voc and motion), which is a great, commercial quality sensor with good bang/buck. I'm using the sensor with my Kube home automation sensor platform https://hackaday.io/project/167052-kube ... r-platform, with ESPEasy as the main firmware. The sensor uses I2C for communications, and thought I thought I had it mostly figured out (submitted a PR to ESPEasyPlayground in February - https://github.com/letscontrolit/ESPEas ... d/pull/127), there was definitely room for improvement, mostly as far as the ability to send all 8 channels of data where ESPEasy only allows 4, and avoiding the use of "delayBackground" calls in the I2C comms.
So, here I am, I've spent some more time looking at the code and rearranging things, but I think I'm stuck and need some help from the experts. I've solved the issue of 8 channels into 4 (by adding webforms with selectable sensor inputs and allowing multiple instances of the plugin to send all 8 channels), but the one main issue I still haven't solved is the fact that the PIR motion sensor requires different handling than the rest of the sensors. So, here's what the sensor requires for I2C comms for handling the different sensors:
PIR Motion (constant polling)
1. Send an I2C write to set register to output PIR motion event
2. Wait 100 ms
3. Read I2C register and check for motion event
OTHER SENSORS (on Plugin Timer period)
1. Send I2C write for AmbiMate to populate values of all sensors on next cycle
2. Wait 100 ms
3. Send I2C write command to read all sensors
4. Read 15 bytes with sensor values and process.
So, due to the different timing/read requirements for the sensors, I decided to restructure my plugin like so:
1. In "PLUGIN_TEN_PER_SEC" I only run step 1 of the PIR Motion sequence, then schedule a timer to event->TaskIndex (setting a flag to let PLUGIN_READ know there is a PIR event to read)
2. In "PLUGIN_READ", I run an if statement with the motion read flag
a. If the motion read flag is set, I run step 3 of the PIR Motion sequence (reading Motion I2C register from the sensor, and setting the appropriate UserVar for the motion sensor)
b. If the motion read flag is NOT set, I run steps 1-4 of the Other Sensors sequence. (I have not worked out a way to avoid the 100ms delay in this sequence, though I could probably reschedule the read with another flag, if it's too troublesome)
Now, I've come across a few issues with this, especially as I try to combine it with the openHAB MQTT controller:
1. The motion events don't come through the MQTT controller (with logs, I'm able to see the event all the way through setting it in UserVar)
2. The other sensors appear to get weird/random values, even though I think I'm setting the UserVars correclty based on my Webform config
3. The MQTT controller is constantly updating (sending all the way up to the nimimum send rate, and flooding the MQTT broker) <- I'm pretty sure this is because PLUGIN_READ is now being called 10 times per second.
So, what am I doing wrong? I've spent a full day looking at this code, and probably am missing some simple things, but I don't nearly have the full level of understanding of how the plugin architecture works to be able to figure these special cases out.
Rather than post the code, I'll link to the file on my GitHub https://github.com/bkpsu/ESPEasy/blob/m ... e_MS4_.ino. I'd appreciate any suggestions/help with this, and I'll be publishing the full design (including my sensor) once completed.
Thanks!
-BK Hobby
So, here I am, I've spent some more time looking at the code and rearranging things, but I think I'm stuck and need some help from the experts. I've solved the issue of 8 channels into 4 (by adding webforms with selectable sensor inputs and allowing multiple instances of the plugin to send all 8 channels), but the one main issue I still haven't solved is the fact that the PIR motion sensor requires different handling than the rest of the sensors. So, here's what the sensor requires for I2C comms for handling the different sensors:
PIR Motion (constant polling)
1. Send an I2C write to set register to output PIR motion event
2. Wait 100 ms
3. Read I2C register and check for motion event
OTHER SENSORS (on Plugin Timer period)
1. Send I2C write for AmbiMate to populate values of all sensors on next cycle
2. Wait 100 ms
3. Send I2C write command to read all sensors
4. Read 15 bytes with sensor values and process.
So, due to the different timing/read requirements for the sensors, I decided to restructure my plugin like so:
1. In "PLUGIN_TEN_PER_SEC" I only run step 1 of the PIR Motion sequence, then schedule a timer to event->TaskIndex (setting a flag to let PLUGIN_READ know there is a PIR event to read)
2. In "PLUGIN_READ", I run an if statement with the motion read flag
a. If the motion read flag is set, I run step 3 of the PIR Motion sequence (reading Motion I2C register from the sensor, and setting the appropriate UserVar for the motion sensor)
b. If the motion read flag is NOT set, I run steps 1-4 of the Other Sensors sequence. (I have not worked out a way to avoid the 100ms delay in this sequence, though I could probably reschedule the read with another flag, if it's too troublesome)
Now, I've come across a few issues with this, especially as I try to combine it with the openHAB MQTT controller:
1. The motion events don't come through the MQTT controller (with logs, I'm able to see the event all the way through setting it in UserVar)
2. The other sensors appear to get weird/random values, even though I think I'm setting the UserVars correclty based on my Webform config
3. The MQTT controller is constantly updating (sending all the way up to the nimimum send rate, and flooding the MQTT broker) <- I'm pretty sure this is because PLUGIN_READ is now being called 10 times per second.
So, what am I doing wrong? I've spent a full day looking at this code, and probably am missing some simple things, but I don't nearly have the full level of understanding of how the plugin architecture works to be able to figure these special cases out.
Rather than post the code, I'll link to the file on my GitHub https://github.com/bkpsu/ESPEasy/blob/m ... e_MS4_.ino. I'd appreciate any suggestions/help with this, and I'll be publishing the full design (including my sensor) once completed.
Thanks!
-BK Hobby