Git Product home page Git Product logo

rpi-enviro-mqtt's Introduction

Note this repo is archived as it is no longer maintained and contains a known issue. A maintained and fixed mqtt-all.py script is in the pimoroni repo here

rpi-enviro-mqtt

Send air quality data from a Pimoroni RPi Enviro+ over MQTT. This script works with the Enviroplus with or without the PMS5003 sensor attached.

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).

Installation & setup

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

Run the mqtt-all.py script

From the terminal you can run the script with readings every 5 seconds:

python3 /home/pi/yourdir/rpi-enviro-mqtt/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.

Run as a service

You can run the mqtt-all.py script as a service, which means it can be configured to automatically start on RPi boot, and can be easily started & stopped. Create the service file in the appropriate location on the RPi using: sudo nano /etc/systemd/system/enviro.service

Entering the following (adapted for your mqtt-all.py file location and args):

[Unit]
Description=Enviroplus MQTT Logger
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/github/rpi-enviro-mqtt/mqtt-all.py --broker 192.168.1.164 --topic enviroplus --interval 5
WorkingDirectory=/home/pi/github/rpi-enviro-mqtt
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Once this file has been created you can start the service using: sudo systemctl start enviro.service

View the status and logs with: sudo systemctl status enviro.service

Stop the service with: sudo systemctl stop enviro.service

Restart the service with: sudo systemctl restart enviro.service

You can have the service auto-start on rpi boot by using: sudo systemctl enable enviro.service

You can disable auto-start using: sudo systemctl disable enviro.service

Home Assistant integration

I 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:

Hardware

I bought the following:

Total cost £86, which is considerably cheaper than commercial devices containing similar sensors, e.g. uHoo (£325) or Kaiterra (£260)

My study

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.

Publication

This setup was used in the following publication: Indoor Air Pollution from Residential Stoves: Examining the Flooding of Particulate Matter into Homes during Real-World Use

Presentation

My former colleage Oliver Crask (@olivercrask) presented on IAQ monitoring with the PMS sensor at an event in London in 2018, and the pdf presentation is included in this repo. A video of the presentation is below:

rpi-enviro-mqtt's People

Contributors

robmarkcole avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rpi-enviro-mqtt's Issues

Script Causes PMS Sensor Buffering

Hi! Hope you are well! 😄

I was recently following the mqtt-all.py script and tutorials from the main Pimoroni repo, and found an issue you might be interested in. I've proposed a fix in the main repo here.

TL;DR: Using time.sleep(args.interval) inside the main loop with a PMS5003 sensor connected results in the sensor buffering it's readings. The longer the device is running, the more buffered the data gets, resulting in very delayed readings being sent over MQTT.

Thought you might be interested in this fix! Your tutorials and this particular script have been very helpful to me, so thank you!

Report hectopascal?

Currently we are reporting Pa (int(bme280.get_pressure() * 100), -1) but more common to report hPa (hectopascal)?

image

Running Home Assistant Along Side MQTT

Curious at this deployment and whether or not you are running Home Assistant on the same Pi along side MQTT or how you have that all configured. There is no messaging feature so I apologize if this is not the way you'd prefer to communicate.

Add service info

Something like

--- Run
sudo nano /etc/systemd/system/envmqtt.service

--- Edit file
[Unit]
Description=Enviro+ MQTT Logger
After=network.target

[Service]
ExecStart=/usr/bin/python3 /usr/src/enviroplus/examples/mqtt-all.py --broker <IP-address>
WorkingDirectory=/usr/src/enviroplus/examples
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target

--- Run
sudo systemctl enable envmqtt.service
sudo systemctl start envmqtt.service

Add read interval

Currently reads every second, make this an arg, default to every 5 sec with time.sleep(5)

Enviro.service failing at boot

This is what I am getting in terminal, I have checked the service config adjusted it between what is in your guide and what is on raspi org's site as well.
image
image
image

Issue with import enviroplus

hi,

i have installed pimoroni/enviroplus-python and rpi-enviro-mqtt but during execution it fails at the line 13:
from enviroplus import gas

pi@raspberrypi:~ $ python3 /home/pi/rpi-enviro-mqtt/mqtt-all.py --broker localhost --port 1883 --topic enviroplus --interval 5
Traceback (most recent call last):
File "/home/pi/rpi-enviro-mqtt/mqtt-all.py", line 13, in
from enviroplus import gas
ModuleNotFoundError: No module named 'enviroplus'

Any clue?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.