Unable to connect my ESP32 with static IP on my network with Blynk, please help me!

Hello,
I have to connect an ESP32 on a network with a static local IP address, I mean without using DHCP.
If I try the following sketch I get in return (with the serial debug).
but i can’t log in?

mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5856
entry 0x400806a8
…[5676]


/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v1.0.1 on ESP32
[5677] Connecting to blynk.cloud:80
[10678] Connecting to blynk.cloud:80
[15679] Connecting to blynk.cloud:80

Whereas if I use the second sketch it works, but Blynk.begin() does not allow me to connect my ESP32 with a fixed address on my network, how to solve this problem??

FIRST SKETCH

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on ESP32 chip.

  Note: This requires ESP32 support package:
    https://github.com/espressif/arduino-esp32

  Please be sure to select the right ESP32 module
  in the Tools -> Board menu!

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

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

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID   "xxxxxxxxxxxx"


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

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

char ssid[] = "Linksys-E1200";
char pass[] = "xxxxxxxxxx";

IPAddress device_ip  (192, 168, 10, 222);
IPAddress dns_ip     ( 8, 8, 8, 8);
IPAddress gateway_ip (192, 168, 10, 1);
IPAddress subnet_mask(255, 255, 255, 0);

void setup()
{
  // Debug console
  Serial.begin(115200);
  delay(3000);
  WiFi.config(device_ip, gateway_ip, subnet_mask);
  WiFi.begin(ssid,pass); 
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);                       
      Serial.print(".");              
  } 
  Blynk.config(auth);
  while (Blynk.connect() == false) 
     { }    // wait for Blynk to be connected
  }

void loop()
{
  Blynk.run();
  delay(2000);
}

BLYNK_CONNECTED()
{
     Serial.println ("Blynk connected Succesfully");   
}

SECOND SKETCH

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on ESP32 chip.

  Note: This requires ESP32 support package:
    https://github.com/espressif/arduino-esp32

  Please be sure to select the right ESP32 module
  in the Tools -> Board menu!

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

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

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID   "xxxxxxxxxx"


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

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

// Set password to "" for open networks.
char ssid[] = "Linksys-E1200";
char pass[] = "xxxxxxxxxxx";

IPAddress device_ip  (192, 168, 10, 222);
IPAddress dns_ip     ( 8, 8, 8, 8);
IPAddress gateway_ip (192, 168, 10, 1);
IPAddress subnet_mask(255, 255, 255, 0);

void setup()
{
  // Debug console
  Serial.begin(115200);
  delay(3000);
  
  /*
  WiFi.config(device_ip, gateway_ip, subnet_mask);
  WiFi.begin(ssid,pass); 
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);                       
      Serial.print(".");              
  } 
  Blynk.config(auth);
  while (Blynk.connect() == false) {    // wait for Blynk to be connected
  } 
  //Blynk.begin(auth, ssid, pass);
  */
  
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();
  delay(2000);
}

BLYNK_CONNECTED()
 {
      Serial.println ("Blynk connected Succesfully");   
  }

I get the following result:

[14134] Connected to WiFi

[14134] IP: 192.168.10.174

[14134]


/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v1.0.1 on ESP32

[14140] Connecting to blynk.cloud:80
[14823] Ready (ping: 204ms).
Blynk connected Succesfully

First thing you’ll have to do is removing the delay in your loop void…

I’d remove that, and replace it with Blynk.connect(); as well as removing the delay() from your void loop.

Also, your WiFi.config is missing the dns_ip
It should be:
WiFi.config(device_ip, dns_ip, gateway_ip, subnet_mask);

EDIT - Apologies, I made a mistake with the above syntax, it should be:
WiFi.config(device_ip, gateway_ip, subnet_mask, dns_ip);

Pete.

Thanks Pete for your answer, I tried with the changes you advised me, but it doesn’t work either, so I tried with those changes there but it doesn’t work either. as soon as you want to work in fixed IP it’s a hassle.

void setup()

{
  // Debug console
  Serial.begin(115200);
  delay(3000);

  WiFi.config(device_ip, dns_ip, gateway_ip, subnet_mask);
  WiFi.begin(ssid,pass); 
  while (WiFi.status() != WL_CONNECTED) 
{
      delay(500);                       
      Serial.print(".");              
 } 

  Blynk.config(auth);
  Blynk.connectWiFi(ssid, pass);
}

void loop()

{
  Blynk.run();
  //delay(2000);
}

