ESP work offline

This is a nice thread to study Brown outs or reboots
It’s for Ethernet but the principles about dropping Blynk.begin() and replacing with Blynk.config() and Blynk.connect() etc are the same.

1 Like

That was a nice read. I kept the relevant code in OneNotes for reference. I was wondering if there is a place that some of this can be stored? It would save reading a long post but also great for quick reference. Maybe a different category that can only be published and not responded or added to.

1 Like

Hi Costas,

i tried the reconnect as it was in the post :

void MyWiFi()
{
  int mytimeout = millis() / 1000;
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if((millis() / 1000) > mytimeout + 12){ // try for less than 4 seconds to connect to WiFi router
  break;
}
  }

  if(WiFi.status() == WL_CONNECTED){  
Serial.print("\nIP address: ");
Serial.println(WiFi.localIP()); 
  }
  else{
Serial.println("\nCheck Router ");    
  }
  Blynk.config(auth);
  Connected2Blynk = Blynk.connect(1000);  // 1000 is a timeout of 3333 milliseconds 
  mytimeout = millis() / 1000;
  while (Blynk.connect() == false) { 
if((millis() / 1000) > (mytimeout + 12)){ // try for less than 4 seconds
  break;
}
  }  
} 

But the there is no Reconstruction to the internet, i simulate the connection/disconnection by using my
phone Hot Spot,
so in the phone i see that the ESP is connect, but the ESP show that it not connect to Blynk server:

nowTime: 6
Still connected to Blynk server
Tempfunc
work2: 30
minTime: 24
timeLeft: 24
nowTime: 6
timeLeft: 24
nowTime: 6
Tempfunc
work2: 30
minTime: 24
timeLeft: 24
nowTime: 6
timeLeft: 24
nowTime: 6
Tempfunc
work2: 30
minTime: 24
timeLeft: 24
nowTime: 6
timeLeft: 24
nowTime: 6
Tempfunc
work2: 30
minTime: 24
timeLeft: 24
nowTime: 6
timeLeft: 24
nowTime: 6
Tempfunc
work2: 30
minTime: 23
timeLeft: 23
nowTime: 7
timeLeft: 23
nowTime: 7
Tempfunc
work2: 30
minTime: 23
timeLeft: 23
nowTime: 7
timeLeft: 23
nowTime: 7
Not connected to Blynk server
.........................
Check Router 
[1002049] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.3 on ESP8266

[1002053] Connecting to blynk-cloud.com:8442
[1008195] Connecting to blynk-cloud.com:8442
[1015270] Connecting to blynk-cloud.com:8442
Tempfunc
work2: 30
minTime: 23
timeLeft: 23
nowTime: 7

What can be the reason?

We need to see your full sketch to be sure you have all the required elements.

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

#define boiler 14
#define OnLed  4   
#define systemLED 5

void buttonLedWidget();
void Temperature();
void LEDtoggle();
void OnTime();
void countTime();


int minutse = 0;
unsigned long minTime = 0;
unsigned long work2 = 0;
unsigned long nowTime = 0;
float ESP201ADCVref=1.10;
unsigned long count = 0;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

WidgetLED led1(0);
WidgetLED led5(3);
//WidgetLED led4(16);

SimpleTimer timer;

OneWire  ds(2);  // on pin 2 between VCC&DATA (a 4.7K resistor is necessary)

const int btnPin = 17;

WidgetLED led3(V3);

bool dudon = false;
bool dune = true;
bool manual = false;
int work = 0;
 
char auth[] = "1e0fc8aa350c47e88bbb078dc1f6302a";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Nir&Rotem";
char pass[] = "11335577";
bool Connected2Blynk = false;
//************ Menu widget *************
//
BLYNK_WRITE(V1) 
{
  int getTime = 0;
  switch (param.asInt())
  {
    case 1: // Item 1
      Serial.println("off");
      getTime = 0;
      break;
    case 2: // Item 1
      Serial.println("30 minuts");
      getTime = 30;
      break;
    case 3: // Item 2
      Serial.println("45 minuts");
      getTime = 45;
      break;
    case 4: // Item 3
      Serial.println("60 minuts");
      getTime = 60;
      break;
    case 5: // Item 4
      Serial.println("75 minuts");
      getTime = 75;
      break;
    case 6: // Item 5
      Serial.println("90 minuts");
      getTime = 90;
      break;
    case 7: // Item 6
      Serial.println("120 minuts");
      getTime = 120;
      break;
    default:
      Serial.println("off");
      getTime = 0 ;
  }
  work = getTime;
 // OnTime();
}
void setup()
{
  Serial.begin(115200);
  
//  Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "LEDANI_2.4", "0522941258");
  Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "Nir&Rotem", "11335577");
//   Blynk.begin("5a2d531baafa4c16855dfac4cbbc1275", "NIR", "11335577");
//   Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "hereO", "kgpswifi");
//   Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "iPhone", "123456789");
// attachInterrupt(digitalPinToInterrupt(0), pushButtton, CHANGE);

 pinMode(boiler, OUTPUT);
 pinMode(OnLed, OUTPUT);
 pinMode(systemLED, OUTPUT);
 digitalWrite(OnLed,HIGH);
 digitalWrite(systemLED,LOW);
 digitalWrite(boiler,HIGH);

  timer.setInterval(200L, pushButtton);
  timer.setInterval(10000L,Temperature);
  timer.setInterval(10000L,countTime);
  timer.setInterval(10000L,OnTime);
  timer.setInterval(10000L,sendTime);
  timer.setInterval(60000L, CheckConnection); // check if still connected every 11 seconds
  
 
  while (Blynk.connect()==false){}
    Serial.println("start");
  led1.on();
  //led4.on();
  led5.on();
  Blynk.virtualWrite(V1, "pick", 0);
}

bool lastbutton = 0;
void loop()
{
  unsigned int tout = millis();
  if(Blynk.connected()){    // only process Blynk commands if connected to server
     Blynk.run();
  }
  timer.run();
}

int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;

void pushButtton()
{
  bool button = digitalRead(0);  
  Blynk_Delay(1);
  bool button2 = digitalRead(0);
  button = button && button2;
  
  if(button && !lastbutton)
  {
    Serial.println("pressed"); 
    if (state == HIGH)
    {
      state = LOW;
      work = 130;
    }      
    else
    {
      state = HIGH;
      work = 0;
    }
  } 
  digitalWrite(V3, state);
  lastbutton = button;
}

void Temperature()
{
  Serial.println("Tempfunc");
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  
  if ( !ds.search(addr)) {
    //Serial.println("No more addresses.");
   // Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
 
  Blynk_Delay(1000);
 // delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  for ( i = 0; i < 9; i++)  // we need 9 bytes
  {          
    data[i] = ds.read();
  }
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) 
  {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) 
    {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } 
  else 
  {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  
  celsius = (float)raw / 16.0;
  Serial.print("celsius: ");
  Serial.println(celsius);
  Blynk.virtualWrite(V5,celsius);
}

void OnTime()
{
   unsigned long tout; 
   if((!dudon && dune) || work == 0 || (work == 130 && !dudon) )
   {
     work2 = work;  
     if(count < (work*6))
     {
        BoilerOn();
     }
   }
  if((count >= (work*6)) && dudon)
  { 
      Serial.println("tout");
      BoilerOff();
  }
   Serial.print("work2: ");
   Serial.println(work2);
   if (dudon)
   {
     minTime = work2 - count/6; 
     Serial.print("minTime: ");
     Serial.println((minTime));
     nowTime = (count/6);  
     sendTime();
   }
}

void sendTime()
{
  unsigned long timeLeft = 0;
 
  if(dudon &&((minTime > 0) && (work != 130))) timeLeft = (minTime);
  if(minTime <= 0  )timeLeft = 0;
  if(dudon && (minTime == 130))
  {
    Blynk.virtualWrite(V2,"ON");
    Blynk.virtualWrite(V1, "pick", 0);
  }
  else Blynk.virtualWrite(V2,timeLeft);
  
  if(dudon &&((nowTime > 0))) Blynk.virtualWrite(V4,nowTime);
  else
  {
    nowTime = 0;
    Blynk.virtualWrite(V4,nowTime);
  }
  Serial.print("timeLeft: ");
  Serial.println(timeLeft);
  Serial.print("nowTime: ");
  Serial.println(nowTime);
}

void countTime()
{
 if(dudon)
  {
    count++;
//    Serial.print("countTime:");
//    Serial.println(count);
  }
 else count = 0;
}

void BoilerOn()
{
  digitalWrite(boiler,LOW);
  digitalWrite(OnLed,LOW);
  digitalWrite(systemLED,HIGH);
  dudon = true;
  count = 0;
  Serial.println("dudon");
  Blynk.notify("Boiler ON");
  if(work == 130)Blynk.virtualWrite(V1, "pick", 0);
  state = LOW;
}

void BoilerOff()
{
  digitalWrite(boiler,HIGH);
  digitalWrite(OnLed,HIGH);
  digitalWrite(systemLED,LOW);
  Serial.println("dudoff");
  dudon = false;
  Blynk.virtualWrite(V1, "pick", 0);
  
  
  Blynk.notify("Boiler OFF");
  state = HIGH;
  work = 0;
  work2 = 0;
  minTime = 0;  
}

void Blynk_Delay(int milli)
{
  int end_time = millis() + milli;
  while (millis() < end_time){ }
}

void CheckConnection()
{
  Connected2Blynk = Blynk.connected();
    if(!Connected2Blynk)
    {
      Serial.println("Not connected to Blynk server");
      MyWiFi();  

    }
  else{
    Serial.println("Still connected to Blynk server");    
  }
}

void MyWiFi()
{
  int mytimeout = millis() / 1000;
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if((millis() / 1000) > mytimeout + 12){ // try for less than 4 seconds to connect to WiFi router
      break;
    }
  }

  if(WiFi.status() == WL_CONNECTED){  
    Serial.print("\nIP address: ");
    Serial.println(WiFi.localIP()); 
  }
  else{
    Serial.println("\nCheck Router ");    
  }
  Blynk.config(auth);
  Connected2Blynk = Blynk.connect(1000);  // 1000 is a timeout of 3333 milliseconds 
  mytimeout = millis() / 1000;
  while (Blynk.connect() == false) { 
    if((millis() / 1000) > (mytimeout + 12)){ // try for less than 4 seconds
      break;
    }
  }  
}

@ladan.nir the code you have used is nothing like that used in the link I provided.

Remove checkConnection() and MyWiFi() and see if this works for you. Ideally you shouldn’t use Blynk.begin() in setup because if internet is not available when you first boot the ESP or the ESP resets you whole project will fail. We can come back to that later but see if this works when you turn your phone hotspot off and back on etc.

unsigned int myConnectionTimeout =   5000;  // Connection Timeout (CT)
unsigned int blynkcheckInterval  =  60000;  // Check Server Frequency (CSF)


void setup(){
  Blynk.config(auth);  // put this as the first line of setup()
  //...................
}


void CheckConnection() {
  unsigned long startConnecting = millis();    
  while(!Blynk.connected()){
    Blynk.connect();  
    if(millis() > startConnecting + myConnectionTimeout){
      Serial.println("\tUnable to connect to server. ");
      break;
    }
  }
  Serial.print("\tChecking again in ");
  Serial.print(blynkcheckInterval / 1000.0);
  Serial.println("s.");
}

@ladan.nir I recently provided an up to date template called NoBlynkBlock.ino to bypass Blynk.begin() blocking routine for ESP’s. It’s contained in this thread Blynk is blocking if internet is down but also reproduced below. Basically it gives the ESP a regular WiFi connection and then tries to connect to the Blynk server. You will need to incorporate this into your sketch in place of Blynk.begin(). All tested as working within the last few weeks.

See ESP work offline for latest working template.

Hi Costas

the reconnection work great, thanks a lot.

Why do i need the myfunction() ?
and the checkBlynk() every 5 sec?

