Publish environmental data from Enviro+ via MQTT

pi@raspberrypi:~ $ /usr/bin/python3 -V
Python 3.7.3

guys, it’s working. i had 2 errors: one is that i wrote wrong “enviroplsu” and that is why no such file error and second that i had to manually install paho mqtt client.
now its working.

Evening all,

I read through most of the issues here and tried the fixes, to no avail.

Cat dump:
pi@raspberrypi:~ $ cat /etc/systemd/system/envlogger.service
Unit]
scription=Enviro+ MQTT Logger
After=network.target

[Service]
ExecStart=/usr/bin/python3 /usr/src/enviroplus-mqtt/src/main.py -h 192.168.0.180 -p 1883 -U **** -P ***** --prefix office --interval 5
WorkingDirectory=/usr/src/enviroplus-mqtt
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

I have installed paho using pip3

When using python3 /usr/src/enviroplus-mqtt/src/main.py -h 192.168.0.180 -p 1883 -U **** -P ***** --prefix office --interval 5

It connect to my MQTT broker I only get:
1585942095: New connection from 192.168.0.224 on port 1883.
[INFO] found mqttcaretaker on local database
1585942096: New client connected from 192.168.0.224 as auto-039ECF88-DA8B-2FAA-B5D7-CEF1B73EFFAC (p2, c1, k60, u’mqttcaretaker’).

Then no more updates…

Anyone any idea?

Thanks in advance

Hi Everitt

Firstly, when you run the command python3 /usr/src/enviroplus-mqtt/src/main.py -h 192.168.0.180 -p 1883 -U **** -P ***** --prefix office --interval 5:

  1. Do you get any output?
  2. How do you determine there are no updates? Do you have a MQTT client subscribed to the catch-all topic “#” for example which is showing some messages from other clients but nothing from enviroplus-mqtt?

Secondly, in the first two lines of your service file, you seem to be missing some characters from the start of the lines. Specifically, “Unit]” instead of “[Unit]” and “scription=…” rather than " Description=…". This won’t prevent you from running the script manually as you have been, but you won’t be able to start it automatically at boot.

Hi @SamB

