Publish environmental data from Enviro+ via MQTT

I am so sorry. It seems I dropped a [ when copying your code. Even after going through code many times I did not see it.
So when I enabled it I got:

Created symlink /etc/systemd/system/multi-user.target.wants/envlogger.service → /etc/systemd/system/envlogger.service.

and I got nothing when I started it. Does that mean everything is perfect?

You shouldn’t get any output when you start it, however that doesn’t necessarily mean it’s working.

If it is working, you should see MQTT messages being sent to your broker, or sensors being updated in Home Assistant (if you have configured them) etc.

If it doesn’t appear to be working, you can check the logs of the service by running journalctl -u envlogger.service. You can also stop the service (same command as starting it, but replace start with stop) and then run the same python3 command from the service file manually to see what output you get.

Thanks. It seems the same error is happening

ModuleNotFoundError: No module named 'paho'

And it tries to restart end fails eventually.
I didn’t use pip to install paho. I used this:

git clone https://github.com/eclipse/paho.mqtt.python.git
cd paho.mqtt.python
python setup.py install

Could that be the problem? If so, can I uninstall paho and install again using pip3?

@SamB

Thank you. I tried uninstalling paho but could not find a way. So I just ran pip3 install paho-mqtt

Restarted pi

Now, this is what I get from the logger:

-- Logs begin at Mon 2020-03-16 19:20:23 EDT, end at Mon 2020-03-16 19:46:57 EDT. --
Mar 16 19:20:50 raspberrypi systemd[1]: Started Enviro+ MQTT Logger.
Mar 16 19:21:09 raspberrypi python3[325]: Traceback (most recent call last):
Mar 16 19:21:09 raspberrypi python3[325]:   File "/usr/src/enviroplus-mqtt/src/main.py", line 63, in <module>
Mar 16 19:21:09 raspberrypi python3[325]:     main()
Mar 16 19:21:09 raspberrypi python3[325]:   File "/usr/src/enviroplus-mqtt/src/main.py", line 33, in main
Mar 16 19:21:09 raspberrypi python3[325]:     num_samples=args["interval"]
Mar 16 19:21:09 raspberrypi python3[325]:   File "/usr/src/enviroplus-mqtt/src/logger.py", line 27, in __init__
Mar 16 19:21:09 raspberrypi python3[325]:     self.client.connect(host, port)
Mar 16 19:21:09 raspberrypi python3[325]:   File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 937, in connect
Mar 16 19:21:09 raspberrypi python3[325]:     return self.reconnect()
Mar 16 19:21:09 raspberrypi python3[325]:   File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1071, in reconnect
Mar 16 19:21:09 raspberrypi python3[325]:     sock = self._create_socket_connection()
Mar 16 19:21:09 raspberrypi python3[325]:   File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3522, in _create_socket_connection
Mar 16 19:21:09 raspberrypi python3[325]:     return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
Mar 16 19:21:09 raspberrypi python3[325]:   File "/usr/lib/python3.7/socket.py", line 727, in create_connection
Mar 16 19:21:09 raspberrypi python3[325]:     raise err
Mar 16 19:21:09 raspberrypi python3[325]:   File "/usr/lib/python3.7/socket.py", line 716, in create_connection
Mar 16 19:21:09 raspberrypi python3[325]:     sock.connect(sa)
Mar 16 19:21:09 raspberrypi python3[325]: OSError: [Errno 101] Network is unreachable
Mar 16 19:21:10 raspberrypi systemd[1]: envlogger.service: Main process exited, code=exited, status=1/FAILURE
Mar 16 19:21:10 raspberrypi systemd[1]: envlogger.service: Failed with result 'exit-code'.
Mar 16 19:21:10 raspberrypi systemd[1]: envlogger.service: Service RestartSec=100ms expired, scheduling restart.
Mar 16 19:21:10 raspberrypi systemd[1]: envlogger.service: Scheduled restart job, restart counter is at 1.
Mar 16 19:21:10 raspberrypi systemd[1]: Stopped Enviro+ MQTT Logger.
Mar 16 19:21:10 raspberrypi systemd[1]: Started Enviro+ MQTT Logger.

Don’t know what’s happening? Any help is greatly appreciated

That’s a cool looking case for the Enviro+ @SamB! Is it purchased or 3D printed?

OSError: [Errno 101] Network is unreachable

This suggests the device doesn’t have network access, or the MQTT host address you are trying to connect to is not reachable on the same network as the Enviro+ Pi.

Can you ping the MQTT host or google.com?

Yes. I can ping both Google and my mqtt host local IP

Thanks @davefi. It’s 3D printed. You should be able to access it at https://a360.co/38Wg3X8 to download or open in Fusion360

2 Likes

See this Stack Overflow question: https://stackoverflow.com/questions/58233303/mqtt-publishing-via-python-errno-101-network-is-unreachable

The author resolved the issue by using the IP address of the MQTT host rather than a hostname.

Actually, I am using IP, because I am connecting to a local server. Let me research and see if I can find a solution.
BTW, if I run:
python3 /usr/src/enviroplus-mqtt/src/main.py -h 192.168.1.xxx -p 1883 -U xxxxxx -P xxxxx

It never ends. I have to ^c to stop it. When I do that, every time, I get this:

^CTraceback (most recent call last):
  File "/usr/src/enviroplus-mqtt/src/main.py", line 63, in <module>
    main()
  File "/usr/src/enviroplus-mqtt/src/main.py", line 59, in main
    time.sleep(sleep_duration)
KeyboardInterrupt

Thanks for all the help.

Yes, that’s what should happen - it doesn’t log to standard out unless there is an error of some sort. If you see no output that suggests it has connected successfully, and your MQTT server should start to receive messages after 30 seconds or so.

Maybe you should still paste the output of cat /etc/systemd/system/envlogger.service - it sounds like the logger works fine if you manually invoke it, so something must be wrong with the service configuration.

Yes. You are right. I can see the temperature in HA when I just run like that. Here is the file content:

[Unit]

Description=Enviro+ MQTT Logger

After=network.target

[Service]

ExecStart=/usr/bin/python3 -u /usr/src/enviroplus-mqtt/src/main.py -h 192.168.1.xxx -p 1883 -U xxxxxx -P xxxxxxx --interval 5

WorkingDirectory=/usr/src/enviroplus-mqtt

StandardOutput=inherit

StandardError=inherit

Restart=always

User=pi

[Install]

WantedBy=multi-user.target

Thanks again!

@SamB
The only difference is “-u” as far as I can see.

@SamB

So, I removed -u from the envlogger.service file in ExecStart, and now it’s working. Any idea of how it happened?

BTW, can I get the sound values through MQTT too?

Manjula

I have no idea why that -u is in the example service file… It should be removed.

It looks like Pimoroni have just added some code for accessing the sound sensor, and there’s no reason why it couldn’t be used to send sound data back via mqtt - but you would have to write that code yourself and add it to enviroplus-mqtt, there’s no support for sound reporting at the moment.

1 Like

Hello Sam,
I try to download your enviro case as STL files but it only downloads as one file and i dont know if it will print 2 pieces or only one. Is it possible to receive 2 separated STL files? Thx in advance

I get the following errors:
– Logs begin at Tue 2020-03-24 16:44:46 EET, end at Tue 2020-03-24 19:33:55 EET. –
Mar 24 16:45:09 raspberrypi systemd[1]: Started Enviro+ MQTT Logger.
Mar 24 16:45:09 raspberrypi systemd[366]: envlogger.service: Changing to the requested working directory failed: No such file or dire
Mar 24 16:45:09 raspberrypi systemd[366]: envlogger.service: Failed at step CHDIR spawning /usr/bin/python3: No such file or director
Mar 24 16:45:10 raspberrypi systemd[1]: envlogger.service: Main process exited, code=exited, status=200/CHDIR
Mar 24 16:45:10 raspberrypi systemd[1]: envlogger.service: Failed with result ‘exit-code’.
Mar 24 16:45:11 raspberrypi systemd[1]: envlogger.service: Service RestartSec=100ms expired, scheduling restart.
Mar 24 16:45:11 raspberrypi systemd[1]: envlogger.service: Scheduled restart job, restart counter is at 1.
Mar 24 16:45:11 raspberrypi systemd[1]: Stopped Enviro+ MQTT Logger.
Mar 24 16:45:11 raspberrypi systemd[1]: Started Enviro+ MQTT Logger.
Mar 24 16:45:12 raspberrypi systemd[391]: envlogger.service: Changing to the requested working directory failed: No such file or dire
Mar 24 16:45:12 raspberrypi systemd[391]: envlogger.service: Failed at step CHDIR spawning /usr/bin/python3: No such file or director
Mar 24 16:45:12 raspberrypi systemd[1]: envlogger.service: Main process exited, code=exited, status=200/CHDIR
Mar 24 16:45:12 raspberrypi systemd[1]: envlogger.service: Failed with result ‘exit-code’.
Mar 24 16:45:13 raspberrypi systemd[1]: envlogger.service: Service RestartSec=100ms expired, scheduling restart.
Mar 24 16:45:13 raspberrypi systemd[1]: envlogger.service: Scheduled restart job, restart counter is at 2.
Mar 24 16:45:13 raspberrypi systemd[1]: Stopped Enviro+ MQTT Logger.
Mar 24 16:45:13 raspberrypi systemd[1]: Started Enviro+ MQTT Logger.
Mar 24 16:45:13 raspberrypi systemd[405]: envlogger.service: Changing to the requested working directory failed: No such file or dire
Mar 24 16:45:13 raspberrypi systemd[405]: envlogger.service: Failed at step CHDIR spawning /usr/bin/python3: No such file or director
Mar 24 16:45:13 raspberrypi systemd[1]: envlogger.service: Main process exited, code=exited, status=200/CHDIR
Mar 24 16:45:13 raspberrypi systemd[1]: envlogger.service: Failed with result ‘exit-code’.

Hi Coz

Have you tried loading the STL into your slicing software to see how it will print? You can also try installing Fusion 360 and opening the design in that - then you can export it however you want.

Can you paste the output of running cat /etc/systemd/system/envlogger.service and /usr/bin/python3 -V ?

Based on these two lines from your log file:

Mar 24 16:45:09 raspberrypi systemd[366]: envlogger.service: Changing to the requested working directory failed: No such file or dire
Mar 24 16:45:09 raspberrypi systemd[366]: envlogger.service: Failed at step CHDIR spawning /usr/bin/python3: No such file or director

I suspect you either don’t have Python 3 installed at /usr/bin/python3, or the directory you have specified as the WorkingDirectory in the service file does not exist.

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

[Service]
ExecStart=/usr/bin/python3 -u /usr/src/enviroplus-mqtt/src/main.py -h 192.168.13.200 -p 1883 -U 1234home -P xxxxxxxxxxxx --prefix lounge/enviroplus --interval 5 --use-pms5003
WorkingDirectory=/usr/src/enviroplus-mqtt
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target