How can i set for server IP blynk-cloud.com

Hi Costas,
i will use your example, but blynk can’t connect.
for Server IP i use IPAddress server_ip ( 139, 59, 206, 133); // blynk Server
how can i set for server IP blynk-cloud.com", 80

byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xEF };
IPAddress server_ip ( 139, 59, 206, 133); // blynk Server
IPAddress arduino_ip ( 192, 168, 1, 51);
IPAddress dns_ip ( 192, 168, 1, 10);
IPAddress gateway_ip ( 192, 168, 1, 10);
IPAddress subnet_mask( 255, 255, 255, 0);

WiFi.config(arduino_ip, gateway_ip, subnet_mask);
WiFi.begin(ssid, password);
Blynk.config(auth);
while (Blynk.connect(1000) == false) {
}

when i use this Blynk.begin(auth, ssid, pass);
blynk connected well

Blynk.config() ?

https://docs.blynk.cc/#blynk-firmware-configuration-blynkconfig

Maybe you should read this thread:

Pete.

thanks Pete,
i read so much posts that i dont know nothing

My problem is when i lost the internet connection the program stoped when i use blynk.begin.
Is this correct?

Now i tried to use this

WiFi.begin(ssid, pass);  // Non-blocking if no WiFi available
Blynk.config(auth, server, port);
Blynk.connect();

but i dont know where i put the variables for arduino_ip, gateway_ip, subnet_mask.
I think that is the first step to do the next step that the programm keeps running.
It is realy important that the programm keeps running because its a alarmsystem.
The arduino became a separate 12V connection that the system stay in work, but its possible that the router goes down, from ligtnings or sabotage. Hope you understand my bad english.
After the solution for this problem, did you can give me a actual link where i can get infos how to to do that the system keep running.
Here in the forum are so many posts and very old posts that i dont know what is the solution.

Thanks so much
Uwe

There are many many threads on this forum about ways of avoiding blocking the code execution in the event of being unable to connect to Wi-fi or whichever Blynk server you’re using.

If that is the basis of your query then it would have been good if you would have stated this up-front.

I’d suggest maybe changing rage title of this thread and doing some searching for non-blocking connection methods.

It would also help others to help you if you described in detail the hardware you’re using, as this may have an impact on the eventual solution.

Pete.

Hi,
i will set a static IP on Wemos D1R1.
I use the non blocking Code from Costas, thankyou it works very well.
When i insert the line

  WiFi.config(arduino_ip, gateway_ip, subnet_mask);

in the follow part

void MyWiFi() {
  int mytimeout = millis() / 1000;
  WiFi.config(arduino_ip, gateway_ip, subnet_mask);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
 //   Serial.print(".");
    if ((millis() / 1000) > mytimeout + 3) { // try for less than 3 seconds to connect to WiFi router
      break;
    }
  }

the wifi connection is ok and the ip what i set is in the router, but than Blynk will no more connect.
If i decativate the line, blynk connect well.
Here is the rest of code i use

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

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

BlynkTimer timer;

char auth[] = "xxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxx";
char pass[] = "xxxxxxxxx";
//char server[] = "xxx.xxx.xxx.xxx";  // IP for Local Cloud Server
char server[] = "blynk-cloud.com";  // URL for Blynk Cloud Server
int port = 80;
const char* mqtt_server = "xxx.xxx.xxx.xxx";
byte arduino_mac[] = { 0x5C, 0xCF, 0x7F, 0x0F, 0x63, 0xC0 };
//IPAddress server_ip ( 139, 59, 206, 133); // blynk Server
IPAddress arduino_ip (  192,  168,   1,  80);
IPAddress dns_ip     (  192,   168,   1,   10);
IPAddress gateway_ip (  192,   168,   1,   10);
IPAddress subnet_mask( 255, 255, 255, 0);

WiFiClient espClient;
PubSubClient mqttClient(espClient);

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();

long lastMsg = 0;
bool Connected2Blynk = false;
// MQTT Verbindung
long lastReconnectAttempt = 0;

