como-configurar-una-ip-estatica-en-el-esp8266

How to Set Up a Static IP on ESP32

  • 4 min

A static IP allows an ESP32 or ESP8266 to always use the same address within the local network.

This is very useful when the device acts as a server, when we want to access it from another machine, or simply when we do not want to keep checking which IP the router has assigned after every reboot.

The example is oriented to ESP32. In many cases it can also be adapted to ESP8266 by changing libraries and a few pin details.

In the two previous posts we saw how to connect to an existing WiFi network in STA mode and how to generate our own WiFi network in AP mode.

Now that the board is connected to the WiFi network, the next practical need is to connect to it, which means we have to be able to find it on the network. Or, in other words, we need to know its local IP address.

Until now, we had displayed the IP address via serial port right after connecting. But it’s not very practical to have a computer or a screen attached to the device just to see which IP the router’s DHCP has assigned us, right?

We have several options available to find our device. One is to use a device scanner, another is to use mDNS, but the most secure and robust option is to configure a static IP.

Fortunately, it is very easy to configure a static IP on ESP32 with the WiFi.config() function.

bool config(IPAddress local_ip,
      IPAddress gateway,
      IPAddress subnet,
      IPAddress dns1 = (uint32_t)0x00000000,
      IPAddress dns2 = (uint32_t)0x00000000);
Copied!

This function must be called immediately after WiFi.begin(), which starts the connection with DHCP enabled. With WiFi.config() we can configure the IP, gateway, subnet and, optionally, the DNS.

Example in STA Mode

Let’s see an example of how to configure a static IP in STA mode, i.e., connecting to an existing WiFi. The following example would connect to the network and set the IP to 192.168.1.200.

// Connect
#include <WiFi.h>

// Configure WiFi
const char *ssid = "SSID";
const char *password = "PASSWORD";

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

void setup()
{
  Serial.begin(115200);
  delay(10);
  Serial.println();

  WiFi.mode(WIFI_STA);
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to:\t");
  Serial.println(ssid);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(200);
  Serial.print('.');
  }

  // Show success message and assigned IP address
  Serial.println("Connection established");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());
}

void loop()
{
}
Copied!

It is important to note that the router must be configured to assign us the desired IP. Typically, this means it must be outside the range of IPs designated for DHCP.

Example in AP Mode

In AP mode, configuring a static IP is less critical than in STA mode. After all, we are generating our own WiFi and we can set the IP we want.

Even so, the WiFi.softAPConfig() function allows changing the parameters within the generated WiFi network. The following example shows the configuration, where we also change the board’s address to 192.168.1.200.

#include <WiFi.h>

const char *ssid = "ESP8266_AP";
const char *password = "0123456789";

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

void setup()
{
  Serial.begin(115200);
  delay(10);
  Serial.println();

  WiFi.softAP(ssid, password);
  WiFi.softAPConfig(ip, gateway, subnet);
  Serial.print("AP Started:\t");
  Serial.println(ssid);

  Serial.print("IP address:\t");
  Serial.println(WiFi.softAPIP());
}

void loop() { }
Copied!

Summarized Example

If we split the code into different files, the code becomes more concise and easier to maintain.

#include <WiFi.h>

#include "config.h"  // Replace with your network data
#include "ESP32_Utils.hpp"

void setup()
{
  Serial.begin(115200);

  // For STA mode
  ConnectWiFi_STA(true);

  // For AP mode
  //ConnectWiFi_AP(true);
}

void loop()
{
}
Copied!

An additional file we’ll call ‘ESP32_Utils.hpp’ containing the connection functions.

void ConnectWiFi_STA(bool useStaticIP = false)
{
   Serial.println("");
   WiFi.mode(WIFI_STA);
   WiFi.begin(ssid, password);
   if(useStaticIP) WiFi.config(ip, gateway, subnet);
   while (WiFi.status() != WL_CONNECTED)
   {
     delay(100);
     Serial.print('.');
   }

   Serial.println("");
   Serial.print("STA Started:\t");
   Serial.println(ssid);
   Serial.print("IP address:\t");
   Serial.println(WiFi.localIP());
}

void ConnectWiFi_AP(bool useStaticIP = false)
{
   Serial.println("");
   WiFi.mode(WIFI_AP);
   while(!WiFi.softAP(ssid, password))
   {
     Serial.println(".");
     delay(100);
   }
   if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet);

   Serial.println("");
   Serial.print("AP Started:\t");
   Serial.println(ssid);
   Serial.print("IP address:\t");
   Serial.println(WiFi.softAPIP());
}
Copied!

A file with our WiFi data

const char* ssid     = "ssid";
const char* password = "password";
Copied!

That’s how easy it is! With this our board will always use the same IP address within the network, which is exactly what we want when we plan to connect to it regularly.

Download the Code

All the code from this post is available for download on Github.

github-full

Version for ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples

Version for ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples