Giter VIP home page Giter VIP logo

rpi-enviro-mqtt's Introduction

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

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

Home Assistant integration

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:

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.

rpi-enviro-mqtt's People

Contributors

robmarkcole avatar

Watchers

James Cloos avatar

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.