Arduino Due and Mobile Service

Introduction

 
The previous article Developed With Arduino Due was an introduction to this board, from the setting up of it and the installation of the necessary libraries, to the setting up of the Ethernet shield so the Arduino Due can access the Internet. In this second article, we will see how Arduino can interact with the services of Microsoft Azure, specifically with the Mobile Service. We will start by creating a Mobile Service. We will build a small circuit using the sensor temperature and humidity DHT11, ending with the piece of code that will be responsible for transferring the information detected by the sensor on Microsoft Azure. We will see that in the following order:
  • Create a Mobile Service.
  • Build an electronic circuit.
  • Write the code for to upload data form sensor dht11 to Microsoft Azure.
  • Test application.
  • Conclusion.
Create a Mobile Service
 
Creating a mobile service is very simple, you first need to have an account and a subscription to Microsoft Azure. Enter the portal than on the home screen press the New button that we can see in the lower left as in the following:
 
New Mobile Service Create
 
Figure 1: New Mobile Service Create
 
In the next screen, we need to name the Mobile Service, choose whether to use an existing or create a free 20 MB database, where to place it. In my case, the nearest Data Center is North Europe, so this will not be the same for everyone, but it is advisable to choose the Data Center nearest your location. Finally, select the Backend to be JavaScript.
 
Create Mobile Service
 
Figure 2: Create a Mobile Service
 
Next create a database and give it a name. Choose whether to create a new database SQLServer and then enter your credentials, as in the following figure:
 
Specify Database Setting
 
Figure 3: Specify Database Setting
 
After this activity, Azure creates the Mobile Service and we can see that now we have a Mobile service and one database.
 
Mobile service
 
Figure 4: Mobile service
 
SQL database
 
Figure 5: SQL database
 
We are now ready to create a table within the Database sensorfarm_db. Click with the mouse on sensorfarm under Mobile Service, the next screen, click the button located next to "ADD A TABLE". You will see another screen where we will need to enter the name of the table, insert tabsensor as in the following figure:
 
Create New table
 
Figure 6: Create New table
 
Leave all the other fields as they are and click on the button shown with checkmarks in the lower-right corner and wait until it creates the table.
 
Checkmarks in the lower right corner
 
Figure 7: Checkmarks in the lower right corner
 
This was the procedure end, the procedure for the creation of a Mobile Service. We now turn to the implementation of the electronic circuit.
 
Build an electronic circuit
 
For the realization of the circuit, we need the following components:
  • Board Arduino Due
  • Board Ethernet shield
  • Sensor DHT11
  • BreadBoard (basis for the connection of the sensor)
  • Colmeter (cables for the connection from board Arduino Due and sensor DHT11)
  • Power Supply Cable for the connection board Arduino due
Here I will show the pictures of the final circuit. The sensor can operate from a voltage of 3.3 Volt DC up to a maximum of 5.5 Volts. For greater clarity on the connection and operation, refer to the official documentation.
 
The connections are:
  • Pin Signal Sensor with cable Black on pin 4 Arduino Due
  • Pin +5v Sensor with cable Red on pin 5v Arduino Due
  • Pin – Sensor with cable blue on pin GND Arduino Due
Pin GND Arduino Due
 
Figure 8: Pin GND Arduino Due
 
In the image above, note that the Ethernet Shield is superimposed on the Arduino Due. To see the pins required for connection, see the Black arrow from the right to the left. I left the two boards separate only to display all the necessary components. After the connection, here's how to be practical in our circuit.
 
Practical in circuit
 
Figure 9: Practical in circuit
 
As possible and note the Ethernet Shield is superimposed on the Arduino Due.
 
Write the code for to upload data form sensor dht11 to Microsoft Azure
 
Also finished the construction of the circuit, we are ready to start the software. The first thing to do to run is to download the necessary libraries to use the sensor DHT11 found at this link. After downloading, you need to install it. We can run (both of or one of?) the following two modes:
  • Extracting the files in the Zip file and manually copy the directory C: \ Program Files (x86) \ Arduino \ libraries.
     
  • Follow the procedure below, start the Arduino IDE, let's select the menu Sketch command to include the library and then add the Zip library as shown.
