Adjust Script To Prevent PMS Sensor Buffering #121
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In setting up the PMS5003 sensor to send readings over MQTT to Home Assistant, I noticed there was an ever increasing delay in the sensor readings the longer the device was running. This was confirmed by lighting a match in front of the sensor: if the Raspberry Pi had just been started, the values updated immediately, but if the device had been left on for an hour, the values stopped updating immediately.
It turns out that if the PMS5003 sensor is not read from continually, it will start to buffer the readings. In the current
mqtt-all.py
script, the main loop waits for the duration of the specified interval (defaults to 5 seconds) each pass through the loop. However the PMS5003 is taking a reading roughly every second or less. This results in the data sent over MQTT being an additional 5 seconds late each time a reading is sent.Using the my hour test run, after 60 minutes of running, it's now transmitting data taking at roughly the 12 minute mark.
The fix proposed here copies what was done in the
luftdaten.py
script. The loop now runs continuously, and on each pass, checks if the desired interval has passed. If it's been long enough, it will send the data over MQTT. If not, it just runs the loop again. This allows for the data from the sensor to be captured continuously, and only transmitted as needed.This test was confirmed by leaving the Raspberry Pi and sensor running for a full 24 hours, then lighting a match in front of it again. This time the values in updated immediately.