Internet controlled switch using NodeMCU and MQTT

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:

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
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.


Popular posts from this blog

Controlling Servo Motor with BLYNK app over WiFi

Time scheduled electric switch controlled with Blynk app

Setup Mosquitto MQTT broker in Windows