Figure 10: Include library and then add Zip library
 
We're going to select the Zip file we downloaded. It usually goes in the Download folder.
 
Select the .zip file
 
Figure 11: Select the Zip file
 
Confirm with the Open command and here we will ensure everything is successful. We find the file DHT11 between libraries available.
 
Include Liberary
 
Figure 12: Include Library
 
With this procedure, files are extracted and stored, not in the parent directory, but in C:\Users\username\ Documents\Arduino, where "username" will change depending on what you entered when you installed the operating system, in my case it is C:\Users\CARMELO LA MONICA\Documents\Arduino. We have the required libraries installed. We turn now to writing the code. We start the Arduino IDE and create a file Sketch called DHT11 Sample and insert the following code.
  1. #include <SPI.h>    
  2. #include <Ethernet.h>    
  3. #include <dht11.h>    
  4. // Ethernet shield MAC address (sticker in the back)    
  5. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };    
  6. /*You can find this values in your Mobile Service dashboard  
  7.    server = namemobileservice.azure-mobile.net  
  8.    table_name = nametable  
  9.    ams_key = your keyvalue, find in home page mobile service, with a click on button MANAGE KEYS*/    
  10. const char* server = "sensorfarm.azure-mobile.net";    
  11. const char* table_name = "tabsensor";    
  12. const char* ams_key = "you can find it on mobile service with manage keys button";    
  13. EthernetClient client;    
  14. char buffer[64];    
  15. dht11 DHT;    
  16. #define  DHT11_PIN 4    
  17.     
  18. /*This method use Mobile service REST API fot to write value*/    
  19. void write_sensorvalue(double tempvalue, double humidityvalue)    
  20. {    
  21.    if (client.connect(server, 80)) {    
  22.          
  23.      /*Table name in Mobile service*/    
  24.      sprintf(buffer, "POST /tables/%s HTTP/1.1", table_name);    
  25.      client.println(buffer);    
  26.     /*Sever name in Mobile Service*/    
  27.      sprintf(buffer, "Host: %s", server);    
  28.      client.println(buffer);    
  29.     /*Mobile Services application key*/    
  30.      sprintf(buffer, "X-ZUMO-APPLICATION: %s", ams_key);    
  31.      client.println(buffer);    
  32.     // JSON content type for the information     
  33.      client.println("Content-Type: application/json");    
  34.     /*Write values of temperature and humidity from DHT11 sensor*/    
  35.      sprintf(buffer, "{\"tempvalue\":\"%f\",\"humidityvalue\":\"%f\"}", tempvalue, humidityvalue);    
  36.     // Content length    
  37.      client.print("Content-Length: ");    
  38.      client.println(strlen(buffer));    
  39.     // End of headers    
  40.      client.println();    
  41.     // Request body    
  42.      client.println(buffer);    
  43.   }    
  44.        
  45.    else     
  46.    {    
  47.      Serial.println("no connection available!");    
  48.    }    
  49.      client.stop();    
  50. }    
  51. /*-------------------------------------------------------*/    
  52. /*Setup method*/    
  53. /*-------------------------------------------------------*/    
  54. void setup()    
  55. {    
  56.    Serial.begin(9600);    
  57.   if (Ethernet.begin(mac) == 0)     
  58.    {    
  59.      Serial.println("ethernet shield failed");    
  60.      for (;;);    
  61.    }    
  62.        
  63.    // Ethernet shield initialization:    
  64.    delay(1000);    
  65. }    
  66. /*-------------------------------------------------------*/    
  67. /*Loop method*/    
  68. /*-------------------------------------------------------*/    
  69. void loop()    
  70. {    
  71.    /*Read value from sensor input*/    
  72.    int chk = DHT.read(DHT11_PIN);    
  73.   /*Call method writ_sensorvalue and pass sensor parameter for to save on mobile service*/    
  74.    write_sensorvalue((DHT.temperature), (DHT.humidity));    
  75.   /*Time wait for new input data*/    
  76.    delay(10000);    
  77. }   
