Problem combining ESP8622 Sleep and Blynk_app_connected

Hi again, now I have another problem with NodeMCU. This time, In one state of my program, I send my Node to deepSleep 5 seconds, then it wakes up, test if the time previously setted and the RTC matches, if not matches, it goes again to sleep. The problem is, that I want to make that if the Blynk App is opened, the Node stops the sleep loop until the app is closed. I tried used BLYNK_APP_CONNECTED and BLYNK_APP_DISCONNECTED, but It didn’t work, because it has to be a perfect timing to trigger that event when the Node is awake (when it checks time). So, I wonder, is there a function that checks if the App IS connected, and not only when you open it? I’m using a private server. I hope that I give enough information.

Please post your (formatted) code in case someone needs to go over it and test it themselves.

A couple of ideas…

  1. Did you enable the NOTIFY DEVICES WHEN APP CONNECTED feature in the Project Settings?

  2. Have you checked if the BLYNK_APP_CONNECTED() can be tested as a boolean? (I haven’t yet)
    EDIT - I have tried, but it fails to compile

if (BLYNK_APP_CONNECTED() == 1) {
//App is active
} else {
// App is NOT active
}
  1. Perhaps you could also try experimenting with the BLYNK_READ() function and perhaps a simple display widget set to a 1 second reading rate (the shortest time available)… Unless the developers have changed it, that timed widget should only call and process the function when the the Project is active (EDIT - confirmed this still works that way).
    Not quite sure off the top of my head what you could use in that function to act as a “App is alive” flag…? But then the BLYNK_APP_DISCONNECTED() could be used to resume sleep.
1 Like
  1. Yes, I did, I forgot to mention it.
  2. No, It doesn’t work, it only can be used as a function.
  3. It appears to be working, but sometimes it takes a couple of seconds to the wake up and the reading of the display to match.
    I leave part of my code that make it works.
//controlSleep it's initialy true
/* 
V11 is a value display that shows '0' when the Node is awake, and '1' when is still sleeping (it only sees when you open the app until the program enters to BLYNK_READ).
*/

BLYNK_APP_DISCONNECTED()
{
  Blynk.virtualWrite(V11, 1);
  controlSleep = true;
}

BLYNK_READ(V11)
{
  Blynk.virtualWrite(V11, 0);
  controlSleep = false;
}

//Sleep is called in a function who is always repeating in the void loop()

void Sleep(void)
{
  if (controlSleep)
  {
    ESP.deepSleep(2e6);      
    //Sleep for 2 seconds instead of 5 (to recognize quickly the aperture of the app)
    if (WiFi.status() != WL_CONNECTED)
    {
      Serial.println("Conectando");
      WiFi.begin(ssid, pass);
    }
  }
}

It needs a condition if you forgot the app open, but it will be a work for tomorrow.

You could implement a long timeout timer… so if the App is still open 30 minutes (or whatever) later, then sleep anyhow.

Yeah, i though the same, is a little more of work, but well, I wanna polish my program the best as I can. Surely I will post the updated code when I make it.

2 Likes