Git Product home page Git Product logo

enviroplus-mqtt's Introduction

Enviro+ MQTT Logger

enviroplus-mqtt is a Python service that publishes environmental data from an Enviro+ via MQTT.

Setting Up Your Device

  1. Set up your RPi as you normally would.

  2. Connect the Enviro+ board, and the PMS5003 sensor if you are using one.

  3. Install the Enviro+ library by following the instructions at https://github.com/pimoroni/enviroplus-python/ (make sure the library is installed for Python 3)

  4. Clone this repository to /usr/src/enviroplus-mqtt:

    sudo git clone https://github.com/hotplot/enviroplus-mqtt /usr/src/enviroplus-mqtt
    
  5. Add a new file at /etc/systemd/system/envlogger.service with the following content:

    [Unit]
    Description=Enviro+ MQTT Logger
    After=network.target
    
    [Service]
    ExecStart=/usr/bin/python3 /usr/src/enviroplus-mqtt/src/main.py <arguments>
    WorkingDirectory=/usr/src/enviroplus-mqtt
    StandardOutput=inherit
    StandardError=inherit
    Restart=always
    User=pi
    
    [Install]
    WantedBy=multi-user.target
    

    Note that you must replace <arguments> with flags appropriate to your MQTT server.

  6. Enable and start the service:

    sudo systemctl enable envlogger.service
    sudo systemctl start envlogger.service
    

Supported Arguments

  • The MQTT host, port, username, password and client ID can be specified.

  • The update interval can be specified, and defaults to 5 seconds.

  • The initial delay before publishing readings can be specified, and defaults to 15 seconds.

  • If you are using a PMS5003 sensor, enable it by passing the --use-pms5003 flag.

      usage: main.py -h HOST [-p PORT] [-U USERNAME] [-P PASSWORD] [--prefix PREFIX]
                  [--client-id CLIENT_ID] [--interval INTERVAL] [--delay DELAY]
                  [--use-pms5003] [--help]
    
      optional arguments:
      -h HOST, --host HOST  the MQTT host to connect to
      -p PORT, --port PORT  the port on the MQTT host to connect to
      -U USERNAME, --username USERNAME
                          the MQTT username to connect with
      -P PASSWORD, --password PASSWORD
                          the password to connect with
      --prefix PREFIX       the topic prefix to use when publishing readings, i.e.
                          'lounge/enviroplus'
      --client-id CLIENT_ID
                          the MQTT client identifier to use when connecting
      --interval INTERVAL   the duration in seconds between updates
      --delay DELAY         the duration in seconds to allow the sensors to
                          stabilise before starting to publish readings
      --use-pms5003         if set, PM readings will be taken from the PMS5003
                          sensor
      --help                print this help message and exit
    

Published Topics

Readings will be published to the following topics:

  • <prefix>/proximity
  • <prefix>/lux
  • <prefix>/temperature
  • <prefix>/pressure
  • <prefix>/humidity
  • <prefix>/gas/oxidising
  • <prefix>/gas/reducing
  • <prefix>/gas/nh3
  • <prefix>/particulate/1.0
  • <prefix>/particulate/2.5
  • <prefix>/particulate/10.0

enviroplus-mqtt's People

Contributors

ferazambuja avatar hotplot 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

Watchers

 avatar  avatar  avatar

enviroplus-mqtt's Issues

Retained messages

Would you consider publishing the messages as RETAINED so that the last state is know immediately?

High values from PMS

Hi, I've noticed that the values from the PMS reported via MQTT are way higher than the ones reported locally.

Local reading at 79 and sending at 120. Some times the readings even lock on high value and don't go down even placing the sensor at the exhaust of my air purifier. I've placed with two other sensors running on a ESP and the values are reported via the script are always far from what the other sensors report.

https://imgur.com/a/NbRluZU

Code will not run due to syntax error

There is a bug in the following line:

        sys.exit(f"Connecting to the MQTT server failed: {logger.connection_error}")

A extra "f" needs to be removed from the arguments to sys.exit.

Thanks for the code which runs with Mosquitto in my setup!

Hello Aygotori,

Hello Aygotori,

I was looking into this myself and have managed to make it work with modifications to the logger.py, I'm not the most experienced in Python but I hope it helps, it includes averaging the CPU temp for less jitter:

Edit the logger.py by running sudo nano /usr/src/enviroplus-mqtt/src/logger.py

Add the following line within def __init__() (under self.latest_pms+readings is where I put this) and adjust to suit your compensation:
self.comp_factor = 2.25 - Adjust the 2.25 to suit your compensation.

Add the following inside the EnvLogger class:

# Get the temperature of the CPU for compensation
    def get_cpu_temperature(self):
        with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
            self.temp = f.read()
            self.temp = int(self.temp) / 1000.0
        return self.temp

