Showing posts with label hassbian. Show all posts
Showing posts with label hassbian. Show all posts

ESP 8266 MQTT Switch for Home Assistant V2.0

   
 /* ################################################################  
   ################################################################  
   ########## ESP 8266 MQTT Switch for Home Assistant V2.0 #######  
   ################################################################  
   ###################*** MOSQUITTO ***############################  
 */  
   
   // UPDATED: SEP 5 2018
   // CHANGE SwitchReset and SwitchResetConfirm to SwitchOta and SwitchOtaConfirm in the .yaml file
 //########################################################################################  
   
 // You Can find this sketch on Home Assistant forum i have just modified it according to my  
 // Requirements as it is just a waste of time trying to re-invent the wheel  
 // so thank you for it random citizen :D  
   
 //** INCLUDE LIBRARIES ** make sure you have all the Libraries installed  
 // Get them from within the Arduino IDE Sketch > Include Libraries > Manage Libraries  
   
 //########################################################################################  
 // Update libraries from library Manager and Boards from Boards Manager to latest  
   
   
 #include <PubSubClient.h>  
 #include <ESP8266WiFi.h>  
 #include <ArduinoOTA.h>  
 #include <ESP8266mDNS.h>  
 #include <WiFiUdp.h>  
   
   
   
 //##################################################################  
   
   
 void callback(char* topic, byte* payload, unsigned int length);  
   
   
   
 #define MQTT_SERVER "192.168.1.6"   // IP of your MQTT Server    
   
   
   
 const char* ssid = "Internet";  // SSID for you Wifi Hotspot  
 const char* password = "12345678qwerty";     // Password for Wifi Hotspot  
   
   
 //########### ESP 8266 GPIO Pins ###########################################  
   
   
 const int switchPin1 = 4;  
 const int switchPin2 = 5;  
 const int switchPin3 = 13;  
 const int switchPin4 = 12;  
   
 unsigned long secs = 0;  
   
 //#########################################################################  
   
   
 // ***** MQTT Topics Defined In Home Assistant's Configuration.yaml file ***  
   
   
 //#########################################################################  
   
 char const* switchTopic1 = "/house/switch1/";   //four switches for relays  
   
 char const* switchTopic2 = "/house/switch2/";  
   
 char const* switchTopic3 = "/house/switch3/";  
   
 char const* switchTopic4 = "/house/switch4/";  
   
   
   
 char const* switchSleep = "/house/switchSleep/"; // fifth for putting the Esp in sleep mode  
   
 char const* switchOta = "/house/switchOta/"; // sixth for updating the Esp  // port 8266 //check firewall 
   
 //##########################################################################  
   
   
   
 WiFiClient wifiClient;  
   
   
 PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);  
   
   
   
   
   
   
 //#################################################################################  
 //#################################################################################  
   
 //############## SETUP BEGINS #####################################################  
   
 //#################################################################################  
 //#################################################################################  
   
   
   
 void setup()  
 {  
  digitalWrite(switchPin1, LOW);  
   
  digitalWrite(switchPin2, LOW);  
   
  digitalWrite(switchPin3, LOW);  
   
  digitalWrite(switchPin4, LOW);  
   
  // These lines will keep the Relays off during start-up  
   
  // Because i'm Using NPN transistors for  
   
  // switching the relays which are actually  
   
  // LOW-level-trigger type  
   
  // adding the transistor will invert that logic  
   
  // and LOW will be OFF because Transistor is off  
   
   
   
   
  pinMode(switchPin1, OUTPUT);  // Relay Switch 1  
   
  pinMode(switchPin2, OUTPUT);  // Relay Switch 2  
   
  pinMode(switchPin3, OUTPUT);  // Relay Switch 3  
   
  pinMode(switchPin4, OUTPUT);  // Relay Switch 4  
   
   
   
  //####################################################################################  
   
   
   
   
  Serial.begin(115200);  
  delay(100);  
   
   
   
   
   
   
  WiFi.mode(WIFI_STA);    // Set Esp8266 in Station mode  
   
  WiFi.begin(ssid, password);  
   
   
  secs = millis() / 1000 ;  
   
  while (WiFi.waitForConnectResult() != WL_CONNECTED)  
  {  
   
   Serial.println("Waiting for WiFi Connection ...");  
   
   delay(2000);  
   
   /*if (millis() / 1000 - secs > 60)  
   {  
    Serial.println("Going to Sleep for 10 secs ...");  
   
    ESP.deepSleep(10 * 1000000);  
   }  */
  }  
   
   
   
   
  //############################################################################################  
  //############################################################################################  
   
  //############# CHANGE OTA UPDATE PASSWORD ###################################################  
  //############################################################################################  
  //############################################################################################  
   
  ArduinoOTA.setPassword((const char *)"123"); // Password for OTA Update "123"  
  ArduinoOTA.begin();  
   
   
   
   
 }  
   
 //##########################################################################################  
   
 //############ SETUP END ###################################################################  
   
 //##########################################################################################  
   
   
 bool ota_update = false ; // Don't change this manually  
   
   
   
   
   
 //#########################################################################################  
   
 //######### LOOP BEGINS ##################################################################  
   
 //#########################################################################################  
   
   
 void loop()  
 {  
   
  if (!client.connected() && WiFi.status() == 3)  
   
  {  
   
   reconnect(); // Reconnect if connection is lost  
   
  }  
   
   
  client.loop(); // Maintain MQTT connection  
  if (ota_update)    
  {     secs = millis() / 1000 ;  }
   
  delay(10);   // MUST delay to allow ESP8266 WIFI functions to run  
  OTA();  
   
 }  
   
   
 //#########################################################################################  
 //################# WAIT FOR OTA UPDATE ON EVERY START UP #################  
 //#########################################################################################  
 void OTA()  
 {  
  if (ota_update)    
  {  
   Serial.println("Waiting for Ota update");  
   
   while (millis() / 1000 - secs < 20 )  
   {  
    ArduinoOTA.handle() ;  
    delay(10);  
   }  
  }  
 }  
 //#################################################3  
   
   
   
 //#########################################################  
   
   
   
 void callback(char* topic, byte* payload, unsigned int length) {  
   
   
  String topicStr = topic;  
   
   
   
  Serial.println("Callback update.");  
  Serial.print("Topic: ");  
  Serial.println(topicStr);  
   
   
  //################## Switch 1 ################### COPY THESE BLOCKS FOR MORE SWITCHES ####################  
   
  if (topicStr == "/house/switch1/")  
  {  
   
   if (payload[0] == '1')  
   {  
   
    digitalWrite(switchPin1, HIGH);  
   
    client.publish("/house/switchConfirm1/", "1");  
    delay(1000);  
   }  
   
   
   
   else if (payload[0] == '0')  
   {  
   
    digitalWrite(switchPin1, LOW);  
   
    client.publish("/house/switchConfirm1/", "0");  
   
   }  
   
  }  
   
   
   
   
  //################## Switch 2 #####################################################  
   
   
   
   
   
   
  else if (topicStr == "/house/switch2/")  
  {  
   
   
   
   if (payload[0] == '1')  
   {  
   
    digitalWrite(switchPin2, HIGH);  
   
    client.publish("/house/switchConfirm2/", "1");  
    delay(1000);  
   
   }  
   
   
   
   else if (payload[0] == '0')  
   {  
   
    digitalWrite(switchPin2, LOW);  
   
    client.publish("/house/switchConfirm2/", "0");  
   
   }  
   
  }  
   
   
   
  //################## Switch 3 #####################################################  
   
   
   
   
   
   
  else if (topicStr == "/house/switch3/")  
  {  
   
   
   
   if (payload[0] == '1')  
   {  
   
    digitalWrite(switchPin3, HIGH);  
   
    client.publish("/house/switchConfirm3/", "1");  
   
    delay(1000);  
   
   }  
   
   
   
   else if (payload[0] == '0')  
   {  
   
    digitalWrite(switchPin3, LOW);  
   
    client.publish("/house/switchConfirm3/", "0");  
   
   }  
  }  
   
   
   
   
   
  //################## Switch 4 #####################################################  
   
   
   
   
  else if (topicStr == "/house/switch4/")  
  {  
   
   
   if (payload[0] == '1')  
   {  
    digitalWrite(switchPin4, HIGH);  
   
    client.publish("/house/switchConfirm4/", "1");  
   
    delay(1000);  
   
   }  
   
   
   
   else if (payload[0] == '0')  
   {  
    digitalWrite(switchPin4, LOW);  
   
    client.publish("/house/switchConfirm4/", "0");  
   }  
  }  
   
   
  //################## Switch 5 Sleep Mode #######################################  
   
   
  else if (topicStr == "/house/switchSleep/")  
  {  
   
   
   if (payload[0] == '1')  
   {  
   
    client.publish("/house/switchSleepConfirm/", "1");  
   
    Serial.println("going to sleep for 60 secs");  
   
    delay(1000);  
   
    ESP.deepSleep(60 * 1000000);  
   
   
   }  
   
   
   
   else if (payload[0] == '0')  
   {  
    client.publish("/house/switchSleepConfirm/", "0");  
   }  
  }  
   
   
   
   
  //################## Switch 6 ota #######################################  
   
   
  else if (topicStr == "/house/switchOta/")  
  {  
   
   
   if (payload[0] == '1')  
   {  
    client.publish("/house/switchOtaConfirm/", "1");  
   
    ota_update = true ;  
   
   }  
   
   else if (payload[0] == '0')  
   {  
    client.publish("/house/switchOtaConfirm/", "0");  
    ota_update = false ;  
   
   }  
  }  
   
   
   
   
   
   
   
   
   
   
   
 }  
   
   
 //#########################################################################################  
   
 //######### LOOP ENDS ##################################################################  
   
 //#########################################################################################  
   
   
   
   
   
   
 void reconnect() {  
   
   
   
  if (WiFi.status() != WL_CONNECTED)  
  {  
   
   Serial.print("Connecting to ..");  
   
   Serial.println(ssid);  
   
   
   
   while (WiFi.status() != WL_CONNECTED)  
   {  
   
    delay(500);  
   
    Serial.print(".");  
   
   }  
   
   
   
   Serial.println("");  
   
   Serial.println("WiFi connected");  
   
   Serial.println("IP address: ");  
   
   Serial.println(WiFi.localIP());  
  }  
   
   
   
   
   
  if (WiFi.status() == WL_CONNECTED) {  
   
   while (!client.connected()) {  
   
    Serial.print("Attempting MQTT connection...");  
   
   
   
    // Generate client name based on MAC address and last 8 bits of microsecond counter  
    String clientName;  
    clientName += "esp8266-";  
    uint8_t mac[6];  
    WiFi.macAddress(mac);  
    clientName += macToStr(mac);  
   
   
    //#########################################################################################  
   
    //######### CHANGE MQTT USER NAME AND PASSWORD BELOW ####################################  
   
    //#########################################################################################  
   
   
   
   
    if (client.connect((char*) clientName.c_str(), "newuser", "12345")) {  
   
     Serial.print("\tMQTT Connected");  
   
   
   
     client.subscribe(switchSleep);  
   
   
     client.subscribe(switchOta);    // the order in which topics are arranged here  
   
   
     // is how ESP will Subscribe and if you put  
   
     // relays first and also turn them on and ota or sleep too  
   
     // the switches will turn on momentarily untill it gets the message to sleep or ota update  
   
     client.subscribe(switchTopic1);  
   
     client.subscribe(switchTopic2);  
   
     client.subscribe(switchTopic3);  
   
     client.subscribe(switchTopic4);  
   
     // ##**************************######## Add MORE TOPICS FOR SWITCHES HERE  #####################  
   
   
    }  
   
   
    else  
    {  
     Serial.println("\tFailed.");  
   
     delay(2000);  
   
     break;  
   
    }  
   
   }  
   
  }  
   
 }  
   
   
   
   
   
 String macToStr(const uint8_t* mac)  
 {  
   
  String result;  
   
  for (int i = 0; i < 6; ++i) {  
   result += String(mac[i], 16);  
   
   if (i < 5) {  
    result += ':';  
   }  
  }  
   
  return result;  
 }  

