BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Location of Blynk.config() vs Blynk.connect()


#1

I want to check connectivity to Blynk in a timer and attempt reconnect if the connection was lost.

I’d think that Blynk.config(wifi, auth) should be called once in setup() and then Blynk.connect()' be in a timer function? Or doesBlynk.config(wifi, auth)need to be called right beforeBlynk.connect()’ so it too would need to be in the timer function?


#2

There are many references to this and similar connection checks and reconnects in the forum already… have you tried searching?


#3

Absolutely, but none address this specific question.

I see one that puts the Blynk.config() in the timer loop with Blynk.connect(), however, that doesn’t mean that it is correct, or preferred. That seems like it could be additional overhead, but not knowing the full workings of the code, I wouldn’t know.

Though I am trying it both ways, right now, I am having a heck of a time with continued erratic results in my sketch, and was hoping to eliminate this as a potential issue.

Was hoping someone knowledgeable of best practices as it relates to the code could respond as to best practice for the location of each of these calls within the hierarchy of the overall sketch.


#4

The connection automatically reconnects when it drops. What reason do you need another check? (Just curious)


#5

You didn’t have a specific question, rather some suppositions of how you thought it might work and then a couple of joined questions.

The basic flow (if you would have searched and did some reading) is that you call your WiFi connection first (makes sense, how could the hardware connect if it didn’t have a method established first), then chose to have Blynk.begin()(and block the code flow until it does) or you can chose to setup a non-blocking form of connection attempts with Blynk.config() and even add various timer routines to check on connection with Blynk.connect() and reconnect if required.

However, as there is no One-Way of doing it… I recommend spending some time re-reading those other posts and checking the Doc’s for these these commands:
http://docs.blynk.cc/#blynk-firmware-configuration


#6

Ok, interesting. That was not what I was finding, so I thought I’d put in an explicit timer function to check and force reconnection.


#7

Mine even blocks when the connection drops (which I want, but can be changed via the Blynk.config() method).

New features:
- Blynk.begin() now blocks until server is connected

Edit:

A real-world use case…
I have many sensors all over the house… they are all just ESP with basic Blynk.begin() connections… many out of reach so can’t cycle power.
I often take down the Blynk server to update it, or reboot the NAS/Router etc…
I never touch any sensors’ power… but they reconnect automatically as soon as the server it back up without a reboot or special code.


How to make a pocket hotspot power on if it is not
#8