boolean reconnect() {
  String clientId = "alarmanlage-";   // Create a random client ID
  clientId += String(random(0xffff), HEX);
  }
  return mqttClient.connected();
}

void setup() {
  // Debug console
  Serial.begin(115000);
  delay(10);
  //  Serial.println();
  mySwitch.enableReceive(13);  // Pin 11 oder 7

  timer.setInterval(3600000L, CheckConnection); // check jede Stunde ob Blynk Verbindung besteht 60x60x1000

  MyWiFi();

  mqttClient.setServer(mqtt_server, 1883);
  lastReconnectAttempt = 0;
}

void MyWiFi() {
  int mytimeout = millis() / 1000;
  WiFi.config(arduino_ip, gateway_ip, subnet_mask);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    if ((millis() / 1000) > mytimeout + 3) { // try for less than 3 seconds to connect to WiFi router
      break;
    }
  }
  
    if (WiFi.status() == WL_CONNECTED) {
      Serial.print("\nIP address: ");
      Serial.println(WiFi.localIP());
    }
    else {
      Serial.println("\nCheck Router ");
    }
  
  Blynk.config(auth);
  Connected2Blynk = Blynk.connect(1000);  // 1000 is a timeout of 3333 milliseconds
  mytimeout = millis() / 1000;
  while (Blynk.connect(1000) == false) {
    if ((millis() / 1000) > mytimeout + 3) { // try for less than 4 seconds
      break;
    }
  }
}

void CheckConnection() {
  Connected2Blynk = Blynk.connected();
  if (!Connected2Blynk) {
       Serial.println("Not connected to Blynk server");
    MyWiFi();
  }
  else {
        Serial.println("Still connected to Blynk server");
  }
}

void loop() {
  if (Connected2Blynk) {
    Blynk.run();  // only process Blyk.run() function if we are connected to Blynk server
  }
  timer.run();

  if (!mqttClient.connected()) {
    long now = millis();
    if (now - lastReconnectAttempt > 10000) {
      lastReconnectAttempt = now;
      // Attempt to reconnect
      if (reconnect()) {
        lastReconnectAttempt = 0;
      }
    }
  } else {
    // Client connected

    mqttClient.loop();
  }
}

whatis wrong, i can’t find the mistake.

Thankyou for your Help

This code is a mess of hacked-together sketches that aren’t compatible with each other.

Are you actually planning to use MQTT?

Pete.

oh the code is running well.
blynk connect after disconnect, mqtt connect after disconnect and the code is non blocking.
the only problem is that blynk not connect if i set a static ip.
The code part for mqtt is directly from github PubSubClient for non blocking

The reason I asked about the MQTT stuff is that you’re not subscribing to any topics or publishing to any, and you don’t seem have any callback defined.

I’m not going to take a lot of time to try to pick this apart, but you’re creating a Wi-Fi instance called espClient for use with your MQTT connection, but then abandoning this and doing something else for your Blynk connection - hence my comment about hacked-together sketches that aren’t compatible.
You also have lots of unused variables that are associated with networking and the Blynk connection, which only serve to confuse matters.

If you wnat to take this forward and get some kelp with it then I’d suggest sorting out these issues, then cutting-out all the non-relieve t parts of the code so that you just have your WI-Fi, MQTT and Blynk connection portions of the code. That will give you much more clarity.

Pete.

ok Pete,
i could not understand your problem.
all the variables i need in the rest of programm.
With mqtt i only publish to a raspberry with emoncms, that part is runing.

I will only set a static IP and that will not runing

OK i sort the code out that is not important for this problem

@uwe, whats your reason for doing the static ip and NOT having the router do the assigned dhcp address instead ? In other words, why are you spending all this time to fight this problem when it could be solved with a dhcp assignment ? I believe you are having a real issue, but why fight it with a simple fix?

thankyou, i understand that, but the reason is why wifi connect with the seted ip and blynk not.
I would understand this

Is this what you are using?

Because is is closer to a UNO with ESP as shield then a true Standalone ESP8266 Dev board… and some WiFi processes, like setting static IPs, are not as easy or possible in a “as shield” config… or whatever hacked setup this uses.

