Drain battery when try connecting to wifi

I have ESP8266 project working with battery.
The deepsleep in this project work fine.
But problem is, how to stop draining battery when the ESP8266 can’t connect to wifi?

i thought, Blynk.connect(); has 30 sec timout and after this timeout esp8266 starts deepsleep.
But if i lost connection i have still 70 mA consumption till connection is back.

define ONE_WIRE_BUS 2
define TEMPERATURE_PRECISION 12

OneWire oneWire(2);
DallasTemperature sensors(&oneWire);
DeviceAddress thermometer1, thermometer2;
char auth[] = “”;

void setup()
{
Blynk.begin(auth, “”, “”);
sensors.begin();
sensors.getAddress(thermometer1, 0);
sensors.getAddress(thermometer2, 1);
sensors.setResolution(thermometer1, TEMPERATURE_PRECISION);
sensors.setResolution(thermometer2, TEMPERATURE_PRECISION);
Blynk.connect();
}

void sendTemps()
{
sensors.requestTemperatures();
float tempC1 = sensors.getTempC(thermometer1);
float tempC2 = sensors.getTempC(thermometer2);
Blynk.virtualWrite(1, String(tempC1, 1));
Blynk.virtualWrite(2, String(tempC2, 1));
delay(100);
}

void loop()
{
Blynk.run();
sendTemps();
ESP.deepSleep(300 * 1000000); // 300 sec
delay(100);
}

You can try this:

WiFi.status. It returns if it has an IP address. You can probably use a timer to time-out and goto deep sleep if it can’t connect within a certain time.

I think problem is that program stay on Blynk.connect(); till connection is ok.

You can disconnect manual:

http://docs.blynk.cc/#blynk-firmware-connection-management

There is some info there.

I don’t need disconnect.

I would like to try 30 sec to connect and after this timeout go to deepsleep.
But I stay on “try connect” and i can’t leave from this

After 30s it should retry, but I’m not sure where that is performed, I think in Blynk.run() and since that runs in your loop, you can do things with it before it runs again. e.g. skip it with a state flag or something and time that out or go to deep sleep.

Pseudo code:

int state;

void loop()
{
  if(state != 1)
  {
    Blynk.run();
  }

   if(!blynk.connected)
  {
    state = 0;
    deepsleep;
  }
}

void doSomething()
{
  deepsleep();
  state = 1
}

My logic is probably all flawed, but I think this is sort of the way to do it.

Just look ant the “connect()” function implementation.