The second part of this week’s 2-Bullet Tuesday! See the first part here. You can subscribe to the newsletter on the 2-Bullet Tuesday page!
Omega Tip
Sending Data to the Losant Cloud Platform
Last week we covered how to connect your Omega to the Losant Cloud Platform with a single command, so this week we’ll be showing you the next step: how to easily send data collected by your Omega up to Losant so you can make full use of their awesome platform.
The setup process from last week configures a Mosquitto MQTT bridge to Losant. Now we’ll use Mosquitto to send data points to Losant by publishing to your Losant device’s state topic. The command will look something like:
mosquitto_pub -t losant/[DEVICE-ID]/state -m ‘{ "data": { "[DEVICE-ATTRIBUTE]": [ATTRIBUTE-VALUE] } }’
The command might seem intimidating at first, but it’s not too bad when we break it down into chunks:
- We’re using Mosquitto to publish to the state topic of your Losant device
- The message payload being published is the JSON-formatted object after the -m option
- Losant expects the JSON object to have a data object that holds name-value pairs for one or more of the device attributes
Typing out JSON strings gets a little tedious so we’ve made a script to collect data and generate the JSON for us to make this process easier.
Let’s first grab the script. It’s available on GitHub so we can quickly grab it with:
mkdir /root/losant ; wget -qP /root/losant https://raw.githubusercontent.com/OnionIoT/Losant- Communication/master/send.sh
For this Losant device, we’re collecting the amount of free memory on the Omega’s filesystem, so to follow along, you will need a Losant device with a number attribute named freeMemory. Later on we’ll cover how to customize the script for your needs.
Using the script, our command looks like this:
mosquitto_pub -t losant/[DEVICE-ID]/state -m "$(sh /root/losant/send.sh)"
So instead of writing out the JSON string by hand ourselves, the script will:
- Acquire the data we’re interested in collecting
- Format it in a Losant-friendly JSON object
- Output the JSON object as a string
The $() that wraps the script will ensure the send.sh script is run first and it’s output is placed in the original mosquitto_pub command. In effect, the mosquitto_pub command is publishing the output of the script to the state topic of your Losant device. If you’re curious, try running the script by itself with sh /root/losant/send.sh to see the output it produces.
And that’s that! With this one command, we can send data up to Losant to be tracked or part of a larger workflow.
Time Series Data
Tracking and visualizing time-series data is one of the strengths of the Losant platform, so it follows that we’ll want to send data points at a regular interval. We’ll use our good friend the cron daemon to schedule our mosquitto_pub command to run every so often.
First, run crontab -e to open the scheduling file, then add the following:
# */5 * * * * mosquitto_pub -t losant/[DEVICE-ID]/state -m "$(sh /root/losant/send.sh)" #
Close and save the file by hitting escape and then :wq. Then restart cron for the changes to take effect by running /etc/init.d/cron restart.
Your Omega will now be reporting it’s free memory to Losant once every five minutes. For more details on scheduling jobs and commands, check out our Documentation Article on the cron daemon.
Sending your Own Data
The send.sh script we used above is written to acquire and JSON format the amount of free memory for demonstrative purposes. You might want to collect sensor measurements, GPIO values, or a variety of other things, so feel free to use the send.sh script as a starting point.
Use the addFreeMem() function as a template: it should acquire the data you would like to measure and then add it to the JSON object. There are several functions that can be used to add name-value pairs based on the type of the data:
- json_add_int
- json_add_string
- json_add_double
- json_add_boolean
The functions are all to be used as follows:
json_add_int [NAME] [VALUE]
If you’re unsure, take a look at the script and look for the comments with YOUR TURN, they indicate what needs to be done.
Happy data collection!
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!