Basically, it is old tech and simply might not do what new ESP8266 Dev boards can.

I don’t have a problem, it’s you that has the problem, which I’m trying to help you with.

What I was saying is that there are no MQTT publish or subscribe commands in your code, so you’re not sending any MQTT messages, or receiving any, so I don’t see how publishing to your Raspberry can be working.

Because you’re establishing a Wi-Fi connection for MQTT then abandoning this when you try to connect with Blynk - because of the hacked-together code from different sources.

Pete.

yes exactly,
i will expain you what i did.
i did the follow settings

WiFi.begin(ssid, pass);
Blynk.config(auth);

that is that what i read in many posts here.
That is working well. Wifi and blynk connect
the board has the IP 192.168.1.52

Than i get the info from a post here that i can set a static IP like 192.168.1.80
i try this

IPAddress arduino_ip (  192,  168,   1,  80);
IPAddress dns_ip     (  192,   168,   1,   10);
IPAddress gateway_ip (  192,   168,   1,   10);
IPAddress subnet_mask( 255, 255, 255, 0);
 WiFi.config(arduino_ip, gateway_ip, subnet_mask);
WiFi.begin(ssid, pass);
Blynk.config(auth);

I delete in the router the divice with 192.168.1.52 and restart.
I start the board and the device have the new IP 192.168.1.80
at this point all is ok
but now blynk doesnt connect.

@Pete
i publish mqtt code, but later in the program.
i say it again, all is working without static IP, why not with a static ip

Hi again,
i found the fault, hope it is usefull for someone.
Now the board have a static IP.
in this line
WiFi.config(arduino_ip, dns_ip, gateway_ip, subnet_mask);
was “dns_ip” missing.

Here is my code. I test all with no internet, no wifi, no router, all is working.

This system must run a few weeks alone, its possible that the internetline is down or the router is down, but the sytem must keep running. So its very important that the code is running with out connection and only with short breaks for try to connect.
In this sketch i test every minute, later every hour if a connection is true.

But i have some question again.

What can i do better?

When the system is running and than i cut the wifi or router, Blynk.run(); beguin
every second to try a connection, because the variable Connected2Blynk hold true.

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

char auth[] = "xxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "TP-LINK_AP_B4F2";
char pass[] = "xxxxxxxxxxxxxxx";
char server[] = "blynk-cloud.com";  // URL for Blynk Cloud Server
int port = 80;
byte arduino_mac[] = { 0x5C, 0xCF, 0x7F, 0x0F, 0x63, 0xC0 };
//IPAddress server_ip ( 139, 59, 206, 133); // blynk Server
IPAddress arduino_ip (  192,  168,   1,  80);
IPAddress dns_ip     (  192,   168,   1,   10);
IPAddress gateway_ip (  192,   168,   1,   10);
IPAddress subnet_mask( 255, 255, 255, 0);

WiFiClient espClient;

bool Connected2Blynk = false;
unsigned long startCheckConnection;

void setup() {
  Serial.begin(115000);
  delay(10);
  MyWiFi();
}

void MyWiFi() {
  int mytimeout = millis() / 1000;
  WiFi.config(arduino_ip, dns_ip, gateway_ip, subnet_mask);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    if ((millis() / 1000) > mytimeout + 1) {
      break;
    }
  }

  Blynk.config(auth);
  Connected2Blynk = Blynk.connect();
  mytimeout = millis() / 1000;
  while (Blynk.connect() == false) {
    if ((millis() / 1000) > mytimeout + 1) {
      break;
    }
  }
}

void CheckConnection() {
  if ( millis() > startCheckConnection + 60000) {  //now check every minute, later every hour
    startCheckConnection = millis();
    if (!Connected2Blynk) {
      MyWiFi();
    }
  }
}

void loop() {
  CheckConnection();
  if (Connected2Blynk) {
    Blynk.run();
  }
  Serial.println("running Code");
}

Thankyou for your patience with me
I learned a lot in the last days.