ESP8266 Blynk - Run program when not connected to WiFi

Hi,

I’m using the Blynk ESP8266 Standalone example sketch to control the ESP8266 and was wondering how I could run a section of code (e.g. to blink an LED) when there is no WiFi connected. I’ve tried creating a while (WiFi.status() !=WL_CONNECTED) loop but it doesn’t seem to run at all when WiFi is disconnected.

The code for the example sketch is:

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

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

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "ssid", "pass");
}

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

How can I flash an LED when the connection is dropped/no connection is made?

Thanks

Blynk.run(); really organize the synchro with the server and your variables

You can inject your own code in the loop() after   Blynk.run();

what you need to do there is leverage some of their library functions. you can do something like

 boolean isconnected = Blynk.connected();

and do stuff based on this boolean,

and if it is not connected and you want to retry connecting

Blynk.connect()

etc

Like this?

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

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

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "ssid", "pass");
}

void loop()
{
  Blynk.run();

  bool isconnected = Blynk.connected();
    if (isconnected == false){
    digitalWrite(2,HIGH);
    delay(1000);
    digitalWrite(2,LOW);
    delay(1000);
    }     
  }

I put in an incorrect SSID/Password to test out the code and it still doesn’t seem to function properly. :confused:

I think your problem is

  Blynk.begin(auth, "ssid", "pass");

if you let Blynk handle the connection to the internet then you are at a too high level of abstraction

What you need to do is manage yourself you WiFi session. see the ESP8266_Standalone_SmartConfig example they bundle with the library

SmartConfig let you configure the Wifi SSID and password with your mobile phone (and not in the Arduino sketch). Search for ESP8266 Smart Config in the Google Play Store to download the application.

But you don't need to use beginSmartConfig for what you want to do - it's more as an idea: if you read that code what they do is that if WiFi is not getting connected after 10 tries then they give SmartConfig a try.

What you can see there is that they set up wifi and then they call Blynk.config(auth);

this way you have separated establishing the wifi connection, from identifying yourself to the Blynk server.

So basically if you want to use Blynk without auto-connecting, you manage yourself when you want to have your wifi connection on.

When connection is on, make sure you call Blynk.run(); to ensure synchro is working as part of the main loop and when it's off you do what you want. You can turn on and off the wifi connection as you see fit.