Connect ThingHz ESP32 and Publish Multiple Sensor Data to Ubidots

ESP32 and ESP8266 are very familiar SoC in the field of IoT. These are a kind of boon for IoT projects. ESP 32 is a device with integrated WiFi and BLE. Just give your SSID, password and IP configurations and integrate the things into the cloud. Here in this instructable, We will ponder through some of the basic terms of IoT like IoT platform, MQTT, Captive portals etc. So let us go through it

Here in this instructable, we will be going to demonstrate.

Hardware and Software Specification

To complete this project we have made use of the following:

Software Specification

Creating a Captive Portal

A captive portal is a web page that is displayed to newly connected users before they are granted broader access to network resources. Here we are serving three web pages to select between DHCP and Static IP Settings. we can define the IP address to ESP in two ways.

  • DHCP IP address- it is a way to dynamically assign the IP address to the device. The ESP’s default IP address is
  • The static IP address- assigning a permanent IP Address to our network device. to provide the static IP to the device we need to define the IP address, gateway address, and subnet mask.

The first webpage is being hosted at Here the user is provided with the radio buttons to select between DHCP and Static IP settings. In the next webpage, we have to provide the IP related information to proceed further.


The HTML code for web pages can be found in this Github repository. You can use any IDE or text editor like Sublime or notepad++ to make HTML web pages.

  • First Create an HTML webpage containing two radio buttons to choose between DHCP and Static IP Settings.
  • Now create the button to submit your response
  • Give some name to radio buttons.
  • Now insert a ‘ SUBMIT ‘ button to send the response to the device. In the other web pages, we have text boxes.
  • Give the name value and Input type to the text box and add a submit button to ‘ SUBMIT ‘to submit the response.
  • Create a ‘ RESET ‘ button to reset the content of the text field.

The main problem occurs while managing the WiFi credentials. Even though we have WiFiMulti library for that where we can give multiple SSIDs and passwords to the device and the device will connect to the network available. But, what if the network available is not in the WiFiMulti list. Flashing the ESP32 device all the time is not a reliable solution.

To resolve this problem, We are hosting a webpage where the user can submit the SSID and Password of the available network. It works as follows.

  • The webpage is hosted at the static IP or DHCP IP as chosen by the user from the captive portal
  • After 60-sec Device will automatically disconnect from AP

Publishing Sensor Readings to Ubidots

Here we are using Wireless Temperature and Humidity Sensors with the ESP 32 device to get the temperature and Humidity data. We are sending the data to Ubi=dots using the MQTT protocol. MQTT follows a publish and subscribe mechanism rather than request and response. It is faster and reliable than HTTP. This works as follows.

  • First, include the Task Scheduler header files, its instance and schedules the tasks.
  • We have scheduled two tasks referring to two different control operations.
#define _TASK_TIMEOUT 
#include <TaskScheduler.h>
Scheduler ts;
Task tSensor(4 * TASK_SECOND, TASK_FOREVER,&taskSensorCallback, &ts, false, NULL, &taskSensorDisable);
Task tWiFi(10* TASK_SECOND, TASK_FOREVER, &taskWiFiCallback, &ts, false, NULL, &taskWiFiDisable);
  • Task 1 is for reading the sensor value this task runs for 1 second till it reaches a timeout of 10 secs.
  • Now Task 2 is enabled and we are disabling Task 1
  • When Task 2 reaches its time out Task 1 is enabled again and Task 2 is disabled. Here again, we are getting the updated value and the process goes on.

Reading the I2C Sensor Data

//begin transmission from 0x44;
//for one shot transmisstion with high repeatability we use 0x2C(MSB) and 0x06(LSB)
//end transmission
//request bytes from 0x44
Wire.requestFrom(Addr, 6);
if (Wire.available() == 6) {//data[0] and data[1] contains 16 bit of temperature.
root[0] =;
root[1] =;
//data[2] contains 8 bit of CRC
root[2] =;
//data[3] and data[4] contains 16 bit of humidity
root[3] =;
root[4] =;
//data[5] consists of 8 bit CRC
root[5] =;
int temp = (root[0] * 256) + root[1];//shift MSB by 8 bits add LSB
float cTemp = -45.0 + (175.0 * temp / 65535.0);
float fTemp = (cTemp * 1.8) + 32.0;
//shift the MSB by 8 bits add LSB to it devide by full resolution and *100 for percentage
float humidity = (100.0 * ((root[3] * 256.0) + root[4])) / 65535.0;
tempC = cTemp;
//tempC = 21.8;
tempF = fTemp;
//tempF = 71.24;
humid = humidity;
//humid = 95;
Serial.print("Temperature in C:\t");
Serial.println(String(tempC, 1));
Serial.print("Temperature in F:\t");
Serial.println(String(tempF, 1));
Serial.print("Humidity:\t ");
Serial.println(String(humid, 1));

Connecting to Ubidots MQTT API

#define MQTT_CLIENT_NAME "ClientVBShightime123" 
char mqttBroker[] = "";
char payload[100]; char topic[150];//create variable to store token IDString tokenId;
#define VARIABLE_LABEL_BAT "bat"
#define VARIABLE_LABEL_HUMID "humid"
char topic1[100];
char topic2[100];
char topic3[100];
sprintf(topic1, "%s","");
sprintf(topic1, "%s%s", "/v1.6/devices/", DEVICE_LABEL); sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL_TEMPC); // Adds the value sprintf(payload, "%s{\"value\":%s}", payload, str_cTemp);
//Adds the value
sprintf(payload, "%s}", payload); // Closes the dictionary brackets Serial.println(payload); Serial.println(client.publish(topic1,payload) ? "published" : "notpublished");//Do same for other topic as well

Visualizing the Data

  • Go to Ubidots and log in to your account.
  • Navigate to the Dashboard from the Data tab listed on the top.
  • Select a widget from the list and add a variable and devices.
  • The sensor data can be visualized on the dashboard using different widgets.

Overall Code

The Over code for HTML and ESP32 can be found in this GitHub repository.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
vaibhav sharma

vaibhav sharma


Full Stack IoT developer working on effective Wireless Sensor Network