0

I'm trying to connect NODEMCU to my home network and set static IP address, but the static IP didn't set properly this is the code:

#include <ESP8266WiFi.h>
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.config(IPAddress(192,168,1,129), IPAddress(192,168,1,1), IPAddress(225,225,225,0));
  WiFi.begin("Bam", "123456789");
  Serial.print("\nConnecting");
  while(WiFi.status()!=3){
    Serial.print(".");
    delay(500);
  }
  Serial.println("Connected");
  Serial.println(WiFi.localIP());
  

}

void loop() {
  // put your main code here, to run repeatedly:

}

and this is the output on the Serial monitor:

13:28:46.715 -> Connecting.......Connected
13:28:50.937 -> 192.168.1.100

when I set the same static IP address to my phone it sets with no problem.

I tried to config the IP before connecting to WIFI:

WiFi.mode(WIFI_STA);
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, pass);

I tried to add delay before printing the local IP:

WiFi.mode(WIFI_STA);
WiFi.config(ip, gateway, subnet);
delay(2000);
WiFi.begin(ssid, pass);
//loop to check if nodemcu is connected
Serial.println(WiFi.localIP);

I tried to disconnect and reconnect the WIFI I tried to disable the WIFI and start a new connection I tried to add the 4th and 5th argument which are DNS1 DNS2 none of these work for me.

I wanna see when printing the local IP the address I set to NODEMCU.

4
  • You should configurate the ip before you start the actual connection. Commented Apr 18, 2023 at 13:25
  • @Juraj I said I've tried everything even config first, I mentioned all solution I've tried in the question. Commented Apr 18, 2023 at 13:25
  • add WiFi.disconnect(); after WiFi.mode. mode does begin with stored settings. add WiFi.setAutoReconnect(false) to disable that stored connection. add WiFi.persistent(false) to turn off storing the settings in flash memory Commented Apr 18, 2023 at 13:34
  • @Juraj didn't work Commented Apr 18, 2023 at 13:40

1 Answer 1

0

This is the code that worked for me:

// Configures static IP address
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("STA Failed to configure");
  }
  
  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();

You can find it on the articel here: https://randomnerdtutorials.com/esp8266-nodemcu-static-fixed-ip-address-arduino/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.