[Solved] Using two ESP32 – Reading and Writing data from Thingspeak to control LIGHT

NS1518 Asks: Using two ESP32 – Reading and Writing data from Thingspeak to control LIGHT
What I need How to read and write data from Thingspeak the correct way using ESP32.

Setup:

  1. One ESP32 controlling 5 lights with push buttons
  2. Other ESP32 connected to touch screen display controls lights remotely
  3. The data is written/read from the same data source of Thingspeak cloud.

I’m working on a ESP32 based project in which I am controlling 5 AC power Lights with each light having it’s own push button. The project can be controlled remotely using a touch screen Nextion display which is again controlled with another ESP32. The light state (ON/OFF) data is stored on Thingspeak cloud.

Problem: As the lights can be controlled manually using push buttons as well as remotely using touch display through Thingspeak, I am having difficulty designing the read and write architecture.

Presently I am running a code on ESP32 using a debounce for push button library (JC_Button.h). Every change of button state triggers switches the light ON/OFF and update the state on cloud. Similarly, there is a function that receives the state from cloud and updates the light ON/OFF. Problem is I am not able to properly design the system where I can clearly show seamlessly manage read and write control.

I am not sure how to design the architecture so that my read and write led state is properly visible on the lights to me when I physically press the button as well as if someone monitors and control the setup remotely.

Code snippet

Code:
int deBounceState1 = 0; /* If there is change in push button state, 
block reading data from cloud -> Update cloud with new status -> 
Read data from cloud and set the light state*/

int state0, state1, state2, state3 = 0; // Variable to store state of lights 
int stateR0, stateR1, stateR2, stateR3 = 0; // Read light state from cloud

Code:
//Function that reads button state
btn1.read(); // Reads the state of the button
if (btn1.changed()) {
  deBounceState1 = 1;
  digitalWrite(light1, btn1.toggleState());
  //Serial.println("Button 1 Pressed............");
  state0=btn1.toggleState();     
  //Serial.println(state0);      
} else {
  deBounceState1 = 0;
  //Serial.println(state0);    
  //Serial.println("Button 1 no change");
}

Code:
void loop() {
   deBounce();
   if (deBounceState1==1 || deBounceState2==1 || deBounceState3 ==1 || deBounceState4 ==1){
    controlOutput();
    delayLoop();
   } 
}

Code:
void delayLoop() {
   static long currentMillis;
   static byte statCounter = 0;
   if (millis() - currentMillis >= interval)
   {
      if (statCounter> 10)
      {
       statCounter = 0;
       stateChangeUpdate();
       deBounceState1=0;
       deBounceState2=0;
       deBounceState3=0;
       deBounceState4=0;
       readChannel();
      }
      statCounter++;
      Serial.println(statCounter);
      currentMillis = millis();
   }
}

I know the information is not enough to explain the problem but please feel free to ask questions.

Thanks.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.