BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Blynk_app_connected() / blynk_app_disconnected()

It may very well be that I don’t understand the purpose these two functions. I took them to mean that they would fire if the App on my phone connects / disconnects with the Blynk server. But, nothing I do makes them fire – even turning the phone off then on again. Am I misunderstanding the purpose of these functions?

Below is a simple code I tried to test this. All works as expected except for BLYNK_APP_CONNECTED() / BLYNK_APP_DISCONNECTED().

Thanks if advance for your help.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define mySSID "MY_SSID"
#define PASS "MY_PASSWORD"
#define WAIT_FOR_WIFI 10000

char auth[] = "MY_BLYNK_AUTH";
void initWiFi(void);

void setup()
{
  Serial.begin(115200);
  delay(2000);
  initWiFi();
  Blynk.config(auth);
  Blynk.connect();
  Serial.println(F("Back From Blynk.config"));
}

void loop()
{
  Blynk.run();
}

BLYNK_WRITE(V0) //Button Widget is writing to pin V1
{
  if (param.asInt() == 1) {
    Serial.println("Button Pressed");
  } else {
    Serial.println("Button Released");
  }
}

BLYNK_CONNECTED() {
  Serial.println("Connected to Blynk Server");
}

BLYNK_APP_CONNECTED() {
  Serial.println("Blynk App Connected");
}

BLYNK_APP_DISCONNECTED() {
  Serial.println("Blynk App Disconnected");
}

void initWiFi() {
  uint32_t startTime;
  wl_status_t wifiStatus;
  do {
    WiFi.disconnect();
    startTime = millis();
    Serial.println();
    Serial.println(F("Starting WiFi Connection"));
    WiFi.begin(mySSID, PASS);
    while ((wifiStatus = WiFi.status()) != WL_CONNECTED) { // Wait for connection
      delay(500);
      Serial.print(wifiStatus);
      Serial.print(" ");
      if (millis() - startTime >= WAIT_FOR_WIFI) {
        Serial.println();
        break;
      }
    }
  } while (wifiStatus != WL_CONNECTED);
  Serial.println();
  Serial.print(F("WiFi Status = "));
  Serial.println(WiFi.status());
  Serial.print(F("SSID: "));
  Serial.println(mySSID);
  Serial.println(F("IP Address: "));
  Serial.println(WiFi.localIP());
}

I can’t recall when this came out, but if your installed App version is older, then they will have no effect.

But if you are running current App version, then make sure you have activated the “NOTIFY DEVICES WHEN APP CONNECTED”… I think it is required.

1 Like

Hello. You need also to enable this feature within your project settings.

Got it, works now. Thanks!!!

I notice that the functions fire when starting / stopping app on my phone, but not when hitting the Stop (Square) and Run (Triangle) symbols in the app. Is that by design?

Stopping a Project is not stopping (disabling) the App, as the App is still running (as are any other running projects), just not that particular project that you stopped.

Now, when closing the App (as in having moved on to something else in your phone) it is still “running”, but in background, and might also be considered as still “enabled”, thus no notification to the MCU… but I haven’t tested that.

Understood. Thanks for the quick reply.

Hi, I cannot get the Blynk_app_connected nor disconnected function to fire.
I have a sketch on ESP8266 identical to the one in the user guide except the the wifi libs are added as the per the example at the top of this thread.

I enabled the Notify Devices When App Connected of course.
I have tried IOS and Android Blynk App - no difference.
My Token is correct - since I can connect from the Blynk app to the ESP.

I have stopped and started the blynk app in numerous ways including the play button and killing the app.

What is the trick to getting this to work??
It is critical to my Blynk usage since there is no other way for my Robot drive to know when the Blynk App Joystick has been disconnected and to stop using the velocity values - rather than take out the nearest obstacle.
Thanks again,
Mark

Does this use Wi-Fi or Bluetooth?

Pete.

Hi Pete. Wifi.

Here is my test sketch…

  This example code is in public domain.
 *************************************************************
  This sketch shows how to handle App connected/disconnected events.
  NOTE:
    Be sure to enable "send app connect command"
    in your Project Settings!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <SPI.h>
//#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxx";


// Your WiFi credentials.
 
char ssid[] = "xxxxxxx";
char pass[] = "xxxxxxx";

// This is called when Smartphone App is opened
BLYNK_APP_CONNECTED() {
  Serial.println("App Connected.");
}

// This is called when Smartphone App is closed
BLYNK_APP_DISCONNECTED() {
  Serial.println("App Disconnected.");
}


void setup()
{
  // Debug console
  Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
  // Blynk.begin(auth);
//  Blynk.config(auth);
//  Blynk.connect();
}

void loop()
{
  Blynk.run();
}

@Mark_Hamester please edit the post where you posted your sketch and use triple backticks at the beginning and end of your code rather than the characters that you have used.
Triple backticks look like this:
```

Pete.

Ok done that thanks Pete

It’s been noted in the comments above that this does NOT cause the BLYNK_APP_DISCONNECTED() callback to fire.

How many devices do you have logged-in to your the Blynk account when you are doing these tests? If you have the app running on multiple devices, including your Wife’s phone and that old tablet that you only really use for facebook, then I think they ALL need to have the Blynk app stopped at the same time for the app to be in the disconnected state.

Pete.

Hi Pete, I did what you suggested, logged out of all known devices. I still do not get a connected or disconnected event on the ESP - either using the test sketch or my Robot drive sketch.

Works well from the other end, ie the Blynk app reliably and quickly provides a “app-name connected” and “app-name disconnected” message - at least when using IOS.

Its a shame as we will not be able to use this platform for this application.
But thanks for your help!

Mark

Hey Pete, good news. I found my problem. I was running an old ES8266 core. Updated to the latest and its works well. Thanks again for helping me with this.

When I start or close the blynk App the esp gets a notification within secs.
If I disconnect the internet connection from the device running the blynk app I get a disconnected message about 30-40 secs later. Would be nice if this interval could be made shorter, but at least now I have way to track state end to end for my real time control.
Mark

You can try this setting and see if it works…