Result

Don’t do this!

Pete.

Hello Pete,
with or without it does not work.
It’s a shame that it’s so difficult to work in fixed IP often in business we have a dedicated IP address segment for a particular function. If we cannot work in fixed IP, this means that we must have a DHCP server limited to an IP segment.
I’m still going to look, but Blynk seems to me to be very inflexible to coexist in a heterogeneous IOT system.

It should be quite easy to get it working. The problem is that instead of staying on one logical path, you are jumping around all over the place and using commands that are nor relevant to the task that you are trying to achieve.

EDIT
@plaurenc I owe you an apology - In my earlier post I gave you the wrong syntax for including the DNS within your `WiFi.config) command. It should have been:

WiFi.config(device_ip, gateway_ip, subnet_mask, dns_ip);

I suspect that this may have sent you down a couple of rabbit holes in your testing.

I tested your code with my suggested changes and obtained the same results as you, but when I replaced your WiFi connection code with my standard code it worked

This code works (but you’ll need to add your own credentials and change the IP settings)…

#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_DEVICE_NAME "REDACTED"
#define BLYNK_AUTH_TOKEN "REDACTED"

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char ssid[] = "REDACTED";
char pass[] = "REDACTED";

IPAddress device_ip  (192, 168, 1, 123);
IPAddress dns        (8, 8, 8, 8);
IPAddress gateway    (192, 168, 1, 1);
IPAddress subnet     (255, 255, 255, 0);

char auth[] = BLYNK_AUTH_TOKEN;

// Variables used for Wi-Fi connection attempts etc...
int wifi_attempt_count = 0;                           // Count of how many times we've attempted to connect to the Wi-Fi
const int wait_between_wifi_attempts_millis = 1000;   // How long we wait (in millis) before attempting to connect again
const int max_wifi_connect_attempts =          30;    // How many attempts we will have at connecting to the Wi-Fi before going into stand-alone maode


void setup()
{
  Serial.begin(115200);
  connect_to_wifi();
  Blynk.config(auth);  
  Blynk.connect();
}

void loop()
{
  Blynk.run();
}

BLYNK_CONNECTED()
{
  Serial.println ("Blynk connected Successfully");   
}


void connect_to_wifi()
{
  wifi_attempt_count=0;
  
  Serial.println(F("Connecting to Wi-Fi..."));
  WiFi.config(device_ip, gateway, subnet, dns); // Not needed if you just want to have a DHCP assigned IP address.
    
  if (WiFi.status() != WL_CONNECTED)
  {
      WiFi.begin(ssid, pass); // connect to the network
  }

  while (WiFi.status() != WL_CONNECTED  && wifi_attempt_count < max_wifi_connect_attempts) // Loop until we've connected, or reached the maximum number of attemps allowed
  {
    delay(wait_between_wifi_attempts_millis);
    wifi_attempt_count++;   
    Serial.print(F("Wi-Fi connection - attempt # "));
    Serial.print(wifi_attempt_count);
    Serial.print(F(" of "));
    Serial.println(max_wifi_connect_attempts);       
  }

  // we get to this point when either we're connected to Wi-Fi, or we've tried too many times. We need to do differnet things, depending which it is...
  if (WiFi.status() == WL_CONNECTED)
  {
    WiFi.mode(WIFI_STA);
    Serial.println(F("Wi-Fi CONNECTED"));
    Serial.println();
    Serial.print(F("IP Address = "));
    Serial.println(WiFi.localIP());
    Serial.println();    
  }
  else
  {  
    // we get here if we tried multiple times, but can't connect to Wi-Fi
    Serial.println();
    Serial.println(F("................ Could not connect to WiFi ................"));
  }
}

This is the serial output…

ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
Connecting to Wi-Fi...
Wi-Fi connection - attempt # 1 of 30
Wi-Fi connection - attempt # 2 of 30
Wi-Fi connection - attempt # 3 of 30
Wi-Fi CONNECTED

IP Address = 192.168.1.123

[3149] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.1 on ESP32

[3158] Connecting to blynk.cloud:80
[3201] Ready (ping: 9ms).
Blynk connected Successfully

Pete.

Ok Pete, I will try this ASAP get you informed

YESSS !!! It’s working
In first I tried your sketch by modifying the identifiers only and I still had the same problem, so I upgraded ESP32 core I was in version 1.0.6 I am now in 2.0.2 and everything works perfectly. Thanks Pete for your lights

1 Like