problem when I want to make the ip static in esp 8266.

I am not use the standard arduino board, I use the board from other manufacture which is cheaper.

But I got Some problems when I Want to fix the ip.

Everything works fine when I use the example of the web server in arduino IDE:

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

But it always return the random IP Which is hard to handle.

So I check this link : Fixed IP with ESP8266WiFi · Issue #1959 · esp8266/Arduino · GitHub

And make my code to:

  WiFi.mode(WIFI_STA);
  // config static IP
  IPAddress ip(192, 168, 1, 200); // where xx is the desired IP Address
  IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
  Serial.print(F("Setting static ip to : "));
  Serial.println(ip);
  IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);

But is not worked when I connect the url: http://192.168.1.200/gpio/0

So can anyone tell me why this happened? It is related to my board is not standard?
I get it from a youtuber recommand.

Here is my all code :

/*
    This sketch demonstrates how to set up a simple HTTP-like server.
    The server will set a GPIO pin depending on the request
      http://server_ip/gpio/0 will set the GPIO2 low,
      http://server_ip/gpio/1 will set the GPIO2 high
    server_ip is the IP address of the ESP8266 module, will be
    printed to Serial when the module is connected.
*/

#include <ESP8266WiFi.h>

#ifndef STASSID
#define STASSID "HUAWEI-WQAB58"
#define STAPSK  "fighttilldie"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

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

  // prepare LED
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 0);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print(F("Connecting to "));
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  // config static IP
  IPAddress ip(192, 168, 1, 200); // where xx is the desired IP Address
  IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
  Serial.print(F("Setting static ip to : "));
  Serial.println(ip);
  IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();
  Serial.println(F("WiFi connected"));

  // Start the server
  server.begin();
  Serial.println(F("Server started"));

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println(F("new client"));

  client.setTimeout(5000); // default is 1000

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(F("request: "));
  Serial.println(req);

  // Match the request
  int val;
  if (req.indexOf(F("/gpio/0")) != -1) {
    val = 0;
  } else if (req.indexOf(F("/gpio/1")) != -1) {
    val = 1;
  } else {
    Serial.println(F("invalid request"));
    val = digitalRead(LED_BUILTIN);
  }

  // Set LED according to the request
  digitalWrite(LED_BUILTIN, val);

  // read/ignore the rest of the request
  // do not client.flush(): it is for output only, see below
  while (client.available()) {
    // byte by byte is not very efficient
    client.read();
  }

  // Send the response to the client
  // it is OK for multiple small client.print/write,
  // because nagle algorithm will group them into one single packet
  client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now "));
  client.print((val) ? F("high") : F("low"));
  client.print(F("

Click <a href='http://"));
  client.print(WiFi.localIP());
  client.print(F("/gpio/1'>here</a> to switch LED GPIO on, or <a href='http://"));
  client.print(WiFi.localIP());
  client.print(F("/gpio/0'>here</a> to switch LED GPIO off.</html>"));

  // The client will actually be *flushed* then disconnected
  // when the function returns and 'client' object is destroyed (out-of-scope)
  // flush = ensure written data are received by the other side
  Serial.println(F("Disconnecting from client"));
}

try to add WiFi.disconnect(); for one run

Juraj:
try to add WiFi.disconnect(); for one run

where should I add this line to?

lizhe:
where should I add this line to?

before begin()

Sadly not work when I change the code to this... So strange. Still thank you !

  WiFi.mode(WIFI_STA);
  // config static IP
  IPAddress ip(192, 168, 1, 200); // where xx is the desired IP Address
  IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
  Serial.print(F("Setting static ip to : "));
  Serial.println(ip);
  IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
  WiFi.config(ip, gateway, subnet);
  WiFi.disconnect();
  WiFi.begin(ssid, password);

lizhe:
Sadly not work when I change the code to this... So strange. Still thank you !

  WiFi.mode(WIFI_STA);

// config static IP
  IPAddress ip(192, 168, 1, 200); // where xx is the desired IP Address
  IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
  Serial.print(F("Setting static ip to : "));
  Serial.println(ip);
  IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
  WiFi.config(ip, gateway, subnet);
  WiFi.disconnect();
  WiFi.begin(ssid, password);

try to move it before config()

are you sure this is your gateway ?IPAddress gateway(192, 168, 1, 1);what is the random IP that is assigned by your router ? Apple router tend to useIPAddress gateway(192, 168, 0, 1);Only other option is that the IP you assign is in use (though not so likely, but try 205..) for even easier lookup mDNS is also an option.

OMG, it is the gate way problem, all my sub ip under my router is started with "192.168.3.x"

So when I change the code to :

WiFi.mode(WIFI_STA);
  //config static IP
  IPAddress ip(192, 168, 3, 200); // where xx is the desired IP Address
  IPAddress gateway(192, 168, 3, 1); // set gateway to match your network
  Serial.print(F("Setting static ip to : "));
  Serial.println(ip);
  IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
  WiFi.config(ip, gateway, subnet);

everything works fine! thanks bro, you help me a lot!

Also thanks @Juraj, you help me a lot too.

At first I know my arduino will be used in different router, so I want to fix the ip.

But if the gateway changed by router seems every time the router changed I have to reupload the program after changing the gateway. Which seems a little boring for me. :slight_smile: Gladly this issue solved!

lizhe:
But if the gateway changed by router seems every time the router changed I have to reupload the program after changing the gateway.

That is why i tend to use the dynamic IP, in the following way, I create an Access Point, and from there i show the dynamic IP on 'a' page. Also really look into mDNS then you don't need the IP address. unfortunately mDNS is not supported on Android browsers still.