why you don’t recommend on use blynk.begin ?

It’s just a template for Blynkers to use, they would put their real functions / code in here.

because I don’t like to wait about doing nothing when I am testing code. Obviously Blynkers are expected to tailor the code to their requirements so change to 20s / 60s whatever you want. The only critical one is the connection timeout not how often you check for the server.

The begin in Blynk.begin() is for beginners not real Blynkers, hence the config() and connect() routines. Basically begin() is a blocking routine that causes problems in real projects but it’s good to get Blynkers started.

Ok,

How can i make the sketch run even i don’t have wifi connection from the begging ?
I noticed that the code stuck until the first wifi connection occur.

sorry i meant to beginning

Is that with the NoBlynkBlock.ino or your modified version?
The template should work with or without a WiFi connection.

it happens with this code:

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

#define boiler 14
#define OnLed  4   
#define systemLED 5

void buttonLedWidget();
void Temperature();
void LEDtoggle();
void OnTime();
void countTime();


int minutse = 0;
unsigned long minTime = 0;
unsigned long work2 = 0;
unsigned long nowTime = 0;
float ESP201ADCVref=1.10;
unsigned long count = 0;

unsigned int myConnectionTimeout =   5000;  // Connection Timeout (CT)
unsigned int blynkcheckInterval  =  60000;  // Check Server Frequency (CSF)
unsigned long mytimeout = 3000; // 3.0s timeout

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

WidgetLED led1(0);
WidgetLED led5(3);
//WidgetLED led4(16);

SimpleTimer timer;

OneWire  ds(2);  // on pin 2 between VCC&DATA (a 4.7K resistor is necessary)

const int btnPin = 17;

WidgetLED led3(V3);

bool dudon = false;
bool dune = true;
bool manual = false;
bool Connected2Blynk = false;

int work = 0;
 
char auth[] = "1e0fc8aa350c47e88bbb078dc1f6302a";
char server[] = "blynk-cloud.com";
char ssid[] = "Nir&Rotem";
char pass[] = "11335577";


//************ Menu widget *************
//
BLYNK_WRITE(V1) 
{
  int getTime = 0;
  switch (param.asInt())
  {
    case 1: // Item 1
      Serial.println("off");
      getTime = 0;
      break;
    case 2: // Item 1
      Serial.println("30 minuts");
      getTime = 30;
      break;
    case 3: // Item 2
      Serial.println("45 minuts");
      getTime = 45;
      break;
    case 4: // Item 3
      Serial.println("60 minuts");
      getTime = 60;
      break;
    case 5: // Item 4
      Serial.println("75 minuts");
      getTime = 75;
      break;
    case 6: // Item 5
      Serial.println("90 minuts");
      getTime = 90;
      break;
    case 7: // Item 6
      Serial.println("120 minuts");
      getTime = 120;
      break;
    default:
      Serial.println("off");
      getTime = 0 ;
  }
  work = getTime;
 // OnTime();
}
void setup()
{
  Serial.begin(115200);
  
  Blynk.connectWiFi(ssid, pass);
  Blynk.config(auth,server); 
  checkBlynk();
  
//  Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "LEDANI_2.4", "0522941258");
//  Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "Nir&Rotem", "11335577");
//   Blynk.begin("5a2d531baafa4c16855dfac4cbbc1275", "NIR", "11335577");
//   Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "hereO", "kgpswifi");
//   Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "iPhone", "123456789");
// attachInterrupt(digitalPinToInterrupt(0), pushButtton, CHANGE);

 pinMode(boiler, OUTPUT);
 pinMode(OnLed, OUTPUT);
 pinMode(systemLED, OUTPUT);
 digitalWrite(OnLed,HIGH);
 digitalWrite(systemLED,LOW);
 digitalWrite(boiler,HIGH);

  timer.setInterval(200L, pushButtton);
  timer.setInterval(10000L,Temperature);
  timer.setInterval(10000L,countTime);
  timer.setInterval(10000L,OnTime);
  timer.setInterval(10000L,sendTime);
  timer.setInterval(60000L, CheckConnection); // check if still connected every 11 seconds
  timer.setInterval(30000L, checkBlynk);  // every 5s check if connected to server
  

//  while (Blynk.connect()==false){}
    Serial.println("start");
  led1.on();
  //led4.on();
  led5.on();
  Blynk.virtualWrite(V1, "pick", 0);
}

