ESP32 Static IP Address (Arduino IDE) – Assign Fixed IP

In this tutorial, you’ll learn how to set a static IP for ESP32 and make the IP address fixed any time it connects to a network with Arduino IDE. We’ll discuss the possible ways to make sure that ESP32 gets a static IP address every time it connects to the network.

Before proceeding with this tutorial, you should have installed the ESP32 Arduino Core in your Arduino IDE to be able to compile and build projects for ESP32 in Arduino IDE. Follow the tutorial below to get started if you haven’t done that already.

If you’re just starting with ESP32, it’s highly recommended to begin with the following tutorial to kickstart your journey with ESP32 microcontrollers. Then, go to the second link which directs you to the main page for ESP32 Tutorials Series, where you’ll find all ESP32 tutorials ordered and categorized in a logical way that guarantees you systematic progress in learning ESP32 programming and IoT.

Table of Contents

  1. ESP32 IP Address Assignation
  2. ESP32 Static IP Address Example (Arduino)
  3. Assign Static IP Address To ESP32 With MAC
  4. Concluding Remarks

ESP32 IP Address Assignation

When an ESP32 connects to a network, it will obtain an IP address dynamically using the DHCP (Dynamic Host Configuration Protocol). Every time the ESP32 restarts and re-connects to the network, it’s guaranteed that it’s going to take the same IP address again. The IP address assigned is randomly decided by the DHCP.

The DHCP also provides additional network settings such as subnet mask and gateway. The subnet mask determines the range of IP addresses that are considered local to the network, while the gateway is the IP address of the router that allows the ESP32 to communicate with devices on other networks. These settings are automatically configured by the DHCP.

So we can summarize the network configuration settings that DHCP assigns to the ESP32 once it joins a network as follows:

  • Local IP Address: The local IP address assigned to the ESP32
  • Subnet Mask: The range of IP addresses that are considered local to the network
  • Gateway Address: The IP address of the router that allows the ESP32 to communicate with devices on outside networks
  • DNS IP Address: Primary and secondary IP address of the DNS server that resolves domain names to IP addresses

Given that we’re not going to use DHCP for IP address assignation, we still need to assign all other network settings (information) that were previously done by the DHCP. Even if we set a valid static IP address, if other fields have invalid values the connection will still fail.

The safe way to do it is to run a simple ESP32 WiFi connection example and print all the auto-assigned values by the DHCP (Local IP, subnet mask, gateway address, and DNS address). Then we change the IP address to the desired value and keep the other fields as is and save the settings. And this is what we’re going to do in the next example.


ESP32 Static IP Address Example (Arduino)

As stated earlier, to assign a static IP address to ESP32 without DHCP, we’ll have to define the following parameters.

Then you need to call the WiFi.config() function within the setup() function as shown below.

But as we’ve discussed earlier it’s better to normally connect to your WiFi network without static IP just to get the default parameters. Then assign the desired static IP address. This is what we’ll be doing next.

Get Default ESP32 Network Parameters

Here is a generic ESP32 example to connect it to your WiFi network and print the DHCP auto-assigned parameters Local IP, subnet mask, gateway address, and DNS address).

Don’t forget to change the ssid and password to match your network credentials.

Here are the network setting (auto-assigned by DHCP) after connecting the ESP32 to the WiFi network.

ESP32-Static-IP-Address-Arduino-Example

Now, we can modify the local IP and keep the rest as is to avoid having any wrong settings. And we can also neglect the DNS address which is optional anyway.

Assign Static IP Address For ESP32

Here is how to set a static IP address for ESP32 full Arduino code example.

Don’t forget to change the ssid and password to match your network credentials.

And here is the result after running this example code on my ESP32 board. The ESP32 has been assigned with the desired Static (Fixed) IP address that we’ve defined in the code. And you can change it however you want and give it a try.

ESP32-Static-IP-Address-Arduino-Example-Code-Result


Assign Static IP Address To ESP32 With MAC

Alternatively or if you’ve been facing any issues while assigning a static IP for your ESP32, you can rely on your router to do this job and always assign a fixed IP address to the ESP32 board whenever it attempts to connect to the WiFi network. This can be achieved by giving your router the MAC address of the ESP32 and telling it to assign a static fixed IP address to it whenever a new connection with ESP32 is established.

In this way, you can guarantee that your specific ESP32 board will always get a pre-defined static IP address when it joins the WiFi network of your router (access point). And it doesn’t require any code on the ESP32 side.

However, you still need to go to the admin settings page of your router and do this configuration step manually for once and it’ll stay there.

❕ Note

Occasionally we may in some cases need to reset the router to solve some ISP issues. Resetting your router using the tiny hole button will delete all saved configurations on the router. And obviously, the saved MAC address and static IP address of the ESP32 will be whipped out. You’ll need to redo it again when this happens.

To open the router’s admin settings page, you need to enter 192.168.1.1 in the URL section of the browser.

For example, the TP-Link Archer VR600 admin setting interface has this option under: Advanced Settings > Network > LAN Settings > Address Reservation. And different routers will have the same option but maybe in a different position of its interface.

Write the MAC address of the ESP32 and the desired static IP address and click save. That’s all!

ESP32-Static-IP-Address-Assign-Arduino-Guide


Parts List

Here is the full components list for all parts that you’d need in order to perform the practical LABs mentioned here in this article and for the whole ESP32 series of tutorials found here on DeepBlueMbedded. Please, note that those are affiliate links and we’ll receive a small commission on your purchase at no additional cost to you, and it’d definitely support our work.


Concluding Remarks

To conclude this tutorial, we’ll highlight the fact that you can assign a static IP address to your ESP32 board in two different ways. The first of which is to use the provided Arduino code example to set a Static IP address for ESP32. The other way is to configure your router (access point) to check the MAC address of the ESP32 and assign to it a static IP address when it connects to the network.

If you’re just starting to learn about ESP32 WiFi, it’s highly recommended that you follow the tutorial below and keep it bookmarked in your browser. It is crafted specifically to help beginners with ESP32 WiFi to know everything about this huge topic and draw a clear roadmap for what to learn in a systematic way.

You can easily get overwhelmed when starting with ESP32 WiFi by the number of topics and libraries out there (e.g HTTP, TCP/IP, UDP, WebServers, AsyncWebServers, WebSocket, MQTT, ESP-NOW, etc…). There are too many libraries, modes, topologies, and protocols that make just getting started an even harder task than it should be. And the most reasonable question is where should I get started? The guide below is the perfect answer!

???? Also Read
ESP32 WiFi Tutorial & Library Examples

This is the ultimate guide for ESP32 WiFi, if you’re just starting with ESP32 WiFi, you should definitely check it out. It’ll provide you with a clear roadmap for what to learn and in which order to fully understand everything about ESP32 WiFi.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment