How to assign a fixed IP to an ESP32 ESP8266 or ESP01 project

assigne ip fixe esp01 esp32 esp8266 code arduino
Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

When developing an object connected to WiFi with an ESP32, ESP8266 or ESP-01 (or any other microcontroller for that matter), it is much more practical to assign a fixed IP address. We can thus always connect to the object on the local network even in the event of a change of router or internet box.

 

By carrying out port routing, we can also connect from the Internet to our connected objects. However, beware of security vulnerabilities. It is better to connect your objects to a home automation server which is the job after all!

How to assign a fixed IP address to an ESP8266 or ESP-01 project?

The ESP8266WiFi library allows you to precisely assign the connection parameters:

  • IP IP address you want to assign
  • DNS DNS server, it is he who assigns the IP address. Here, we inform him that we want to reserve a fixed IP address. By default, we use the DNS server of the internet box or the router, so we indicate the same address.
  • GATEWAY This is the IP address of the internet box or router
  • SUBNET subnet. Generally, it is 255, 255, 255, 0. It will be necessary to check directly at the level of the internet box or the router

Here, we will assign the IP address 192.168.1.40 to the ESP01 or ESP8266 module.

IPAddress ip(192, 168, 1, 40);
IPAddress dns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

The WiFi.config() method is used to configure the IP address and connection parameters to the local WiFi network.

WiFi.config(ip, dns, gateway, subnet);

Then we can connect as usual

WiFi.begin(ssid, password);

Read this article to learn more about the ESP8266WiFi library

How to assign a fixed IP address to an ESP project

The WiFi.h library for ESP32 is the equivalent of the ESP8266WiFi library.

The configuration and the call of the methods are perfectly identical!

You just have to include conditional code at the start of the project to load the library that corresponds to the platform. Detection is automatic.

#ifdef ESP32 
  #include <WiFi.h>
#else 
  #include <ESP8266WiFi.h> 
#endif

Upload the Arduino code to test the fixed IP address

Here is a complete code example that you can upload from the Arduino IDE or PlatformIO.

The code is ESP32 and ESP8266 compatible (including ESP01). On the Arduino IDE, you can remove the first line #include <Arduino.h>.

#include <Arduino.h>
#ifdef ESP32
  #include <WiFi.h>
#else
  #include <ESP8266WiFi.h>
#endif

const char* ssid = "enter_your_ssid";
const char* password = "enter_your_password";

WiFiServer server(80);

void setup() {

  Serial.begin(115200);
  delay(10);

  IPAddress ip(192, 168, 1, 40);
  IPAddress dns(192, 168, 1, 1);
  IPAddress gateway(192, 168, 1, 1);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, dns, gateway, subnet);

  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("...");
  }
  Serial.println("WiFi connected");

  server.begin();
  Serial.println("Web server running.");
  delay(500);

  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:
}

PlatformIO configurations for ESP32, ESP8266 or ESP-01

Here are some PlatformIO configurations for a LoLin d1 Mini (ESP8266), an ESP-01 (512KB) / ESP-01S (1MB) WiFi module or a LoLin D32 Pro (ESP32).

LoLin d1 mini (ESP8266)ESP-01 (512Ko) or ESP-01S (1Mo)LoLin D32 Pro (ESP32)
[env:d1_mini_lite]
platform = espressif8266
board = d1_mini_lite
framework = arduino
monitor_speed = 115200
[env:esp01_1m]
platform = espressif8266
board = esp01_1m
framework = arduino
;upload_protocol = esptool
;board_build.flash_mode = dout
;upload_speed = 115200
monitor_speed = 115200

[env:esp01]
platform = espressif8266
board = esp01
framework = arduino
;upload_protocol = esptool
;board_build.flash_mode = dout
;upload_speed = 115200
monitor_speed = 115200
[env:lolin_d32_pro]
platform = espressif32
board = lolin_d32_pro
framework = arduino
monitor_speed = 115200

Check the assigned IP address

Upload the project and open the serial monitor to verify that the IP address has been correctly assigned

From the Arduino IDE

ide arduino fixed ip esp01 esp32 esp8266

From PlatformIO

--- More details at http://bit.ly/pio-monitor-filters
--- Miniterm on /dev/cu.usbserial-1410  115200,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
.....................WiFi connected
Web server running.
192.168.1.40

Updates

20/10/2020 Publication of the article

Click to rate this post!
[Total: 0 Average: 0]

Are you having a problem with this topic?

Maybe someone has already found the solution, visit the forum before asking your question
Ask your question

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

Did you like this project ? Don't miss any more projects by subscribing to our weekly newsletter!

We will be happy to hear your thoughts

      Leave a Reply

      DIY Projects