3 relay switch based on sensor input using esp 32

Hello im trying to switch 3 relay based on BH1750 light sensor input attached is an example of the flowchart , i managed to code the first part , but the problem I'm facing is i want to check the 3 relays status each 1 hour , and act based on their status the following hour , i can see in the serial monitor that the code is actually switching from 8-9 am to 9-10 am , but the case state doesnt change : this is if wifi is connected and time :
while (WiFi.status() == WL_CONNECTED) { // Check for WiFi connection
time(&now);
localtime_r(&now, &timeinfo);
printLocalTime();
Serial.println("sns hons");
float lux = lightMeter.readLightLevel();
lcd.setCursor(0,2);
lcd.print("Light: ");
lcd.print(lux, 0);

delay(500);

lightMeter.begin();
// Check the time and run the appropriate function
if (timeinfo.tm_hour >= 9 && timeinfo.tm_hour < 17 && timeinfo.tm_min >= 0 && timeinfo.tm_min <= 52) {
Serial.println("8-9");
loop_wifi_connected(); // loop_wifi_connected i have the code for first hour ,

else if (timeinfo.tm_hour >= 16 && timeinfo.tm_min >=52 && timeinfo.tm_hour < 24) {
Serial.println("13-14");

  loop_wifi_connected1(); // in this i have code for seond hour 
}

this is the first hour code works perfectly :

State state = STATE1;
void loop_wifi_connected() {
bool isFinished = false;
float lux = lightMeter.readLightLevel();

lightMeter.begin();
// Check the state of the state machine
switch (state) {
case STATE1:
time_t now;
struct tm timeinfo;
char buffer[32];
time(&now);
localtime_r(&now, &timeinfo);
if (digitalRead(relay1Pin) == LOW){

  state = STATE0_1;
  waitMillis = 2000;
    lastMillis = millis();
  }
  else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == LOW){



   state = STATE2;
  waitMillis = 2000;
    lastMillis = millis();
  }
  else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == HIGH && digitalRead(relay3Pin) == LOW){
    
    
    
     state = STATE3;
  waitMillis = 2000;
    lastMillis = millis();
    
    }
     else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == HIGH && digitalRead(relay3Pin) == HIGH){
    
    
    
     state = STATE5SEC3;
  waitMillis = 2000;
    lastMillis = millis();


  }
 break;
 case STATE0_1:
 delay (2000);

  // If the lux value is less than the threshold, turn on the relay and move to the next state
  if (lux < LUX_THRESHOLD ) {
      digitalWrite(relay1Pin, HIGH); // turn on relay1
      Serial.println("r1 on");
      
    state = STATE1_1;
    waitMillis = 2000;
    lastMillis = millis();
  }
  break;

case STATE1_1:
delay (2000);
// If the lux value is still less than the threshold after the wait time, stay in this state
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {

    state = STATE2;

    waitMillis = 0;
    lastMillis = millis();}
  
  // If the lux value is greater than or equal to the threshold, turn off the relay and go back to the first state
  else if (lux >= LUX_THRESHOLD) {
       digitalWrite(relay1Pin, LOW); // turn off relay1
    
    waitMillis = 0;
    lastMillis = 0;
  }
  break;
       case STATE2:
       delay (2000);
  // If the lux value is less than the threshold, turn on the relay and move to the next state
  if (lux < LUX_THRESHOLD) {
      digitalWrite(relay2Pin, HIGH); // turn on relay2
      Serial.println("r2 on");
    state = STATE5SEC1;
    waitMillis = 2000;
    lastMillis = millis();
  }
  break;
  case STATE5SEC1:
  delay (2000);
  // If the lux value is still less than the threshold after the wait time, stay in this state
  if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
    
    state = STATE3;

    waitMillis = 0;
    lastMillis = millis();
  }
  // If the lux value is greater than or equal to the threshold, turn off the relay and go back to the first state
  else if (lux >= LUX_THRESHOLD) {
       digitalWrite(relay2Pin, LOW); // turn off relay2
       
    state = STATE5SEC2;
    waitMillis = 5000;
    lastMillis = 0;
  }
  break;

case STATE5SEC2:
delay (2000);
// If the lux value is still less than the threshold after the wait time, stay in this state
if (lux < LUX_THRESHOLD) {

    state = STATE5SEC2_1;
    waitMillis = 2000;
    lastMillis = millis();
  }
   else if (lux >= LUX_THRESHOLD) {
       digitalWrite(relay1Pin, LOW); // turn off relay1
  
    waitMillis = 5000;
    lastMillis = 0;
  }
   break;
   case STATE5SEC2_1:
   delay (2000);
  if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
    

    waitMillis = 0;
    lastMillis = millis();
  }
   else if (lux >= LUX_THRESHOLD) {
       digitalWrite(relay1Pin, LOW); // turn off relay1
  
    waitMillis = 5000;
    lastMillis = 0;
    isFinished = true;
    }
    break;

   case  STATE3:
   delay (2000);
   
    if (lux < LUX_THRESHOLD) {
      digitalWrite(relay3Pin, HIGH); // turn on relay3

Serial.println("r3 on");
state = STATE5SEC3;
waitMillis = 5000;
lastMillis = millis();
}

  case  STATE5SEC3:
  delay (2000);
  if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
    Serial.println("wait light ");

    waitMillis = 0;
    lastMillis = millis();
  }
   else if (lux >= LUX_THRESHOLD) {
       digitalWrite(relay3Pin, LOW); // turn off relay3
       Serial.println("r3 off");
       state = STATE5SEC3_1;
  
    waitMillis = 5000;
    lastMillis = 0;
    }
    break;

    case STATE5SEC3_1:
    delay (2000);
    if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
      Serial.println("wait light ");
  
    

    waitMillis = 0;
    lastMillis = millis();
  }
   else if (lux > LUX_THRESHOLD) {
       Serial.println("r2 off ");
       digitalWrite(relay2Pin, LOW);
       state = STATE5SEC3_2;
  
    waitMillis = 5000;
    lastMillis = 0;
    }
    break;
    case STATE5SEC3_2:
    delay (2000);
    if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
    

    waitMillis = 0;
    lastMillis = millis();
  }
   else if (lux >= LUX_THRESHOLD) {
   
       state = STATE5SEC2_1;
  
    waitMillis = 5000;
    lastMillis = 0;
    }
    


   }}

but then when the second hour starts and take off from wherever the state in hour 1 code was , and does not reset :
void loop_wifi_connected2() {
bool isFinished = false;
float lux = lightMeter.readLightLevel();
State state = STATE1;
lightMeter.begin();
// Check the state of the state machine
switch (state) {
case STATE1:
time_t now;
struct tm timeinfo;
char buffer[32];
time(&now);
localtime_r(&now, &timeinfo);
if (digitalRead(relay1Pin) == LOW){

  state = STATE0_1;
  waitMillis = 2000;
    lastMillis = millis();
  }
  else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == LOW){



   state = STATE2;
  waitMillis = 2000;
    lastMillis = millis();
  }
  else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == HIGH && digitalRead(relay3Pin) == LOW){
    
    
    
     state = STATE3;
  waitMillis = 2000;
    lastMillis = millis();
    
    }
     else if (digitalRead(relay1Pin) == HIGH && digitalRead(relay2Pin) == HIGH && digitalRead(relay3Pin) == HIGH){
    
    
    
     state = STATE5SEC3;
  waitMillis = 2000;
    lastMillis = millis();


  }
 break;
 case STATE0_1:
 delay (2000);

  // If the lux value is less than the threshold, turn on the relay and move to the next state
  if (lux < LUX_THRESHOLD ) {
      digitalWrite(relay1Pin, HIGH); // turn on relay1
      Serial.println("r1 on");
      
    state = STATE1_1;
    waitMillis = 2000;
    lastMillis = millis();
  }
  break;

case STATE1_1:
delay (2000);
// If the lux value is still less than the threshold after the wait time, stay in this state
if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {

    state = STATE2;

    waitMillis = 0;
    lastMillis = millis();}
  
  // If the lux value is greater than or equal to the threshold, turn off the relay and go back to the first state
  else if (lux >= LUX_THRESHOLD) {
       digitalWrite(relay1Pin, LOW); // turn off relay1
    
    waitMillis = 0;
    lastMillis = 0;
  }
  break;
       case STATE2:
       delay (2000);
  // If the lux value is less than the threshold, turn on the relay and move to the next state
  if (lux < LUX_THRESHOLD) {
      digitalWrite(relay2Pin, HIGH); // turn on relay2
      Serial.println("r2 on");
    state = STATE5SEC1;
    waitMillis = 2000;
    lastMillis = millis();
  }
  break;
  case STATE5SEC1:
  delay (2000);
  // If the lux value is still less than the threshold after the wait time, stay in this state
  if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
    
    state = STATE3;

    waitMillis = 0;
    lastMillis = millis();
  }
  // If the lux value is greater than or equal to the threshold, turn off the relay and go back to the first state
  else if (lux >= LUX_THRESHOLD) {
       digitalWrite(relay2Pin, LOW); // turn off relay2
       
    state = STATE5SEC2;
    waitMillis = 5000;
    lastMillis = 0;
  }
  break;

case STATE5SEC2:
delay (2000);
// If the lux value is still less than the threshold after the wait time, stay in this state
if (lux < LUX_THRESHOLD) {

    state = STATE5SEC2_1;
    waitMillis = 2000;
    lastMillis = millis();
  }
   else if (lux >= LUX_THRESHOLD) {
       digitalWrite(relay1Pin, LOW); // turn off relay1
  
    waitMillis = 5000;
    lastMillis = 0;
  }
   break;
   case STATE5SEC2_1:
   delay (2000);
  if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
    

    waitMillis = 0;
    lastMillis = millis();
  }
   else if (lux >= LUX_THRESHOLD) {
       digitalWrite(relay1Pin, LOW); // turn off relay1
  
    waitMillis = 5000;
    lastMillis = 0;
    isFinished = true;
    }
    break;

   case  STATE3:
   delay (2000);
   
    if (lux < LUX_THRESHOLD) {
      digitalWrite(relay3Pin, HIGH); // turn on relay3

Serial.println("r3 on");
state = STATE5SEC3;
waitMillis = 5000;
lastMillis = millis();
}

  case  STATE5SEC3:
  delay (2000);
  if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
    Serial.println("wait light ");

    waitMillis = 0;
    lastMillis = millis();
  }
   else if (lux >= LUX_THRESHOLD) {
       digitalWrite(relay3Pin, LOW); // turn off relay3
       Serial.println("r3 off");
       state = STATE5SEC3_1;
  
    waitMillis = 5000;
    lastMillis = 0;
    }
    break;

    case STATE5SEC3_1:
    delay (2000);
    if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
      Serial.println("wait light ");
  
    

    waitMillis = 0;
    lastMillis = millis();
  }
   else if (lux > LUX_THRESHOLD) {
       Serial.println("r2 off ");
       digitalWrite(relay2Pin, LOW);
       state = STATE5SEC3_2;
  
    waitMillis = 5000;
    lastMillis = 0;
    }
    break;
    case STATE5SEC3_2:
    delay (2000);
    if (lux < LUX_THRESHOLD && millis() - lastMillis >= waitMillis) {
    

    waitMillis = 0;
    lastMillis = millis();
  }
   else if (lux >= LUX_THRESHOLD) {
   
       state = STATE5SEC2_1;
  
    waitMillis = 5000;
    lastMillis = 0;
    }
    


   }}

Your code is missing parts.

Try again to paste your code. (1) edit your post (click the pencil). (2) click the < CODE > buttonc(3) paste your code where it says, '''type or paste your code here'''

Paste different sketches into different code blocks. Tnx.

Yes let me send the whole code in a second

I moved your topic to an appropriate forum category @aasaleh024.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Help us help you.

could you include the issue in a new post?

Still some broken stuff... this for one...

  {
    Serial.println("im here3");
    Serial.println("no no");
  }

And where can I find this:

i cant attach photos , also im not recieving any errors its just that code is not functioning like its suppose to , what i want lets say per example : between the 8 and 9 am : turn on relay 1 and 2
and then between 9 and 10 : check on relay 1 and 2 status if 1 is on and 2 is off , turn on relay 2
what is actually happening is : between 8 and 9 am the code is functioning correctly
but between 9 and 10 the code is not checking the relay status from the hour before instead of that the code is straightly goimg to the last state the code was at at the previous hour

You are not receiving this error?

94:3: error: 'configTime' was not declared in this scope

no the code is compiling and running .

Always remember: Your code functions EXACTLY as it is told... right now you are trying to find your error in coding.

In post #1, you wrote,

"...switch 3 relay based on BH1750 light sensor input..."

Is the above still valid, or are you changing your search to this...

"...between 9 and 10 the code is not checking the relay status..."

And what is this doing?

  {
    Serial.println("im here3");
    Serial.println("no no");
  }

And... what are you intending here:

    } else if (timeinfo.tm_hour >= 20 && timeinfo.tm_min >= 44 && timeinfo.tm_hour < 20 && timeinfo.tm_min <= 50) {
      Serial.println("9-10");
      loop_wifi_connected();
    }

Yes my bad i took of this part of code
Serial.println("im here3");
Serial.println("no no")
and regarding the time is not matching 9 to 10 is because i was trying the code during diffrent time , what im trying to say is that 9 to 10 is an example, what im looking for is to have two different switch cases each case works individually in its time interval

Hi, @aasaleh024
Welcome to the forum.

Can you tell us what the application is.
What are you sensing and what are you controlling?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

You have overlapping times. Straighten these out before proceeding...

    // Check the time and run the appropriate function

BETWEEN 08:40 and 20:43

          if (timeinfo.tm_hour >= 8 && timeinfo.tm_min >= 40 && timeinfo.tm_hour < 20 && timeinfo.tm_min <= 43) {
      Serial.println("8-9");
      loop_wifi_connected();

BETWEEN 20:44 and 20:50

    } else if (timeinfo.tm_hour >= 20 && timeinfo.tm_min >= 44 && timeinfo.tm_hour < 20 && timeinfo.tm_min <= 50) {
      Serial.println("9-10");
      loop_wifi_connected();

BETWEEN 20:52 and 20:55

    } else if (timeinfo.tm_hour >= 20 && timeinfo.tm_min >= 52 && timeinfo.tm_hour < 20 && timeinfo.tm_min <= 55) {
      Serial.println("10-11");
      loop_wifi_connected();

BETWEEN 20:00 and 21:00

    } else if (timeinfo.tm_hour >= 20 && timeinfo.tm_hour < 21) {
      Serial.println("11-12");
      loop_wifi_connected2();

BETWEEN 23:07 and 23:10

    } else if (timeinfo.tm_hour >= 23 && timeinfo.tm_min >= 7 && timeinfo.tm_hour < 23 && timeinfo.tm_min <= 10) {
      Serial.println("12-13");
      loop_wifi_connected();

BETWEEN 23:12 and 24:00 (which never happens)

    } else if (timeinfo.tm_hour >= 23 && timeinfo.tm_min >= 12 && timeinfo.tm_hour < 24) {
      Serial.println("13-14");
      loop_wifi_connected2();
    }
  }

Why would relay 2 turn off?

Thanks for the explanation of the issue.

Thank you for your help i finally figured it out, i had two switch states like this example:

Int State = state 1;
Int State2 = state 2;

Switch (state)
That has cases
Case State1 :
Do something
State=state1-1

And the second switch was
Switch (state2)
Case 2:
Do something
State=state2 (this was my mistake it should've been ) state2=state2

Now im going to clean up the overlapping time and other warnings
Thank you

1 Like

Thank you for your help i finally figured it out, i had two switch states like this example:

Int State = state 1;
Int State2 = state 2;

Switch (state)
That has cases
Case State1 :
Do something
State=state1-1

And the second switch was
Switch (state2)
Case 2:
Do something
State=state2 (this was my mistake it should've been ) state2=state2

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.