PMS5003 air quality sensor library

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
kevinkk525
Posts: 528
Joined: Sat Feb 03, 2018 7:02 pm

PMS5003 air quality sensor library

Post by kevinkk525 » Sun Jun 24, 2018 10:00 pm

I had the crazy idea that I need an air quality sensor (and hopefully detect the smoke from my neighbours to close the windows :lol: ) and I bought the PMS5003, which is quite accurate and not very expensive.
So I started writing a library to use it. It is mainly controlled by UART commands but also has some pins.
My library tries to cover all features and completely depends on uasyncio which means that there are no long blocking calls.
The library got a lot bigger and complex than I initially thought but works very reliable.

I wanted to share it on the forum in case someone gets interested in this sensor :D

Also feedback would be highly appreciated of course.
https://github.com/kevinkk525/pms5003_micropython
Kevin Köck
Micropython Smarthome Firmware: https://github.com/kevinkk525/pysmartnode

User avatar
philwilkinson40
Posts: 59
Joined: Tue Nov 14, 2017 3:11 am
Location: Perth, Australia

Re: PMS5003 air quality sensor library

Post by philwilkinson40 » Mon Jun 25, 2018 4:42 am

Thanks @kevinkk525, I will give your library a try.

Also available for others to consider, not using uasyncio, is Rob Blaggar's library

A great blog on developing and optimizing a PMS5003 set up (on Pycom WiPy board) is by Dominik along with his library.

kevinkk525
Posts: 528
Joined: Sat Feb 03, 2018 7:02 pm

Re: PMS5003 air quality sensor library

Post by kevinkk525 » Mon Jun 25, 2018 12:16 pm

Oh thanks for pointing these out. I did not find them.
They are pretty basic though and don't have much error handling and not all features.
They mainly use the pins but do not check if it worked and read the uart but do no have much error handling in there either.
But if you just want to read periodically in active mode and don't expect many errors then these are perfectly fine and a lot more leightweight than my library.
Dominik did a great job with his unit and made me think about cutting the power to the sensor instead of putting it to sleep as I am planning on using it on battery too.
Kevin Köck
Micropython Smarthome Firmware: https://github.com/kevinkk525/pysmartnode

User avatar
Mike Teachman
Posts: 71
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: PMS5003 air quality sensor library

Post by Mike Teachman » Sun Nov 18, 2018 7:11 pm

This driver was very easy to get running on the Lobo port. Implementation with the uasyncio module is a huge bonus. Excellent design approach. Big thanks for writing and sharing! Excellent docs too.

Here is some sensor output, before and after a "smoking match test".

Initial Readings
---------------------------------------------
PM 1.0: 8 PM2.5: 10 PM10: 10
---------------------------------------------
Particles > 0.3um / 0.1L air: 1332
Particles > 0.5um / 0.1L air: 428
Particles > 1.0um / 0.1L air: 54
Particles > 2.5um / 0.1L air: 2
Particles > 5.0um / 0.1L air: 0
Particles > 10 um / 0.1L air: 0
---------------------------------------------


Smoking Match Test
---------------------------------------------
PM 1.0: 187 PM2.5: 507 PM10: 616
---------------------------------------------
Particles > 0.3um / 0.1L air: 45579
Particles > 0.5um / 0.1L air: 15028
Particles > 1.0um / 0.1L air: 6310
Particles > 2.5um / 0.1L air: 1161
Particles > 5.0um / 0.1L air: 226
Particles > 10 um / 0.1L air: 166
---------------------------------------------

User avatar
pythoncoder
Posts: 3725
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: PMS5003 air quality sensor library

Post by pythoncoder » Mon Nov 19, 2018 6:31 am

@kevinkk525 That looks great - I feel inspired to get one of those devices to try it.

I hope you don't mind me raising a rather academic point (I have skin in the game as I raised the original issue).

I'm puzzled by this comment. I thought that the point of the (merged) PR was to fix the issue so asynchronous context managers now work correctly in the presence of a return statement. If this is correct it removes the need for the prior self._lock.release() statement. My quick test seems to confirm that the bug is fixed:

Code: Select all

import uasyncio as asyncio

_DEFAULT_MS = 20
class Lock:
    def __init__(self):
        self._locked = False

    async def __aenter__(self):
        while True:
            if self._locked:
                await asyncio.sleep_ms(_DEFAULT_MS)
            else:
                self._locked = True
                break

    async def __aexit__(self, *args):
        print('aexit')
        self._locked = False
        await asyncio.sleep_ms(_DEFAULT_MS)

    def locked(self):
        return self._locked

async def test():
    lock = Lock()
    async with lock:
        asyncio.sleep(1)
        print('Locked')
        return

loop = asyncio.get_event_loop()
loop.run_until_complete(test())
This produces (on Unix with official uasyncio):

Code: Select all

$ upython 
MicroPython v1.9.4-683-gd94aa57-dirty on 2018-11-15; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import rats31
Locked
aexit
>>> 
Peter Hinch

kevinkk525
Posts: 528
Joined: Sat Feb 03, 2018 7:02 pm

Re: PMS5003 air quality sensor library

Post by kevinkk525 » Mon Nov 19, 2018 8:58 am

Mike Teachman wrote:
Sun Nov 18, 2018 7:11 pm
This driver was very easy to get running on the Lobo port. Implementation with the uasyncio module is a huge bonus. Excellent design approach. Big thanks for writing and sharing! Excellent docs too.
Thanks a lot Mike! I'm happy to see that you like it and that it works as expected.


@pythoncoder: The bug has been fixed but the commit is not included in the 1.9.4 release, therefore it would affect everyone using a prebuilt firmware on the esp8266. On the other hand, this library can't be used on the esp8266 anyway and the bug is probably fixed on every esp32 port prebuilt firmware. Therefore I probably could remove those workarounds but I wanted to wait for the new version as I'd remove these workarounds in every project then. (This workaround is also used in my extensions to your mqtt_as library).

This is the result on esp8266 1.9.4:

Code: Select all

>>> async def test():
...     global lock
...     async with lock:
...         await asyncio.sleep(1)
...         print("Locked")
...         return True
...     return False
...
...
...
>>> loop.run_until_complete(test())
Locked
>>> lock.locked()
True
Kevin Köck
Micropython Smarthome Firmware: https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 3725
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: PMS5003 air quality sensor library

Post by pythoncoder » Mon Nov 19, 2018 10:38 am

Good point, thank you. We're surely overdue a MicroPython point release.
Peter Hinch

Post Reply