All Collections
Connect your Devices
Connect an ESP32-DevKitC to Ubidots over MQTT
Connect an ESP32-DevKitC to Ubidots over MQTT

Learn to connect the ESP32 to Ubidots Application Development Platform over MQTT

S
Written by Sergio M
Updated over a week ago

The ESP32, successor to the ESP8266, is a low cost (less than $15), low power systems on a chip microcontroller with integrated Wi-Fi & dual-mode Bluetooth. The ESP32 series employs a Tensilica Xtensa LX6 microprocessor in both dual-core 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 PUBLISH and SUBSCRIBE data to/from Ubidots using the ESP32 in just a couple of minutes!

Requirements

Table of Contents

  1. Set up your ESP32 with the Arduino IDE

  2. Publish values to a variable

  3. Subscribe to a variable

  4. Summary

1. Set up your ESP32 with the Arduino IDE

To start this tutorial, we will begin by connecting your ESP32 to your computers USB port to program the device.

1. Begin by downloading the Arduino IDE, if you have not done so already.

2. Next, install the ESP32 Platform to your Arduino IDE. Please reference the following links depending of your operating system. The following Espressif links will prompt you to install the ESP32 to your computer. 

Note: If you are using Linux or Mac operating systems, an additional step is required as you need to install the ESP32 Platform to the Arduino IDE from your computer's terminal first. Use the below links to execute the required Terminal functions and then return to this guide to continue the ESP32 integration.

Reference image using a MacOS terminal:

3. After installing the ESP32 platform using the above Espressif Systems links, you will be prompted to restart the Arduino IDE - be sure to do so.

4. After the reboot, select your ESP32 Dev Module from Tools > Board menu.

5. Additionally, we need to be able to communicate with the ESP32, Let's be sure to select the port communication location. Go to Tools > Port >  Select the appropriate PORT for your device.

NOTE: If you are using Windows, you need to establish a serial connection between ESP32 and your computer please reference to this link to install the drivers needed and verify the connection. Also, as a MAC or Linux user, the link describes how to verify serial communication should you want to.

6. Next, download and install the PubSubClient Library. For a detailed explanation of how to install libraries using the Arduino IDE, refer to this guide.

7. Next, download and install the Ubidots ESP MQTT Library. For a detailed explanation of how to install libraries using the Arduino IDE, refer to this guide.

2. Publish values to a variable

With the following sample code you will be able to publish ANALOG reading taken from the GPIO34 pin of the ESP32.

1. To publish your first value to Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, you must assign your unique Ubidots TOKEN, SSID (WiFi Name) and Password of the available network.

/******************************************
*
* This example works for both Industrial and STEM users.
*
* Developed by Jose Garcia, https://github.com/jotathebest/
*
* ****************************************/

/****************************************
* Include Libraries
****************************************/
#include "UbidotsEsp32Mqtt.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 = ""; // Put here your Device label to which data will be published
const char *VARIABLE_LABEL = ""; // Put here your Variable label to which data will be published

const int PUBLISH_FREQUENCY = 5000; // Update rate in milliseconds

unsigned long timer;
uint8_t analogPin = 34; // Pin used to read data from GPIO34 ADC_CH6.

Ubidots ubidots(UBIDOTS_TOKEN);

/****************************************
* Auxiliar Functions
****************************************/

void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}

/****************************************
* Main Functions
****************************************/

void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
// ubidots.setDebug(true); // uncomment this to make debug messages available
ubidots.connectToWifi(WIFI_SSID, WIFI_PASS);
ubidots.setCallback(callback);
ubidots.setup();
ubidots.reconnect();

timer = millis();
}

void loop()
{
// put your main code here, to run repeatedly:
if (!ubidots.connected())
{
ubidots.reconnect();
}
if (abs(millis() - timer) > PUBLISH_FREQUENCY) // triggers the routine every 5 seconds
{
float value = analogRead(analogPin);
ubidots.add(VARIABLE_LABEL, value); // Insert your variable Labels and the value to be sent
ubidots.publish(DEVICE_LABEL);
timer = millis();
}
ubidots.loop();
}

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 ESP32 Dev Module. 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. 

NOTE: If no response is seen, try unplugging your ESP32 and then plugging it again. Make sure the baud rate of the Serial monitor is set to the same one specified in your code 115200

Now you should see the published data in your Ubidots account, inside the device called "ESP32".

3. Subscribe to a variable

With the following sample code you will be able to subscribe a value from Ubidots to start controlling any asset from the cloud. 

1. To begin 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:

  • SSID (WiFi Name) & Password of the available network connection.

  • Device Label of the device which contains the variable to want to subscribe to.

  • Variable Label of the variable you want to subscribe to

aaa
/******************************************
*
* This example works for both Industrial and STEM users.
*
* Developed by Jose Garcia, https://github.com/jotathebest/
*
* ****************************************/

/****************************************
* Include Libraries
****************************************/
#include "UbidotsEsp32Mqtt.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 = ""; // Replace with the device label to subscribe to
const char *VARIABLE_LABEL = ""; // Replace with your variable label to subscribe to

Ubidots ubidots(UBIDOTS_TOKEN);

/****************************************
* Auxiliar Functions
****************************************/

void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}

/****************************************
* Main Functions
****************************************/

void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
ubidots.setDebug(true); // uncomment this to make debug messages available
ubidots.connectToWifi(WIFI_SSID, WIFI_PASS);
ubidots.setCallback(callback);
ubidots.setup();
ubidots.reconnect();
ubidots.subscribeLastValue(DEVICE_LABEL, VARIABLE_LABEL); // Insert the dataSource and Variable's Labels
}

void loop()
{
// put your main code here, to run repeatedly:
if (!ubidots.connected())
{
ubidots.reconnect();
ubidots.subscribeLastValue(DEVICE_LABEL, VARIABLE_LABEL); // Insert the dataSource and Variable's Labels
}
ubidots.loop();
}

2. Verify & Upload the code into the board following the same steps provided in the PUBLISH 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. 

NOTE: If no response is seen, try unplugging your ESP32 and then plugging it again. Make sure the baud rate of the Serial monitor is set to the same one specified in your code 115200.

4. In the serial monitor, you will be able to see the last value received in Ubidots of the variable specified in the firmware.  Also, you will be able to control any actuator connected to the pin 16. 

5. Summary  

With this simple tutorial you are able to PUBLISH & SUBSCRIBE data to/from Ubidots with the ease of the Arduino IDE and an ESP32.

For additional examples using the ESP32 MQTT library, please visit its Github repository.

Now its time to create Ubidots Dashboards to visualize your data and deploy your IoT solution!  Happy Hacking! :) 

Other readers have also found useful...

Did this answer your question?