We continue with the entries dedicated to the ESP8266 and the ESP32. This time we will see how to set up a static IP in our connection.
We will refer to the ESP8266, but the same code is compatible for the ESP32, adjusting the name of the libraries. At the end you have the code for both the ESP8266 and the ESP32.
In the two previous entries we have seen 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.
So far, we had shown the IP address as soon as we connected by serial port. But it is not very practical to have to have a computer or a screen connected to the device to see which IP address 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 is to set up a static IP on the ESP8266.
Fortunately, it is very easy to set up 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);
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 are looking for tutorials on the internet, because sometimes they get confused.
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 set up a static IP in STA mode, that is, 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 generated 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()
{
}
It is important to note that the router must be configured to assign us the desired IP. Typically, this means that it must be outside the range of IPs designated for DHCP.
Example in AP mode
In AP mode, setting up the 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.
Still, the WiFi.softAPConfig() function allows us to change the parameters within the generated WiFi network. The following example shows the configuration, where we also change the address of the ESP8266 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("Started AP:\t");
Serial.println(ssid);
Serial.print("IP address:\t");
Serial.println(WiFi.softAPIP());
}
void loop() { }
Summary Example
As we did in the previous entries, if we divide the code into different files, the code is summarized 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()
{
}
An additional file that we will 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("Started STA:\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("Started AP:\t");
Serial.println(ssid);
Serial.print("IP address:\t");
Serial.println(WiFi.softAPIP());
}
A file with the data of our WiFi network.
const char* ssid = "ssid";
const char* password = "password";
That’s it! In the next entry we will see another way to find our device with mDNS. See you soon!
Download the code
All the code in this post is available for download on Github.
Version for the ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples
Version for the ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples