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

Blynk.config vs Blynk.connect

#1

I know there are many posts about this. I think I have read most (all?) of them. However still confused.

I use unixtime to time the Blynk connection (in the Setup function).

//Blynk////////////////////////////// using config

  blynkstart=rtc.getUnixTime(rtc.getTime());
  Serial.println(rtc.getUnixTime(rtc.getTime())   );// I use Unix time to time the Blynk connection
   pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
if (digitalRead(A9) == HIGH) {//switch to chose whether or not to try connecting
  Blynk.begin(auth, "blynk-cloud.com", 8442);
//    Blynk.config(auth, "blynk-cloud.com", 8442);
//    Blynk.connect();
    Serial.println("Blynk End");Serial.println(rtc.getUnixTime(rtc.getTime())   );Serial.println(rtc.getUnixTime(rtc.getTime())-blynkstart);
    }

It generally connects between the 14 and 35 seconds.

However if I use Blynk.connect() instead of Blynk.config() it will never connect:

//Blynk////////////////////////////// using connect

  blynkstart=rtc.getUnixTime(rtc.getTime());
  Serial.println(rtc.getUnixTime(rtc.getTime())   );// I use Unix time to time the Blynk connection
   pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
if (digitalRead(A9) == HIGH) {//switch to chose whether or not to try connecting
  // Blynk.begin(auth, "blynk-cloud.com", 8442);
   Blynk.config(auth, "blynk-cloud.com", 8442);
   Blynk.connect();
    Serial.println("Blynk End");Serial.println(rtc.getUnixTime(rtc.getTime())   );Serial.println(rtc.getUnixTime(rtc.getTime())-blynkstart);
    }

The serial monitor will show several attempt to connect but it will fail to do so:
Connecting to blynk-cloud.com:8442
Connecting to blynk-cloud.com:8442

I understand defaut timeout for connect is 30 sec but I have tried several value for Blynk.connect(t). t up to1800000. Always with the same result just more failed attempts to connect.
(Indeed with t at 180000 it times out after 180 sec)

(Arduino Mega 2560 with ethernet card W5100)

0 Likes

#2

Well, the ONLY difference between these two methods is shown below:

void begin (...) {
...
config(auth, domain, port);
while(this->connect() != true) {}
...
}

So I don’t see any reason it can’t work, except the port you are using. For “blynk-cloud.com” it is port 80 now.
Can’t you just leave defaults by using Blynk.config(auth)?

EDIT:
OK, not the only one:

if (!Ethernet.begin(SelectMacAddress(auth, mac))) {
            BLYNK_FATAL(BLYNK_F("DHCP Failed!"));
        }

I guess Ethernet.begin() is needed here

0 Likes

#3

Using port 80 or default value makes no difference.

Not sure I understand your other comment. sorry

0 Likes

#4

Then just look at one of the constructors in BlynkEthernet.h:

void begin( const char* auth,
                const char* domain = BLYNK_DEFAULT_DOMAIN,
                uint16_t port      = BLYNK_DEFAULT_PORT,
                const byte mac[]   = NULL)
    {
        BLYNK_LOG1(BLYNK_F("Getting IP..."));
        if (!Ethernet.begin(SelectMacAddress(auth, mac))) {
            BLYNK_FATAL(BLYNK_F("DHCP Failed!"));
        }
        // give the Ethernet shield a second to initialize:
        BlynkDelay(1000);
        IPAddress myip = Ethernet.localIP();
        BLYNK_LOG_IP("IP:", myip);

        config(auth, domain, port);
        while(this->connect() != true) {}
    }
1 Like

ESP32 cannot auto connect to WiFi
#5

Sorry Marvin my programming abilities are probably not sufficient enough.
What code modification do you suggest?

0 Likes

#6

The Ethernet.begin(..) but it seems it requires correct arguments. I suppose this entry just initialises network connection, and you may look at some non Blynk related connection example for your connection method. Currently I’m away and can’t look at any code.

0 Likes

#7

I used Ethernet.begin() some time ago in another project. I will try.

Thanks again for your help.

Blynk.connect() should be better documented in the “docs”…

0 Likes

#8

You are comparing two disimular commands. There is no “one or other”, they do different things.

Blynk.config() is just that, it sets up your required configuration like AUTH, Server, Port…etc. It is only used once in your setup()

Blynk.connect() does just what it says and trys to take your configured sketch and connect it… this command can be ran whenever needed in your sketch… for example as part of a “keep running when no internet available, but reconnect when able” routine.

0 Likes

#9

@Gunner, look at his code. There is nothing wrong with command’s understanding (I think). It’s just the network connection initialization, that is missing here :slight_smile:

0 Likes

#10

His title and comments said different :stuck_out_tongue:

But if everything is working after proper network init, then I will leave it alone :slight_smile:

0 Likes

#11

True. Just the code uses those ALMOST correctly… Somehow.

1 Like

#12

This Is understand. I indeed want to use Blynk.connect for a ‘reconnection’ routine.

I think the problem is with the network init which I did not realize was necessary with Blynk.config and Blynk.connect. (from there my comment about a more detailed doc but it might be obvious for better programmers than me :wink:)

Going on holidays so the test will have to wait.

0 Likes

#13

OK. However, manual networking configuration, prior to running Blynk.config() and the relation to Blynk.connect() is already mentioned in the docs :wink:

image

image

0 Likes

#14

So much for me then…:wink:

0 Likes

#15

I also see a port problem in you snippets above.

When you are back from holidays, show you whole code…

0 Likes

#16

Blynk is a part of a much larger project: ± 2900 lines of codes… Difficult to show the whole code…

0 Likes

#17

Well, Blynk now uses port 8080 instead of 8442 for Blynk.config() & Blynk begin().

Not really sure of your initial issue, aside from understanding of various commands, so hopefully all else works now.

0 Likes

#18

I’m just guessing, but I stand for “less than optimal way” the connection is handled by Blynk.begin(). Do You remember the not that old @Nixon 's issue? I’m left with his modifications and I’m quite happy with that :smiley:

0 Likes

#19

Marvin, you were right. The solution was as simple as:

>    Ethernet.begin(mac);//just add this
>    Blynk.config(auth, "blynk-cloud.com", 8080);
>    Blynk.connect();

Case can be closed!

0 Likes

#20

Then I’m glad too :slight_smile: Case “solved”

1 Like