The Development Kit based on ESP8266, integrates GPIO, PWM, IIC, 1-Wire and ADC all in one board. Power your development in the fastest way by combining with NodeMCU Firmware!
By following this guide you will be able to POST and GET data to/from Ubidots using the NodeMCU ESP8266 module in just a couple of minutes!
Requirements
1. Setting up Arduino IDE
The following steps asume that you already have installed either Arduino IDE, if you haven't meet this requirement yet, please go to the corresponding source:
Launch Arduino
Go to File > Preferences and copy the following URL into the Additional boards manager URL, as shown in the image below:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Click on the Board Manager icon and search "esp8266". Make sure to install esp8266 by ESP8266 Community version 2.7.4:
At last, you will need to select your board from Tools > Board and select the correct COM port:
Launch a web browser and download the Ubidots library in zip format. Save it to a known location:
In the Arduino IDE Go to Sketch > Include Library > Add .ZIP Library... and select the .zip file downloaded in the previous step:
That's it for the Setup. You can proceed now to coding!
2. POST values to Ubidots
The sample code in this section will POST to Ubidots 3 random values to 3 separate variables from the ESP32, namely:
Variable_Name_1
Variable_Name_2
Variable_Name_3
Copy and paste the following code snippet to your IDE's code editor, but first make sure to change the following variables:
UBIDOTS_TOKEN: Set this variable to a valid Ubidots token
WIFI_SSID: Set this to your WiFi's SSID
WIFI_PASS: Set this to your WiFi's password.
UBI_HTTP (Optional): You can change this parameter in the Ubidots class constructor to change the protocol used. Possible values are:
UBI_HTTP
UBI_TCP
UBI_UDP
// This example sends data to multiple variables to
// Ubidots through HTTP protocol.
/****************************************
* Include Libraries
****************************************/
#include "Ubidots.h"
/****************************************
* Define Instances and Constants
****************************************/
const char* UBIDOTS_TOKEN = "..."; // Put here your Ubidots TOKEN
const char* WIFI_SSID = "..."; // Put here your Wi-Fi SSID
const char* WIFI_PASS = "..."; // Put here your Wi-Fi password
// Create a pointer to a instance of the Ubidots class to use it globally
Ubidots* ubidots{nullptr};
/****************************************
* Auxiliar Functions
****************************************/
// Put here your auxiliar functions
/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(115200);
Ubidots::wifiConnect(WIFI_SSID, WIFI_PASS);
ubidots = new Ubidots(UBIDOTS_TOKEN, UBI_HTTP);
//ubidots->setDebug(true); // Uncomment this line for printing debug messages
}
void loop() {
float value1 = random(0, 9) * 10;
float value2 = random(0, 9) * 100;
float value3 = random(0, 9) * 1000;
ubidots->add("Variable_Name_One", value1); // Change for your variable name
ubidots->add("Variable_Name_Two", value2);
ubidots->add("Variable_Name_Three", value3);
bool bufferSent = false;
bufferSent = ubidots->send(); // Will send data to a device label that matches the device Id
if (bufferSent) {
// Do something if values were sent properly
Serial.println("Values sent by the device");
}
delay(5000);
}
After this, all you have to do is flash the firmware to the ESP32. By following the steps above, you should see a new Device in your Ubidots account named after your ESP32's MAC address.
3. GET values from Ubidots
The following sample code will GET the last value of a variable of the selected device from Ubidots. Copy and paste it to your IDE's code editor, but first make sure to change the following variables:
UBIDOTS_TOKEN: Set this variable to a valid Ubidots token
WIFI_SSID: Set this to your WiFi's SSID
WIFI_PASS: Set this to your WiFi's password.
DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM: This is the label of the device containing the variable that you want to retrieve its value.
VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM: This is the label of the variable that you want to retrieve its value.
UBI_HTTP (Optional): You can change this parameter in the Ubidots class constructor to change the protocol used. Possible values are:
UBI_HTTP
UBI_TCP
UBI_UDP
// This example retrieves last value of a variable from the Ubidots API
// using HTTP protocol.
/****************************************
* Include Libraries
****************************************/
#include "Ubidots.h"
/****************************************
* Define Constants
****************************************/
const char* UBIDOTS_TOKEN = "..."; // Put here your Ubidots TOKEN
const char* WIFI_SSID = "..."; // Put here your Wi-Fi SSID
const char* WIFI_PASS = "..."; // Put here your Wi-Fi password
const char* DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM = "weather-station"; // Replace with your device label
const char* VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM = "humidity"; // Replace with your variable label
// Create a pointer to a instance of the Ubidots class to use it globally
Ubidots* ubidots{nullptr};
/****************************************
* Auxiliar Functions
****************************************/
// Put here your auxiliar functions
/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(115200);
Ubidots::wifiConnect(WIFI_SSID, WIFI_PASS);
ubidots = new Ubidots(UBIDOTS_TOKEN, UBI_HTTP);
//ubidots->setDebug(true); //Uncomment this line for printing debug messages
}
void loop() {
/* Obtain last value from a variable as float using HTTP */
float value = ubidots->get(DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM, VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM);
// Evaluates the results obtained
if (value != ERROR_VALUE) {
Serial.print("Value:");
Serial.println(value);
}
delay(5000);
}
After this, all you have to do is flash the firmware to the ESP32.
Once flashed the firmware, launch the Serial Terminal on your IDE and you'll be able to see the retrieved value being printed.
4. Summary
For additional examples depicting how to include a timestamp or context to your data, refer to the Ubidots' ESP8266 library Example folder.