Introduction
This article will guide you through the process of hooking up your Edison with Windows Azure, and how to do some cool stuff, such as controlling it. This example will just switch on and off an LED connected to any of the digital pins on the Edison board paired up with the Arduino breakout board. Let's see how it really works. An Azure mobile service will act as a bridge between the Edison and the controller (a Windows phone app is used here). Edison will read the value of an attribute from the table of the mobile service. A Windows phone app will update the value in the table, and thus we will make changes in the Edison code depending on the value.
Prerequisites
It is assumed that the reader has basic knowledge of Windows phone app development and can hook up the app with an Azure mobile service. If you are unfamiliar with creating a back end for your app, then go through this
link.
Things Required (Hardware)
- Intel Edison with the Arduino breakout board
- A LED
Required Software
- Visual Studio
- Arduino IDE
Services required
1: Azure mobile service
Firstly, let's create an Azure mobile service. There are lots of articles on creating an Azure mobile service. Follow this
link and create a mobile service that will contain a single table and let's name it as
controltable. The table will contain a column named "
status" and another named "
name". The value of the
status column will either be 1 or 0, while the value for
the name will be set to Edison/Arduino.
Now we will develop a Windows phone app that will add/modify the value of the attribute. The picture below shows us the screenshot of the Azure SQL table. Please note that the value of the attribute for “device” doesn’t matter. You can even exclude this attribute if you want to.
The next section will deal with the development of the Windows phone app.
Windows Phone App
Our app will consist of only two buttons: An
on the button and an
off button. The screenshot of the app is shown below.
![]()
You can ignore the logout button, Actually, I was trying to add some features. Now, what will these buttons? This will update the value of the
“status” attribute. When we press on the button, the value of “
status” will be 1, else it will be 0.
There is a bit of a problem though. What to update if nothing is created? For that, we will deploy the app two times. The first time, we will create the table and assign some default values. The next time we will just update the previously updated values.
Now, our app is ready. Test the app and check whether the value of “status” is getting updated or not. Once it works, then there you go. The app is ready. The next part will concentrate on the Edison code.
Edison Code
Let’s get into Edison. Before kick-starting your code for your Edison, follow the initial steps mentioned
here for configuring your Edison. After connecting your Edison, note your comport number. Then open up your Arduino IDE and select Intel Edison from the board. If you don’t find the Intel Edison option, then you need to download the necessary files from the "
Boards Manager" option.
Once you open up the Arduino IDE, two functions will be mentioned from before: Void setup() and void loop(). The Edison has a built-in Wi-Fi. We will use the Wi-Fi to connect it to the Internet. So our first action is to include a library for Wi-Fi. Go to
Sketch, include a library, then
Wi-Fi. After that, let’s add up this code to connect it to Wi-Fi.
- #include <ArduinoJson.h>
- #include <SPI.h>
- #include <WiFi.h>
-
- char ssid[] = "avirup171";
- char pass[] = "avirupbasu";
- int keyIndex = 0;
-
- void setup()
- {
- pinMode(13,OUTPUT);
- digitalWrite(13,HIGH);
- Serial.begin(115200);
- while (!Serial) {
- ;
- }
-
-
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println("WiFi shield not present");
-
- while(true);
- }
-
- String fv = WiFi.firmwareVersion();
- if( fv != "1.1.0" )
- Serial.println("Please upgrade the firmware");
-
-
- while (status != WL_CONNECTED) {
- Serial.print("Attempting to connect to SSID: ");
- Serial.println(ssid);
-
- status = WiFi.begin(ssid, pass);
-
- }
- }
The above code is responsible for connecting the Edison to the internet by Wi-Fi. Also, we have set PIN 13 to output mode and the current status is off. We will use the serial monitor of the IDE to monitor the process. Now, let's go to void loop(). Retrieving data from Azure is done by HTTP get method.
- void loop()
- {
- send_request();
- wait_response();
- read_response();
- end_request();
- delay(100);
- }
These functions will retrieve the data from the Azure table. But before deep diving into the methods, we need to add some global variables to link our Edison to Azure service. Add these global variables:
- const char* server= "genesis-iot-control.azure-mobile.net";
- const char* table_name= "cloudcontroller";
- const char* ams_key="Your application key";
- char buffer[150];
The application key can be found in the manage keys button at your Azure portal. Now, we will write the code for the methods in void loop().
- void send_request()
- {
- Serial.println("\nconnecting...");
-
- if (client.connect(server, 80)) {
-
-
-
- sprintf(buffer, "GET /tables/%s HTTP/1.1", table_name);
- client.println(buffer);
-
-
- sprintf(buffer, "Host: %s", server);
- client.println(buffer);
-
-
- sprintf(buffer, "X-ZUMO-APPLICATION: %s", ams_key);
- client.println(buffer);
-
-
- client.println("Content-Type: application/json");
-
- client.print("Content-Length: ");
- client.println(strlen(buffer));
-
-
- client.println();
-
-
- client.println(buffer);
-
- } else {
- Serial.println("connection failed");
- }
- }
We performed an HTTP request and called GET where we have indicated the name of the table previously, the name of the Server and code key; this to allow the correct retrieval of the data. Then we specified in what format the data needs to be retrieved and specified it as JSON. Let's write the code for wait_response().
- void wait_response()
- {
- while (!client.available())
- {
- if (!client.connected())
- {
- return;
- }
- }
- }
Then we need to read the data retrieved. Since it's in JSON format, we need to parse the JSON string to get our desired value. Below is the example of a sample string.
{"id":"2492D996-C471-48F0-B3C9-F33E3B37477F","status":"0","name":"arduino"}
A very efficient library known as the ArduinoJson exist. This will do most of the parsing. But the retrieved JSON string is enclosed within '[' and ']'. These must be removed for the library to work. So, firstly you need to include the library in your code and add the following global variables and add:
- #include <ArduinoJson.h>
-
- #define RESPONSE_JSON_DATA_LINENNO 10
-
- int charIndex=0;
- StaticJsonBuffer<200> jsonbuffer;
Then write the following code in your
read_response() method:
- void read_response()
- {
- boolean bodyStarted;
- int jsonStringLength;
- int jsonBufferCntr=0;
- int numline=RESPONSE_JSON_DATA_LINENNO;
-
- while (client.available())
- {
-
- char c = client.read();
- if (c == '\n')
- {
- numline -=1;
- }
- else
- {
- if (numline == 0 && (c!='[')&& (c!=']') )
- {
- buffer[jsonBufferCntr++] = c;
- buffer[jsonBufferCntr] = '\0';
- }
- }
- }
- Serial.println("Received:");
- Serial.println(buffer);
- Serial.println("");
- parse();
- }
The above code will read the response and the parse method is responsible for decoding the string. The parse() method is shown below. In the parse() method itself, we will change the status of the PIN13.
- void parse()
- {
- StaticJsonBuffer<150> jsonbuffer;
- JsonObject& root = jsonbuffer.parseObject(buffer);
- if(!root.success())
- {
- Serial.println("PARSING FAILED!!!");
- return;
- }
- int f= root["status"];
- Serial.println("Decoded: ");
- Serial.println(f);
- if(f==0)
- digitalWrite(13,LOW);
- else
- digitalWrite(13,HIGH);
- }
Here, in the above code, f stores the value of the attribute of status. Then, we check the value of f and ultimately set the PIN either to HIGH or LOW.
For a detailed documentation on ArduinoJson library visit this
link. However, there is a bit of a problem with this library. It will be discussed later on. Now, we will write the remaining method's code.
Now, you will see that when you compile the code, you will most likely encounter some errors. These errors needs to be removed. Before moving further have a look over this
issue. The file WString.h misses some lines of code. The difference can be seen
here. You need to update the file located here.
C:\Users\Username\AppData\Roaming\Arduino15\packages\Intel\hardware\i686\1.6.2+1.0\cores\arduino\WString.h
After updating your errors will be resolved. After you compile the code, burn it into your Edison and then you are done. When the code is burnt in your Edison, take a LED, and attach the longer leg of the LED to PIN13 and the shorter leg to Gnd. An optional 233-ohm resistor can be added with the longer leg. Thus, our Windows Phone controlled Edison via Windows Azure is ready. The screenshot of the serial window, while the Edison is in action, is shown below.
The first stage is when the Edison is attempting to connect to the Wi-Fi network.
The next stage is when the Edison is trying to connect to the Azure mobile service. After connecting, it obtains the JSON data which is then decoded by the JSON library and then we obtain the decoded result. The decoded result is responsible for the status of the PIN13 on the Edison.
Conclusion
Thus we have learned in this article how to control your Edison using Azure mobile services. Mobile service acts as a bridge. The Windows phone app alters data in Azure tables and ultimately the Edison is controlled. The article didn't cover the Windows phone development but a look at the links will provide you the info required. The entire Edison code is uploaded as a text file. The YouTube link for the video is
here. If any problem regarding Wstring is encountered, then please leave a comment about the error.
Note: The pink lines in the article appears as it was copied from another article written by me at Intel Software for IoT. The link is
here. Please ignore that.