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

How to set up a static IP on the ESP8266 or ESP32

  • 4 min

We continue with the posts dedicated to the ESP8266 and ESP32. This time we will see how to configure a static IP for our connection.

We will refer to the ESP8266, but the same code is compatible with the ESP32, adjusting the library names. At the end you have the code for both the ESP8266 and the ESP32.

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 we have our ESP8266 connected to the WiFi network, the next need will be to connect to our device, for which we will have to be able to find it within 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 way is to configure a static IP on the ESP8266.

Fortunately, it is very easy to configure a static IP on the ESP2866 with the ‘config’ function of ESP8266WiFi.

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

Note that the order of the parameters is different from the Arduino WiFi library (the ESP8266WiFi and Arduino WiFI libraries are similar, but not identical). Keep this in mind if you look for tutorials on the Internet, because sometimes they mix them up.

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.

In the case of the ESP32, the library is simply called ‘WiFi’.

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 <ESP8266WiFi.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 ESP8266’s address to 192.168.1.200.

#include <ESP8266WiFi.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

As we did in the previous posts, if we split the code into different files, the code becomes more concise and easier to maintain.

#include <ESP8266WiFi.h>

#include "config.h"  // Replace with your network data
#include "ESP8266_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 ‘ESP8266_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! In the next post we will see another way to find our device with mDNS. See you soon!

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