The ESP32, successor to the ESP8266, is a low cost (less than $15), low power SoC (System on a Chip) microcontroller with integrated Wi-Fi & dual-mode Bluetooth. The ESP32 series employs a Tensilica Xtensa LX6 microprocessor in both dual- and single-core variations. The ESP32 was created and developed by Espressif Systems, a Shanghai-based Chinese company with a proven record of quality microcontroller production and distribution.
To learn more about the ESP32, reference the device documentation here.
Following this guide you will be able to POST and GET data to/from Ubidots using the ESP32 in just a couple of minutes!
Requirements
Table of Contents
Set up Arduino IDE
POST values to Ubidots
GET values from Ubidots
Additional information
1. Set up Arduino IDE
To be able to work with the ESP32 in the Arduino IDE, you will need to install the board using Arduino’s Board Manager. If you are not familiar with adding a board to Arduino IDE, refer to this article for additional guidance.
1. Launch Arduino.
2. Go to File > Preferences and copy the following URL into the Additional boards manager URL, as shown in the image below.
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json`
3. Install the ESP32 board library from Tools > Boards > Boards Manager.
4. At last, you will need to select your board from Tools > Board and select the correct COM port.
5. Download and install the Ubidots library. For a detailed explanation on how to install libraries using the Arduino IDE, refer to this guide.
2. POST values to Ubidots
The following sample code will POST to Ubidots 3 random values to 3 separate variables from the ESP32. Copy and paste it to an Arduino file. Make sure to:
Step 1:
– Enter your Ubidots token, WiFi SSID and Password.
– Select which protocol, HTTP, TCP or UDP to communicate the ESP32 with Ubidots, in the respective lines.
– [OPTIONAL] Enable Debug messages uncommenting the respective line in the setup()
function.
/****************************************
* 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
Ubidots ubidots(UBIDOTS_TOKEN, UBI_HTTP);
// Ubidots ubidots(UBIDOTS_TOKEN, UBI_TCP); // Uncomment to use TCP
// Ubidots ubidots(UBIDOTS_TOKEN, UBI_UDP); // Uncomment to use UDP
/****************************************
* Auxiliar Functions
****************************************/
// Put here your auxiliar functions
/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(115200);
ubidots.wifiConnect(WIFI_SSID, WIFI_PASS);
// 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);
}
Step 2: Compile the code using Arduino IDE's "Verify" (check-mark) button located at the top-left corner.
Step 3: Flash the code to the ESP32 using the "Upload" arrow-like button next to the "Verify" button.
Step 4: Open Arduino IDE's Serial Monitor to see the debug messages.
After following the above steps, 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 from Ubidots. Copy and paste it to an Arduino file. Make sure to:
Step 1:
– Enter your Ubidots token, WiFi SSID and Password.
– Enter the Device and Variable label from which the last value is to be retrieved.
– Select which protocol, HTTP or TCP (UDP doesn't support retrieving data) to communicate the ESP32 with Ubidots, in the respective lines.
– [OPTIONAL] Enable Debug messages uncommenting the respective line in the setup()
function.
/****************************************
* 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 = "DEV_LABEL"; // Replace with your device label
const char* VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM = "VAR_LABEL"; // Replace with your variable label
Ubidots ubidots(UBIDOTS_TOKEN, UBI_HTTP);
// Ubidots ubidots(UBIDOTS_TOKEN, UBI_TCP); // Uncomment to use TCP
/****************************************
* Auxiliar Functions
****************************************/
// Put here your auxiliar functions
/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(115200);
ubidots.wifiConnect(WIFI_SSID, WIFI_PASS);
// 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);
}
Step 2: Compile the code using Arduino IDE's "Verify" (check-mark) button located at the top-left corner.
Step 3: Flash the code to the ESP32 using the "Upload" arrow-like button next to the "Verify" button.
Step 4: Open Arduino IDE's Serial Monitor to see the debug messages.
After following the above steps, you should see the last value being retrieved periodically in Arduino IDE's Serial Monitor.
4. Additional information
For additional examples depicting how to include a timestamp or context to your data, refer to the Ubidots' ESP32 library Example folder.
Other users also found helpful...