How can I Fix a Static IP Address on ESP-01 and ESP-01S

Hi All,

I have been working around with all kind of ESP8266 devices and most of my work includes NodeMCU and D1 Mini boards. Of which I have been satisfied with all the way.

But, When I uploaded a NodeMCU and D1 mini 100% working sketch on the ESP-01S board - I could not make it sign up for the Static IP I set it for - It always uses DHCP. That part I need to do since I want to use it for IoT and I need the device to keep the IP Provided to it As I have done with the NodeMCU and D1 Min Boards.

My Includes:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>
#include <ArduinoJson.h>
#include <StreamString.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;

  //Static IP address configuration

This is the Settings that I have put at the TOP of the sketch .ino file.

  //Static IP address configuration
  IPAddress ip(192, 168, 0, 24); //ESP static ip
  IPAddress gateway(192, 168, 0, 1);   //IP Address of your WiFi Router (Gateway)
  IPAddress subnet(255, 255, 255, 0);  //Subnet mask
  IPAddress dns(8, 8, 8, 8);  //DNS
  const char* deviceName = "duino";
  //On board LED Connected to GPIO2
  #define LED 2  
  //SSID and Password of your WiFi router
  const char* ssid = "MySSID";
  const char* password = "MyPass";

Then I have added my functions and all the other settings I needed for the IoT device to work, of which it works 100% But uses a DHCP IP and Not the fixed IP I provided. It gets or any other IP besides the that I set up.

void setup() {

  WiFi.hostname(deviceName);      // DHCP Hostname
  // HERE IS THE SETUP I AM TALKING ABOUT: If you use .config before .begin it should use it, correct?
  WiFi.config(ip, subnet, gateway, dns);
  WiFi.begin(ssid, password);

  Serial.println("WiFi connected. ");
  Serial.print("Hostname: ");
  Serial.print("IP address: ");
// Here I am getting and if I add a delay I get the IP

No matter how hard I try to make it get the Static IP - it fails all the time. But when I use the SAME CODE on NodeMCU and D1 Mini Boards - it works 100% and they get the static IP I provide them. And yes I have given each one a different IP so there are NO IP collisions at all.

I would be happy if you could assist me in understanding what I am doing wrong here - is it me? Is it the ESP-01S board - is it ESP8266Wifi Library? :confused:

I would be happy to be able to keep a Static IP on this board since I have them already planned to be on each of my IoT devices.

Would be happy if you could assist me on that :frowning: :frowning:
Thank you!

because it already connected to WiFi at that time. try WiFi.setAutoConnect(false)

Yes, I have tried that and I am getting:

exit status 1
'WiFi' does not name a type

Any idea why?

I have managed to figure out why I got the error - But, I still have the device auto connect through DHCP even with the WiFi.setAutoConnect(false).
Not sure what is forcing it to connect through the DHCP?
Please assist, thanks.

SOLVED! thanks

SOLVED! thanks

could you post how you solved it, to help others in future may be

Here is the code I have used - and I could make the ESP-01S to get a Static IP Address:

// config static IP
  WiFi.setAutoConnect(false);   // Not working by its own
  WiFi.disconnect();  //Prevent connecting to wifi based on previous configuration
  IPAddress ip(192, 168, 0, 25); 
  IPAddress gateway(192, 168, 0, 1);
  Serial.print(F("Setting static ip to : ")); // Adding these lines to see the IP that the device is getting
  IPAddress subnet(255, 255, 255, 0);
  IPAddress dns(8, 8, 8, 8); 
  WiFi.config(ip, gateway, subnet, dns);
//  WiFi.config(ip, subnet, gateway, dns);

Hope that would help :slight_smile:

Thanks for your assistance.