Deploy your home automation application with Ubidots and realize the value the Internet of Things can bring to your home or office.

In the follow tutorial we describe how you can automate your home or office with an ESP8266 (our project focussed on light control) and then incorporate Google Assistant to control devices with voice commands.

Table of Contents:

  1. Application Requirements
  2. Hardware Assembly
  3. Programming with the Arduino IDE 
  4. Ubidots Home Automation Application Development
  5. IFTTT setup for Google Assistant voice commands

Application Requirements:

1. Hardware Assembly

Here at Ubidots, we do not like to cut the cords of our appliances so we recommend that your don't either. To control any device without cutting its AC power cord, we instead recommend creating your own extension cable and splicing in a relay as the midpoint between the appliance and the AC wall outlet.

This relay cable will act as the device's ON / OFF switch. This is the relay wiring connection map:

Follow the table below to establish the connections between the sensors and the NodeMCU ESP8266:

[OPTIONAL] We recommend to protect your hardware with a case for protect it from external factors like dust, heat, and humidity. 

Assemble hardware containing extension cable with a spliced in relay, the NodeMCU Grove Shield, the NodeMCU ESP8266, and the DHT sensor. 

Follow the table below to establish the connections between the NodeMCU ESP8266 with the DHT Sensor and Relay:

The AC power plug for the wall outlet and its respective socket for the device being controlled. 

2. Programming with the Arduino IDE

1. If not done yet, download the Arduino IDE.
1a. Open the IDE and select Files -> Preferences
1b. Add the url below into the Additional Board Manager URLs field. You can add multiple URLs by separating them with commas.

NOTE: If you're a Mac user, you will notice that the interface is different from Windows; you should installs this driver if you cannot install the NodeMCU ESP8266.

2. Open and Install the ESP8266 board in the Boards Manager:
Tools -> Board -> Boards Manager
2a. You can easily find the board by typing “ESP8266” in the search bar. 

3. Now select the NodeMCU 1.0 board from Tools -> Board menu

4. Define or double check the Port of your PC which the device is communicating with. Go to Tools -> Port: -> Select the port

4b. Ensure your IDE Upload Speed is 115200 by going to Tools -> Upload Speed -> 115200

5. Download the Ubidots MQTT ESP library from our GitHub repository. To download the library, click the "Clone or download" button and select "Download ZIP".

6. Now back in the Arduino IDE, click on Sketch > Include Library > Add .ZIP Library

7. Upload the Ubidots MQTT library in the Arduino IDE.

Go to Sketch -> Include library -> Add .ZIP library and choose the Ubidots MQTT ESP library

If properly uploaded, you get the response: "Library added to you libraries."

8. Repeat the exact same process to download and install the DHT Sensor Library

9. Install the library: "PubSubClient"
Sketch/Program -> Include Library -> Library Manager and install the "PubSubClient" library. 

10. Close and open again the Arduino IDE.

Programing Time & Ubidots Visualization 

Once your ESP8266 is set up, we can post and get data from Ubidots in order to monitor and control your home appliances. 

1. Copy and paste the following code in the Arduino IDE. Don’t forget to customize the Wi-Fi SSID and password and your Ubidots Token


 * Include Libraries


#include "UbidotsESPMQTT.h"

#include "DHT.h"


 * Define Constants


#define TOKEN "xxxx" // Your Ubidots TOKEN

#define WIFINAME "xxxx" //Your SSID

#define WIFIPASS "xxxx" // Your Wifi Pass

#define DEVICE_LABEL "esp8266";

#define DHTPIN D1 // pin we're the sensor connected to

#define RELAY D3   // pin we're the relay connected to

// Uncomment whatever type you're using!

#define DHTTYPE DHT11   // DHT 11

//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

//#define DHTTYPE DHT21   // DHT 21 (AM2301)

Ubidots ubiClient(TOKEN);

WiFiClient client;



 * Auxiliar Functions