I correct the [ and description, its now working

Also looks like I hadn’t subscribed properly.

Thanks!

1 Like

Hi Team,

Wondering if you might be able to help me out. I followed / attempted the fixes described in this thread but I am still receiving some errors…

When I run:

python3 /usr/src/enviroplus-mqtt/src/main.py -h 192.168.1.xxx -p 1883 -U myusernane -P mypassword --interval 10 --delay 10

The errors I receive are:

Traceback (most recent call last):
  File "/usr/src/enviroplus-mqtt/src/main.py", line 65, in <module>
    main()
  File "/usr/src/enviroplus-mqtt/src/main.py", line 40, in main
    logger.update(publish_readings=False)
  File "/usr/src/enviroplus-mqtt/src/logger.py", line 99, in update
    self.samples.append(self.take_readings())
  File "/usr/src/enviroplus-mqtt/src/logger.py", line 76, in take_readings
    gas_data = gas.read_all()
  File "/usr/local/lib/python3.7/dist-packages/enviroplus-0.0.3-py3.7.egg/enviroplus/gas.py", line 79, in read_all
  File "/usr/local/lib/python3.7/dist-packages/enviroplus-0.0.3-py3.7.egg/enviroplus/gas.py", line 50, in setup
  File "/usr/local/lib/python3.7/dist-packages/ads1015-0.0.5-py3.7.egg/ads1015/__init__.py", line 167, in set_mode
    self._ads1015.set('CONFIG', mode=value)
  File "/usr/local/lib/python3.7/dist-packages/i2cdevice-0.0.6-py3.7.egg/i2cdevice/__init__.py", line 215, in set
    self.read_register(register)
  File "/usr/local/lib/python3.7/dist-packages/i2cdevice-0.0.6-py3.7.egg/i2cdevice/__init__.py", line 183, in read_register
    self.values[register.name] = self._i2c_read(register.address, register.bit_width)
  File "/usr/local/lib/python3.7/dist-packages/i2cdevice-0.0.6-py3.7.egg/i2cdevice/__init__.py", line 283, in _i2c_read
    for x in self._i2c.read_i2c_block_data(self._i2c_address, register, bit_width // self._bit_width):
OSError: [Errno 121] Remote I/O error

Also, running:

journalctl -u envlogger.service

Results in this repeating:

Jun 18 22:35:11 enviropi systemd[1]: Started Enviro+ MQTT Logger.
Jun 18 22:35:25 enviropi python3[350]: Traceback (most recent call last):
Jun 18 22:35:25 enviropi python3[350]:   File "/usr/src/enviroplus-mqtt/src/main.py", line 65, in <module>
Jun 18 22:35:25 enviropi python3[350]:     main()
Jun 18 22:35:25 enviropi python3[350]:   File "/usr/src/enviroplus-mqtt/src/main.py", line 33, in main
Jun 18 22:35:25 enviropi python3[350]:     num_samples=args["interval"]
Jun 18 22:35:25 enviropi python3[350]:   File "/usr/src/enviroplus-mqtt/src/logger.py", line 27, in __init__
Jun 18 22:35:25 enviropi python3[350]:     self.client.connect(host, port)
Jun 18 22:35:25 enviropi python3[350]:   File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 937, in connect
Jun 18 22:35:25 enviropi python3[350]:     return self.reconnect()
Jun 18 22:35:25 enviropi python3[350]:   File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1071, in reconnect
Jun 18 22:35:25 enviropi python3[350]:     sock = self._create_socket_connection()
Jun 18 22:35:25 enviropi python3[350]:   File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3522, in _create_socket_connection
Jun 18 22:35:25 enviropi python3[350]:     return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
Jun 18 22:35:25 enviropi python3[350]:   File "/usr/lib/python3.7/socket.py", line 727, in create_connection
Jun 18 22:35:25 enviropi python3[350]:     raise err
Jun 18 22:35:25 enviropi python3[350]:   File "/usr/lib/python3.7/socket.py", line 716, in create_connection
Jun 18 22:35:25 enviropi python3[350]:     sock.connect(sa)
Jun 18 22:35:25 enviropi python3[350]: OSError: [Errno 101] Network is unreachable
Jun 18 22:35:26 enviropi systemd[1]: envlogger.service: Main process exited, code=exited, status=1/FAILURE
Jun 18 22:35:26 enviropi systemd[1]: envlogger.service: Failed with result 'exit-code'.
Jun 18 22:35:26 enviropi systemd[1]: envlogger.service: Service RestartSec=100ms expired, scheduling restart.
Jun 18 22:35:26 enviropi systemd[1]: envlogger.service: Scheduled restart job, restart counter is at 1.
Jun 18 22:35:26 enviropi systemd[1]: Stopped Enviro+ MQTT Logger.
Jun 18 22:35:26 enviropi systemd[1]: Started Enviro+ MQTT Logger.

However, when I run the Enviro Plus examples, such as weather.py, they work fine. Mine doesn’t have a gas sensor, so could that be part of the issue? (I would have assumed the code would just ignore the gas sensor if it wasn’t present?)

Thanks in advanced for your assistance.

Hi metho

The gas sensor is assumed to be present, and if your board doesn’t have one then you need to comment out the relevant lines in logger.py. The take_readings method would become:

    def take_readings(self):
        # gas_data = gas.read_all()
        readings = {
            "proximity": ltr559.get_proximity(),
            "lux": ltr559.get_lux(),
            "temperature": self.bme280.get_temperature(),
            "pressure": self.bme280.get_pressure(),
            "humidity": self.bme280.get_humidity(),
            # "gas/oxidising": gas_data.oxidising,
            # "gas/reducing": gas_data.reducing,
            # "gas/nh3": gas_data.nh3,
        }

        readings.update(self.latest_pms_readings)
        
        return readings

Thank you so, so much, Sam!

That worked a treat. It is working with zero issues now.

@SamB. I’m using your work to import readings from newly installed Enviro+ with PMS5003 sensor into Home Assistant. Thanks for making this so easy for a novice like me !

Would it be possible for you to add a EPA AQI calculation as well using the PM2.5 readings and publish the AQI value through MQTT ?

Thanks !

1 Like

Hi, @SamB!

First of all, thanks a lot for your work and for the many useful comments on this page.

Although I’m able to listen to the Enviroplus using the MQTT integration on Home Assistant, I’m unable to add the required entries to my configuration.yaml file – and MQTT has yet to discover my Enviroplus.

These are some of the errors I see:

# Enviroplus configuration.yaml entry

  - platform: mqtt     Missing property "device_id"
    name: 'Office temperature'
    state_topic: 'lounge/enviroplus/temperature'
    unit_of_measurement: '°C'
    value_template: '{{ value | round(1) }}'
    expire_after: 600

According to the logs, though, what I think is the device ID of the Enviroplus changes all the time.

And if I change the platform to mqtt_home, which is what Studio Code Server suggests, I get the following error:

The system cannot restart because the configuration is not valid: Platform error sensor.mqtt_home - Integration ‘mqtt_home’ not found.

Any idea of what I’m doing wrong?

Would you be willing to share your case design somewhere besides F360 as it doesn’t seem like it can be downloaded any longer.

Fusion just shows the case but there seems to be no button to do more than print an image now, even when signed into F360 myself?