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:
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.
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:
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.
Figure 4: Mobile service
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:
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.
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
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.
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.
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.
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.
- #include <SPI.h>
- #include <Ethernet.h>
- #include <dht11.h>
-
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
-
-
-
-
- const char* server = "sensorfarm.azure-mobile.net";
- const char* table_name = "tabsensor";
- const char* ams_key = "you can find it on mobile service with manage keys button";
- EthernetClient client;
- char buffer[64];
- dht11 DHT;
- #define DHT11_PIN 4
-
-
- void write_sensorvalue(double tempvalue, double humidityvalue)
- {
- if (client.connect(server, 80)) {
-
-
- sprintf(buffer, "POST /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");
-
- sprintf(buffer, "{\"tempvalue\":\"%f\",\"humidityvalue\":\"%f\"}", tempvalue, humidityvalue);
-
- client.print("Content-Length: ");
- client.println(strlen(buffer));
-
- client.println();
-
- client.println(buffer);
- }
-
- else
- {
- Serial.println("no connection available!");
- }
- client.stop();
- }
-
-
-
- void setup()
- {
- Serial.begin(9600);
- if (Ethernet.begin(mac) == 0)
- {
- Serial.println("ethernet shield failed");
- for (;;);
- }
-
-
- delay(1000);
- }
-
-
-
- void loop()
- {
-
- int chk = DHT.read(DHT11_PIN);
-
- write_sensorvalue((DHT.temperature), (DHT.humidity));
-
- delay(10000);
- }
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.
- #include <dht11.h>
- const char* server = "sensorfarm.azure-mobile.net";
- const char* table_name = "tabsensor";
- 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:
- char buffer[64];
- dht11 DHT;
- #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.
-
- sprintf(buffer, "POST /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);
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.
-
- client.println("Content-Type: application/json");
-
- 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.
- void loop()
- {
-
- int chk = DHT.read(DHT11_PIN);
-
- write_sensorvalue((DHT.temperature), (DHT.humidity));
-
- delay(10000);
- }
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.
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.
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.