Send air quality data from a Pimoroni RPi Enviro+ over MQTT. This script works with the Enviroplus with or without the PMS5003 sensor attached. Note that the mqtt-all.py
script is now included in the pimoroni repo here
The mqtt-all.py
script is a fork of the official luftdaten.py script. The main difference being that this script uses MQTT to publish data over the local network (vs internet to Luftdaten) so there no dependency on internet connection. Also since you don't need to poll the Luftdaten website for data, latency is almost eliminated and you can visualise data in real time, using a tool like mqtt-explorer (screenshot below).
You should first install the Pimoroni enviroplus-python library. There is one additional dependency required for this script, which is paho-mqtt. This can be installed with pip3 install paho-mqtt
. You need an MQTT broker to receive the data, if you haven't already set one up, a broker can be installed on the rpi with sudo apt-get install mosquitto mosquitto-clients
. Note this broker will run at startup automatically, which is very convenient. Note also that this broker is unsecured by default, and that this code is only for personal (not professional) use on a secure local network. You should publish some MQTT data from the command line to check the broker is functioning, and verify receipt of this data using the tool of your choice (e.g. mqtt-explorer). Your device is assigned an MQTT client_id of format raspi-device_serial_number
. Clone this repo to your rpi, making note of the path to mqtt-all.py
From the terminal you can run the script with readings every 5 seconds:
python3 /home/pi/yourdir/mqtt-all.py --broker localhost --port 1883 --topic enviroplus --interval 5
Note that the arguments passed here are the defaults, and just shown as an example. If you are issuing this command over SSH you probably want it to continue after you disconnect. This can be achieved by using nohup
, so for example you would issue command:
nohup python3 /home/pi/yourdir/mqtt-all.py --broker 192.168.1.164 --topic enviro &
To see the process ID run top
, and you can then kill the nohup process by running sudo kill -9 PID
where PID is the process ID of the running python process.
TODO add service file.
I personally am using home-assistant to receive and log the enviro mqtt data. The benefits are: logging to .db
sqlite file & graphing. Simply configure an mqtt-sensor on the enviro
topic (or whatever you selected) and break out the individual readings using a template. Example below, and assuming you have a PMS5003 attached, in sensor.yaml
:
- platform: mqtt
state_topic: "enviroplus"
value_template: "{{ value_json.pm1 }}"
name: "enviro_pm1"
unit_of_measurement: 'pm'
icon: "mdi:thought-bubble-outline"
- platform: mqtt
state_topic: "enviroplus"
value_template: "{{ value_json.pm10 }}"
name: "enviro_pm10"
unit_of_measurement: 'pm'
icon: "mdi:thought-bubble-outline"
- platform: mqtt
state_topic: "enviroplus"
value_template: "{{ value_json.pm25 }}"
name: "enviro_pm2"
unit_of_measurement: 'pm'
icon: "mdi:thought-bubble"
- platform: mqtt
state_topic: "enviroplus"
value_template: "{{ value_json.humidity }}"
name: "enviro_humidity"
unit_of_measurement: '%'
icon: "mdi:water-percent"
- platform: mqtt
state_topic: "enviroplus"
value_template: "{{ value_json.pressure }}"
name: "enviro_pressure"
unit_of_measurement: 'Pa'
icon: "mdi:arrow-down-bold"
- platform: mqtt
state_topic: "enviroplus"
value_template: "{{ value_json.temperature }}"
name: "enviro_temperature"
unit_of_measurement: 'C'
icon: "mdi:thermometer"
- platform: mqtt
state_topic: "enviroplus"
value_template: "{{ value_json.lux }}"
name: "enviro_lux"
unit_of_measurement: 'lx'
icon: "mdi:weather-sunny"
- platform: mqtt
state_topic: "enviroplus"
value_template: "{{ value_json.nh3 }}"
name: "enviro_nh3"
unit_of_measurement: 'nh3'
icon: "mdi:thought-bubble"
- platform: mqtt
state_topic: "enviroplus"
value_template: "{{ value_json.reduced }}"
name: "enviro_reduced"
unit_of_measurement: 'CO'
icon: "mdi:thought-bubble"
- platform: mqtt
state_topic: "enviroplus"
value_template: "{{ value_json.oxidised }}"
name: "enviro_oxidised "
unit_of_measurement: 'no2'
icon: "mdi:thought-bubble"
Using the created entities you can add an entity card like the following:
I bought the following:
Total cost £86, which is considerably cheaper than commercial devices containing similar sensors, e.g. uHoo (£325) or Kaiterra (£260)
I live on a road that can get congested during rush hour, and I want to know if the pollution in the house is rasied during these times. I have the Enviro+ on a windowsill streetside. I've only just started capturing data, and also owing to covid lock-down there is hardly any traffic. Analytics of my data is in the analytics
folder of this repo.