The IT industry is full of buzzwords and “The Internet of Things” (IoT) is one that’s getting thrown about a lot lately. The IoT promises to connect billions of devices and sensors to the internet. How this data is stored, sorted, analysed and surfaced will determine the amount of value it is to your business. With this in mind I thought it’s time to start playing around with some bits and pieces to see if I could create my very own IoT connected array of sensors.

To get started I’ll need a micro-controller that I can attach some sensors to. Second I’ll need some kind of web service and storage to accept and store my raw sensor data. I’m not a developer so I’ve decided to keep things simple to start with. My design goals however, are to make use of cloud services to accept and store my raw data. Azure Mobile Services seems like a good place to start.

I’ve chosen the following components for my IoT Project

  1. Arduino Yun – the Micro-controller board
  2. Temperature Sensor – to detect ambient temperature
  3. Light Sensor – to detect light levels
  4. Pressure Sensor – to detect barometric pressure
  5. Azure Mobile Services – to connect the Arduino Yun to the cloud
  6. NoSQL database – to store my raw data

Arduino Yun

From the Arduino website the board’s capabilities are as follows: “The Arduino Yún is a microcontroller board based on the ATmega32u4 and the Atheros AR9331. The Atheros processor supports a Linux distribution based on OpenWrt named OpenWrt-Yun. The board has built-in Ethernet and WiFi support, a USB-A port, micro-SD card slot, 20 digital input/output pins (of which 7 can be used as PWM outputs and 12 as analog inputs), a 16 MHz crystal oscillator, a micro USB connection, an ICSP header, and a 3 reset buttons.”

Further Details on the Arduino Yun can be found at: http://arduino.cc/en/Main/ArduinoBoardYun?from=Products.ArduinoYUN

Schematics

The following schematic diagram illustrates the wiring arrangements between the Arduino Yun and the sensors. In this blog I’m not going to provide any specific detail in this area, instead we are going to focus on how the Arduino Yun can be programed to send its sensor data to a database in Microsoft’s Azure cloud. (There are loads of other blogs that focus specifically on connecting sensors to the Arduino boards, checkout http://www.arduino.cc/)

 

 

Azure Mobile Services

To make use of Azure Mobile Services you will need an Azure subscription. Microsoft offer a free one month trial with $210 credit to spend on all Azure Services. So what are you waiting for J
http://azure.microsoft.com/en-us/pricing/free-trial/

Ok Back to Azure Mobile Services, Microsoft define Azure Mobile Services as “a scalable and secure backend that can be used to power apps on any platform–iOS, Android, Windows or Mac. With Mobile Services, it’s easy to store app data in the cloud or on-premises, authenticate users, and send push notifications, as well as add your custom backend logic in C# or Node.js.” For my IoT project I’m just going to use Azure Mobile Services as a place to accept connections from the Arduino Yun and store the raw sensor data.

Create a Mobile Service

Creating the mobile service is pretty straight forward. Within the web management portal select New, Compute, Mobile Service then Create.

 

Azure Mobile Services will prompt you for:

  • A URL – This is the end point address the Arduino Yun will use to connect to Azure Mobile Services
  • Database – A NoSQL database to store our raw sensor data
  • Region – Which geographic region will host the mobile service and db
  • Backend – the code family used in the back end. I’m using JavaScript

Next you’ll be asked to specify some database settings including server name. You can either choose an existing server (if you have one) or alternatively create a brand new one on the fly. Azure Mobile Services will prompt you for:

  • Name – That’s the name of your database
  • Server – I don’t have an existing one so I’m selecting “New SQL database Server”
  • Server Login Name – A login name for your new db
  • Server Login Password – The password

Now that we have a database it’s time to create a table to store our raw data. The Azure Management Portal provides an easy to use UI to create a new table. Go to the Service Management page, select the Data tab, and click the “+” sign at the bottom of the page. As we are creating a NoSQL table there is no need to specify a schema. Simply provide a table name and configure the permissions “insert/update/delete/read” operations.

Retrieval of the Application Key

Now it’s time to retrieve the Application Key. This will be used to authenticate the REST API calls, when we post data to the table. To retrieve the application key go to the Dashboard page and select the “manage keys” button at the bottom of the page. Two keys will be displayed, copy the “application” one.

 

 

Create the Table

Within the Mobile Service Management page, select Data

Click Create

 

Once the table has been create its ready to accept values. The Arduino Yun can be programed to send its sensor values to our new database via the Azure Mobile Services REST API. http://msdn.microsoft.com/en-us/library/jj710108.aspx

The Application key retrieved earlier will be used to authenticate the API calls.

 

The Arduino Yun Sketch

Here is the basic code inside the Arduino Yun sketch, the code has the some core functions as follows:

Setup()

Every Arduino Yun sketch contains a setup function, this is where things like the serial port and bridge are initialized.

loop()

Every Arduino Yun sketch contains a loop this is where the sensor values are read and the other functions are called.

send_request()

The send_request function is used to establish a connection with the Azure Mobile Services endpoint. A HTTP POST is formed, authentication takes place and a JSON object is generated with our sensor values and placed in the body. Currently the sample code below sends a single sensor value (lightLevel) to Azure Mobile Services. This could easily be expanded to include all sensor values from the array of sensors connected to the Arduino Yun.

wait_response()

This function waits until there are free bytes available on the connection.

read_response()

This function reads the response bytes from Azure Mobile Services and outputs the HTTP response code to the serial console for debugging / troubleshooting purposes.

 

/* Arduino Yun sketch writes sensor data to Azure Mobile Services.*/

 // Include Arduino Yun libraries
#include <Bridge.h>
#include <YunClient.h>
#include <SPI.h>

 // Azure Mobile Service address
const char *server = “iotarduino.azure-mobile.net”;

// Azure Mobile Service table name
const char *table_name = “iotarduino_data”;

// Azure Mobile Service Application Key
const char *ams_key = “HJRxXXXXXXXXXXXXXXXmuNWAfxXXX”;

 YunClient client;
char buffer[64];

/*Send HTTP POST request to the Azure Mobile Service data API */
void send_request(int lightLevel)
{
Serial.println(“\nconnecting…”);
if (client.connect(server, 80)) {
Serial.print(“sending “);
Serial.println(lightLevel);

// POST URI
sprintf(buffer, “POST /tables/%s HTTP/1.1”, table_name);
client.println(buffer);

 // Host header
sprintf(buffer, “Host: %s”, server);
client.println(buffer);

 // Azure Mobile Services application key
sprintf(buffer, “X-ZUMO-APPLICATION: %s”, ams_key);
client.println(buffer);

 // JSON content type
client.println(“Content-Type: application/json”);

 // POST body
sprintf(buffer, “{\”LightLevel\”: %d}”, lightLevel);

 // Content length
client.print(“Content-Length: “);
client.println(strlen(buffer));

 // End of headers
client.println();

 // Request body
client.println(buffer);

 } else {
Serial.println(“connection failed”);
}
}

 /* Wait for a response */
void wait_response()
{
while (!client.available()) {
if (!client.connected()) {
return;
}
}
}

 /* Read the response and output to the serial monitor */
void read_response()
{
bool print = true;

 while (client.available()) {
char c = client.read();
// Print only until the first carriage return
if (c == ‘\n’)
print = false;
if (print)
Serial.print(c);
}
}

 /* Terminate the connection*/
void end_request()
{
client.stop();
}

 /* Arduino Yun Setup */
void setup()
{
Serial.begin(9600);
Serial.println(“Starting Bridge”);
Bridge.begin();
}

 /* Arduino Yun Loop */
void loop()
{
int val = analogRead(A0);
send_request(val);
 wait_response();
 read_response();
 send_request();
 delay(1000);
}

 

The Sensor Data in Azure

Once the sketch is uploaded to the Arduino Yun and executed, the sensor data can be viewed within the Azure Mobile Services dashboard.

 

The Arduino Yun Serial monitor displays the serial (debugging / troubleshooting) comments as the sketch executes.

 

Conclusion

This was just a bit of fun and this is obviously not an enterprise grade solution, however, I hope it goes some way to illustrating the possibilities that are readily available to all of us. Things can be done today in far fewer steps. Access to powerful compute and storage is easier than ever.

The Arduino Yun is an open source electronic prototyping board that allows people like me, without any real developer skills to mess around with electronics, explore ideas and interact with the outside world. There are loads of interesting Arduino code samples and ideas for projects with real world use cases.

My goal here was to illustrate how easy it is to obtain raw sensor data from the outside world and store it in a place where I have loads of options for data processing. By placing my data in Azure I have access to the power and resources of the Microsoft Azure cloud platform literally at my fingertips. . .Enjoy the possibilities!

 

 

Category:
Azure Platform, Technology
Tags:
, , , ,

Join the conversation! 11 Comments

  1. I love it!
    Its been ages since I saw a sprintf
    More of that

  2. Hi Guys,

    I am trying to do the same on my imX53 eval board. Basically send sensor data from imx53 to azure using mobile services.
    Should I need a rest api and json stack to establish communication? Can I not use a standard HTTP communication?

    Thanks

    • Sriram – you will need to create a simple API on the Azure Mobile Services (now Mobile Apps) side to accept the data you want to send. This API should really accept JSON as a simple transport format, but theoretically you can make it even simpler than that if you wish. I would start with a standard Mobile Apps API and use something like Postman to test it out and familiarise yourself with it before diving into wiring up to a device like the imX53.

  3. Do you have an updated version of this posting? The new Portal doesn’t reflect some of the items you atlk about here.

    • Hi Lauren, change is indeed one of the challenges of our industry, but it’s also the one thing that for me keeps it interesting and fresh. With change comes new possibilities. Azure has indeed evolved since I wrote this blog. Where are you having difficulty?

  4. Matt,

    Sorry for my delay in replying to you…
    I was away and had to step away from this 🙁
    I just can seem to have the data logged into the table and I get many errors in the code

  5. I have Arduino uno..and I am trying to connect to azure mobile servies using esp 8266 WiFi shield…im not getting it..so please can you post the code in the case of esp8266 instead of using Ethernet cable!

Comments are closed.