Finally add the below within def take_readings() I added before the gas_data = gas.read_all() for reference:

        cpu_temps = [self.get_cpu_temperature()] * 5
        cpu_temp = self.get_cpu_temperature()
        # Smooth out with some averaging to decrease jitter
        cpu_temps = cpu_temps[1:] + [cpu_temp]
        avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
        raw_temp = self.bme280.get_temperature()
        comp_temp = raw_temp - ((avg_cpu_temp - raw_temp) / self.comp_factor)

In my example I have both readings so within the def take_readings() inside the readings I added a new line for compensated temperature (above the existing temperature):

"compensated_temperature": comp_temp,
"temperature": self.bme280.get_temperature(),

This is just my own take and I'm sure there are cleaner ways but this works for my setup.

Originally posted by @sl-ux in #9 (comment)

Getting Syntax errors

Hi, can you please help me? I've edited /etc/systemd/system/envlogger.service as follows;

ExecStart=/usr/bin/python3 -u /usr/src/enviroplus-mqtt/src/main.py -host XX.XX.XX.XX --port 1883 --username 'XXX' --password 'XXX' --prefix 'outdoor/wetterstation' but I keep getting

File "main.py", line 47
sys.exit(f"Connecting to the MQTT server failed: {logger.connection_error}")
^
SyntaxError: invalid syntax

PMS5003 Checksum Mismatch 600 != 256

I get this error when I check the status of the service - any idea why?

sean@enviro:~ $ sudo systemctl status envlogger.service
● envlogger.service - Enviro+ MQTT Logger
Loaded: loaded (/etc/systemd/system/envlogger.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2020-11-15 22:14:46 GMT; 9min ago
Main PID: 531 (python3)
Tasks: 3 (limit: 992)
CGroup: /system.slice/envlogger.service
└─531 /usr/bin/python3 -u /usr/src/enviroplus-mqtt/src/main.py -h x.x.x.x -p xxxx -U mqtt -P xxxxxx --prefix homeassistant/sensor/tvroom --client-id enviro --interval 300 --delay 120 --use-pms5003

Nov 15 22:23:36 enviro python3[531]: File "/usr/local/lib/python3.7/dist-packages/pms5003-0.0.5-py3.7.egg/pms5003/init.py", line 155, in read
Nov 15 22:23:36 enviro python3[531]: raise ChecksumMismatchError("PMS5003 Checksum Mismatch {} != {}".format(checksum, data.checksum))
Nov 15 22:23:36 enviro python3[531]: pms5003.ChecksumMismatchError: PMS5003 Checksum Mismatch 682 != 28
Nov 15 22:23:41 enviro python3[531]: Failed to read from PMS5003. Resetting sensor.
Nov 15 22:23:41 enviro python3[531]: Traceback (most recent call last):
Nov 15 22:23:41 enviro python3[531]: File "/usr/src/enviroplus-mqtt/src/logger.py", line 63, in __read_pms_continuously
Nov 15 22:23:41 enviro python3[531]: pm_data = pms.read()
Nov 15 22:23:41 enviro python3[531]: File "/usr/local/lib/python3.7/dist-packages/pms5003-0.0.5-py3.7.egg/pms5003/init.py", line 155, in read
Nov 15 22:23:41 enviro python3[531]: raise ChecksumMismatchError("PMS5003 Checksum Mismatch {} != {}".format(checksum, data.checksum))
Nov 15 22:23:41 enviro python3[531]: pms5003.ChecksumMismatchError: PMS5003 Checksum Mismatch 600 != 256

Service failed to start

On reboot I get the message that the logger failed to start and exceeded the maximum retry count.
I think it is because the pms is not yet ready.
I made the following changes to the startup script:

[Unit]
Description=Enviro+ MQTT Logger
After=network.target
StartLimitIntervalSec=60
StartLimitBurst=10

[Service]
ExecStart=/usr/bin/python3 /usr/src/enviroplus-mqtt/src/main.py <arguments>
WorkingDirectory=/usr/src/enviroplus-mqtt
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Receiving values with a long delay

After a day and a half of continuous running, the readings sent from my enviro+ are 4 hours late.
It looks it longer it runs bigger it gets. I have 2 (esphome) PMS reading side by side, and as you can see on the image, the readings are hours late.
An easy test is to cover the lux meter and see how long it takes to respond.

I thought that it might be related to the delay in publishing the first message, but that doesn't seem to be the case after running a few minutes with a 600-second initial delay.

https://community-home-assistant-assets.s3.dualstack.us-west-2.amazonaws.com/original/3X/a/6/a66e1716e9bce4e4162727c7b9e46aaf960b8a19.png

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.