Skip to main content
All CollectionsConnect your Devices
Connect your ESP32 to Ubidots over HTTP, TCP or UDP
Connect your ESP32 to Ubidots over HTTP, TCP or UDP

Learn how to connect the ESP32 to Ubidots Application Enablement Platform.

David Sepúlveda avatar
Written by David Sepúlveda
Updated over 3 months ago

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

1. Set 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:

  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. Click on the Board Manager icon and search "esp32". Make sure to install esp32 by Espressif Systems version 3.0.0 or above:

  4. At last, you will need to select your board from Tools > Board and select the correct COM port:

  5. Launch a web browser and download the Ubidots library in zip format. Save it to a known location:

  6. 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. Additional information

For additional examples depicting how to include a timestamp or context to your data, refer to the Ubidots' ESP32 library Example folder.

Did this answer your question?