IoT Set-up to Operate (ON/OFF) 220-240V device with NodeMCU & 5V Relay over the internet using MQTT broker

What are we messing with?

IoT (Internet of things) set-up to operate 220-240 volt bulb.

NodeMCU Amica + 5V Relay + MQTT + Arduino IDE + Web Browser (HTML + JavaScript/jQuery)

 

WARNING!! – THIS PROJECT INVOLVES WORKING WITH HIGH VOLTAGES THAT CAN CAUSE SERIOUS INJURY, DEATH, AND/OR SET YOUR HOUSE ON FIRE. PLEASE PROCEED WITH CAUTION, AND ALWAYS MAKE SURE CIRCUITS ARE UN-PLUGGED BEFORE WORKING ON THEM

 

Hardware:

NodeMCU Amica

  • Executes Arduino program(sketch)
  • Has built-in ESP8266(WIFI module) which is the main reason to prefer NodeMCU over Arduino UNO board

5V Relay

  • To switch any 240V device(bulb in our case) ON/OFF

Miscellaneous

  • Jumper wires to connect the circuit
  • Breadboard
  • 240V light bulb with lamp holder
  • Electrical wires to connect bulb & relay to 240V AC power supply
  • USB wire to upload program to NodeMCU board from computer

 

​Software:

 

MQTT (MQ Telemetry Transport)

  • Medium of communication between NodeMCU development board and client(web/mobile) application - publish/subscribe pattern
  • You may use MQTTLens chrome plugin to test out the connection to broker and try subscribing & publishing (not necessary needed)

Arduino IDE

  • To develop a program which will subscribe to MQTT broker and fetch the latest message available and accordingly switch the 5V Relay

Web Browser

  • Takes the user input command and publish to MQTT broker

 

NodeMCU 3V3 >> Relay VCC [red jumper wire]

NodeMCU D0 >> Relay IN [blue jumper wire]

NodeMCU GND >> Relay GND [green jumper wire]

Relay NO (Normally Open) >> Bulb +ve

Relay COM (common terminal) >> 240V AC power +ve

Bulb -ve >> 240V AC power -ve

 

HTML script:

When you open this html in the browser

  • Script will connect to the MQTT broker(iot.eclipse.org) and subscribes to a topic(/lamp/status/)
  • Once you see connection is successful, click on the switch button which will accordingly publish the message to the topic

Arduino program(sketch):

This is the program should be uploaded to NodeMCU board, it will do the following work

  • Connects to our WIFI(to specified SSID & password)
  • Then connects to the MQTT public broker(iot.eclipse.org) and subscribe to the topic(/lamp/status/) same as HTML script
  • When HTML does a publish to the topic in MQTT broker, callback is executed which will digitalWrite HIGH/LOW to D0 GPIO pin in NodeMCU board which in turn triggers 5V Relay ON/OFF

Wrap Up

If you will just replace the bulb connections to any other electrical device like fan, computer, fridge etc., you should see it working for all of them.

 

But make sure you got the bulb working in the first place.

 

Thanks for reading.!

 

NOTE: Although I haven't found any article doing all the stuff that is being discussed here, I have followed/referred to many different articles (from wiring to source code) to finally get to this set up, unfortunately I have lost track of most of them. If you have found any posts which have similar content kindly let me know. I am happy to credit them here.