MyHomeThings
Published © GPL3+

Smart altenative switch with ESP8266, ioBroker and MQTT

We can make the lighting switches in our house smarter with this ESP8266 circuit, so if no WiFi it will continue to work as a normal switch.

IntermediateFull instructions provided2 hours6,397
Smart altenative switch with ESP8266, ioBroker and MQTT

Things used in this project

Hardware components

NodeMCU ESP8266 ESP-12E
×1
Relay modul with optocoupler
×1
pc817 optocoupler
×1
AC-DC power supply 3.3V
×1
AC-DC power supply 5V
×1
Resistor set
×1

Software apps and online services

Arduino IDE
Arduino IDE
iobroker

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Multitool, Screwdriver
Multitool, Screwdriver

Story

Read more

Schematics

AlternativeSwitchESP8266Schematic

Code

AlternativeSwitchESP8266.ino

Arduino
/**************************************/
//  https://myhomethings.eu           //
//  Alternative switch - ESP8266      //
//  Board: NodeMCU 1.0 ESP-12E        //
/**************************************/

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

const char* ssid = "SSID";
const char* password = "Password";
const char* mqtt_server = "192.168.x.xxx";

WiFiClient espClient;
PubSubClient client(espClient);

int relayPin = D5;
int lightControllPin = D2;
long previousMillis = 0;
int relayState = 1;
int switchState = 0;
int switchFlag = 0;


void setup_wifi() 
{
  delay(100);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
  }
  randomSeed(micros());
}

void reconnect() 
{
  while (!client.connected()) 
  {
    String clientId = "ESP8266-AlternativeSwitch";
    
    if (client.connect(clientId.c_str()))
    {
      client.subscribe("Light_topic");
    } 
    else 
    {
      delay(6000);
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length) 
{
  payload[length] = '\0';
  String strTopic = String(topic);
  String strPayload = String((char * ) payload);
  
  if(strTopic == "Light_topic" && switchFlag == 0) 
  {
    if(strPayload == "false") 
    {
      if(digitalRead(lightControllPin) == LOW)
      {
        if(relayState == 1)
        {
          digitalWrite(relayPin, LOW);
          relayState = 0;
        }
        else
        {
          digitalWrite(relayPin, HIGH);
          relayState = 1;
        }
      }
    }
    
    if(strPayload == "true") 
    {
      if(digitalRead(lightControllPin) == HIGH)
      {
        if(relayState == 1)
        {
          digitalWrite(relayPin, LOW);
          relayState = 0;
        }
        else
        {
          digitalWrite(relayPin, HIGH);
          relayState = 1;
        }
      }
    }
  }
}

void setup() 
{
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  
  pinMode(relayPin, OUTPUT);
  pinMode(lightControllPin, INPUT);
  digitalWrite(relayPin, HIGH);
}

void loop() 
{
  unsigned long Millis = millis();
  if (Millis - previousMillis >= 1000) 
  {
    previousMillis = Millis;
    switchFlag = 0;
  }
  
  if (!client.connected()) 
  {
    reconnect();
  }
  client.loop();
 
  if(digitalRead(lightControllPin) == LOW) 
  {
    if(switchState == 0)
    {
      client.publish("Light_topic", "true");
      switchState = 1;
      switchFlag = 1;
    } 
  }
  
  if(digitalRead(lightControllPin) == HIGH) 
  {
    if(switchState == 1)
    {
      client.publish("Light_topic", "false");
      switchState = 0;
      switchFlag = 1;
    }
  }
}

Credits

MyHomeThings

MyHomeThings

11 projects • 6 followers

Comments