Enviro+ and Luftdaten instructions error?

I followed all the instructions here:

https://learn.pimoroni.com/tutorial/sandyj/enviro-plus-and-luftdaten-air-quality-station

Everything went well until it came to getting the script to log data to Luftdaten run automatically at reboot. The command described for the crontab is:

@reboot sudo python /home/pi/enviroplus-python/examples/luftdaten.py &

The problems seem to be:

  1. It doesn’t need to be run using sudo. I’m not sure the root user has the right environment to be successful
  2. The LCD on the Enviro+ sometimes displayed a random set of coloured pixels. I think this was because it was being started too soon and the Pi wasn’t fully ready. I added a sleep 60 command and then didn’t have any further problems
  3. Running luftdaten.py outside of the examples directory fails. To fix this I make a bash script to cd to /home/pi/enviroplus-python/examples and then execute the luftdaten.py script

Now everything seems to be working fine. In the crontab I have:

@reboot python /home/pi/enviroplus-python/examples/startonboot.sh  &

and in startonboot.sh I have:

#!/bin/bash
cd /home/pi/enviroplus-python/examples
luftdaten.py

I had the same issue and followed your advice successfully.

I had a couple of problems with the device where it would randomly hang or stop sending data so I put a daily restart into Crontab to prevent this occurring, however, I then found that I was getting spikes in my data when the script restarted. I looked at the data being written and realised that it takes a little while for the sensors to stabilise after a reboot, so I added a startup delay into my code which forces the script to wait 5 minutes after the first data read before it submits data to Luftdaten. This seems to have smoothed out my data.

Today I go to check my sensor readings and I found I have been marked as “spamming”, I assume the script is pushing data too often by default. Anyone else have this problem? I have written to them asking for advice how I prevent this happening and how I get reset. Will post an update when I get somewhere.

Here’s my modified main code loop -

# Main loop to read data, display, and send to Luftdaten
delayStart = 0
while True:
    try:
        values = read_values()
        print(values)
        if delayStart < 1:
            print(delayStart)
            print("Startup delay 5 mins")
            time.sleep(300)
            delayStart =1

        else:
            print("Ready to go")
            print(delayStart)
            values = read_values()
            print(values)
            resp = send_to_luftdaten(values, id)
            print("Response: {}\n".format("ok" if resp else "failed"))
            display_status()

Yes, I noticed that the code was sending data in a tight loop with no delay. I looked at what other sensors were logging in the Luftdaten archive at http://archive.luftdaten.info/2019-08-09/ and then put a delay in the code so it sends once every 30 seconds. That cuts the file size of my sensor log to something similar to others (about 100k rather then 4M)

For me this worked:

@reboot cd /home/pi/enviroplus-python/examples/ && /usr/bin/python /home/pi/enviroplus-python/examples/luftdaten.py &

What’s working for me now is:

@reboot cd enviroplus-python/examples && python luftdaten.py”

in a cron entry (omit quotes).
Seems like a simple solution as it mimics the commands one uses to boot luftdaten on manual startup. Any one see potential problems with this approach?

One other problem I had was when running completely headless which is obviously required when actually installing the thing for real outside in its bit of drain pipe.

I got around this by installing the haveged package

sudo apt-get install haveged

The problem is that the Luftdaten code uses the random number generator. This in turn accesses the mouse to generate its seed. This works fine until you go headless at which point the code breaks on startup.

This topic describes things in more detail:

https://www.raspberrypi.org/forums/viewtopic.php?t=246534

Hey, I saw your post, how did you accomplish the 30 sec delay? Is that separate to the 5 min wait mentioned by @ deskyeti?

thanks!