Home Assistant Basic Sun Automations Sunrise / Sunset Mqtt Lights On / Off

##############################################
## First the Switches in Configuration.yaml file #####
##############################################


###############################################################
# MOSQUITTO MQTT ##########################################
###############################################################

mqtt:
  broker: localhost
  port: 1883
  client_id: home-assistant-1
  keepalive: 60
  username: newuser
  password: 12345
  protocol: 3.1

###############################################################
# SWITCHES ##################################################
###############################################################


switch 1:
  - platform: mqtt
    name: "Wall Light"
    state_topic: "/house/switchConfirm1/"
    command_topic: "/house/switch1/"
    payload_on: "1"
    payload_off: "0"
    qos: 0
    retain: true
#####################

switch 2:
  - platform: mqtt
    name: "Garage Light One"
    state_topic: "/house/switchConfirm2/"
    command_topic: "/house/switch2/"
    payload_on: "1"
    payload_off: "0"
    qos: 0
    retain: true
#####################

switch 3:
  - platform: mqtt
    name: "Garage Light Two"
    state_topic: "/house/switchConfirm3/"
    command_topic: "/house/switch3/"
    payload_on: "1"
    payload_off: "0"
    qos: 0
    retain: true
#####################

switch 4:
  - platform: mqtt
    name: "Garage Light Three"
    state_topic: "/house/switchConfirm4/"
    command_topic: "/house/switch4/"
    payload_on: "1"
    payload_off: "0"
    qos: 0
    retain: true


####################

switch 5:
  - platform: mqtt
    name: "Switch Sleep"
    state_topic: "/house/switchSleepConfirm/"
    command_topic: "/house/switchSleep/"
    payload_on: "1"
    payload_off: "0"
    qos: 0
    retain: true
####################

switch 6:
  - platform: mqtt
    name: "Switch Reset"
    state_topic: "/house/switchResetConfirm/"
    command_topic: "/house/switchReset/"
    payload_on: "1"
    payload_off: "0"
    qos: 0
    retain: true
######################


##########################################################################
##########################################################################


# NOTE : when using "switch " like above the Service "switch.turn_on" will work
# but if you use " light " the "light.turn_on" will work with it 
# for example : if mqtt switches are defined as "light" below:





################################################################
# LIGHTS ######################################################
###############################################################
#light 1:
# - platform: mqtt
#  name: "Light 01"
#    state_topic: "/house/switchConfirm5/"
#    command_topic: "/house/switch5/"
#    payload_on: "1"
#    payload_off: "0"
#    qos: 0
#    retain: true
#####################


####################################################################
# in the " light 1 " example above the  name: "Light 01 " can be changed but 
# the top light 1 is important here as it will change the service name too 
# see at the end of this post 
####################################################################








###########################################
## This is my Automations.yaml file right now ##
###########################################



