skip to Main Content
Creating A Linux Service To Automatically Receive Losant Commands

Creating a Linux Service to Automatically Receive Losant Commands

The second bullet of this week’s 2-Bullet Tuesday! Check out the first bullet here. Subscribe to receive the newsletter on the 2-Bullet Tuesday page!

Omega Tip

Creating a Service to Automatically Receive Losant Commands

Last week, we learned how to receive commands from the Losant Cloud Platform and pipe them into a script so they can be parsed and executed. This week we’ll be using the OpenWRT/LEDE init.d system to run last week’s scripts as a service at boot-time. That means that as soon as your Omega boots, it will be ready to receive and act on commands from Losant with no intervention required from your end. The best part is that if your program crashes, the Linux system will start it back up again since it’s a registered service. This opens up a whole world of IoT possibilities when combined with the flexibility and power of Losant’s workflows.

As a refresher, last week we used the following command:

mosquitto_sub -t losant/[DEVICE-ID]/command | sh /root/losant/receive.sh

For this tip, we’ll create an init.d service with a shell script, configure it to run the command we want, and then set it up to launch the service during the Omega’s boot sequence.

We’re assuming that you’ve done all of last week’s steps with everything that follows!

To start, we’ll need an intermediary, wrapper script; all it will do is accept our Losant Device ID as an argument and call the script from above. We have this wrapper script since we’re interested in creating a service from a command that includes a pipe character.

Instead of doing all sorts of trickery with quotes and escape characters in the init.d script, we’re using a wrapper script since it’s the quickest and simplest way forward. In most cases, little workarounds and shortcuts like this in a project are a good idea in order to keep your forward momentum. Don’t derail your progress by going on a tangent that doesn’t have any significant impact on the final outcome 

In any case, grab the wrapper script from GitHub with:

wget -qP /root/losant https://raw.githubusercontent.com/OnionIoT/Losant-Communication/master/listen.sh

It will be downloaded to the /root/losant directory.

Ok, let’s move on to the main event: the script that implements an init.d service! To save you some typing, we’ve made a template for a basic init.d service, you can check it out on GitHub. In a nutshell, it registers the command you would like to run as a service with procd, the main process management system in OpenWRT/LEDE.

Grab the template with:

wget -qO /etc/init.d/losant-listen https://raw.githubusercontent.com/OnionIoT/init.d-example/master/service

This will download the template and put it into a file called /etc/init.d/losant-listen. We’ll need to modify it so that the command it registers with procd is the listen.sh script we previously downloaded.

If you don’t already know it from last week, you’ll first need to grab your Losant Device ID; either online from Losant or by looking at the /etc/mosquitto/mosquitto.conf file on your Omega, it will be the hex digits on the line starting with remote_clientid.

Now, open the /etc/init.d/losant-listen file and modify the line directly under the comment YOUR TURN – COMMAND to be:

BIN="sh /root/losant/listen.sh [YOUR-LOSANT-DEVICE-ID]"

The init.d system requires that all of the service scripts are executable, so let’s do just that:

chmod +x /etc/init.d/losant-listen

Let’s start the service:

/etc/init.d/losant-listen start

Go ahead and try it out by sending your Omega a command from Losant:

The RGB LED on your Dock will now be red!

Starting Automatically at Boot

The really useful part is having our losant-listen service start automatically at boot. So let’s inform init.d that we would like this service to run when the Omega boots:

/etc/init.d/losant-listen enable

And that’s it! Now whenever you power on your Omega, you can rest assured that when the Omega’s LED stops blinking, it’s ready for action and listening for Losant commands without any input required from you (as long as you have an internet connection).

Going Further

Learning how to create init.d services is a great skill to have in your Omega repertoire and lends itself to creating truly automated systems and devices. You can use this week’s tip, as is, to setup your Omega to automatically listen for commands from Losant. This is handy if you’ve gone ahead and implemented handlers for your very own device commands based on last week’s tip.

As a bonus, now you know how to use the init.d system and have a template for creating your very own services. This is very useful for when you write programs that you want to be launched automatically when the Omega boots and then continuously run while the Omega is powered on.

We’re sure you makers out there will find a ton of cool things to make using this system!

 

Thanks for reading & have a great week!

Have you seen the Omega2S, the smaller and surface-mount version of the Omega2 for high volume commercial and industrial OEMs? See our Omega2S page for more details!

Let us know what kind of stuff you would like to see featured on 2-Bullet Tuesday! Send a tweet to @OnionIoT with your suggestions!

 

P.S. The Onion Summer ‘17 Omega-thon Contest is still on Hackster.io for students of all ages in North America! Check out the Contest Page for more info and register soon since the deadline is approaching!

P.P.S. Get free shipping on all Onion Store orders over $75 that include a Dock, Expansion, or a Kit!

 

Back To Top