Arduino is an open-source electronics platform based on easy-to-use hardware and software. The primary board for Arduino is the Arduino UNO which connects to a series of different shields to expand it’s possibilities.
The Arduino UNO is a microcontroller board based on the ATmega328P, which contains everything needed to support the microcontroller including simple power supplies via a USB cable, an AC-to-DC adapter, or by battery. For official overviews of the Arduino UNO please refer to its documentation here.
The Arduino Ethernet Shield is based on the Ethernet chip Wiznet W5100, which supports up to 4 socket connections simultaneously and is ideal for IoT solutions in the home, office, or reliable non-industrial applications.
Following this guide you will be able to POST and GET data to/from Ubidots using the Ethernet Shield connected to an Arduino UNO Board in just a couple of minutes!
Requirements
Step-by-Step
1. Hardware Setup
2. Setting up the Arduino IDE
3. Sending (POST) Data to Ubidots
4. Receiving (GET) Data from Ubidots
5. Summary
1. Hardware Setup
1. To begin, place the Ethernet Shield atop the Arduino UNO and apply gentle pressure to connect the shield's pins with the board's headers. Now with the Arduino Uno + Ethernet shield assembled you are able to connect to the internet via Ethernet wired connection.
2. To establish the connection with the network, plug one end of the Ethernet cable to the Ethernet Shield and the other one into the router or socket which provides network services.
2. Setting up the Arduino IDE
1. Download the Ubidots Ethernet library and install it. For a detailed explanation of how to install libraries using the Arduino IDE, refer to this guide.
2. Before programming your Ethernet Shield it is important verify if the hardware is working properly. For this, we highly recommend making the sample test provided by Arduino. Go to Sketch -> Examples -> Ethernet and select the "WebClient" example.
To verify the test and confirm connectivity, Open the Serial using the "magnifying glass" icon located in the top-right of the IDE. If everything is working properly, you will receive a server response from Google.com with the Arduino page information.
2. Sending (POST) Data to Ubidots
With the following sample code you will be able to post the ANALOG readings taken from Arduino Board analog port A0.
1. To post your first value in Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, you will need to assign your unique Ubidots TOKEN and the MAC Address of the Ethernet shield.
/********************************
* Libraries included
*******************************/
#include <Ethernet.h>
#include <SPI.h>
#include <UbidotsEthernet.h>
/********************************
* Constants and objects
*******************************/
/* Assigns the Ubidots parameters */
char const * TOKEN = "Assign_your_Ubidots_TOKEN_here"; // Assign your Ubidots TOKEN
char const * VARIABLE_LABEL_1 = "temperature"; // Assign the unique variable label to send the data
char const * VARIABLE_LABEL_2 = "humidity"; // Assign the unique variable label to send the data
char const * VARIABLE_LABEL_3 = "pressure"; // Assign the unique variable label to send the data
/* Enter a MAC address for your controller below */
/* Newer Ethernet shields have a MAC address printed on a sticker on the shield */
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
/* initialize the instance */
Ubidots client(TOKEN);
/********************************
* Main Functions
*******************************/
void setup() {
Serial.begin(9600);
//client.setDebug(true);// uncomment this line to visualize the debug message
/* start the Ethernet connection */
Serial.print(F("Starting ethernet..."));
if (!Ethernet.begin(mac)) {
Serial.println(F("failed"));
} else {
Serial.println(Ethernet.localIP());
}
/* Give the Ethernet shield a second to initialize */
delay(2000);
Serial.println(F("Ready"));
}
void loop() {
Ethernet.maintain();
/* Sensors readings */
float value_1 = analogRead(A0);
float value_2 = analogRead(A1);
float value_3 = analogRead(A2);
/* Sending values to Ubidots */
client.add(VARIABLE_LABEL_1, value_1);
client.add(VARIABLE_LABEL_2, value_2);
client.add(VARIABLE_LABEL_3, value_3);
client.sendAll();
delay(5000);
}
2. Verify your code within the Arduino IDE. To do this, in the top left corner of our Arduino IDE you will see the "Check Mark" icon; press it to verify your code.
3. Upload the code into your Arduino UNO + Ethernet Shield. To do this, choose the "right-arrow" icon beside the "check mark" icon.
4. To verify the connectivity of the device and the data sent, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs.
5. Confirm your data in Ubidots. Now you should see the posted data in your Ubidots account, located the device called "arduino-ethernet".
3. Receiving (GET) Data from Ubidots
With the following sample code you will be able to get a value from Ubidots to start controlling any asset you desire.
1. To start getting values from Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, be sure to assign the following parameters:
Device Label of the device which contains the variable to want to GET.
Variable Label of the variable you want to GET.
/********************************
* Libraries included
*******************************/
#include <Ethernet.h>
#include <SPI.h>
#include <UbidotsEthernet.h>
/********************************
* Constants and objects
*******************************/
/* Assigns the Ubidots parameters */
char const * TOKEN = "Assign_your_Ubidots_TOKEN_here"; // Assign your Ubidots TOKEN
char const * DEVICE_LABEL = "Assign_device_label_here"; // Assign the unique device label
char const * VARIABLE_LABEL = "Assign_variable_label_here"; // Assign the unique variable label to get the last value
/* Enter a MAC address for your controller below */
/* Newer Ethernet shields have a MAC address printed on a sticker on the shield */
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
/* initialize the instance */
Ubidots client(TOKEN);
/********************************
* Main Functions
*******************************/
void setup() {
Serial.begin(9600);
//client.setDebug(true);// uncomment this line to visualize the debug message
/* start the Ethernet connection */
Serial.print(F("Starting ethernet..."));
if (!Ethernet.begin(mac)) {
Serial.println(F("failed"));
} else {
Serial.println(Ethernet.localIP());
}
/* Give the Ethernet shield a second to initialize */
delay(2000);
Serial.println(F("Ready"));
}
void loop() {
Ethernet.maintain();
/* Getting the last value from a variable */
float value = client.getValue(DEVICE_LABEL, VARIABLE_LABEL);
/* Print the value obtained */
Serial.print("the value received is: ");
Serial.println(value);
delay(5000);
}
2. Verify & Upload the code into the board following the same steps provided in the POST step above.
3. To verify the connectivity of the device and the data which is being received, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs.
4. In the serial monitor, you will be able to see the last value received in Ubidots of the variable specified in the firmware.
4. Summary
With this simple tutorial we are able to POST & GET data to/from Ubidots with the ease of the Arduino IDE and a Arduino UNO + Ethernet Shield. If your wish to find more examples to handle context or timestamp values in your request checkout Ubidots documentation with the Ethernet Shield by clicking here.
Now its time to create Ubidots Dashboards to visualize your data and deploy your IoT solution! Happy Hacking! :)
Other readers have also found useful...