#############################################################
## TURN ON OUTSIDE LIGHTS AT SUN-SET ###################
############################################################################


## Copy it so that the minus sign before " id:" is on extreme left the rest should be as it is 
## for example the letter "a" in alias should be beneath the letter " i" in id 

############################################################################

- id: auto_one
  alias: 'Turn On Outside Lights at Sunset'
  trigger:
  - platform: sun
    event: sunset
    offset: '+00:17:00'
  action:
  - service: switch.turn_on
    entity_id: switch.garage_light_one
  - delay: 00:02:00
  - service: switch.turn_on
    entity_id: switch.garage_light_two
  - delay: 00:02:00
  - service: switch.turn_on
    entity_id: switch.garage_light_three
  - delay: 00:03:00
  - service: switch.turn_on
    entity_id: switch.wall_light

#############################################################
## TURN OFF OUTSIDE LIGHTS AT SUN-RISE #################
#############################################################

############################################################################
## Note that the dash/minus sign " - " before id is on extreme left with NO spaces from left
############################################################################

- id: auto_two
  alias: 'Turn Off Outside Lights at Sunrise'
  trigger:
  - platform: sun
    event: sunrise
    offset: '-00:25:00'
  action:
  - service: switch.turn_off
    entity_id: switch.wall_light
  - delay: 00:05:00
  - service: switch.turn_off
    entity_id: switch.garage_light_one
  - delay: 00:04:00
  - service: switch.turn_off
    entity_id: switch.garage_light_two
  - delay: 00:03:00
  - service: switch.turn_off
    entity_id: switch.garage_light_three


###########################################################################

###########################################################################
#############################################################
## TURN ON LIGHTS AT SUNSET ###           Using light            #####
#############################################################



#- id: auto
#  alias: 'Turn On Lights'
#  trigger:
#  - platform: sun
#    event: sunset
#    offset: '-02:44:00'
#  action:
#  - service: light.turn_on
#    entity_id: light.light_01
#  - delay: 00:00:05
#  - service: light.turn_on
#    entity_id: light.light_02
#  - delay: 00:00:05
#  - service: light.turn_on
#    entity_id: light.light_03
#  - delay: 00:00:05
#  - service: light.turn_on
#    entity_id: light.light_04


