Home

Awesome

pms5003-micropython-minimal

Minimal driver for pms5003 air quality sensor for micropython, based on the excellent work of Kevin K�ck (https://github.com/kevinkk525/pms5003_micropython)

Supports:

Description and features

This driver for the pms5003 air quality sensor is specifically made for micropython. It can be used with esp32 (tested on esp32_loboris fork) and will not work on esp8266 as it only has one UART. It is completely based on uasyncio

The driver covers all features of the device:

The library is built to be reslient, so if a reset pin is given, it will reset the device after a few tries if commands are not answered or no new data is received within 60s in active mode. If a reset fails (even because no reset pin is given) it will stop the device.

Reset and Set pin are completely optional though. The reset pin could make sense to have, the Set pin for sleep/wakeUp seems unnecessary as it's functionality is covered by uart commands.

Dependencies

How to use

import pms5003
import machine
import uasyncio as asyncio

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):
        self._locked = False
        await asyncio.sleep_ms(_DEFAULT_MS)

    def locked(self):
        return self._locked

    def release(self):
        self._locked = False
        
lock = Lock()
uart = machine.UART(1, tx=25, rx=26, baudrate=9600)
pm = pms5003.PMS5003(uart, lock) 
pm.registerCallback(pm.print)

loop=asyncio.get_event_loop()
loop.run_forever()

An output of pm.print() looks like this:

---------------------------------------------
Measurement 12ms ago at 2018-06-24 22:28:21
---------------------------------------------
Concentration Units (standard)
---------------------------------------------
PM 1.0: 1       PM2.5: 2        PM10: 2
Concentration Units (environmental)
---------------------------------------------
PM 1.0: 1       PM2.5: 2        PM10: 2
---------------------------------------------
Particles > 0.3um / 0.1L air: 528
Particles > 0.5um / 0.1L air: 151
Particles > 1.0um / 0.1L air: 18
Particles > 2.5um / 0.1L air: 0
Particles > 5.0um / 0.1L air: 0
Particles > 10 um / 0.1L air: 0
---------------------------------------------

After the creation of the pms5003 object, which is configurable in the constructor, the library takes care of everything and you just have to register your callbacks or events. Of course you can change everything later as you see fit and change the reading mode, the eco mode or the polling interval in passive mode.

Methods explained

The last read values (or None if the sensor is not active) can be accessed as properties:

Useful tips:

Possible problems: