[Solved] Compile error with Blynk.config / .begin when passing Strings for server parameter


I’m trying to pass a String for blynk server when calling Blynk.config or Blynk.begin but getting compiler errors. Not sure why this is occurring as I define my variables as String before passing to function (screen shots below for both Blynk.config and Blynk.begin examples):


BLYNK.BEGIN example:

Check here… note that it was never confirmed if my edit worked or not.


Not sure if I explained my problem clearly

The code all works if I replace local_server String with text “mylocal.com” within the Blynk.config function call, it only breaks (or compile error as shown) if I actually use the String local_server in the function call as per screen shot. I can’t work out why Blyn.config or Blynk.begin won’t take a string ?

For example below will not compile:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

String cloud_server = "blynk-cloud.com";

void setup()
  // Debug console

  // Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  // Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  Blynk.begin(auth, ssid, pass, cloud_server, 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

void loop()
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!

Throws the following compiler error:

@mars are you actually using String or char?

String will not work, use this example plus the port:

  char local_server[] = "";
  Blynk.begin(auth_local_server, ssid, pass, local_server);

hi @costas

yes I was using String as I saw the arguments that Blynk.config accepted was String& for server so I naturally thought I could pass String.

I just changed it to char[] and it compiled without errors - thank you :wink:

From an education point of view I still don’t know why not passing a String did/will not work :frowning:

You didn’t, that is char too.
The compiler is showing String& as your error for begin and config.

But if you switch to Blynk.config() (for sketch operation even without connection) will having char work or do you still need to break out the WiFi connection first… and then is this correct syntax for both?

Blynk.connectWiFi(ssid, pass);
Blynk.config(auth_local_server, local_server, 8442);

I don’t have ESP (yet) so still working only with concept and unable to test for myself.

@mars sorry for tagging along… I thought my addition might have been relevant… but perhaps not :blush:

I understand now - thank you. I shouldn’t always trust what the compiler is telling me.

no worries @Gunner

Yes, I need to ensure I have a valid connection first.

   WiFi.begin(ssid, pass);                           // Connect to WiFi network
   Serial.println(F("Waiting to connect to Wifi:"));
   long waittime = millis()+14000;  // 14 seconds wait time   

   Serial.print ("Current time = ");
   Serial.println (millis());
   while (WiFi.status() != WL_CONNECTED) {           // Wait for board to connect to WiFi network
      if (millis() > waittime)
              { Serial.println ("breaking !");
                break; }                          
   Serial.print ("WL_CONNECTED = ");
   Serial.println (WL_CONNECTED);
   Serial.print ("WiFi status = ");
   Serial.println (WiFi.status());
   Serial.print ("Current time = ");
   Serial.println (millis());

   Serial.println("WiFi connected");  
   Serial.println("IP address: ");
   Serial.println(F(" "));                               
     Blynk.config(auth_local_server, local_server);   // mars000 local blynk  server

I usually #define any var that wont change.

#define SERVER IPAddress(192, 168, 1, 2)

then you can do compile logic

#ifdef SERVER
  Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);

so do I in some situations.

in my case I need option to dynamically change bynk servers (for redundancy)

1 Like

Interesting idea… but wouldn’t that potentially cause all sorts of issues when switching servers, all your widget settings would get reset (or changed to that server’s last know state - if you have that function coded)?

yes but I have the app cloned on different servers and when I switch I
Blynk.disconnect and Blynk.connect again with different auth tokens.

Obviously the logic is more convoluted than that as there are many other conditions to consider but you should get the idea.

I could see that working if there was some way of keeping the servers themselves synchronized - may not even have to change Auth that way… Hey it’s open sourced, I’m sure someone will figure out a way :wink:

true. it really depends on the app. if you are relying on external sensor information you can just do a simple refresh and you are re-synced. Any other settings would revert to default values.

Very true. I would hate to be in the smart house run by multiple servers in a majority rule architecture with random server failures… lights a flickering, doors randomly opening and closing, the fridge giving up it frosty role and heading somewhere tropical.

Sci Fi dreaming aside… how would you get the app to determine which server to link with? It currently doesn’t even have the option to remember the Local IP if you temporarily switch to Cloud :unamused:


obviously this is for redundancy or when maintenance required on a server you can switch to the other ! the hope is it hardly ever needs to occur. But when it does you have a plan !

a simple approach would be that you could have a simple rule that switches to next round-robin server if number of RECONNECT retries exceeds a specific value.

1 Like