Home Assistant / Hassbian MQTT Switches Using ESP8266 / Arduino IDE v1.0



   
 /* ################################################################  
    ################################################################  
    ########## ESP 8266 MQTT Switch for Home Assistant #############  
    ################################################################  
    ###################*** MOSQUITTO ***############################  
 */  
   
   
   
 //########################################################################################  
   
  // You Can find this sketch on Home Assistant forum i have just modified it according to my 
  // Requirements as it is just a waste of time trying to re-invent the wheel 
  // so thank you for it random citizen :D  
  
  //** INCLUDE LIBRARIES ** make sure you have all the Libraries installed   
  // Get them from within the Arduino IDE Sketch > Include Libraries > Manage Libraries   
   
 //########################################################################################   
   
   
   
 #include <PubSubClient.h>  
 #include <ESP8266WiFi.h>  
 #include <ArduinoOTA.h>  
 #include <ESP8266mDNS.h>  
 #include <WiFiUdp.h>  
   
   
   
 //##################################################################  
   
   
 void callback(char* topic, byte* payload, unsigned int length);  
   
   
   
 #define MQTT_SERVER "192.168.1.3"      // IP of your MQTT Server   
   
   
 const char* ssid = "cool-spot";        // SSID for you Wifi Hotspot  
 const char* password = "12345678";     // Password for Wifi Hotspot   
   
   
 //########### ESP 8266 GPIO Pins ###########################################  
   
   
 const int switchPin1 = 4;  
 const int switchPin2 = 5;  
 const int switchPin3 = 13;  
 const int switchPin4 = 12;  
   
   
 //#########################################################################  
         
         
 // ***** MQTT Topics Defined In Home Assistant's Configuration.yaml file ***  
         
   
 //#########################################################################  
   
 char const* switchTopic1 = "/house/switch1/";      //four switches for relays  
   
 char const* switchTopic2 = "/house/switch2/";  
   
 char const* switchTopic3 = "/house/switch3/";  
   
 char const* switchTopic4 = "/house/switch4/";  
   
   
   
 char const* switchSleep  = "/house/switchSleep/";  // fifth for putting the Esp in sleep mode   
   
 char const* switchReset  = "/house/switchReset/";  // sixth for Reseting the Esp  
   
 //##########################################################################  
   
   
   
 WiFiClient wifiClient;  
   
   
 PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);  
   
   
   
   
   
   
 //#################################################################################  
 //#################################################################################  
   
 //############## SETUP BEGINS #####################################################  
   
 //#################################################################################  
 //#################################################################################  
   
   
   
 void setup() {  
   
        
                                  // These lines will keep the Relays off during start-up    
    
  pinMode(switchPin1, OUTPUT);    // Relay Switch 1    
    
  digitalWrite(switchPin1, LOW);  // Because i'm Using NPN transistors for   
   
                                  // switching the relays which are actually   
                     
                                  // LOW-level-trigger type   
   
                                  // adding the transistor will invert that logic  
   
                                  // and LOW will be OFF because Transistor is off    
                         
    
    
  pinMode(switchPin2, OUTPUT);   // Relay Switch 2  
    
  digitalWrite(switchPin2, LOW);  
   
   
   
  pinMode(switchPin3, OUTPUT);   // Relay Switch 3  
   
  digitalWrite(switchPin3, LOW);  
   
   
   
  pinMode(switchPin4, OUTPUT);   // Relay Switch 4  
   
  digitalWrite(switchPin4, LOW);  
   
   
  //####################################################################################  
   
    
    
    
  Serial.begin(115200);  
  delay(100);  
    
    
   
    
    
    
  WiFi.mode(WIFI_STA);        // Set Esp8266 in Station mode  
   
  WiFi.begin(ssid, password);       
   
    
    
    
    
   while (WiFi.waitForConnectResult() != WL_CONNECTED) {  
    
   Serial.println("Connection Failed! Rebooting...");  
     
   delay(2000);  
          

  }  
   
 //############################################################################################  
 //############################################################################################  
   
 //############# CHANGE OTA UPDATE PASSWORD ###################################################    
 //############################################################################################  
 //############################################################################################  
   
  ArduinoOTA.setPassword((const char *)"123"); // Password for OTA Update "123"  
  ArduinoOTA.begin();                          // on every reset it waits for 60 secs   
                                               // During that time it will accept OTA updates  
  Serial.println("Ready");                     // Turn Firewall off on the PC when trying to   
                                               // Over The Air update sketch  
    
  Serial.print("IP address: ");  
  Serial.println(WiFi.localIP());  
   
   
   
 }  
   
 //##########################################################################################  
   
 //############ SETUP END ###################################################################  
   
 //##########################################################################################   
   
   
  bool ota_update = true ;  // Don't change this manually   
   
   
   
   
   
 //#########################################################################################  
   
 //######### LOOP BEGINS ##################################################################  
   
 //#########################################################################################  
   
   
 void loop()  {  
   
    
    
    
    
   
 //################# WAIT FOR OTA UPDATE ON EVERY START UP #################  
    
   
    
  if(ota_update)                 // on Every Reset ota update boolean will be set to TRUE   
     
  {  
     
     
     
   while(millis()/1000 < 15 ) // then it will count to 60 seconds *******WHEN Reset Switch is Turned ON **********  
                          // it will constantly Reboot and wait for 60 Secs and then after Subscribing Reboots   
   {                      // again  
   
      
     
    ArduinoOTA.handle() ;  // During that time update ota can be done  
      
      
    Serial.println(millis()/1000);  
   
      
    delay(10);  
      
      
   }  
     
     
   Serial.println("Time up");  
   
   ota_update = false ;  // then after time up it will be set to false  
                         // and the rest of the code runs for that instance   
                         // it will happen if the reset switch is then turned off 
     
     
   }  
    
    
    
    
    
 //#########################################################  
    
    
    
    
    
    
    
  if (!client.connected() && WiFi.status() == 3)   
     
   {  
     
    reconnect(); // Reconnect if connection is lost  
      
   }   
   
    
    
    
    
    
  client.loop();  // Maintain MQTT connection  
   
   
  delay(10);      // MUST delay to allow ESP8266 WIFI functions to run  
   
   
 }  
   
   
   
   
   
 void callback(char* topic, byte* payload, unsigned int length) {  
   
    
 String topicStr = topic;   
    
   
    
  Serial.println("Callback update.");  
  Serial.print("Topic: ");  
  Serial.println(topicStr);  
   
    
  //################## Switch 1 ################### COPY THESE BLOCKS FOR MORE SWITCHES ####################   
     
   if (topicStr == "/house/switch1/")   
   {  
   
     
      
    if(payload[0] == '1'){  
      
     digitalWrite(switchPin1, HIGH);  
           
     client.publish("/house/switchConfirm1/", "1");  
     delay(700);  
     }  
   
     
      
    else if (payload[0] == '0'){  
      
     digitalWrite(switchPin1, LOW);  
       
     client.publish("/house/switchConfirm1/", "0");  
       
     }  
      
    }  
      
   
     
      
  //################## Switch 2 #####################################################   
      
      
      
      
      
      
    else if (topicStr == "/house/switch2/")   
    {  
      
     
      
    if(payload[0] == '1'){  
      
     digitalWrite(switchPin2, HIGH);  
       
     client.publish("/house/switchConfirm2/", "1");  
     delay(700);  
      
     }  
       
   
     
     else if (payload[0] == '0'){  
      
     digitalWrite(switchPin2, LOW);  
       
     client.publish("/house/switchConfirm2/", "0");  
       
     }  
       
    }  
      
      
      
  //################## Switch 3 #####################################################   
   
      
      
      
      
      
    else if (topicStr == "/house/switch3/")   
    {  
      
      
      
    if(payload[0] == '1'){  
      
     digitalWrite(switchPin3, HIGH);  
       
     client.publish("/house/switchConfirm3/", "1");  
     delay(700);  
       
     }  
   
      
      
    else if (payload[0] == '0'){  
       
     digitalWrite(switchPin3, LOW);  
       
     client.publish("/house/switchConfirm3/", "0");  
       
     }  
    }  
      
      
      
      
      
 //################## Switch 4 #####################################################   
   
      
      
      
    else if (topicStr == "/house/switch4/")   
    {  
      
      
    if(payload[0] == '1'){  
      
     digitalWrite(switchPin4, HIGH);  
       
     client.publish("/house/switchConfirm4/", "1");  
     delay(700);  
       
     }  
   
      
      
    else if (payload[0] == '0'){  
      
    digitalWrite(switchPin4, LOW);  
      
     client.publish("/house/switchConfirm4/", "0");  
     }  
    }  
   
   
   
   
    
    
    
 //################## Switch 5 Sleep Mode #######################################   
     
   
    
    
    
    
    
  else if (topicStr == "/house/switchSleep/")   
    {  
      
    
    if(payload[0] == '1'){  
   
     client.publish("/house/switchSleepConfirm/", "1");  
   
     Serial.println("going to sleep for 10 secs");  
   
       
     delay(2000);  
          ESP.deepSleep(60*1000000);  
     }  
   
    
      
    else if (payload[0] == '0'){  
      
       
     client.publish("/house/switchSleepConfirm/", "0");  
     }  
    }  
   
   
   
   
 //################## Switch 6 Reset #######################################   
   
   
  else if (topicStr == "/house/switchReset/")   
    {  
      
    
    if(payload[0] == '1'){  
   
     client.publish("/house/switchResetConfirm/", "1");  
   
     Serial.println("going to Reset ");  
   
       
     delay(2000);  
     ESP.deepSleep(5*1000000);  
  
     }  
   
    
      
    else if (payload[0] == '0'){  
      
       
     client.publish("/house/switchResetConfirm/", "0");  
     }  
    }  
   
   
   
   
   
   
   
   
   
   
      
 }  
   
   
 //#########################################################################################  
   
 //######### LOOP ENDS ##################################################################  
   
 //#########################################################################################  
   
   
   
   
   
   
 void reconnect() {  
   
    
    
  if(WiFi.status() != WL_CONNECTED){  
     
   Serial.print("Connecting to ");  
    
   Serial.println(ssid);  
   
     
     
   while (WiFi.status() != WL_CONNECTED) {  
     
    delay(500);  
      
    Serial.print(".");  
     
   }  
   
     
     
   Serial.println("");  
     
   Serial.println("WiFi connected");   
     
   Serial.println("IP address: ");  
     
   Serial.println(WiFi.localIP());  
  }  
   
    
    
    
    
  if(WiFi.status() == WL_CONNECTED){  
    
   while (!client.connected()) {  
    
    Serial.print("Attempting MQTT connection...");  
   
      
      
    // Generate client name based on MAC address and last 8 bits of microsecond counter  
    String clientName;  
    clientName += "esp8266-";  
    uint8_t mac[6];  
    WiFi.macAddress(mac);  
    clientName += macToStr(mac);  
   
       
 //#########################################################################################  
   
 //######### CHANGE MQTT USER NAME AND PASSWORD BELOW ####################################  
   
 //#########################################################################################  
    
      
      
      
    if (client.connect((char*) clientName.c_str(),"newuser", "12345")) {    
      
     Serial.print("\tMQTT Connected");  
       
       
       
       
     client.subscribe(switchReset);        // the order in which topics are arranged here  
   
       
     client.subscribe(switchSleep);        // is how ESP will Subscribe and if you put   
   
                            // relays first and also turn them on and reset or sleep too  
       
                            // the switches will turn on momentarily untill it gets the message to sleep or reset   
       
     client.subscribe(switchTopic1);       // so keep them on the top and then even if the Relay switches are turned on from the page   
       
     client.subscribe(switchTopic2);       // the switch will reset or sleep even before subscribing to the later topics in the list   
       
     client.subscribe(switchTopic3);  
       
     client.subscribe(switchTopic4);  
   
     // ##**************************########  Add MORE TOPICS FOR  SWITCHES HERE   #####################  
       
       
    }  
   
      
    else{Serial.println("\tFailed.");  
      
    delay(2000);  
   //  ESP.deepSleep(5*1000000); 
break;}  
     
   }  
    
  }  
   
 }  
   
   
   
   
   
 String macToStr(const uint8_t* mac){  
   
  String result;  
   
  for (int i = 0; i < 6; ++i) {  
   result += String(mac[i], 16);  
   
   if (i < 5){  
    result += ':';  
   }  
  }  
   
  return result;  
 }