void callback(char* topic, byte* payload, unsigned int length) {

  Serial.print("Message arrived [");


  Serial.print("] ");

  for (int i=0;i



  if ((char)payload[0]=='1'){

digitalWrite(RELAY, HIGH);



digitalWrite(RELAY, LOW);





 * Main Functions


void setup() {

  // put your setup code here, to run once:

  ubiClient.ubidotsSetBroker(""); // Sets the broker properly for the business account

  ubiClient.setDebug(true); // Pass a true or false bool value to activate debug messages


  pinMode(RELAY, OUTPUT);


  ubiClient.wifiConnection(WIFINAME, WIFIPASS);





  char* deviceStatus = getUbidotsDevice(DEVICE_LABEL);

  if (strcmp(deviceStatus, "404") == 0) {

ubiClient.add("light", 0); //Insert your variable Labels and the value to be sent




  ubiClient.ubidotsSubscribe(DEVICE_LABEL,"light"); //Insert the Device and Variable's Labels


void loop() {

  // put your main code here, to run repeatedly:



  ubiClient.ubidotsSubscribe(DEVICE_LABEL,"light"); //Insert the Device and Variable's Labels


  // Read humidity

  float humValue = dht.readHumidity();

  // Read temperature as Celsius (the default)

  float tempCValue = dht.readTemperature();

  // Read temperature as Fahrenheit (isFahrenheit = true)

  float tempFValue = dht.readTemperature(true);

  ubiClient.add("temperature-c", tempCValue); //Insert your variable Labels and the value to be sent


  ubiClient.add("temperature-f", tempFValue); //Insert your variable Labels and the value to be sent


  ubiClient.add("humidity", humValue); //Insert your variable Labels and the value to be sent






char* getUbidotsDevice(char* deviceLabel) {

  char* data = (char *) malloc(sizeof(char) * 700);

  char* response = (char *) malloc(sizeof(char) * 400);

  sprintf(data, "GET /api/v1.6/devices/%s/", deviceLabel);

  sprintf(data, "%s HTTP/1.1\r\n", data);

  sprintf(data, "%sHost:\r\nUser-Agent:esp8266/1.0\r\n", data);

  sprintf(data, "%sX-Auth-Token: %s\r\nConnection: close\r\n\r\n", data, TOKEN);

  if (client.connect("", 80)) {


  } else {

return "e";



  int timeout = 0;

  while(!client.available() && timeout < 5000) {


if (timeout >= 4999){

    return "e";




  int i = 0;

  while (client.available()) {

response[i++] = (char);

if (i >= 399){




  char * pch;

  char * statusCode;

  int j = 0;

  pch = strtok (response, " ");

  while (pch != NULL) {

if (j == 1 ) {

  statusCode = pch;


pch = strtok (NULL, " ");




  return statusCode;


Now, verify your code is correct by clicking the check button in the Arduino IDE above the editor. 

Once the code is verified, you will receive a response similar to the one below, indicating that it is properly set up. 

Next, your have to upload the code into your NodeMCU. To do this, choose the right-arrow icon besides the check icon.

Once the code is uploaded, you will receive the message below in the Arduino IDE:

2. Go to the Device section of your Ubidots account, and visualize the device previously created —ESP8266— receiving temperature and humidity data. 

3. As our device is already connected and sending data to Ubidots, we can create a dashboard and widget to control the lamp’s light from a web or mobile dashboard. 

4. Go to your Ubidots account and choose Device Management ->Dashboards and create a new dashboard by clicking the blue plus icon. 

4. Now, create a control widget to switch on/off the lamp. It is required by our sample code that your control widget be named "light," or modify the sample code above to reflect your desired variable label:

Now you have a basic control application for turning on and off lights, that you can access from a desktop or a mobile phone by downloading the Ubidots explorer app in the Playstore.

Yet, we can go far beyond this. Ubidots also lets you create custom visualizations such as gauge indicators, line charts, bars, and metrics to extract insights from your assets conditions, like your Lamp’s temperature and humidity. 

If you want to learn more about Ubidots dashboards and these custom visualizations, we recommend the following articles: 

For this tutorial, the IoT dashboards looks like this: 

Setup a recipe in IFTTT to control your system using Google Assistant

Last but not least, you will learn to control lights by using voice commands with IFTTT, a web service that lets you connect applications and automate tasks, and Google Voice Assistant.

1. Sign up or log in to your IFTTT account. 

2. Go to My applets -> New applets. 

3. Click + This (trigger) and select Google Assistant. Then, click Connect and follow the authentication process:

4. Once the connection is properly established, select the first option “Say a simple phrase” as trigger:

5. In the following step, you need to fill the fields with the google assistant custom voice commands. For instance:

  • What do you want to say? “Turn the lamp on”
  • What's another way to say it? (optional) “Turn on the lamp”
  • And another way? (optional) “Turn the light on”
  • What do you want the Assistant to say in response? “Ok, turning on the lamp”
  • Language English
  • And click Create Trigger

6. With the Trigger already set, it’s time to create the action “+ that”:

Then, search for Webhooks as action service:

To finish, select the action Make a web request:

7. Fill the fields of the Webhooks setup with the following parameters: 

The result should look like below. To finish just press Create action:

8. Once the trigger and the action are already configured and properly created, you will see the following message in the applets you have just created:

Now you can go to your Google Assistant and say “Turn the lamp on” and see how your lamp is actually turned on.

9. Now repeat the same process to create the applet to turn off the lamp. All the previously steps provided are the same, the only difference are the mentioned below:

  • Trigger configuration (Google Assistant): switch “on” for “off”
  • Action configuration (Webhooks): The body should be {"light": 0} instead of {"light": 1}

10. Once both applets are created, you should have something like below in  your IFTTT portal: 

Let’s give a try to Google Assistant responding to our voice commands: 

And you’re done; a very impressive smart home application for remotely controlling your lights by using Ubidots and Google Assistant!

Did this answer your question?