Hello - this is my first post and with so many topics in this forum not sure if Beginners forum the right home. Apologies if I got this wrong.
- Create an environment monitor for my home network cabinet to monitor temperature etc. using a Raspberry Pi with BMP280 Breakout and send the results a cloud service to view private data remotely
Hardware & Software:
- Raspberry Pi Zero, running Raspbian and powered by USB 2.0 from a device within network cabinet
- Pimoroni BMP280 breakout - soldered at 90 degrees away from GPIO board to reduce CPU heat on BMP280 sensor
- Code written in Python 3 using Pimoroni BMP280 Library (as per Pimoroni website recommendations) and Adafruit IO library
- Send BMP280 Temperature &, Pressure (and altitude for fun) sensor data and CPU temperature data [using open('/sys/class/thermal/thermal_zone0/temp') ], as feeds to pre-configured Adafruit IO Dashboard & feeds.
So the python code works and the results can be seen via my own private Adafruit IO dashboard plus I also send same data using various print statements to terminal for debugging. So all good. The only niggle is that the BMP280 temperature is out by +4 degree C (?) but thats for another post!
Script runs in in terminal as: python3 myscript.py >> log.txt
MY ISSUE IS : I wish to leave the Pi Zero running 24/7 and only occasionally access the device remotely using either a terminal shell from MacOS or or via VNCServer. The Pi has no keyboard or mouse physically connected. I don't want to keep the Mac switched on all the time using an active terminal shell to execute the Python 3 script. However no matter what I do I cannot seem to execute the same Python 3 ".py' script on boot (plus pipe output to a log file). I only want the script to run when the Pi Zero is rebooted not run multiple times upon opening a terminal shell too
Have tried my two preferred methods (as don't wish to go down paths of adding script to .bashrc, init.d or SYSTEMD) being:
A) /etc/rc.local - where I added line sudo python3 /home/pi/myscript.py >> /home/pi/log.txt &"
B) crontab - @reboot python3 /home/pi/myscript.py >> /home/pi/log.txt &. For noting Cron is working as a number appears when I check.
Any advice on what I'm doing wrong would be greatly appreciated please?
For those interested main loop of code as follows with feeds and variables setup further up the code. Note LOOP_DELAY has value = 10:
# MAIN - GETS SENSOR DATA AND SENDS TO ADAFRUIT IO DASHBOARD
# Read Pimoroni BMP280 breakout
temp_data = bmp280.get_temperature()
pressure_data = bmp280.get_pressure()
alt_data = bmp280.get_altitude(qnh=baseline_pressure)
# Get Pi CPU temperature from Bash system call
cpu_temp_data = int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3
print('sending data to adafruit io...')
# Send BMP280 breakout and Pi Zero CPU data to Adafruit IO.
print('Temperature: %0.1f C' % temp_data)
print("CPU Temperature: %0.1f hPa" % cpu_temp_data)
print("Pressure: %0.1f hPa" % pressure_data)
print("Altitude = %0.2f meters" % alt_data)
# avoid timeout from adafruit io
time.sleep(LOOP_DELAY * 60)