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...