MQTT Cayenne connection using RaspberryPi4 and automated startup

Good morning Cayenne-fellows,

The manual connection over MQTT to cayenne works perfectly (manual=python script executed in shell)
I’ve been trying for some time to get a python script executed automatically at startup and followed this nice tutorial:

I’ve tried all methods excluding the init.d-variant. Adding a service or using crontab looked like the best options to me.
So for example what I did for adding the auto startup is:

crontab -e
@reboot sudo python3 /home/pi/Desktop/pyprog/pytest.py > /home/pi/Desktop/pyprog/log.txt
crontab -l showed that the cronjob is running

The script connects to MQTT-Cayenne:

I see that there is one successful connection over Cayenne but then the RPI loses connection again. When i execute the script using python3 myscript.py it works fine.

I have also tried adding a sleep time of 30-120seconds to wait for internet connection to establish and used & to rund the process in the background.

Have you got an idea on how to troubleshoot this? The project involves three parts: RPI with cronjob, python3 and mqtt-cayenne.

Thank you!

I have used following library:

DHT11/DHT22 with Raspberry Pi in this topic it shown all to add run the file at reboot

Thank you Shramik, I have followed that tutorial and also tried the cronjob method. The script is executed (with and without the &) just once after the startup and a single MQTT connection is established with message upload. Shortly after, the connection is offline again and stays offline.

Do I need to specifiy something in the preamble of the python script?

can you share your python script

Have you tried running the script manually? Sounds like it’s not looping correctly. As Shramik said, share the code.

1 Like

Solution for me: just create a .desktop file:
https://learn.sparkfun.com/tutorials/how-to-run-a-raspberry-pi-program-on-startup/method-2-autostart

and select the wait for network option from the raspberry pi settings menu.

thanks for sharing the solution.