I have 4 relays installed in my bedroom with an esp8266 microcontroller. I have programmed it with Arduino using rcswitch and Espalexa library… its been an year everything was working absolutely fine.
recently I saw some videos about Home Assitant Mqtt and wanted to give it a try…so I installed a Home Assistant and also installed Mqtt Add-On…I then Programmed my Esp8266 with Ardunio using PUBSUBClient library. here my problem starts all of a sudden my light switches off/on by it self even if I did not given any command. here is my mqtt configuration and ardunio code
switch:
- unique_id: cool_light
name: "Cool Light"
state_topic: "home/bedroom/cool_state"
command_topic: "home/bedroom/cool"
payload_on: "ON"
payload_off: "OFF"
state_on: "ON"
state_off: "OFF"
qos: 0
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h> // For OTA with ESP8266
#include <WiFiUdp.h> // For OTA
#include <ArduinoOTA.h> // For OTA
#include <RCSwitch.h>
#include <EEPROM.h>
#include <Espalexa.h>
#include <PubSubClient.h>
BlynkTimer timer;
RCSwitch mySwitch = RCSwitch();
#define EEPROM_SIZE 512
Espalexa espalexa;
void firstLightChanged(uint8_t brightness);
void secondLightChanged(uint8_t brightness);
void thirdLightChanged(uint8_t brightness);
void fourthLightChanged(uint8_t brightness);
String Device_1_Name = "cool light";
String Device_2_Name = "warm light";
String Device_3_Name = "fan";
String Device_4_Name = "dressing light";
unsigned long previousTime = 0;
const unsigned long eventInterval = 300;
const unsigned long reconnectInterval = 5000;
int relay1State = LOW;
int pushButton1State = HIGH;
int relay2State = LOW;
int pushButton2State = HIGH;
int relay3State = LOW;
int pushButton3State = HIGH;
int relay4State = LOW;
int pushButton4State = HIGH;
int relay5State;
#define BLYNK_TEMPLATE_ID "**********"
#define BLYNK_DEVICE_NAME "Dressing light"
#define BLYNK_AUTH_TOKEN "******************"
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "mywifi";
char pass[] = "mypass";
char* mqttServer = "home-assistant IP";
const char* mqttUserName = "home-assistant user"; // MQTT username
const char* mqttPwd = "password"; // MQTT password
const char* clientID = "Esp"; // client id
char auth[] = BLYNK_AUTH_TOKEN;
//#define SERVER "blynk-cloud.com " // Comment-out if use Blynk hosted cloud service
//#define PORT 8442
#define RELAY_PIN_1 4
#define RELAY_PIN_2 14
#define RELAY_PIN_3 12
#define RELAY_PIN_4 13
//#define PUSH_BUTTON_1 32
//#define PUSH_BUTTON_2 35
//#define PUSH_BUTTON_3 34
//#define PUSH_BUTTON_4 39
#define VPIN_BUTTON_1 V11 //warmlight
#define VPIN_BUTTON_2 V10 //fan
#define VPIN_BUTTON_3 V7 //cool-light
#define VPIN_BUTTON_4 V9 //dressing light
#define VPIN_BUTTON_5 V8 //rest
#define sub2 "home/bedroom/fan"
#define sub3 "home/bedroom/cool"
#define sub4 "home/bedroom/dressing"
#define sub1 "home/bedroom/warm"
#define pub2 "home/bedroom/fan_state"
#define pub3 "home/bedroom/cool_state"
#define pub4 "home/bedroom/dressing_state"
#define pub1 "home/bedroom/warm_state"
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (80)
char msg[MSG_BUFFER_SIZE];
int value = 0;
#define OTA_HOSTNAME "cupboard_side_alexa_feedback_v7.4"
void reconnect() {
while (!client.connected()) {
if (client.connect(clientID, mqttUserName, mqttPwd)) {
Serial.println("MQTT connected");
// ... and resubscribe
client.subscribe(sub1);
client.subscribe(sub2);
client.subscribe(sub3);
client.subscribe(sub4);
if (relay1State == LOW) {
digitalWrite(RELAY_PIN_1, relay1State);
//Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
EEPROM.write(0, relay1State);
Serial.println("Device2 ON");
client.publish(pub1, "ON", true);
} else if (relay1State == HIGH) {
digitalWrite(RELAY_PIN_1, relay1State);
//Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
EEPROM.write(0, relay1State);
Serial.println("Device2 OFF");
client.publish(pub1, "OFF", true);
}
if (relay2State == LOW) {
digitalWrite(RELAY_PIN_2, relay2State);
//Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
EEPROM.write(1, relay2State);
Serial.println("Device3 ON");
client.publish(pub2, "ON", true);
} else if (relay2State == HIGH) {
digitalWrite(RELAY_PIN_2, relay2State);
//Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
EEPROM.write(1, relay2State);
Serial.println("Device3 OFF");
client.publish(pub2, "OFF", true);
}
if (relay3State == LOW) {
digitalWrite(RELAY_PIN_3, relay3State);
// Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
EEPROM.write(2, relay3State);
Serial.println("Device1 ON");
client.publish(pub3, "ON", true);
} else if (relay3State == HIGH) {
digitalWrite(RELAY_PIN_3, relay3State);
// Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
EEPROM.write(2, relay3State);
Serial.println("Device1 OFF");
client.publish(pub3, "OFF", true);
}
if (relay4State == LOW) {
digitalWrite(RELAY_PIN_4, relay4State);
//Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
EEPROM.write(3, relay4State);
Serial.println("Device4 ON");
client.publish(pub4, "ON", true);
} else if (relay4State == HIGH) {
digitalWrite(RELAY_PIN_4, relay1State);
//Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
EEPROM.write(3, relay4State);
Serial.println("Device4 OFF");
client.publish(pub2, "OFF", true);
}
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
// delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String message = "";
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
if (strcmp(topic, sub1) == 0) {
if (message == "ON") {
relay1State = LOW;
digitalWrite(RELAY_PIN_1, relay1State);
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
EEPROM.write(0, relay1State);
Serial.println("Device2 ON");
client.publish(pub1, "ON", true);
} else
{
relay1State = HIGH;
digitalWrite(RELAY_PIN_1, relay1State);
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
EEPROM.write(0, relay1State);
Serial.println("Device2 OFF");
client.publish(pub1, "OFF", true);
}
}
if (strcmp(topic, sub2) == 0) {
if (message == "ON") {
relay2State = HIGH;
digitalWrite(RELAY_PIN_2, relay2State);
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
EEPROM.write(1, relay2State);
Serial.println("Device3 ON");
client.publish(pub2, "ON", true);
} else
{
relay2State = LOW;
digitalWrite(RELAY_PIN_2, relay2State);
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
EEPROM.write(1, relay2State);
Serial.println("Device3 OFF");
client.publish(pub2, "OFF", true);
}
}
if (strcmp(topic, sub3) == 0) {
if (message == "ON") {
relay3State = LOW;
digitalWrite(RELAY_PIN_3, relay3State);
Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
EEPROM.write(2, relay3State);
Serial.println("Device1 ON");
client.publish(pub3, "ON", true);
}else
{
relay3State = HIGH;
digitalWrite(RELAY_PIN_3, relay3State);
Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
EEPROM.write(2, relay3State);
Serial.println("Device1 OFF");
client.publish(pub3, "OFF", true);
}
}
if (strcmp(topic, sub4) == 0) {
if (message == "ON") {
relay4State = LOW;
digitalWrite(RELAY_PIN_4, relay4State);
Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
EEPROM.write(3, relay4State);
Serial.println("Device4 ON");
client.publish(pub4, "ON", true);
}else
{
relay4State = HIGH;
digitalWrite(RELAY_PIN_4, relay1State);
Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
EEPROM.write(3, relay4State);
Serial.println("Device4 OFF");
client.publish(pub4, "OFF", true);
}
}
}
BLYNK_CONNECTED() {
// Request the latest state from the server
// Blynk.syncVirtual(VPIN_BUTTON_1);
// Blynk.syncVirtual(VPIN_BUTTON_2);
// Blynk.syncVirtual(VPIN_BUTTON_3);
// Blynk.syncVirtual(VPIN_BUTTON_4);
// Alternatively, you could override server state using:
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
Blynk.virtualWrite(VPIN_BUTTON_5, relay5State);
}
// When App button is pushed - switch the state
BLYNK_WRITE(VPIN_BUTTON_1) {
relay1State = param.asInt();
if (relay1State == LOW) {
client.publish(pub1, "ON");
} else if (relay1State == HIGH) {
client.publish(pub1, "OFF", true);
}
// digitalWrite(RELAY_PIN_1, relay1State);
// Serial.print("bt0.val=");
// Serial.print(digitalRead(RELAY_PIN_1));
// Serial.write(0xff);
// Serial.write(0xff);
// Serial.write(0xff);
EEPROM.write(0, relay1State);
// Blynk.setProperty(VPIN_BUTTON_1, "color", ( relay1State ? "#FF0000" : "#00FF00"));
}
BLYNK_WRITE(VPIN_BUTTON_2) {
relay2State = param.asInt();
if (relay2State == LOW) {
client.publish(pub2, "ON");
} else if (relay2State == HIGH) {
client.publish(pub2, "OFF", true);
}
// digitalWrite(RELAY_PIN_2, relay2State);
// Serial.print("bt1.val=");
// Serial.print(digitalRead(RELAY_PIN_2));
// Serial.write(0xff);
// Serial.write(0xff);
// Serial.write(0xff);
// Blynk.setProperty(VPIN_BUTTON_2, "color", ( relay2State ? "#FF0000" : "#00FF00"));
EEPROM.write(1, relay2State);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
relay3State = param.asInt();
if (relay3State == LOW) {
client.publish(pub3, "ON");
} else if (relay3State == HIGH) {
client.publish(pub3, "OFF", true);
}
// digitalWrite(RELAY_PIN_3, relay3State);
// Serial.print("bt2.val=");
// Serial.print(digitalRead(RELAY_PIN_3));
// Serial.write(0xff);
// Serial.write(0xff);
// Serial.write(0xff);
// Blynk.setProperty(VPIN_BUTTON_3, "color", ( relay3State ? "#FF0000" : "#00FF00"));
EEPROM.write(2, relay3State);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
relay4State = param.asInt();
if (relay4State == LOW) {
client.publish(pub4, "ON");
} else if (relay4State == HIGH) {
client.publish(pub4, "OFF", true);
}
// digitalWrite(RELAY_PIN_4, relay4State);
// Serial.print("bt3.val=");
// Serial.print(digitalRead(RELAY_PIN_4));
// Serial.write(0xff);
// Serial.write(0xff);
// Serial.write(0xff);
// Blynk.setProperty(VPIN_BUTTON_4, "color", ( relay4State ? "#FF0000" : "#00FF00"));
EEPROM.write(3, relay4State);
}
BLYNK_WRITE(VPIN_BUTTON_5) {
relay5State = param.asInt();
delay(500);
// Toggle Relay state
ESP.restart();
}
void rfreciever()
{
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
} else {
if (mySwitch.getReceivedValue() == 12969944) {
Serial.println("Unlock");
delay(500);
// Toggle Relay state
relay1State = !relay1State;
digitalWrite(RELAY_PIN_1, relay1State);
EEPROM.write(0, relay1State);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
// Blynk.setProperty(VPIN_BUTTON_1, "color", ( relay1State ? "#FF0000" : "#00FF00"));
if (relay1State == LOW) {
client.publish(pub1, "ON", true);
} else if (relay1State == HIGH) {
client.publish(pub1, "OFF", true);
}
}
}
}
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
} else {
if (mySwitch.getReceivedValue() == 12969948) {
Serial.println("Unlock");
delay(500);
// Toggle Relay state
relay2State = !relay2State;
digitalWrite(RELAY_PIN_2, relay2State);
EEPROM.write(1, relay2State);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
//Blynk.setProperty(VPIN_BUTTON_2, "color", ( relay2State ? "#FF0000" : "#00FF00"));
if (relay2State == LOW) {
client.publish(pub2, "ON", true);
} else if (relay2State == HIGH) {
client.publish(pub2, "OFF", true);
}
}
}
}
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
} else {
if (mySwitch.getReceivedValue() == 12969940) {
Serial.println("Unlock");
delay(500);
// Toggle Relay state
relay3State = !relay3State;
digitalWrite(RELAY_PIN_3, relay3State);
EEPROM.write(2, relay3State);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
//Blynk.setProperty(VPIN_BUTTON_3, "color", ( relay3State ? "#FF0000" : "#00FF00"));
if (relay3State == LOW) {
client.publish(pub3, "ON", true);
} else if (relay3State == HIGH) {
client.publish(pub3, "OFF", true);
}
}
}
}
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
} else {
if (mySwitch.getReceivedValue() == 12969945) {
Serial.println("Unlock");
delay(500);
// Toggle Relay state
relay4State = !relay4State;
digitalWrite(RELAY_PIN_4, relay4State);
EEPROM.write(3, relay4State);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
// Blynk.setProperty(VPIN_BUTTON_4, "color", ( relay4State ? "#FF0000" : "#00FF00"));
if (relay4State == LOW) {
client.publish(pub4, "ON", true);
} else if (relay4State == HIGH) {
client.publish(pub4, "OFF", true);
}
}
}
}
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
} else {
if (mySwitch.getReceivedValue() == 12969939) {
Serial.println("Unlock");
delay(500);
// Toggle Relay state
ESP.restart();
}
}
}
}
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
ArduinoOTA.setHostname(OTA_HOSTNAME); // For OTA - Use your own device identifying name
ArduinoOTA.begin(); // For OTA
EEPROM.begin(EEPROM_SIZE);
pinMode(RELAY_PIN_1, OUTPUT);
// pinMode(PUSH_BUTTON_1, INPUT);
relay1State = EEPROM.read(0);
digitalWrite(RELAY_PIN_1, relay1State);
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
pinMode(RELAY_PIN_2, OUTPUT);
//pinMode(PUSH_BUTTON_2, INPUT);
relay2State = EEPROM.read(1);
digitalWrite(RELAY_PIN_2, relay2State);
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
pinMode(RELAY_PIN_3, OUTPUT);
// pinMode(PUSH_BUTTON_3, INPUT);
relay3State = EEPROM.read(2);
digitalWrite(RELAY_PIN_3, relay3State);
Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
pinMode(RELAY_PIN_4, OUTPUT);
// pinMode(PUSH_BUTTON_4, INPUT);
relay4State = EEPROM.read(3);
digitalWrite(RELAY_PIN_4, relay4State);
Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
mySwitch.enableReceive(5); // Receiver on interrupt 0 => that is D3 or GPIO pin #0
// Blynk.begin(auth, ssid, pass);
// Setup a function to be called every 100 ms
espalexa.addDevice(Device_1_Name, firstLightChanged); //simplest definition, default state off
espalexa.addDevice(Device_2_Name, secondLightChanged);
espalexa.addDevice(Device_3_Name, thirdLightChanged);
espalexa.addDevice(Device_4_Name, fourthLightChanged);
espalexa.begin();
client.setServer(mqttServer, 1883);
client.setCallback(callback);
}
void loop()
{
Blynk.run();
espalexa.loop();
delay(1);
ArduinoOTA.handle();
Blynk.run();
unsigned long currentTime = millis();
if (currentTime - previousTime >= reconnectInterval) {
if (!client.connected()) {
reconnect();
}
previousTime = currentTime;
}
if (WiFi.status() != WL_CONNECTED) {
rfreciever();
}
rfreciever();
timer.run();
EEPROM.commit();
// nextion();
mySwitch.resetAvailable();
client.loop();
}
//our callback functions
void firstLightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
relay3State = LOW;
digitalWrite(RELAY_PIN_3, relay3State);
Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
EEPROM.write(2, relay3State);
Serial.println("Device1 ON");
client.publish(pub3, "ON", true);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
relay3State = HIGH;
digitalWrite(RELAY_PIN_3, relay3State);
Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
EEPROM.write(2, relay3State);
Serial.println("Device1 OFF");
client.publish(pub3, "OFF", true);
}
}
void secondLightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
relay1State = LOW;
digitalWrite(RELAY_PIN_1, relay1State);
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
EEPROM.write(0, relay1State);
Serial.println("Device2 ON");
client.publish(pub1, "ON", true);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
relay1State = HIGH;
digitalWrite(RELAY_PIN_1, relay1State);
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
EEPROM.write(0, relay1State);
Serial.println("Device2 OFF");
client.publish(pub1, "OFF", true);
}
}
void thirdLightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
relay2State = HIGH;
digitalWrite(RELAY_PIN_2, relay2State);
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
EEPROM.write(1, relay2State);
Serial.println("Device3 ON");
client.publish(pub2, "ON", true);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
relay2State = LOW;
digitalWrite(RELAY_PIN_2, relay2State);
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
EEPROM.write(1, relay2State);
Serial.println("Device3 OFF");
client.publish(pub2, "OFF", true);
}
}
void fourthLightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
relay4State = LOW;
digitalWrite(RELAY_PIN_4, relay4State);
Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
EEPROM.write(3, relay4State);
Serial.println("Device4 ON");
client.publish(pub4, "ON", true);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
relay4State = HIGH;
digitalWrite(RELAY_PIN_4, relay1State);
Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
EEPROM.write(3, relay4State);
Serial.println("Device4 OFF");
client.publish(pub4, "OFF", true);
}
}
```````````````````````````````````````````
I have shown just one mqtt switch configuration to make things short as all my 4 switches are configured in the same way....but I have posted my full Arduino code just in order to pinpoint my mistake IF I REMOVE THE MQTT PART FROM THE CODE EVERYTHING WORKS FINE
I WOULD BE GRATEFUL IF SOMEONE HELPS....!