Check connection status of smartphone using ESP8266

I need help with my project. This code initially runs, but I want to add a function where if my phone is disconnected from the Blynk server, the ESP8266 will turn off all pins. In the code, I relied on ChatGPT for the issue but did not get a solution. The method used by ChatGPT is by sending “ping-pong” messages to the Blynk server to check whether the phone is disconnected from the internet. I tried simplifying the code as much as I can (I am a beginner) but the code still wouldn’t work. I would like to ask for your assistance in this issue. I marked the lines that ChatGPT added to my code as //ChatGPT code and the serial monitor messages below as well.

ESP8266 code:

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_FIRMWARE_VERSION "1.3.2"

#define BLYNK_PRINT Serial
#define BLYNK_DEBUG
#define APP_DEBUG

#include "BlynkEdgent.h"

int physicalButtonPin = D1; // Pin connected to the pullup button
int sliderPin1 = D2;        // Pin connected to slider 1
int sliderPin2 = D5;        // Pin connected to slider 2
int sliderPin3 = D6;        // Pin connected to slider 3

int virtualSliderPin = V1; // Virtual Pin for the slider
int virtualPingPin = V2;   // Virtual Pin for sending "ping"

BlynkTimer timer;
bool phoneConnected = true;  // ChatGPT code

void setup()
{
  Serial.begin(115200);
  delay(100);

  pinMode(physicalButtonPin, INPUT_PULLUP);
  pinMode(sliderPin1, OUTPUT);
  pinMode(sliderPin2, OUTPUT);
  pinMode(sliderPin3, OUTPUT);

  digitalWrite(sliderPin1, LOW);
  digitalWrite(sliderPin2, LOW);
  digitalWrite(sliderPin3, LOW);

  timer.setInterval(5000L, checkPhoneConnection); // ChatGPT code
  BlynkEdgent.begin();
}

void loop()
{
  BlynkEdgent.run();
  timer.run();
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(virtualSliderPin);
  Blynk.syncVirtual(virtualPingPin);   //ChatGPT code
}

BLYNK_WRITE(V1)
{
  int sliderValue = param.asInt();
  if (sliderValue == 1)
  {
    digitalWrite(sliderPin1, HIGH);
    digitalWrite(sliderPin2, LOW);
    digitalWrite(sliderPin3, LOW);
  }
  else if (sliderValue == 2)
  {
    digitalWrite(sliderPin1, LOW);
    digitalWrite(sliderPin2, HIGH);
    digitalWrite(sliderPin3, LOW);
  }
  else if (sliderValue == 3)
  {
    digitalWrite(sliderPin1, LOW);
    digitalWrite(sliderPin2, LOW);
    digitalWrite(sliderPin3, HIGH);
  }
  else
  {
    digitalWrite(sliderPin1, LOW);
    digitalWrite(sliderPin2, LOW);
    digitalWrite(sliderPin3, LOW);
  }
}

BLYNK_WRITE(V2)   //ChatGPT code
{
  // Read the value received on virtual pin V2
  String response = param.asStr();

  // Print the received response to Serial Monitor
  Serial.print("Received response: ");
  Serial.println(response);

  // Check if the received response is a "ping"
  if (response.equalsIgnoreCase("ping")) {
    // The phone is connected
    phoneConnected = true;
  }
}

void checkPhoneConnection()   //ChatGPT code
{
  if (phoneConnected)
  {
    Serial.println("Phone is connected");
  }
  else
  {
    Serial.println("Phone is not connected");
  }

  phoneConnected = false; // Assume the phone is disconnected
  Blynk.virtualWrite(virtualPingPin, "ping"); // Send "ping" on the virtualPingPin

  // Add a delay to avoid spamming Blynk server
  delay(1000);
}

Serial Monitor:

[7090] Ready (ping: 12ms).
[7090] Free RAM: 21392
[7090] >[14|00|01|00|08]
[7091] >pm[00]5[00]out
[7157] <[11|00|02|00]|mcu[00]1.3.2[00]fw-type[00]TMPL6vFLdJdIl[00]build[00]Dec[20]11[20]2023[20]18:33:57[00]blynk[00]1.3.2[00]h-beat[00]45[00]buff-in[00]1024[00]dev[00]ESP8266[00]tmpl[00]TMPL6vFLdJdIl
[7182] >[00|00|02|00|C8]
[7248] <[10|00|03|00|04]vr[00]1
[7270] >[14|00|03|00|06]
[7270] >vw[00]1[00]0
[7334] <[10|00|04|00|04]vr[00]2
[7360] >[14|00|04|00|09]
[7361] >vw[00]2[00]ping
Received response: ping
[7362] CONNECTING_CLOUD => RUNNING
Phone is connected
[7428] <[14|00|05|00|09]vw[00]2[00]ping
Phone is not connected
[10391] <[14|00|06|00|09]vw[00]2[00]ping
Phone is not connected
[15391] <[14|00|07|00|09]vw[00]2[00]ping
Phone is not connected
//and so on...

Why?
What’s the benefit to you of doing this?

The ChatGPT code is nonesense and achieves nothing useful.

BTW, I’d reccomend commenting-out this line…

because the debug data isn’t telling you anything useful.

Pete.

In case of the disconnection, I want to be able to control the system manually. But if I don’t turn off all the pins, the system won’t function correctly. Hope that helps.

Noted, I will comment it out.
Thanks Pete.

This makes no sense.
Your question was about detecting if a device that is running the Blynk app is connected to the server. Blynk is designed so that the mobile dashboard can be running on multiple devices simultaneously, or none at all, without affecting the on/offline status of the device itself.

The only thing that makes sense would be if your phone was being used as the hotspot to provide internet connectivity for your device.

I think it would help if you explained your setup in more detail, and the issue 8n more detail.

Pete.

Yes, by that I mean if the phone is connected to the internet to connect to the server. If the phone is disconnected from the internet, I cannot control the widgets on the Blynk app.

I understand this, but I am using only one device for the mobile dashboard and I want to check the connection between the mobile device and the Blynk server. If the mobile device is disconnected from the internet, the ESP8266 will turn off all output pins. The situation of the problem is that if the ESP8266 is still connected to the internet (Blynk server) but the mobile device is not connected. I want it to do this so that I can control my project manually if the mobile device is disconnected from the internet. And to control it manually, I need to turn off all output pins for it to work properly. Hope that helps.

Thanks Pete.

Not really.

Blynk.Legacy did have a way to tell if a mobile device was connected, but that functionality isn’t available in Blynk.IoT.

Pete.

I see. Thank you anyways. I will try and find another method.

I’m still unclear about the need for any method.

You’d normally write code to run on your device that works autonomously, controlling whatever systems you need without any user input.
If you take that approach then it doesn’t matter whether your phone is connected or not, or if it’s connected and your asleep, in a meeting or driving and unable to control your device from the app as a result.

Pete.