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

Receiving Commands from the Losant Cloud Platform

Over the past two weeks, we’ve covered how to connect your Omega to the Losant Cloud Platform with a single command and then how to send data-points from your Omega up to Losant. To continue our Losant series, this week we’ll show you how to receive and parse commands from Losant on your Omega.

Receiving and reacting to commands on your Omega is powerful since it enables you to create Losant workflows for your IoT project. This works really well when the workflow reads sensor values and then takes different actions based on the readings. The readings can be various measurements and come from a variety of different sources – other Omegas and physical devices, online services – allowing you to create truly useful IoT devices and applications.

Losant sends commands by publishing JSON objects to your device’s command topic, it looks something like:

{"name":"[COMMAND-NAME]","payload":"[COMMAND-PAYLOAD]","time":{"$date":"[TIME-DATE-OF-SENT-COMMAND]"}}

On the Omega, we’ll use mosquitto_sub to subscribe to your device’s command topic, this will output the received messages to the command line. Try it out, run the following on your Omega:

mosquitto_sub -t losant/[DEVICE-ID]/command

To follow along, you’ll need an Omega that’s configured to communicate with Losant. To follow along 100% of the way, you’ll need an Expansion Dock as well.

Now, on the Losant side:

  • Navigate to the page of your Device
  • Go to the Debug tab
  • Send a command with:
  • The Command Name set to expled
  • The Command Payload set to 0xff0000

Hit Send Command and you’ll see something like the following on your Omega:

 

It’s nice to see the communication going through, but we want to the Omega to take action based on the contents of the command. So let’s pipe the received messages to a script that will:

  1. Parse the JSON
  2. Perform an action based on the command and payload

Just like last week, the script we’re using is available on GitHub. Let’s grab it with:

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

Let’s run the same mosquitto_sub command but pipe the output to the script:

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

Go back to Losant and try sending that same debug message, the RGB LED on the Expansion Dock will now be red!

Using a Workflow

Now that your Omega can act on received commands, you’re free to use the Device Command nodes in Losant Workflows.

Take this workflow for example, each button changes the RGB LED to a different color:

Try it out yourself! Download the workflow configuration from GitHub and import it into your own Losant Workflow.

Receiving your Own Custom Commands

The receive.sh script was written to parse the received JSON message, read the command name and command payload, and then, based on the command name, call a function to execute a specific action.

It’s quite simple to extend the script to receive and handle your own custom commands:

  • Look for the YOUR TURN – HANDLER comment, write a function that should be called when your command is received. Take a look at the handleExpLed function for ideas on how arguments are handled in Shell.
  • Look for the YOUR TURN – IF comment, extend the if statement to have a case for your command name that calls the handler function you just wrote.

Enjoy building awesome IoT projects with the Omega and Losant!

 

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!