bool lastbutton = 0;
void loop()
{
  if(Blynk.connected())  // only process Blynk commands if connected to server
  {    
     Blynk.run();
  }
  timer.run();
}

int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;

void pushButtton()
{
  bool button = digitalRead(0);  
  Blynk_Delay(1);
  bool button2 = digitalRead(0);
  button = button && button2;
  
  if(button && !lastbutton)
  {
    Serial.println("pressed"); 
    if (state == HIGH)
    {
      state = LOW;
      work = 130;
    }      
    else
    {
      state = HIGH;
      work = 0;
    }
  } 
  digitalWrite(V3, state);
  lastbutton = button;
}

void Temperature()
{
  Serial.println("Tempfunc");
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  
  if ( !ds.search(addr)) {
    //Serial.println("No more addresses.");
   // Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
 
  Blynk_Delay(1000);
 // delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  for ( i = 0; i < 9; i++)  // we need 9 bytes
  {          
    data[i] = ds.read();
  }
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) 
  {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) 
    {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } 
  else 
  {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  
  celsius = (float)raw / 16.0;
  Serial.print("celsius: ");
  Serial.println(celsius);
  Blynk.virtualWrite(V5,celsius);
}

void OnTime()
{
   unsigned long tout; 
   if((!dudon && dune) || work == 0 || (work == 130 && !dudon) )
   {
     work2 = work;  
     if(count < (work*6))
     {
        BoilerOn();
     }
   }
  if((count >= (work*6)) && dudon)
  { 
      Serial.println("tout");
      BoilerOff();
  }
   Serial.print("work2: ");
   Serial.println(work2);
   if (dudon)
   {
     minTime = work2 - count/6; 
     Serial.print("minTime: ");
     Serial.println((minTime));
     nowTime = (count/6);  
     sendTime();
   }
}

void sendTime()
{
  unsigned long timeLeft = 0;
 
  if(dudon &&((minTime > 0) && (work != 130))) timeLeft = (minTime);
  if(minTime <= 0  )timeLeft = 0;
  if(dudon && (minTime == 130))
  {
    Blynk.virtualWrite(V2,"ON");
    Blynk.virtualWrite(V1, "pick", 0);
  }
  else Blynk.virtualWrite(V2,timeLeft);
  
  if(dudon &&((nowTime > 0))) Blynk.virtualWrite(V4,nowTime);
  else
  {
    nowTime = 0;
    Blynk.virtualWrite(V4,nowTime);
  }
  Serial.print("timeLeft: ");
  Serial.println(timeLeft);
  Serial.print("nowTime: ");
  Serial.println(nowTime);
}

void countTime()
{
 if(dudon)
  {
    count++;
//    Serial.print("countTime:");
//    Serial.println(count);
  }
 else count = 0;
}

void BoilerOn()
{
  digitalWrite(boiler,LOW);
  digitalWrite(OnLed,LOW);
  digitalWrite(systemLED,HIGH);
  dudon = true;
  count = 0;
  Serial.println("dudon");
  Blynk.notify("Boiler ON");
  if(work == 130)Blynk.virtualWrite(V1, "pick", 0);
  state = LOW;
}

void BoilerOff()
{
  digitalWrite(boiler,HIGH);
  digitalWrite(OnLed,HIGH);
  digitalWrite(systemLED,LOW);
  Serial.println("dudoff");
  dudon = false;
  Blynk.virtualWrite(V1, "pick", 0);
  
  
  Blynk.notify("Boiler OFF");
  state = HIGH;
  work = 0;
  work2 = 0;
  minTime = 0;  
}

void Blynk_Delay(int milli)
{
  int end_time = millis() + milli;
  while (millis() < end_time){ }
}

//void CheckConnection()
//{
//  Connected2Blynk = Blynk.connected();
//    if(!Connected2Blynk)
//    {
//      Serial.println("Not connected to Blynk server");
//      MyWiFi();  
//
//    }
//  else{
//    Serial.println("Still connected to Blynk server");    
//  }
//}
//
//void MyWiFi()
//{
//  int mytimeout = millis() / 1000;
//  WiFi.begin(ssid, pass);
//  while (WiFi.status() != WL_CONNECTED) {
//    delay(500);
//    Serial.print(".");
//    if((millis() / 1000) > mytimeout + 20){ // try for less than 4 seconds to connect to WiFi router
//      break;
//    }
//  }
//
//  if(WiFi.status() == WL_CONNECTED){  
//    Serial.print("\nIP address: ");
//    Serial.println(WiFi.localIP()); 
//  }
//  else{
//    Serial.println("\nCheck Router ");    
//  }
//  Blynk.config(auth);
//  Connected2Blynk = Blynk.connect(2000);  // 1000 is a timeout of 3333 milliseconds 
//  mytimeout = millis() / 1000;
//  while (Blynk.connect() == false) { 
//    if((millis() / 1000) > (mytimeout + 12)){ // try for less than 4 seconds
//      break;
//    }
//  }  
//}

void CheckConnection() {
  unsigned long startConnecting = millis();    
  while(!Blynk.connected()){
    Blynk.connect();  
    if(millis() > startConnecting + myConnectionTimeout){
      Serial.println("\tUnable to connect to server. ");
      break;
    }
  }
  Serial.print("\tChecking again in ");
  Serial.print(blynkcheckInterval / 1000.0);
  Serial.println("s.");
}


void checkBlynk() 
{ 
  Serial.println("checkBlynk");
  if (WiFi.status() == WL_CONNECTED)  
  {
    unsigned long startConnecting = millis();    
    while(!Blynk.connected()){
      Blynk.connect();  //
      if(millis() > startConnecting + mytimeout){
        Serial.print("Unable to connect to server. ");
        break;
      }
    }
  }
  if (WiFi.status() == 1) {
    Serial.print("No WiFi. ");
  } 
  Serial.println("Checking again in 5s."); 
}

this is thee log:
Üø[294]Connecting to Nir&Rotem //stuck in here and not execute the sketch

Just run the template for me and confirm it’s OK without WiFi. I’m 99% sure it’s OK.

i just program the code i shared in my last msg, and if there is no wifi connection when the ESP
turn on , it will stuck in this line: Üø[294]Connecting to Nir&Rotem

Your last code has got 2 lots of connection management code.

It’s down to you now but I suggest you test and study the template and incorporate into your sketch.

i don’t know where you see 2 connections managment,
i noticed that if i don’t have wifi connection from the start, the code stuck on Blynk.connectWiFi(ssid, pass);
and not move on.

Keep looking.

that’s because you are not using the template code correctly.

I’m sorry but i don’t see where my mistake is

Serial Monitor from the template, starting with the router off and then turned on. Wow I here you say perhaps I should use the template. You are missing the WiFi connection part in your sketch and the doubling up of connection management in checkConnection() AND checkBlynk().


	WiFi not connected yet.
	Check the WiFi router. 
[8540] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.8 on Arduino

	No WiFi. 	Checking again in 25s.

	Look, no Blynk  block.
	Look, no Blynk  block.
	Look, no Blynk  block.
	No WiFi. 	Checking again in 25s.

	Look, no Blynk  block.
	Look, no Blynk  block.
	Look, no Blynk  block.
	No WiFi. 	Checking again in 25s.

	Look, no Blynk  block.
	Look, no Blynk  block.
	Look, no Blynk  block.
	Look, no Blynk  block.
	WiFi still  connected.
[80039] Connecting to blynk-cloud.com:8442
[80278] Ready (ping: 76ms).
	Checking again in 25s.

	Look, no Blynk  block.
	WiFi still  connected.
	Look, no Blynk  block.
	WiFi still  connected.
	Look, no Blynk  block.
	WiFi still  connected.