I have written a shell script for my Raspberry Pi to start a Node app that connects to the Losant MQTT broker. I added it to the crontab with a minute sleep prior to ensure the wifi was already connected. However, I don’t think that the connection is happening, or if it is may be closing immediately. I checked the logs and it seems to make a connection and immediately close.
When I check the device connection log on the dashboard, there was never a connection made. Has anyone else experienced this or know how I might continue troubleshooting? Thank you!
Could be many different things. The MQTT spec doesn’t provide many details for why things disconnected, which makes debugging this stuff difficult. Check the application communication log, which is on your main application page. It logs in real-time, so open the page and then kick off the Raspberry Pi and see if anything gets logged.
The most common cause is simply invalid credentials. Make sure the access key/secret and device ID are all correct.
It’s listening but I don’t get anything when I reboot. I think the credentials are ok since I am able to run the script and connect on the command line.
Can you please run with the DEBUG environment variable set to losant* - that will print a decent amount of debug info and could provide a clue. In the meantime, we’re testing your script on our end.
With the debugger, on reboot there was no difference in the logs or on the application communication log. When I run sudo DEBUG=losant* node index.js from the command line (no reboot) I get this:
Usually, for a Rasberry Pi, we would use init.d or rc.local to start tasks. I’m not too familiar with using the @reboot cron. However, I recommend using Supervisord:
Thanks anaptfox for the suggestion to try init.d or rc.local. It seems my initial google search on the topic led me astray with crontabs, but your suggestion got me to the right answer. Simply calling the script from /home/pi/.bashrc connected to the MQTT broker on login. If anyone else is interested this stackoverflow has a great explanation of the different ways to run scripts on startup (the first .bashrc method is the one that worked for me):
I really appreciate that you guys are here to help a beginner like myself out. Have a happy holidays!