Home Automation using NodeMCU and Ubidots

Hi guys, This tutorial is about Home Automation using NodeMCU and Ubidots. First, to setup NodeMCU board in Arduino Software have a look at this post.

What is Ubidots?

Ubidots is an IoT cloud Platform with code-free application development tools, Rules Engine, Scheduled Reporting and Real-time dashboards. It helps us to build an IoT application in an easy manner with real-time control using the MQTT protocol.

OBJECTIVE:

In this tutorial, we will visualize the temperature, humidity, and heat index values of the DHT11 sensor in the ubidots dashboard. We will also control two led’s from the ubidots dashboard.

DIAGRAM:

Please refer the diagram below to connect the DHT-11 and led’s with the NodeMCU.

 

UBIDOTS SETUP:

  1. Create an ubidots for education account for free from here
  2. After logging into ubidots for education, we will be asked to create devices as shown below
  3. Create a new device by clicking on Add device and name the device
  4. Now double click on the device, then we will be prompted to create variables. To create a variables click add variables and select the default as shown below
  5. Then name the variable as Temperature and create variables for humidity, heat index, led 1 and led 2 using the same procedure
  6. Now variables are successfully created. We should move to dashboards to create a widget to visualize the variables. Click on + to add a widget
  7.  We can visualize the data from the device in form of charts, metrics, maps, tables, indicators, and controls. To visualize the temperature in values, select the metric and select the last value. 
  8. Then select the device and the variable that this widget should show. Click the finish button to create the widget
  9. Repeat the steps 7,8 to create widgets for humidity and heat index
  10. Then to control the led’s select the control option. Control option has two types of controls. They are Switch and Slider. For digital control, we can use a switch and for analog control, we can use the slider. We can select the switch to control the led’s
  11. Then select the device and the variable for the widget. We have to specify the ON and OFF message for the switch
  12. After creating widgets for all the five variables, the dashboard looks like this
  13. Then we show know our token to connect with our account in ubidots. To know more about tokens visit here. To find the token refer the images below

LIBRARIES REQUIRED:

  1. To establish an MQTT connection with the Ubidots, download the UbidotsESPMQTT library from here.
  2. For MQTT communication, download the pubsubclient library from here.
  3. Extract the libraries and paste it into the library folder of Arduino (C:\Program Files (x86)\Arduino\libraries).

PROGRAM:

Firstly, Import the UbidotsESPMQTT and DHT library. Then define the constants such as token, WI-FI name and, WI-FI password.

1
2
3
4
5
#include "UbidotsESPMQTT.h"
#include "DHT.h"
#define TOKEN "A1E-LIFbJVjc3bIaC4JD3Zy42HuOchUhnA" // Your Ubidots TOKEN
#define WIFINAME "MSK" //Your SSID
#define WIFIPASS "minixplore1239" // Your Wifi Pass

Define the Pin that is used for DHT-11 and the type of DHT used.

1
2
#define DHTPIN D4     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11

Create an object named dht for the class DHT with the DHT pin and DHT type as parameters.

1
DHT dht(DHTPIN, DHTTYPE);

Create an object named client for the class Ubidots with our token as the parameter.

1
Ubidots client(TOKEN);

In the setup loop, initialize the Serial and WI-FI connection as shown below.

1
2
3
4
Serial.begin(115200);
client.setDebug(true); // Pass a true or false bool value to activate debug messages
client.wifiConnection(WIFINAME, WIFIPASS);
client.begin(callback);//if any topic published or subscribed call the function callback

As we use digital pins D7 and D8 for led, declare it as an output using pinMode.

1
2
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);

And we should subscribe with the led-1 and led-2 topics already created in ubidots.

1
2
client.ubidotsSubscribe("home-automation","led-1");
client.ubidotsSubscribe("home-automation","led-2");

And we should initialize the DHT library in the setup loop.

1
dht.begin();

Determine the temperature, humidity and heat-index data using the dht object created as shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  int t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  int f = dht.readTemperature(true);
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  int hic = dht.computeHeatIndex(t, h, false);
 
  if(!client.connected()){
      client.reconnect();
      client.ubidotsSubscribe("home-automation","led-1");
      client.ubidotsSubscribe("home-automation","led-2");//Insert the dataSource and Variable's Labels
 
      }

Then publish the temperature, humidity and heat-index values to the ubidots cloud using client object as shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if(tp!=t)
  {
  client.add("temperature", t);
 
  client.ubidotsPublish("home-automation");
  }
  if(hp!=h)
  {
  client.add("humidity", h);
  client.ubidotsPublish("home-automation");
  }
  if(hic!=hicp)
  {
  client.add("heat-index", hic);
  client.ubidotsPublish("home-automation");
  }
  client.loop();
  tp=t;
  hp=h;
  hicp=hic;
  }

To avoid frequent data traffic we send data only if the current data differs from the previous value. This reduces data traffic up to 90%.

Full Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "UbidotsESPMQTT.h"
#include "DHT.h"
#define DHTPIN D4     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11
 
DHT dht(DHTPIN, DHTTYPE);
 
int tp,hp,hicp;
 
#define TOKEN "A1E-LIFbJVjc3bIaC4JD3Zy42HuOchUhnA" // Your Ubidots TOKEN
#define WIFINAME "MSK" //Your SSID
#define WIFIPASS "minixplore1239" // Your Wifi Pass
 
Ubidots client(TOKEN);
 
 
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.println();
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  if (strcmp(topic,"/v1.6/devices/home-automation/led-1/lv")==0){
 
  if ((char)payload[0]=='1'){
    digitalWrite(D7, HIGH);
  }
  else{
    digitalWrite(D7, LOW);
  }
  Serial.println();
 
}
 
if (strcmp(topic,"/v1.6/devices/home-automation/led-2/lv")==0){
 
  if ((char)payload[0]=='1'){
    digitalWrite(D8, HIGH);
  }
  else{
    digitalWrite(D8, LOW);
  }
  Serial.println();
 
}
}
 
 
 
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  client.setDebug(true); // Pass a true or false bool value to activate debug messages
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  //if any topic published or subscribed call the function callback
  pinMode(D7, OUTPUT);
  pinMode(D8, OUTPUT);
  //Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe("home-automation","led-1");
  client.ubidotsSubscribe("home-automation","led-2");
  dht.begin();
  }
 
void loop() {
  int h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  int t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  int f = dht.readTemperature(true);
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  int hic = dht.computeHeatIndex(t, h, false);
 
  if(!client.connected()){
      client.reconnect();
      client.ubidotsSubscribe("home-automation","led-1");
      client.ubidotsSubscribe("home-automation","led-2");//Insert the dataSource and Variable's Labels
 
      }
  if(tp!=t)
  {
  client.add("temperature", t);
 
  client.ubidotsPublish("home-automation");
  }
  if(hp!=h)
  {
  client.add("humidity", h);
  client.ubidotsPublish("home-automation");
  }
  if(hic!=hicp)
  {
  client.add("heat-index", hic);
  client.ubidotsPublish("home-automation");
  }
  client.loop();
  tp=t;
  hp=h;
  hicp=hic;
  }

OUTPUT:


Note:
To work with thingspeak platform refer this

THANKYOU
SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.