PMS5003 air quality sensor library
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
PMS5003 air quality sensor library
I had the crazy idea that I need an air quality sensor (and hopefully detect the smoke from my neighbours to close the windows ) 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
Also feedback would be highly appreciated of course.
https://github.com/kevinkk525/pms5003_micropython
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
Also feedback would be highly appreciated of course.
https://github.com/kevinkk525/pms5003_micropython
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
- philwilkinson40
- Posts: 63
- Joined: Tue Nov 14, 2017 3:11 am
- Location: Perth, Australia
Re: PMS5003 air quality sensor library
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.
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.
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: PMS5003 air quality sensor library
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.
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 (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
- Mike Teachman
- Posts: 155
- Joined: Mon Jun 13, 2016 3:19 pm
- Location: Victoria, BC, Canada
Re: PMS5003 air quality sensor library
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
---------------------------------------------
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
---------------------------------------------
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: PMS5003 air quality sensor library
@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:
This produces (on Unix with official uasyncio):
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())
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
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: PMS5003 air quality sensor library
Thanks a lot Mike! I'm happy to see that you like it and that it works as expected.Mike Teachman wrote: ↑Sun Nov 18, 2018 7:11 pmThis 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.
@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 (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: PMS5003 air quality sensor library
Good point, thank you. We're surely overdue a MicroPython point release.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: PMS5003 air quality sensor library
Hi,
I'm trying to use pms5003 with pyboard.
The line "uart = machine.UART(4, tx='X1', rx='X2', baudrate=9600)" return an error "extra-argument"
So I have changed this by " uart = machine.UART(4, baudrate=9600)"
I have error : " module has no PMS5003 attribute"
With "uart = machine.UART(4, baudrate=9600)" and "uart.init(9600, bits=8, parity=None, stop=1)"
Always the same error " module pms hasen't PMS5003 attribute"...
Thanks for your help.
I'm trying to use pms5003 with pyboard.
The line "uart = machine.UART(4, tx='X1', rx='X2', baudrate=9600)" return an error "extra-argument"
So I have changed this by " uart = machine.UART(4, baudrate=9600)"
I have error : " module has no PMS5003 attribute"
With "uart = machine.UART(4, baudrate=9600)" and "uart.init(9600, bits=8, parity=None, stop=1)"
Always the same error " module pms hasen't PMS5003 attribute"...
Thanks for your help.
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: PMS5003 air quality sensor library
The library was only tested on ESP32 as I don't have any pyboards.
But it should of course work, except for one issue: I don't think the uart has a "flush()" method, so every such occurence would need to be replace with something like:
About your error "module pms hasen't PMS5003 attribute":
How did you install the library? Did you only copy the pms5003.py file or did you copy the repository?
If you cloned the repository as "pms" and copied the repository to the pyboard, then you need to import it like "from pms.pms5003 import PMS5003"
If you copied "pms5003.py" as "pms.py" then "from pms import PMS5003" should work.
Maybe you can post the output of "dir(pms)" too if that doesn't work.
(Also make sure you still use uasyncio v2 because I haven't ported the library to v3 yet)
But it should of course work, except for one issue: I don't think the uart has a "flush()" method, so every such occurence would need to be replace with something like:
Code: Select all
while self._uart.any():
self._uart.read(self._uart.any())
About your error "module pms hasen't PMS5003 attribute":
How did you install the library? Did you only copy the pms5003.py file or did you copy the repository?
If you cloned the repository as "pms" and copied the repository to the pyboard, then you need to import it like "from pms.pms5003 import PMS5003"
If you copied "pms5003.py" as "pms.py" then "from pms import PMS5003" should work.
Maybe you can post the output of "dir(pms)" too if that doesn't work.
(Also make sure you still use uasyncio v2 because I haven't ported the library to v3 yet)
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Re: PMS5003 air quality sensor library
Hi Kevin,
Thanks for your support.
Thanks for sharing your knowledge.
After test alls yours recommandations, the pms looks initiate correctly. But I have had an error about the debug line "pms5003.set_debug(True)"
So I have changed it by "pms.pms5003.set_debug(True)" and "imported the module like " import pms.pms5003" and "from pms.pms5003 import PMS5003"
It works, but I have "
wakeUp
wakeUp got lock
Sending command: 228,1,False,16000
waiting 40.0s
"
Thanks a lot for everything
Thanks for your support.
Thanks for sharing your knowledge.
After test alls yours recommandations, the pms looks initiate correctly. But I have had an error about the debug line "pms5003.set_debug(True)"
So I have changed it by "pms.pms5003.set_debug(True)" and "imported the module like " import pms.pms5003" and "from pms.pms5003 import PMS5003"
It works, but I have "
wakeUp
wakeUp got lock
Sending command: 228,1,False,16000
waiting 40.0s
"
Thanks a lot for everything