[Solved] Connecting to mqtt-losant-js on reboot of raspberry pi


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.

Device(s) RaspberryPi-IO
Connected RaspberryPi-IO
Repl Initialized
Board Closing.

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!

Edit: Here’s the crontab
@reboot sleep 60 && sh/home/pi/Desktop/losant/launcher.sh >/home/pi/logs/cronlog 2>&1

and my launcher script:

cd /
cd home/pi/Desktop/losant
sudo node index.js

The script runs node and connects the pi to Losant normally if called manually from the command line.



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.



Would it be possible to upload your index.js to a gist? It would help narrowing the problem.



Sure thing: https://gist.github.com/andrewpatterson3001/10b9541341038c7c07e538a5ae189c98



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.

sudo DEBUG=losant* node index.js


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:

1482356280117 Device(s) RaspberryPi-IO
1482356280151 Connected RaspberryPi-IO
1482356280162 Repl Initialized

losant:mqtt MQTT successfully connected to mqtts://broker.losant.com +863ms
losant:device Subscribing to MQTT topic: losant/58198bba46c5b40100d73ac8/command +4ms

and the communication log shows successful authentication and connection.



Instead of using cron to execute the script directly. Since it’s long running, have you tried starting it in the background? Like so:

@reboot sleep 60 && sh/home/pi/Desktop/losant/launcher.sh >/home/pi/logs/cronlog 2>&1 &



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:

It’s an overall great process monitor.



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!

1 Like


No problem at all! I happy you figured it out :slight_smile:

Happy Holidays!