Skip to content

Adjust Script To Prevent PMS Sensor Buffering #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2023
Merged

Adjust Script To Prevent PMS Sensor Buffering #121

merged 1 commit into from
Jul 26, 2023

Conversation

jspauley
Copy link
Contributor

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.

@Gadgetoid
Copy link
Member

Thank you- yes, it fires out data every one second whether you read it or not.

The data buffers into the RPis serial read buffer,, which will eventually overflow and cause a boundary error trying to read a full packet of data.

Looks like I need to fix the CI/tests 😭

@jspauley
Copy link
Contributor Author

@Gadgetoid I rebased on your updated tests 😄 Looks like it's now waiting for a maintainer.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 5660825516

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 94.531%

Totals Coverage Status
Change from base Build 5656516571: 0.0%
Covered Lines: 121
Relevant Lines: 128

💛 - Coveralls

@Gadgetoid Gadgetoid merged commit 37252b6 into pimoroni:master Jul 26, 2023
@Gadgetoid
Copy link
Member

Thank you!

@jspauley jspauley deleted the fix_pms_buffer branch July 26, 2023 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants