ESP32 and app status gets out of sync

Tranzmetro
Posts: 9
Joined: Fri Jan 05, 2018 1:38 am

ESP32 and app status gets out of sync

Postby Tranzmetro » Thu Jan 12, 2023 3:29 am

I am having an issue with my esp32 and the app status getting out of sync.
I am setting up a button to open my garage door. This needs to be a momentary button but I can't find one in Rainmaker so I am simulating it by turning a switch on for a few seconds then turning it off. This works, press the app button, the esp32 switch (LED) turns on, the app status updates to on, wait for a few seconds then the switch (LED) turns off and the app status updates to off however almost immediately the app status turns on again but the LED stays off.

I preset the switch status to off and update the app status when first connected to WiFi.

This is the Serial.println output from the callback code below:

Starting ESP-RainMaker
E (87) wifi_prov_scheme_ble: bt_mem_release of classic BT failed 259
E (182) wifi_prov_scheme_ble: bt_mem_release of BTDM failed 259

Connected to Wi-Fi!
Startup state: 0
Startup LED to LOW
Received value = true for Button - Power
Incoming state: True
Changed LED to HIGH
Changed LED to LOW

This is correct but is not reflected in the app status which returns to on.

Below is my callback code.
  1. void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx)
  2. {
  3.   const char *device_name = device->getDeviceName();
  4.   const char *param_name = param->getParamName();
  5.  
  6.   if (strcmp(device_name, "Button") == 0)
  7.   {
  8.     if (strcmp(param_name, "Power") == 0)
  9.     {
  10.       Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name);
  11.       if (val.val.b)
  12.       {
  13.         Serial.println("Incoming state: True");
  14.         digitalWrite(ButtonPin, HIGH);
  15.         GarageDoorButton.updateAndReportParam("Power", true);
  16.         Serial.println("Changed LED to HIGH");
  17.         delay(5000);
  18.         digitalWrite(ButtonPin, LOW);
  19.         GarageDoorButton.updateAndReportParam("Power", false);
  20.         Serial.println("Changed LED to LOW");
  21.       }
  22.       else
  23.       {
  24.         digitalWrite(ButtonPin, LOW);
  25.         GarageDoorButton.updateAndReportParam("Power", false);
  26.         Serial.println("Reset to LOW");
  27.       }
  28.     }
  29.   }
  30. }
  31.  
Any help would be gratefully received, thanks.

Tranzmetro
Posts: 9
Joined: Fri Jan 05, 2018 1:38 am

Re: ESP32 and app status gets out of sync

Postby Tranzmetro » Thu Jan 19, 2023 9:54 pm

I solved the problem by taking the switching statements out of the callback. I set a flag in the callback then change the status in the main loop.

cloudmiracle
Posts: 8
Joined: Sat Aug 26, 2023 6:43 am

Re: ESP32 and app status gets out of sync

Postby cloudmiracle » Sat Aug 26, 2023 6:49 am

@Transmetro. Can you please share the code. I am trying to operate relay momentary on esp32 Arduino Rainmaker code many options but not successful

Who is online

Users browsing this forum: No registered users and 15 guests