I have done hours of reading. And the docs are not very detailed or consistent. (for example, the docs just say that Blynk.config() can be called one of 2 ways:
Blynk.config(auth)
Blynk.config(auth, server, port)
Wrong…it depends on which specific library you are using. I spent hours dissecting the code to find that out and didn’t find that solution anywhere in the docs or this forum (this post)

I do know the basic flow and have done that. My flow is establish a wifi connection, then call Blynk.config() then call Blynk.connect().

I did have a specific question. The docs are not specific as to that question. To reiterate:.Does Blynk.config() get called only once in 'setup()or should it be called each time right before you callBlynk.connect()` which is in a timer loop.

If you don’t know the answer or don’t want to answer it, don’t waste your time responding…no need to get cranky about it.


#9

Who’s cranky? If you don’t like my answers… move along :wink:

The Documents are a work in progress, but the meat in any open technology like Blynk is in the forums… And it is expected that people READ before asking; All of your questions were answered in many other posts.


#10

As I’ve stated multiple times now.
I did read.
I did search.
And if you read the link to my other post in my last response, you’d notice I went even further than that by combing through source code providing an answer that no forum post, nor doc, nor example, nor anyone else could. So don’t accuse me of being lazy and not searching and reading.

As you keep insinuating that I didn’t search and read, despite me telling you otherwise, you should justify that by showing me one post that answers this question that one could readily find with a related search term. It sounds like it should be easy for you.

Otherwise, give people the benefit of the doubt, instead of trolling their posts without offering any useful information other than to “READ”.


#11

Thanks Jamin. I just went back to an older version of my sketch that does not do any sort of explicit reconnection logic. It just uses Blynk.begin() as well. But I tested disconnecting the power supply to the ESP8266-01 module and reconnecting. Blynk never reconnects.

The Serial Monitor shows it pinging and posting to Blynk server and then after the cycling the power on the wifi module, the serial monitor is just filled with Cmd skipped:20 errors and no output with regard to any reconnect attempt.

Are you doing anything different than what I’m doing below?

Sketch:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#define BLYNK_DEBUG // Optional, this enables lots of prints
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>
#include <NewPing.h>

//constants to set pin numbers
const int wifiConnectedLED = 8;     // the number of LED pin

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "myssid";
char pass[] = "<mypwd>";

//LED variables
int wifiLEDState = 0;  //variable for reading LED pin status

//UltreaSonic Sensor Variables
#define TRIGGER_PIN  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int distance;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

//Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

WidgetLED led1(V1);

//SimpleTimer timerAppLEDWidget;
SimpleTimer timerCheckConnectionStatus;
SimpleTimer timerDistancePing;

void setup()
{
  // Set console baud rate  
  Serial.begin(115200);
  delay(10);
  
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  //init LED pin 8
  pinMode(wifiConnectedLED, OUTPUT);
    
  //connect to blynk servers via wifi - easy way
  Serial.println("connecting to Blynk...");
  Blynk.begin(auth, wifi, ssid, pass); 
  
  //set timer interval and function to call for App LED
//  Serial.println("Init App LED timer");
//  timerAppLEDWidget.setInterval(3000L, blinkLedWidget);
//  Serial.println("App LED timer Initialized");

  //set timer interval and function to call for Connection Status LED
//  Serial.println("Init App LED timer");
//  timerCheckConnectionStatus.setInterval(3000L, blinkConnectedBoardLed);
//  Serial.println("App LED timer Initialized");

  //set timer interval and function to call for Distance Ping for UltraSonic Sensor
  Serial.println("Init Ultra Sonic ping timer");
  timerDistancePing.setInterval(3500L, getDistanceTimerFunction);
  Serial.println("App Ultra Sonic ping timer Initialized");
}

void loop()
{
  Blynk.run();
  //Serial.println("Timer 1");
//  timerAppLEDWidget.run();
//  timerCheckConnectionStatus.run();
  timerDistancePing.run();
}

// V1 LED Widget in Blynk App is blinking
void blinkLedWidget()
{
//  if (led1.getValue()) {
//    led1.off();
//    Serial.println("LED on V1: off");
//  } else {
//    led1.on();
//    Serial.println("LED on V1: on");
//  }
}

//Check if blynk is connected and set board led accordingly.
void blinkConnectedBoardLed()
{
  wifiLEDState = digitalRead(wifiConnectedLED);
  
  if (Blynk.connected()) {
    if(wifiLEDState == LOW){ //LED is not on, so turn it on
      //turn on LED
      Serial.println("Turning on Board LED 8");
      digitalWrite(wifiConnectedLED, HIGH);
      Serial.println("Board LED 8 on");
    }
  } else {
    //turn off LED
    Serial.println("Turning OFF Board LED 8");
    digitalWrite(wifiConnectedLED, LOW);
    Serial.println("Board LED 8 OFF");

    //try connecting to Blynk
    Serial.println("Not connected to Blynk server...trying to connect again."); 
    //Blynk.connect(10000);  // timeout set to 10 seconds and then continue without Blynk
  }
}

//Check if blynk is connected and set board led accordingly.
void getDistanceTimerFunction()
{
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  distance = uS / US_ROUNDTRIP_IN / 12; // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_IN / 12); 
  Serial.println("FT");
  Blynk.virtualWrite(V3, distance);
}

Serial Output:

Ping: 6FT
[3846604] <[14|05|B8|00|06]vw[00]3[00]6
Ping: 6FT
[3850112] <[14|05|B9|00|06]vw[00]3[00]6
Ping: 6FT
[3853604] <[14|05|BA|00|06]vw[00]3[00]6
[3855410] <[06|05|BB|00|00]
[3855595] >[00|05|BB|00|C8]
Ping: 6FT
[3857103] <[14|05|BC|00|06]vw[00]3[00]6
Ping: 6FT
[3860608] <[14|05|BD|00|06]vw[00]3[00]6
Ping: 0FT
[3864114] <[14|05|BE|00|06]vw[00]3[00]0
[3869142] Cmd error        <------------------------------ WiFi power cycled here
Ping: 0FT
[3889265] Cmd skipped:20
Ping: 0FT
[3904365] Cmd skipped:20
Ping: 0FT
[3919465] Cmd skipped:20
Ping: 0FT
[3934567] Cmd skipped:20
Ping: 0FT
[3949667] Cmd skipped:20
Ping: 0FT
[3964767] Cmd skipped:20
Ping: 0FT
[3979867] Cmd skipped:20
Ping: 0FT
[3994967] Cmd skipped:20
Ping: 0FT
[4010066] Cmd skipped:20
Ping: 0FT
[4025166] Cmd skipped:20
Ping: 0FT
[4040265] Cmd skipped:20
Ping: 0FT
[4055365] Cmd skipped:20
Ping: 0FT
[4070465] Cmd skipped:20
Ping: 0FT
[4085565] Cmd skipped:20
Ping: 0FT
[4100666] Cmd skipped:20
Ping: 0FT
[4115766] Cmd skipped:20
Ping: 0FT
[4130866] Cmd skipped:20
Ping: 0FT
[4145966] Cmd skipped:20
Ping: 0FT
[4161065] Cmd skipped:20
...etc, etc.

#12

I am using an ESP8266 Dev Board called a WeMos… Maybe thats the difference? It’s not a shield…


#13

Chill out man… no one has accused you of anything, but based on your rather confusing title and mixed suppositions and questions, one certainly gets the impression that you might not have red up, or reasoned out, the general flow of the Blynk connection and communication process.

Stop blaming the documents or anyone who tries (not trolls :stuck_out_tongue_winking_eye: ) to advise. I have done almost nothing but read this forum for the last three+ months, often answering my own posted questions… so I know that my advice to you is founded on fact. I also answered your vague question on the order of the commands… as well as explaining that there was NO One-Way to establish stable connection… thus no single post to point you to.

Now as for your “new?” issue… you are calling getDistanceTimerFunction() constantly in your main loop. At risk of redundant repetition, there are also many posts referring to the need to keep Blynk applications from flooding too much data and/or blocking data, lest communication gets dropped. ESPs are particularly susceptible to dropping connection due to timing issues and ESP shields (or add-ons - essentially same thing) just add to the fun with interface issues.

Look up SimpleTimer and start putting all your actions in timed blocks, that should go a long way toward resolving, or at least assisting in the troubleshooting of, your connection issues.

And you are welcome… most attitudes just get ignored. But having some similar opinions on documentation, I understand and choose grace over grudge.


#14

Hello brother I did this to my sketch it might help u with what u are asking
So when I loose the connection it will start to connect to internet for 8 seconds if the connection made then it will connect if not the project will work for 30 seconds and then For 8 seconds it will keep trying to connect again etc…

read this plz it will help u as it helpped me


#15

'1. Bail out of the while loop that you MUST have in setup()

int mytimeout = millis() / 1000;
while (Blynk.connect() == false) { 
  if((millis() / 1000) > mytimeout + 8){  // try for less than 9 seconds
    break;
  }
}
'2. Only process blynk.run() in the main loop() when you are connected.

  if(Blynk.connected()){
    Blynk.run();
  }

#16

Also as the other freinds said u need simple time for that


#17

Yeah, mine is not a shield either, but a separate board like yours…pretty much the same thing as yours except a slightly different version of the ESP8266.


#18

Hey, I appreciate you trying to offer some actual help with regards to SimpleTimer, but unfortunately, it is not applicable in this case, as that is exactly what I am already doing in that sketch!

“Re-read”…I think was some of your prior advice???

So what is it exactly that you’ve provided that you feel I should thank you for? Oh yeah, telling me to re-read, hey…thanks man! Super helpful.


#19

At least you are researching and learning, that was my goal.


#21