Dilip Kumar
5 min readMar 14, 2018

Home Automation using MQTT, Node-red and NodeMCU.

There are few other ways to automate things, i’m going with Node-red as client side for controlling things. i will use BUZZER for output which can be controlled using Node-red. We can replace buzzer with other things ,but that will require external power supplies , Relay Module , etc.. so for easy understanding , i ll try to explain it with fewer parts.

Hardware Needed

  1. NodeMCU board
  2. Buzzer
  3. Jumper wires

MQTT

I use cloudmqtt as broker. You can use local Mqtt broker which can be installed your local machine.

After creating an account with cloudmqtt , save the details .

CloudMqtt Broker Details

Programming and Setting up the device.

Connect buzzer +ve in NodeMcu D5 pin and buzzer ground in ground of NodeMcu.

Long leg is the positive in Buzzer

Now connect the Nodemcu device to PC for embedding the code.

open arduino ide and ctrl + c & + v this code .enter WiFi credentials in ssid and pass . enter the cloudmqtt details in the respective field and compile the code.as you can see in the code , i have declared constant variable pin as D5(which will be the OUTPUT pin). if we breakdown the code, you can see callback function. as the device is subscribed to a topic .if the topic “ledcontrol” receives a message “onn” , it will trigger the output pin to high, else the topic receives message “off” , it will trigger the output pin to low. Now lets jump to Nodered part. if you want to check mqtt broker is connected, you can publish message from cloudmqtt websocket UI page.

#include <ESP8266WiFi.h>
#include<PubSubClient.h>

const char *ssid = “********”; // cannot be longer than 32 characters!
const char *pass = “*********”;

const char *mqtt_server = “m14.cloudmqtt.com”;
const int mqtt_port = 19405;
const char *mqttuser = “******”;
const char *mqttpass = “*********”;

const int pin = D5;

void callback(String topic,byte* payload,unsigned int length1){
Serial.print(“message arrived[“);
Serial.print(topic);
Serial.println(“]”);
String msgg;

for(int i=0;i<length1;i++){
Serial.print((char)payload[i]);
msgg += (char)payload[i];

}
if(topic == “ledcontrol”)
{
if(msgg == “onn”){
digitalWrite(pin,HIGH);
Serial.print(“\n up \n “);

}
else if(msgg == “off”)
{
digitalWrite(pin,LOW);

Serial.print(“\n down”);
}
}
}

WiFiClient espclient;
PubSubClient client(mqtt_server,19405,callback,espclient);

void setup() {
Serial.begin(115200);
pinMode(pin,OUTPUT);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi..”);
}
Serial.println(“Connected to the WiFi network”);
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}

void reconnect(){
while (!client.connected()) {
Serial.println(“Connecting to MQTT…”);
if (client.connect(“ESP8266Client”, mqttuser, mqttpass )) {
Serial.println(“connected”);
client.subscribe(“ledcontrol”);
} else {
Serial.print(“failed with state “);
Serial.print(client.state());
delay(2000);
}
}
}

void loop()
{
if(!client.connected()){
reconnect();
}
client.loop();

}

Node-Red

  1. Install Node-red on your system. Node-red can be downloaded from here.
  2. After installing Node-red , try to install nodered dashboard. for learning about Dashboard , check this.
  3. Type node-red on command prompt to run nodered. you will find the nodered url in the command prompt window. it will be http://127.0.0.1:1880
  4. We are going to use switch as button for controlling things. Drag and drop switch button in dashboard column from left panel and mqtt from output panel

5. connect the end of switch to start of mqtt , i.e output of switch to input of mqtt.

6. Double click on switch to edit .

7. create a group for your flow. you can rename the label if you want. here comes the important part, On payload select string from dropdown, as it is going to send string value “onn” and “off”. enter “onn” in the text field and “off “ in Off payload. next enter the topic “ledcontrol”. click done to save the settings.

8. Double click on mqtt to configure the broker. enter the topic and select server,if its already configured from server . if else we configure now.

9. Fill the details which we saved from cloudmqtt broker. dont forget to enter user and password details in SECURITY tab. select update to save the settings.

10. Click (right top corner)“Deploy” . now mqtt should show “connected” notification. if its in disconnected or connecting , check mqtt configuration.

Now Lets Play Around

  1. Navigate to Node-red UI page(127.0.0.1:1880/ui)

2. After deploying the code into NodeMcu. open the serial monitor.

3. Enable the switch from Nodered UI Tab to switch on.

Switch onn
Switch off

Thanks for Reading