1

i made a home automation system using esp 32 and 8 channel relay but within 2 or 3 months my relay automatically turns off after sending the signal to it through esp32 but if i connect directly without esp32 it functions absolutely fine. that means that my relay if fine but what must be the possible reason for this malfunctioning. this is my code

#include <ESP8266WiFi.h>
#include <Espalexa.h>
Espalexa espalexa;


// define the GPIO connected with Relays and switches
#define RelayPin1 5  //D23
#define RelayPin2 4  //D22
#define RelayPin3 0  //D21
#define RelayPin4 2  //D19
#define RelayPin5 14  //D18
#define RelayPin6 12  //D5
#define RelayPin7 13  //D25
#define RelayPin8 15  //D26

#define SwitchPin1 3  //D13
#define SwitchPin2 1  //D12
#define SwitchPin3 10  //D14
#define SwitchPin4 9  //D27
#define SwitchPin5 8  //D33
#define SwitchPin6 11  //D32
#define SwitchPin7 7  //D15
#define SwitchPin8 6   //D4

bool oldStateSwitch_1;
bool oldStateSwitch_2;
bool oldStateSwitch_3;
bool oldStateSwitch_4;
bool oldStateSwitch_5;
bool oldStateSwitch_6;
bool oldStateSwitch_7;
bool oldStateSwitch_8;


#define wifiLed    16   //D2

// WiFi Credentials
const char* ssid = "akkikisu";
const char* password = "anirudh@123";

// device names
String Device_1_Name = "a1";
String Device_2_Name = "a2";
String Device_3_Name = "a3";
String Device_4_Name = "a4";
String Device_5_Name = "a5";
String Device_6_Name = "a6";
String Device_7_Name = "a7";
String Device_8_Name = "a8";

// prototypes
boolean connectWifi();

//callback functions
void firstLightChanged(uint8_t brightness);
void secondLightChanged(uint8_t brightness);
void thirdLightChanged(uint8_t brightness);
void fourthLightChanged(uint8_t brightness);
void fifthLightChanged(uint8_t brightness);
void sixthLightChanged(uint8_t brightness);
void senventhLightChanged(uint8_t brightness);
void eighthLightChanged(uint8_t brightness);

boolean wifiConnected = false;

//our callback functions
void firstLightChanged(uint8_t brightness)
{
  //Control the device
  if (brightness == 255)
    {
      digitalWrite(RelayPin1, LOW);
      Serial.println("Device1 ON");
    }
  else
  {
    digitalWrite(RelayPin1, HIGH);
    Serial.println("Device1 OFF");
  }
}

void secondLightChanged(uint8_t brightness)
{
  //Control the device 
  if (brightness == 255)
    {
      digitalWrite(RelayPin2, LOW);
      Serial.println("Device2 ON");
    }
  else
  {
    digitalWrite(RelayPin2, HIGH);
    Serial.println("Device2 OFF");
  }
}

void thirdLightChanged(uint8_t brightness)
{
  //Control the device  
  if (brightness == 255)
    {
      digitalWrite(RelayPin3, LOW);
      Serial.println("Device3 ON");
    }
  else
  {
    digitalWrite(RelayPin3, HIGH);
    Serial.println("Device3 OFF");
  }
}

void fourthLightChanged(uint8_t brightness)
{
  //Control the device 
  if (brightness == 255)
    {
      digitalWrite(RelayPin4, LOW);
      Serial.println("Device4 ON");
    }
  else
  {
    digitalWrite(RelayPin4, HIGH);
    Serial.println("Device4 OFF");
  }
}

void fifthLightChanged(uint8_t brightness)
{
  //Control the device
  if (brightness == 255)
    {
      digitalWrite(RelayPin5, LOW);
      Serial.println("Device5 ON");
    }
  else
  {
    digitalWrite(RelayPin5, HIGH);
    Serial.println("Device1 OFF");
  }
}

void sixthLightChanged(uint8_t brightness)
{
  //Control the device
  if (brightness == 255)
    {
      digitalWrite(RelayPin6, LOW);
      Serial.println("Device6 ON");
    }
  else
  {
    digitalWrite(RelayPin6, HIGH);
    Serial.println("Device6 OFF");
  }
}

void seventhLightChanged(uint8_t brightness)
{
  //Control the device
  if (brightness == 255)
    {
      digitalWrite(RelayPin7, LOW);
      Serial.println("Device7 ON");
    }
  else
  {
    digitalWrite(RelayPin7, HIGH);
    Serial.println("Device7 OFF");
  }
}

void eighthLightChanged(uint8_t brightness)
{
  //Control the device
  if (brightness == 255)
    {
      digitalWrite(RelayPin8, LOW);
      Serial.println("Device8 ON");
    }
  else
  {
    digitalWrite(RelayPin8, HIGH);
    Serial.println("Device8 OFF");
  }
}

// connect to wifi – returns true if successful or false if not
boolean connectWifi()
{
  boolean state = true;
  int i = 0;

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 20) {
      state = false; break;
    }
    i++;
  }
  Serial.println("");
  if (state) {
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Connection failed.");
  }
  return state;
}

void addDevices(){
  // Define your devices here.
  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.addDevice(Device_5_Name, fifthLightChanged); 
  espalexa.addDevice(Device_6_Name, sixthLightChanged);
  espalexa.addDevice(Device_7_Name, seventhLightChanged);
  espalexa.addDevice(Device_8_Name, eighthLightChanged);

  espalexa.begin();
}

void setup()
{
  Serial.begin(115200);

  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(RelayPin5, OUTPUT);
  pinMode(RelayPin6, OUTPUT);
  pinMode(RelayPin7, OUTPUT);
  pinMode(RelayPin8, OUTPUT);

  pinMode(wifiLed, OUTPUT);

  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  pinMode(SwitchPin3, INPUT_PULLUP);
  pinMode(SwitchPin4, INPUT_PULLUP);
  pinMode(SwitchPin5, INPUT_PULLUP);
  pinMode(SwitchPin6, INPUT_PULLUP);
  pinMode(SwitchPin7, INPUT_PULLUP);
  pinMode(SwitchPin8, INPUT_PULLUP);

  //During Starting all Relays should TURN OFF
  digitalWrite(RelayPin1, HIGH);
  digitalWrite(RelayPin2, HIGH);
  digitalWrite(RelayPin3, HIGH);
  digitalWrite(RelayPin4, HIGH);
  digitalWrite(RelayPin5, HIGH);
  digitalWrite(RelayPin6, HIGH);
  digitalWrite(RelayPin7, HIGH);
  digitalWrite(RelayPin8, HIGH);

  oldStateSwitch_1 = ! digitalRead( SwitchPin1 ) ;  // note inverted
  oldStateSwitch_2 = ! digitalRead( SwitchPin2 ) ;  // note inverted
  oldStateSwitch_3 = ! digitalRead( SwitchPin3 ) ;  // note inverted
  oldStateSwitch_4 = ! digitalRead( SwitchPin4 ) ;  // note inverted
  oldStateSwitch_5 = ! digitalRead( SwitchPin5 ) ;  // note inverted
  oldStateSwitch_6 = ! digitalRead( SwitchPin6 ) ;  // note inverted
  oldStateSwitch_7 = ! digitalRead( SwitchPin7 ) ;  // note inverted
  oldStateSwitch_8 = ! digitalRead( SwitchPin8 ) ;  // note inverted
    



    // repeat for seitched 2 to 8
  
    


  // Initialise wifi connection
  wifiConnected = connectWifi();

  if (wifiConnected)
  {
    addDevices();
  }
  else
  {
    Serial.println("Cannot connect to WiFi. So in Manual Mode");
    delay(1000);
  }
}

void loop()
{
   if (WiFi.status() != WL_CONNECTED)
  {
    //Serial.print("WiFi Not Connected ");
    digitalWrite(wifiLed, LOW); //Turn off WiFi LED
  }
  else
  {
    //Serial.print("WiFi Connected  ");
    digitalWrite(wifiLed, HIGH);
    //Manual Switch Control
    //WiFi Control
    if (wifiConnected){
      espalexa.loop();
      delay(1);
    }
    else {
      wifiConnected = connectWifi(); // Initialise wifi connection
      if(wifiConnected){
      addDevices();
      }
    }
  }


//main code

static uint32_t lastSwitchTest = 0 ;  // static initialised once only

      if ( millis() - lastSwitchTest > 250 ) {  // every 250ms
      lastSwitchTest = millis() ;

      // switch 1

      bool stateSwitch_1 = digitalRead( SwitchPin1 ) ;
      if (  oldStateSwitch_1 != stateSwitch_1 ) {
         oldStateSwitch_1 = stateSwitch_1 ;
         Serial.println("Switch 1 changed");
         EspalexaDevice* d1 = espalexa.getDevice(0); //this will get "first device", the index is zero-based
         if ( stateSwitch_1 == HIGH )  {  // HIGH = off
            Serial.println("Switch 1 off");
            d1->setPercent(0); //set value "brightness" in percent
            digitalWrite(RelayPin1, HIGH);
         }
         else { // switch on 
            Serial.println("Switch 1 on");
            d1->setPercent(100); //set value "brightness" in percent
            digitalWrite(RelayPin1, LOW);
         }
      }

      // switch 2

      bool stateSwitch_2 = digitalRead( SwitchPin2 ) ;
      if ( oldStateSwitch_2 != stateSwitch_2 ) {
         oldStateSwitch_2 = stateSwitch_2 ;
         Serial.println("Switch 2 changed");
         EspalexaDevice* d2 = espalexa.getDevice(0); //this will get "first device", the index is zero-based
         if ( stateSwitch_2 == HIGH )  {  // HIGH = off
            Serial.println("Switch 2 off");
            d2->setPercent(0); //set value "brightness" in percent
            digitalWrite(RelayPin2, HIGH);
         }
         else { // switch on 
            Serial.println("Switch 2 on");
            d2->setPercent(100); //set value "brightness" in percent
            digitalWrite(RelayPin2, LOW);
         }
      }
 
      // switch 3

      bool stateSwitch_3 = digitalRead( SwitchPin3 ) ;
      if ( oldStateSwitch_3 != stateSwitch_3 ) {
         oldStateSwitch_3 = stateSwitch_3 ;
         Serial.println("Switch 3 changed");
         EspalexaDevice* d3 = espalexa.getDevice(0); //this will get "first device", the index is zero-based
         if ( stateSwitch_3 == HIGH )  {  // HIGH = off
            Serial.println("Switch 1 off");
            d3->setPercent(0); //set value "brightness" in percent
            digitalWrite(RelayPin3, HIGH);
         }
         else { // switch on 
            Serial.println("Switch 3 on");
            d3->setPercent(100); //set value "brightness" in percent
            digitalWrite(RelayPin3, LOW);
         }
      }
      // switch 4

      bool stateSwitch_4 = digitalRead( SwitchPin4 ) ;
      if ( oldStateSwitch_4 != stateSwitch_4 ) {
         oldStateSwitch_4 = stateSwitch_4 ;
         Serial.println("Switch 4 changed");
         EspalexaDevice* d4 = espalexa.getDevice(0); //this will get "first device", the index is zero-based
         if ( stateSwitch_4 == HIGH )  {  // HIGH = off
            Serial.println("Switch 1 off");
            d4->setPercent(0); //set value "brightness" in percent
            digitalWrite(RelayPin4, HIGH);
         }
         else { // switch on 
            Serial.println("Switch 4 on");
            d4->setPercent(100); //set value "brightness" in percent
            digitalWrite(RelayPin4, LOW);
         }
      }
      // switch 5

      bool stateSwitch_5 = digitalRead( SwitchPin5 ) ;
      if ( oldStateSwitch_5 != stateSwitch_5 ) {
         oldStateSwitch_5 = stateSwitch_5 ;
         Serial.println("Switch 5 changed");
         EspalexaDevice* d5 = espalexa.getDevice(0); //this will get "first device", the index is zero-based
         if ( stateSwitch_5 == HIGH )  {  // HIGH = off
            Serial.println("Switch 1 off");
            d5->setPercent(0); //set value "brightness" in percent
            digitalWrite(RelayPin5, HIGH);
         }
         else { // switch on 
            Serial.println("Switch 5 on");
            d5->setPercent(100); //set value "brightness" in percent
            digitalWrite(RelayPin5, LOW);
         }
      }
      // switch 6

      bool stateSwitch_6 = digitalRead( SwitchPin6 ) ;
      if ( oldStateSwitch_6 != stateSwitch_6 ) {
         oldStateSwitch_6 = stateSwitch_6 ;
         Serial.println("Switch 6 changed");
         EspalexaDevice* d6 = espalexa.getDevice(0); //this will get "first device", the index is zero-based
         if ( stateSwitch_6 == HIGH )  {  // HIGH = off
            Serial.println("Switch 1 off");
            d6->setPercent(0); //set value "brightness" in percent
            digitalWrite(RelayPin6, HIGH);
         }
         else { // switch on 
            Serial.println("Switch 6 on");
            d6->setPercent(100); //set value "brightness" in percent
            digitalWrite(RelayPin6, LOW);
         }
      }
      // switch 7

      bool stateSwitch_7 = digitalRead( SwitchPin7 ) ;
      if ( oldStateSwitch_7 != stateSwitch_7 ) {
         oldStateSwitch_7 = stateSwitch_7 ;
         Serial.println("Switch 7 changed");
         EspalexaDevice* d7 = espalexa.getDevice(0); //this will get "first device", the index is zero-based
         if ( stateSwitch_7 == HIGH )  {  // HIGH = off
            Serial.println("Switch 1 off");
            d7->setPercent(0); //set value "brightness" in percent
            digitalWrite(RelayPin7, HIGH);
         }
         else { // switch on 
            Serial.println("Switch 7 on");
            d7->setPercent(100); //set value "brightness" in percent
            digitalWrite(RelayPin7, LOW);
         }
      }
      // switch 8

      bool stateSwitch_8 = digitalRead( SwitchPin8 ) ;
      if ( oldStateSwitch_8 != stateSwitch_8 ) {
         oldStateSwitch_8 = stateSwitch_8 ;
         Serial.println("Switch 8 changed");
         EspalexaDevice* d8 = espalexa.getDevice(0); //this will get "first device", the index is zero-based
         if ( stateSwitch_8 == HIGH )  {  // HIGH = off
            Serial.println("Switch 1 off");
            d8->setPercent(0); //set value "brightness" in percent
            digitalWrite(RelayPin8, HIGH);
         }
         else { // switch on 
            Serial.println("Switch 8 on");
            d8->setPercent(100); //set value "brightness" in percent
            digitalWrite(RelayPin8, LOW);
         }
      }


  
      
      // switch 2 to 8
      // just copy/paste/adjust using switch 1 as a template


    } // if ( millis
} // loop()
8
  • Every 2 or 3 months you get a spurious triggering of your relay module which you attribute to a software problem on the esp32. Is that correct? If so, it could be a bad handling of the millis() rollover somewhere in your code or a library. This rollover occurs every 49 days.
    – 6v6gt
    Apr 5, 2023 at 2:54
  • @6v6gt and how can i rectify my problem Apr 5, 2023 at 10:11
  • I'd suspect the button library acebutton.h . It appears to use 16bit datatypes for holding timings, probably derived from millis(), for button debouncing etc. 32 bit integers would be more appropriate. Using another library may solve this. But you have to conduct a long term test.
    – 6v6gt
    Apr 5, 2023 at 11:24
  • which library can i use . and can you tell me the appropriate code for that Apr 5, 2023 at 11:37
  • Let's get the problem description right before looking deeper for a solution. When you say that it works without the ESP32, do you mean that you are able to connect the 8 channel relay module directly to the Alexa? You said this spurious behaviour happens within 2 or 3 months. Is it possible that it happens regularly every 49 days? Are you using push buttons or toggle type switches for the manual relay control? Is it so that you will only know you have a working solution after testing over a 2 to 3 month period?
    – 6v6gt
    Apr 6, 2023 at 5:53

1 Answer 1

1

You are using the following project as a code base: https://iotcircuithub.com/esp32-alexa-home-automation-system/

You appear to have confirmed that the same problem symptoms appear when the ESP32 is reset, that is that some lights are in the wrong state at system start. A reset can be caused by such things as a system reset following a power outage, a crash or simply pressing the reset button on the device.

You are using a button handling library AceButton.h in combination with latched switches. The problem here is that the button handling library is intended for momentory push-to-make buttons and assumes that the switch is in the "un-pushed" state at system start. This may not reflect the position of the latched switches where some could be in the on position and others in the off position. This could result in missed events on system start.

Anyway, you do not need a button library. Especially for toggle type switches, like you are using, where it is necessary only to read the switch say every 250ms and test if the state has been changed since the last reading. No other debouncing is required.

In setup() you'd read each switch and store the inverse of its current state in a variable, say oldStateSwitch_1. It is necessary to store the inverse of the state to trigger a first time switch state change action in the loop().

Something like . . .

void setup() {
    . . .
    
    oldStateSwitch_1 = ! digitalRead( switchPin1 ) ;  // note inverted
    // repeat for seitched 2 to 8
    . . . 
    
}

In the loop(), you'd do something like . . .

void loop() {

. . .
. . .

    static uint32_t lastSwitchTest = 0 ;  // static initialised once only

    if ( millis() - lastSwitchTest > 250 ) {  // every 250ms
      lastSwitchTest = millis() ;

      // switch 1

      bool stateSwitch_1 = digitalRead( switchPin1 ) ;
      if ( oldStateSwitch_1 != stateSwitch_1 ) {
         oldStateSwitch_1 = stateSwitch_1 ;
         Serial.println("Switch 1 changed");
         EspalexaDevice* d1 = espalexa.getDevice(0); //this will get "first device", the index is zero-based
         if ( stateSwitch_1 == HIGH )  {  // HIGH = off
            Serial.println("Switch 1 off");
            d1->setPercent(0); //set value "brightness" in percent
            digitalWrite(RelayPin1, HIGH);
         }
         else { // switch on 
            Serial.println("Switch 1 on");
            d1->setPercent(100); //set value "brightness" in percent
            digitalWrite(RelayPin1, LOW);
         }
      }
      
      // switch 2 to 8
      // just copy/paste/adjust using switch 1 as a template


    } // if ( millis
} // loop()

All AceButton code should be deleted.

11
  • now again the same kind of thing is happening but this time the switches automatically go off and agfter some time they turn on if the board's blue light is on but sometimes that blue light goes off and the board is not able to connect to wifi now how do i resolve this Apr 16, 2023 at 6:50
  • @RathoreBrothers Spurious resetting of a micro controller together with WiFi connectivity problems are notoriously difficult to investigate. What happens when the system is running normally and (test a) you press the reset button on the esp32 and (test b) you switch off the WiFi router? Do you get similar symptoms to the ones you have described?
    – 6v6gt
    Apr 17, 2023 at 3:23
  • when i press reset the boards normally resets and i am able to access the lights and when i turn off the wifi then also i am able to access through switches but not over alexa but the thing is after a period of time the lights go off and on after some time Apr 17, 2023 at 10:14
  • can you please reply Apr 24, 2023 at 14:13
  • @RathoreBrothers If you press reset on the esp32, do the lights return to the state they were in before the reset? Are the switches of the same type as illustrated in the project, that is they hold their position as either ON or OFF or are you using push buttons? If the code you are using is not the same as in your original question, please update the question with the new code. Have you looked through the comments in the project and video to see if other people have the same problem?
    – 6v6gt
    Apr 25, 2023 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.