Wall's Corners Wall's Corners Author
Title: Stream Sensor Readings Data with TI LaunchPad
Author: Wall's Corners
Rating 5 of 5 Des:
Hello world! The kind folks at PubNub were nice enough to let me hijack their blog today. In this tutorial, I’d like to showcase what is pos...

Hello world! The kind folks at PubNub were nice enough to let me hijack their blog today. In this tutorial, I’d like to showcase what is possible when we combine PubNub with embedded hardware, in the form of the LaunchPad Development Kits from Texas Instruments.

We are going to use PubNub to stream sensor readings from 4 different sensors (from Seeed Studio) & visualizing those readings on Freeboard.io

  • Potentiometer (turn knob)
  • Moisture sensor
  • PIR motion sensor
  • Ambient sensor

Exciting stuff! Within a few minutes, we can easily demonstrate how real-world information can simply become just another variable for web developers & iOS/Android developers to incorporate into their applications. This enables “app-cessories” like a phone-connected tennis racket, or the cloud-enabled crock pot, or maybe (just maybe) we can finally live in a world that has the quintessential “Internet of Things” product — the tweeting fridge!

Introducing the TI LaunchPad and Energia

The brain of this operation is a tiny microcontroller from Texas Instruments. TI offers a broad portfolio of low-cost, easy-to-use microcontroller development kits, called “LaunchPad”. These LaunchPad development kits start at just $9.99 and offer a low-cost & intuitive point of entry for microcontroller development (www.ti.com/launchpad).

These LaunchPad development kits can be programmed using intuitive programming tools, including the easy-to-use Energia IDE (www.energia.nu). Energia is an open source project that is a fork of the popular Wiring framework.

stream sensor readings
This means a HUGE repository of code examples & libraries will work on these LaunchPad development kits. The same Wiring APIs that you may already be used to will work on these LaunchPad boards (i.e. analogRead(), digitalWrite(), etc). Even the Wiring-based Ethernet & WiFi libraries have been ported over. Energia is open source, cross platform & free.

With the latest version of Energia (v14 & newer), the PubNub library comes pre-packaged with the tool.

So let’s get on with the tutorial!

Hardware

  • MSP-EXP430F5529LP LaunchPad Kit
  • CC3100 WiFi BoosterPack plug in module
  • Grove Base BoosterPack from Seeed Studio
  • Grove sensor modules
    • Ambient Light (plugged into pin 23)
    • PIR motion (plugged into pin 24)
    • Potentiometer (plugged into pin 25)
    • Moisture (plugged into pin 26)

Once we plug them all together, our hardware is ready to go!

The video below walks through plugging in the hardware:

 

So how about the software?

Download the latest version of Energia @ www.energia.nu/guide

You will want version 14 or newer, since this is when we started to package the PubNub library in Energia. Once installed, go ahead & launch it.

Once launched, we need to configure Energia. First, we need to tell Energia which board we are using. In this tutorial, I am using the TM4C123 LaunchPad. However, the MSP430F5529 LaunchPad can be used as well.

File > Tools > Board > [Select your LaunchPad]

Then, we need to tell Energia which Serial Port we are using:

File > Tools > Serial Port [Select your COM port. Check your computer’s device manager to identify which one.

Now, we can use the example sketch below, which is a modified version of the default “PubNubJsonWifi” example that comes packaged in Energia. A few changes were made to interface with the sensors in our example.

In the example sketch below, we are only publishing data to PubNub. However, the PubNub Energia library also enables the LaunchPad kit to subscribe to specific PubNub channels as well to enable bi-directional communication with the cloud. PubNub also provides several SDKs for different runtimes & environments, which makes it possible for our cloud-connected LaunchPad kits to interface with different environments!

Lastly, this demo is encoding the 4 sensor readings into a JSON message that is published to a single PubNub channel. Alternatively, it is possible for the sketch to publish the individual sensor readings to independent PubNub channels — up to you & your application’s needs!

The video below walks through the software:

In short, the Energia example does a few things:

  • setup() function
    • Start & configure Serial port
    • Connect to WiFi
    • Connect to PubNub
  • loop() function
    • Read analog sensor readings
    • add latest sensor readings into a JSON object
    • Publish and stream sensor readings data to PubNub
    • Here’s the code:
PubNub sample JSON-parsing client with WiFi support
This combines two sketches: the PubNubJson example of PubNub library
and the WifiWebClientRepeating example of the WiFi library.
This sample client will properly parse JSON-encoded PubNub subscription
replies using the aJson library. It will send a simple message, then
properly parsing and inspecting a subscription message received back.
This is achieved by integration with the aJson library. You will need
a version featuring Wiring Stream integration, that can be found
at http://bit.ly/2730lnW as of 2013-05-30.
Please refer to the PubNubJson example description for some important
notes, especially regarding memory saving on Uno/Duemilanove.
You can also save some RAM by not using WiFi password protection.
created 30 May 2013
by Petr Baudis
http://bit.ly/2730lnY
This code is in the public domain.
*/
#include <SPI.h>
#include <WiFi.h>
#include <PubNub.h>
#include <aJSON.h>
static char ssid[] = “your_wifi_SSID”; // your network SSID (name)
static char pass[] = “your_wifi_pass”; // your network password
static int keyIndex = 0; // your network key Index number (needed only for WEP)
const static char pubkey[] = “YOUR_PUBNUB_PUBKEY”;
const static char subkey[] = “YOUR_PUBNUB_SUBKEY”;
const static char channel[] = “YOUR_DESIRED_CHANNEL_NAME”;
#define NUM_CHANNELS 4 // How many analog channels do you want to read?
const static uint8_t analog_pins[] = {23, 24, 25, 26}; // which pins are you reading?
void setup()
{
Serial.begin(9600);
Serial.println(“Start WiFi”);
WiFi.begin(ssid, pass);
while(WiFi.localIP() == INADDR_NONE) {
Serial.print(“.”);
delay(300);
}
Serial.println(“WiFi set up”);
PubNub.begin(pubkey, subkey);
Serial.println(“PubNub set up”);
delay(5000);
}
void loop()
{
// create JSON objects
aJsonObject *msg, *analogReadings;
msg = aJson.createObject();
aJson.addItemToObject(msg, “analogReadings”, analogReadings = aJson.createObject());
// get latest sensor values then add to JSON message
for (int i = 0; i < NUM_CHANNELS; i++) {
String analogChannel = String(analog_pins[i]);
char charBuf[analogChannel.length()+1];
analogChannel.toCharArray(charBuf, analogChannel.length()+1);
int analogValues = analogRead(analog_pins[i]);
aJson.addNumberToObject(analogReadings, charBuf, analogValues);
}
// convert JSON object into char array, then delete JSON object
char *json_String = aJson.print(msg);
aJson.deleteItem(msg);
// publish JSON formatted char array to PubNub
Serial.print(“publishing a message: “);
Serial.println(json_String);
PubNub.publish(channel, json_String);
free(json_String);
delay(500);
}
view raw1 hosted with ❤ by GitHub

Visualizing Sensor Data with Freeboard.io

Now that our LaunchPad is publishing sensor data, let’s visualize it using freeboard.io.
Let’s start to visualize our data! Navigate to freeboard.io & create an account. Freeboard offers PubNub integration as a data source. The video below showcases how to quickly receive data from PubNub. Freeboard offers intuitive JSON parsing, which can break up our incoming sensor data that we can associate to different cloud-side widgets.

That’s a wrap!

Rapid prototyping internet-connected applications are getting easier than ever. The TI LaunchPad development kits, especially when paired with the Grove Starter Kit from Seeed Studio, offer an easy & affordable open source, modular approach to create hardware solutions that can interact to the cloud.

Share This:

View more at: http://bit.ly/1XReQFr

About Author

Advertisement

Post a Comment

 
Top