Please help to finish esp8266 based wall switch what im just printed

Hello awesome community. Im wanna make a prototype and swape all wall switches im my home.

It contains:
ESP8266 LoLin v3
DHT 22
5V Relay x2
12mm button x2

My problem is what im do not how to sync physical buttons on wall switch, and im very newbie in programming.
Can someone review my code and recommend me anything?
Thanks for all.

1 Like
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <Bounce2.h>

#define MQTT_VERSION MQTT_VERSION_3_1_1
#define DHTTYPE DHT22
#define DHTPIN 14

// Wifi: SSID and password
const char* WIFI_SSID = "xxxx";
const char* WIFI_PASSWORD = "xxxx";

// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "smart_switch_V1_01";
const PROGMEM char* MQTT_SERVER_IP = "xxxx";
const PROGMEM uint16_t MQTT_SERVER_PORT = 11312;
const PROGMEM char* MQTT_USER = "xxxx";
const PROGMEM char* MQTT_PASSWORD = "xxxx";

// MQTT: topics
const char* MQTT_LIVINGROOM_TEMPERATURE1_STATE_TOPIC = "home/livingroom/smartsensor1/temperature";
const char* MQTT_LIVINGROOM_HUMIDITY1_STATE_TOPIC = "home/livingroom/smartsensor1/humidity";
const char* MQTT_LIVINGROOM_LIGHT1_STATE_TOPIC = "home/livingroom/smartlight1/status";
const char* MQTT_LIVINGROOM_LIGHT1_COMMAND_TOPIC = "home/livingroom/smartlight1/switch";
const char* MQTT_LIVINGROOM_LIGHT2_STATE_TOPIC = "home/livingroom/smartlight2/status";
const char* MQTT_LIVINGROOM_LIGHT2_COMMAND_TOPIC = "home/livingroom/smartlight2/switch";


// payloads by default (on/off)
const char* LIGHT_ON = "ON";
const char* LIGHT_OFF = "OFF";

const int ledPin1 = 15;
const int ledPin2 = 12;
const int btnPin1 = 0;
const int btnPin2 = 4;

boolean livingroom1_smartlight_state = false; // light is turned off by default
boolean livingroom2_smartlight_state = false; // light is turned off by default

WiFiClient wifiClient;
PubSubClient client(wifiClient);
DHT dht(DHTPIN, DHTTYPE, 22); // 11 works fine for ESP8266

// function called to publish the state of the light (on/off)
void publishLightState() {
  if (livingroom1_smartlight_state) {
client.publish(MQTT_LIVINGROOM_LIGHT1_STATE_TOPIC, LIGHT_ON, true);
  } else {
client.publish(MQTT_LIVINGROOM_LIGHT1_STATE_TOPIC, LIGHT_OFF, true);
  }
  if (livingroom2_smartlight_state) {
client.publish(MQTT_LIVINGROOM_LIGHT2_STATE_TOPIC, LIGHT_ON, true);
  } else {
client.publish(MQTT_LIVINGROOM_LIGHT2_STATE_TOPIC, LIGHT_OFF, true);
  }
}

// function called to turn on/off the light
void setLightState() {
  if (livingroom1_smartlight_state) {
digitalWrite(ledPin1, HIGH);
Serial.println("INFO: Turn light on...");
  } else {
digitalWrite(ledPin1, LOW);
Serial.println("INFO: Turn light off...");
  }
  if (livingroom2_smartlight_state) {
digitalWrite(ledPin2, HIGH);
Serial.println("INFO: Turn light on...");
  } else {
digitalWrite(ledPin2, LOW);
Serial.println("INFO: Turn light off...");
  }
}

// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
  // concat the payload into a string
  String payload;
  for (uint8_t i = 0; i < p_length; i++) {
payload.concat((char)p_payload[i]);
  }
  
  // handle message topic
  if (String(MQTT_LIVINGROOM_LIGHT1_COMMAND_TOPIC).equals(p_topic)) {
// test if the payload is equal to "ON" or "OFF"
if (payload.equals(String(LIGHT_ON))) {
  if (livingroom1_smartlight_state != true) {
    livingroom1_smartlight_state = true;
    setLightState();
    publishLightState();
  }
} else if (payload.equals(String(LIGHT_OFF))) {
  if (livingroom1_smartlight_state != false) {
    livingroom1_smartlight_state = false;
    setLightState();
    publishLightState();
  }
}
  }
  if (String(MQTT_LIVINGROOM_LIGHT2_COMMAND_TOPIC).equals(p_topic)) {
// test if the payload is equal to "ON" or "OFF"
if (payload.equals(String(LIGHT_ON))) {
  if (livingroom2_smartlight_state != true) {
    livingroom2_smartlight_state = true;
    setLightState();
    publishLightState();
  }
} else if (payload.equals(String(LIGHT_OFF))) {
  if (livingroom2_smartlight_state != false) {
    livingroom2_smartlight_state = false;
    setLightState();
    publishLightState();
  }
}
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
Serial.print("INFO: Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
  Serial.println("INFO: connected");
  // Once connected, publish an announcement...
  publishLightState();
  // ... and resubscribe
  client.subscribe(MQTT_LIVINGROOM_LIGHT1_COMMAND_TOPIC);
  client.subscribe(MQTT_LIVINGROOM_LIGHT2_COMMAND_TOPIC);
} else {
  Serial.print("ERROR: failed, rc=");
  Serial.print(client.state());
  Serial.println("DEBUG: try again in 5 seconds");
  // Wait 5 seconds before retrying
}
  }
}


bool checkBound(float newValue, float prevValue, float maxDiff) {
  return !isnan(newValue) &&
     (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
}

long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 0.1;

void setup() {
  // init the serial
  Serial.begin(115200);

  // init the led
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  analogWriteRange(255);
  setLightState();

  // init the WiFi connection
  Serial.println();
  Serial.println();
  Serial.print("INFO: Connecting to ");
  Serial.println(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
  }

  Serial.println("");
  Serial.println("INFO: WiFi connected");
  Serial.print("INFO: IP address: ");
  Serial.println(WiFi.localIP());

  // init the MQTT connection
  client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
reconnect();
  }
  client.loop();
long now = millis();
  if (now - lastMsg > 1000) {
lastMsg = now;

float newTemp = dht.readTemperature();
float newHum = dht.readHumidity();

if (checkBound(newTemp, temp, diff)) {
  temp = newTemp;
  Serial.print("New temperature:");
  Serial.println(String(temp).c_str());
  client.publish(MQTT_LIVINGROOM_TEMPERATURE1_STATE_TOPIC, String(temp).c_str(), true);
}

if (checkBound(newHum, hum, diff)) {
  hum = newHum;
  Serial.print("New humidity:");
  Serial.println(String(hum).c_str());
  client.publish(MQTT_LIVINGROOM_HUMIDITY1_STATE_TOPIC, String(hum).c_str(), true);
}
}
}