Blynk.config vs Blynk.connect

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)

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

Using port 80 or default value makes no difference.

Not sure I understand your other comment. sorry

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

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

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.

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ā€ā€¦

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.

@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:

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:

True. Just the code uses those ALMOST correctlyā€¦ Somehow.

1 Like

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.

1 Like

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

So much for me thenā€¦:wink:

I also see a port problem in you snippets above.

When you are back from holidays, show you whole codeā€¦

Blynk is a part of a much larger project: Ā± 2900 lines of codesā€¦ Difficult to show the whole codeā€¦

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.

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:

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!

Then Iā€™m glad too :slight_smile: Case ā€œsolvedā€

1 Like