Blynk not connecting to server if during start-setup time not have wifi connection

That’s not the way I’d do it.
Have you tested this fully is a variety of different scenarios?

Pete.

You are right, it is not checking that function.

/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v1.1.0 on Arduino Mega

#StandWithUkraine https://bit.ly/swua

[521] Connecting to xxxx
[3569] WIFI DISCONNECT
AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
compile time:May 20 2016 15:08:19
OK
[13611] Failed to connect WiFi

That’s because Blynk.begin is a blocking function and will halt all code execution at that point if a connection to WiFi or the Blynk server can’t be established.

That’s why is said…

Pete.

Dear Pete,

Have wifi check function for BlynkSimpleShieldEsp8266.h library ?
Inside library (https://github.com/blynkkk/blynk-library/blob/master/src/BlynkSimpleShieldEsp8266.h) already tested “connectWiFi” , “BlynkWifi” , “wifi”, “WiFi.status()” and all give error.

How are you testing these functions?

Where is your sketch?

How is this connected with the fact that Blynk.begin is a blocking function for all board types?

Pete.

I have turned off wifi router from power supply and started arduino to check connection
Code:

#define BLYNK_TEMPLATE_ID "xxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxx"

// Send Blynk user messages to the hardware serial port…
#define BLYNK_PRINT Serial 

//blynk library
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

//blynk auth
char auth[] = BLYNK_AUTH_TOKEN;

// wifi password, login
char ssid[] = "xxxx";
char pass[] = "xxxxx";


// Define your ESP8266 baud rate:
#define ESP8266_BAUD 115200

// Tell the Blynk library to use the  Hardware Serial port 1 for WiFi..
ESP8266 wifi(&Serial3);

BlynkTimer timer;

void setup() 

{

   
   // Initialise the debug serial port
      Serial.begin(115200);

   // Initialise the Hardware serial1 port…
      Serial3.begin(ESP8266_BAUD);
      delay(10);
      Blynk.begin(auth, wifi, ssid, pass);
      while ( !Blynk.connected())
            {
                timer.setInterval(7000L,ConnectServerAgain); 
            }
}

  void  ConnectServerAgain() 
 {
   Blynk.begin(auth, wifi, ssid, pass);
   Serial.println("reconnection to server");
   }

And on the Serial monitor i dont see any reconnection to server comments, mean that it is freeze somewhere.

Where is your void loop?

Pete.

Sorry,

void loop() 
{

  Blynk.run();
  timer.run();
}

You don’t seem to understand the concept of a blocking function.

Pete.

On some topic adviced to add Wifi.h library so i added, now i can check WiFi.status()

void setup() 

 {

    //Blink ve serial xeberlesme ucun 
    // Initialise the debug serial port
       Serial.begin(115200);

    // Initialise the Hardware serial1 port…
       Serial3.begin(ESP8266_BAUD);
       delay(10);
       WiFi.begin(ssid, pass);
      while (WiFi.status()!=WL_CONNECTED  )
             {
              delay(500);
              Serial.print(".");
                 timer.setInterval(2000L,ConnectServerAgain); 
             }

             
 if(WiFi.status()==WL_CONNECTED  ) 

 {
   Blynk.begin(auth, wifi, ssid, pass);
  }
}

   void  ConnectServerAgain() 
  {
    WiFi.begin(ssid, pass);
Serial.println("connection to wifi");
    delay(500);
    
    }

Blynk.config(auth) give error .
Upper codes not give any error but device not connection to wifi with this function (WiFi.begin(ssid, pass); , it is strange.

Serial monitor just show “…” comments

Blynk.connectWiFi(ssid, pass); also not work
Serial monitor just continiously show
[510] Connecting to xxxx

Dear Pete,

I’ve looked a little deeper, but there are errors that don’t match with the documentation.
@Gunner wrote in this topic how to solve it

char server[] = "blynk-cloud.com";
unsigned int port = 8080;
  wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
  Blynk.config(wifi, auth, server, port);
  if (Blynk.connectWiFi(ssid, pass)) {
    Blynk.connect();
  }

In my case (maybe blynk 2 version something changed) auth not taken

[23] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.1.0 on Arduino Mega

 #StandWithUkraine    https://bit.ly/swua


[534] Connecting to Ibadovlar
[3582] WIFI CONNECTED
AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
compile time:May 20 2016 15:08:19
OK
[8652] +CIFSR:STAIP,"192.168.1.7"
+CIFSR:STAMAC,"dc:4f:22:22:29:8d"
[8653] Connected to WiFi
[19844] Invalid auth token

Also, i have tried to delay before begin function, it is strange that after a while delay time begin function not working.

Yes, the server is now blynk.cloud and the port is 80

Pete.

1 Like

yes, i found it also from sketch generator.
Thanks

You’d also be well advised to look at how the BlynkTimers are initialised in the Sketch Builder examples.

Pete.

Tested, work normally for now )
Thanks for support

My code like this

  wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266 - (i dont know what it is ) )))
    Blynk.config(wifi, auth, server, port);
  while (Blynk.connectWiFi(ssid, pass)==false) {
    delay(500);
    Serial.println("repeat checking");

  } 


  while (Blynk.connect() == false) {
    // Wait until connected
  }

I’d avoid using while commands if I were you, they will result in code blocking in just the same way that your original use of Blynk.begin did.

Pete.

I release that, begin function working once and if fault accur, block system, but while working repeatly, trying to connect server.
If function also working once, i dont know blynk timer inside if will work or not.

Okay, we’ll if you’re happy with the functionality then leave it as it is.

Pete.

1 Like

I will happy also if you share another solution)
Thanks for support