Let's see how the code works. We include in this case the library installed for the sensor DHT11, in this case the file dht11.h, while the other two libraries were already in the previous article. There are three variables of type const char *, that are nothing more than the name of the table, the server name and the code key, all you need to upload the data.
  1. #include <dht11.h>    
  2. const char* server = "sensorfarm.azure-mobile.net";    
  3. const char* table_name = "tabsensor";    
  4. const char* ams_key = " you can find it on mobile service with manage keys button "
 We're going to declare yet another three variables as in the following:
  1. char buffer[64];    
  2. dht11 DHT;    
  3. #define DHT11_PIN 4   
 Where the first will be the data buffer in the upload, the second represents the file where DHT11, by the methods and other variables, we will be able to read the values of temperature and humidity by the sensor. What the last variable does is set the pin 4 of Arduino Two as input to the sensor, so we need to necessarily connect the pin "Signal" sensor DHT11 on pin 4 of the board. The method write_sensorvalue, is the most interesting, since it uploads data to Azure, we see the most significant pieces of code.
  1. /*Table name in Mobile service*/    
  2. sprintf(buffer, "POST /tables/%s HTTP/1.1", table_name);    
  3. client.println(buffer);    
  4. /*Sever name in Mobile Service*/    
  5. sprintf(buffer, "Host: %s", server);    
  6. client.println(buffer);    
  7. /*Mobile Services application key*/    
  8. sprintf(buffer, "X-ZUMO-APPLICATION: %s", ams_key);    
  9. client.println(buffer);   
 We perform an HTTP request and a call POST where we must indicate the name of the table previously, the name of the Server and code key, this to allow the correct upload of all the data.
  1. // JSON content type for the information     
  2. client.println("Content-Type: application/json");    
  3. /*Write values of temperature and humidity from DHT11 sensor*/    
  4. sprintf(buffer, "{\"tempvalue\":\"%f\",\"humidityvalue\":\"%f\"}", tempvalue, humidityvalue);   
With the preceding code, we specify in what format we are sending data and then type JSON and in the last piece of code, we specify the pattern, or what will meet in the table tabsensor when we're going to see if the data has been loaded properly. The Setup method we have already seen in the previous article and more interesting and the Loop method.
  1. void loop()    
  2. {    
  3.    /*Read value from sensor input*/    
  4.    int chk = DHT.read(DHT11_PIN);    
  5.   /*Call method writ_sensorvalue and pass sensor parameter for to save on mobile service*/    
  6.    write_sensorvalue((DHT.temperature), (DHT.humidity));    
  7.   /*Time wait for new input data*/    
  8.    delay(10000);    
  9. }    
Declare a variable of type int that does nothing but read the status of the sensor temperature and humidity, this means that the read method as a parameter requires the input pin. We recall then the method write_sensorvalue passing as parameters the values of temperature and humidity with DHT.temperature and DHT.humidity. The last instruction code is nothing more than a waiting time of ten seconds before starting a new reading. We have now completed the part relating to the code, we are now ready to do the function tests, but first, we must load the sketch code on the Arduino Due, to this procedure refer you to the previous article.
 
Test application
 
Let's have a look now inside the table tabsensor, we will find this situation.
 
Table Tabsensor
 
Figure 13: Table Tabsensor
 
This is because we have not defined any record within it, but also any column. We can feed now Arduino Due in various ways, either with its power supply or by attacking the USB port of our PC, where we are now because we have previously uploaded the sketch code. If all goes correctly you should see the green LEDs flash card Ethernet Shield, this means that it went into operation, but especially when we go to the Azure portal and check if the data has been loaded and if all are done properly here is what we will see.
 
Tab Sensor
 
Figure 14: Tab Sensor
 
We note that from the services of Azure has added for us humidityvalue and tempvalue fields with other fields, id, timestamp and version, the latter are not part of our code but are added automatically when you insert a new record into the tabsensor table, in other words, we did not need to worry about any of this, we let Azure do it.
 

Conclusion

 
In this second article, we saw how Arduino can interact with Microsoft Azure and Mobile Services by creating a simple circuit where we read, using sensor DHT11, the values of temperature and humidity and then saved the data in the table tabsensor. In the next article, we will create an application Windows Phone 8.1 that will ensure the display of the data and then take a specific action.