IOT Switch

For a while now we have seen lamps that can be turned on or off via your smartphone, or using something like Amazon Alexa or Google Home. I decided to give it a shot at building a device that can plug in into existing bulb sockets and allow for any type of bulb to be integrated into IOT.


For this project the list of parts is as follows.

  • Nodemcu
  • 5V relay
  • 120/220V to 5V smartphone charger
  • Socket for light bulb


The image below shows a diagram of how the parts for this project should be conected together.


The first prototype I designed had a plug socket, but the funciontioning is the same as if it had a bulb socket. Below you can see a picture of the design I used for testing.



Stripped 5V phone charger



The software side was developed using arduino, and consists on a sketch that connects to a MQTT broker and subscribes to a pre-defined topic. Within this topic we receive the on or off signal which in turn activates or deactivates the 5V relay which turns the bulb or on off. The MQTT broker I used was Mosquitto running on a Raspberry Pi, but you can very well use something like cloudmqtt or

Below is the arduino code used.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "xxxxx";     // Your WIFI SSID
const char* password =  "xxxxxx";  // Your WIFI Password
const char* mqttServer = ""; // Your Broker IP Address
const int mqttPort = 1883;

long lastReconnectAttempt = 0;

WiFiClient espClient;
PubSubClient client(espClient);

void callback(char* topic, byte* payload, unsigned int length) {
    if (strcmp(topic,"bedroom1")==0){
      if( (byte)payload[0] == '1'){
        digitalWrite(16, HIGH);  // Turn the LED off by making the voltage HIGH
      if( (byte)payload[0] == '0'){
          digitalWrite(16, LOW);   // Turn the LED on by making the voltage LOW

boolean reconnect() {
  if (client.connect("Nodemcu 3.0")) {
    // Once connected, publish an announcement...
    client.publish("bedroom1","bedroom connected");
    // ... and resubscribe
  return client.connected();

void setup() {

  pinMode(16, OUTPUT);
  pinMode(5, OUTPUT); 
  pinMode(4, OUTPUT); 
  pinMode(0, OUTPUT);  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("Connected to the WiFi network");
  client.setServer(mqttServer, mqttPort);
  lastReconnectAttempt = 0;

void loop() {
  if (!client.connected()) {
    long now = millis();
    if (now - lastReconnectAttempt > 5000) {
      lastReconnectAttempt = now;
      // Attempt to reconnect
      if (reconnect()) {
        lastReconnectAttempt = 0;
  } else {
    // Client connected

    client.publish("bedroom1", "stillalive");

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.




Leave a Comment

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