Internet controlled switch using NodeMCU and MQTT

Posted by

Hello… Sailors! Today we are going to build an internet controlled switch. Woah! Internet controlled Switch, sounds cool! With this switch we will be able to switch ON/OFF our appliances from anywhere in the world like a BOSS.

What’s NodeMCU?

NodeMCU is an open-source IOT module which is based on ESP-12 module, we can create WIFI Access point with this or connect this to our wifi router and then control relays, servos etc. over internet or wifi.

What’s MQTT?

MQTT is a light weight messaging protocol for small sensors and mobile devices. It consists of a broker(can be on cloud or local network) which routes the messages from publisher to subscriber.

In this project, we will be sending message to NodeMCU which is connected to the internet. The NodeMCU then switches the relay ON/OFF based on the message received. Sounds genius!

How this works?

We are going to create an MQTT topic and make our NodeMCU it’s subscriber and MQTT dash, the publisher. MQTT ¬†dash will publish the commands to broker which will be routed to the NodeMCU connected to the internet. NodeMCU will translate and command and act accordingly.

Let’s begin…

Things we need:

Note: After completing CloudMQTT registration create a topic over there e.g. “uniqueID/topic”. We shall be using this topic to communicate with NodeMCU.

Circuit Setup:

NodeMCU controlled Relay
NodeMCU controlled Relay

Connection details:

  • D5 pin of NodeMCU is connected to input/signal pin of Relay.
  • 3v3 pin of NodeMCU to 3v3 source.
  • Vcc of relay to 5V source.
  • All ground pins are connected to common ground.

Android Setup:

Now, to communicate with the device we need to set up our android/iphone. We have downloaded the MQTT Dash application from Google Play Store.

Open the app, add a dashboard using + sign on top right and fill in the details from CloudMQTT console page:

Name: Super Awesome Switch
Address: e.g.
Port: e.g. 1883
UserName: Will be provided at CloudMQTT Console
Password: Will be provided at CloudMQTT Console

Click save button on top right, a new dashboard is now available. Open this dashboard, it will connect and display a blank page. Again, click on + sign now to add Switch on screen. It will ask for few details:

Name: Relay 1
Sub: uniqueID/topic

We will leave other 2 check boxes checked and ON as 1 and OFF as 0. Click Save on top right and we are done with App configuration.

Code Setup:

We are set to upload our code to NodeMCU. Connect NodeMCU to computer with Micro USB cable, and open Arduino IDE. Now we shall check the port connected and the device selected is NodeMCU 1.0 from the Tools menu in Arduino and finally upload our code. Code on GitHub

#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
// Connect to the WiFi
const char* ssid = "YOUR_WIFI_SSID"; // wifi SSID
const char* password = "YOUR_WIFI_PASSWORD"; // wifi password
const char* mqtt_server = ""; // change this with your server name
const char* device_id = "uniqueID"; // write any unique ID
WiFiClient espClient;
PubSubClient client(espClient);
//const byte ledPin3 = 0;
//const byte ledPin0 = 16;
const byte ledPin5 = 14;
//const byte ledPin6 = 12;
//const byte ledPin7 = 13;
//const byte ledPin8 = 15;
//const byte ledPin9 = 3;
//const byte ledPin10 = 1;
//const byte ledPin1 = 5;
//const byte ledPin2 = 4;
//const byte ledPin4 = 2;
//const byte ledPin12 = 10;
//const byte ledPin11 = 9;

char message_buff[100];
void callback(char* topic, byte* payload, unsigned int length) {
 //payload[length] = '\0';
 Serial.print("Message arrived [");
 Serial.println("] ");
 int i;
 for(i=0; i<length; i++) {
    message_buff[i] = payload[i];
  message_buff[i] = '\0';
  String msgString = String(message_buff);
 if (strcmp(topic, "uniqueID/topic")==0){ // write your unique ID here
    if(msgString == "1"){
    digitalWrite(ledPin5, LOW); // PIN HIGH will switch OFF the relay
    if(msgString == "0"){
    digitalWrite(ledPin5, HIGH); // PIN LOW will switch ON the relay

void reconnect() {
 // Loop until we're reconnected
 while (!client.connected()) {
 Serial.print("Attempting MQTT connection...");
 // Attempt to connect
 if (client.connect(device_id, "cloud_username", "cloud_password")) { // enter cloudmqtt provided username and password in console
  // ... and subscribe to topic
  client.subscribe("uniqueID/topic"); // write your unique ID here
 } else {
  Serial.print("failed, rc=");
  Serial.println(" try again in 5 seconds");
  // Wait 5 seconds before retrying
void setup()
 client.setServer(mqtt_server, 1883); // change port number as mentioned in your cloudmqtt console
 pinMode(ledPin5, OUTPUT);
void loop()
 if (!client.connected()) {

Lines to be manipulated are mentioned within the code as comments.

Once the code is uploaded to NodeMCU, open Serial Monitor to check the print commands. If it is connected to CloudMQTT server, open your MQTT dash app in your Android phone and try switching ON/OFF the relay, it will work like a BOSS. Now, we can remove the usb cable and connect our NodeMCU to 3V3 source.

So, guys that’s it for today. I hope you enjoyed the post, feel free to ask questions or provide suggestions. There is a lot of room for improvement in code and circuit.

Till then, Happy IOTing.

Leave a Reply

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