So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Findus LeChat
#81397 Dear community,

I am experiencing problems with an ESP connected with static IP to a WiFi network. I start a webserver on the device that I want to reach from within the network. This actually works perfectly fine from some devices but does not work from others (website not being reachable).

The setup is the following:

Connexion:
- There are 2 networks, one is an extender of the first, both share the same password.
- No difference in behaviour is observed with the node and clients connected to either of the two networks
- The ESP connects rapidly to either of the two networks
- The setup routine does not wait for the connection to be established as I am running a light animation in the background. I rather check its status in the main loop and launch the server once the device connected

Observations:
- With the current IP, I do not see or ping the ESP from neither of 2 windows 10 PCs (Firefox)
- 3 android phones all loaded the website correctly (also Firefox) until recently one of the 3 phones did not anymore (page not reachable error)
- I chose other static IP resulting in the laptops seeing the device but then the android phones wouldn't
- No errors seen on the ESP, connects rapidly and with the correct static IP
- I reserved an IP address for the ESP on the network, IP is within the accepted range (192.168.1.100 to .199)

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h>
#include <FS.h>
#include <ArduinoOTA.h>


const char* ssid1 = "NW1";
const char* ssid2 = "NW2";
bool ssidID = 0;
const char* password = "XXX"; // remplacer par le mot de passe de votre WiFi
const char* htmlfile = "/index.html";
const char* deviceName = "XXX";
//Static IP address configuration
IPAddress staticIP(192, 168, 1, 110); //ESP static ip
IPAddress gateway(192, 168, 1, 1);   
IPAddress subnet(255, 255, 255, 0);

ESP8266WebServer server(80);

uint32_t tms1;
uint32_t tms2;
uint32_t tms3;
bool wifistat = 0;

void setup(void){
    Serial.begin(115200);
    delay(15);
    Serial.println("Starting up.");

    WiFi.mode(WIFI_STA);
    Serial.print("Try to connect to ");
    if(ssidID == 0){
        WiFi.begin(ssid1, password);
        Serial.print(ssid1);
        WiFi.disconnect();
        WiFi.hostname(deviceName);
        WiFi.config(staticIP, subnet, gateway);
        WiFi.begin(ssid1, password);
       
    }else{
        WiFi.begin(ssid2, password);
        Serial.print(ssid2);
        WiFi.disconnect();
        WiFi.hostname(deviceName);
        WiFi.config(staticIP, subnet, gateway);
        WiFi.begin(ssid2, password);
    }
   
  ArduinoOTA.onStart([]() {
    Serial.println("Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.setPort(8266);
  ArduinoOTA.setHostname("Ringlampe");
  ArduinoOTA.begin();

  //Initialize Webserver
  server.on("/",handleRoot);
  server.onNotFound(handleWebRequests);

   
    tms2 = millis();
}
void loop(void){
    ArduinoOTA.handle();
    delay(1);
    if(WiFi.status() != WL_CONNECTED){
    //setup();
        if(wifistat == 1){
            wifistat = 0;
            Serial.println("Wifi disconnected");
        }   
    }else{
        if(wifistat == 0){
            wifistat = 1;
            Serial.print("Connected to ");
            Serial.print(WiFi.SSID());
            delay(1);
            Serial.print(" with IP ");
            Serial.println(WiFi.localIP());
            Serial.println("begin server");
            server.begin();
        }
        tms2 = millis();
    }
    //in case I get a disconnect status for more than 10s, restart and switch network
    if(millis()-tms2>10000){
        ssidID = !ssidID;
        WiFi.disconnect();
        Serial.println("Change Wifi and restart");
        delay(10);
        setup();
    }
    server.handleClient();
    delay(1);
    //other code to animate
    if(millis()-tms3>20000){
        Serial.println("Alive");
        tms3 = millis();
    }
}

NOTE: I excluded some of the code I consider less important.
I suppose that the static IP is part of the problem.

Am I missing out on something?
Thanks so much for your advice.
Findus
User avatar
By mel
#82318 hi, dont know if this will help you with this problem, but I have been having some wifi connections problems with the Nodemcu, the one with to two chips not the one with the square silver chip,,, ok if I upload the sketch to the Nodemcu from a windows 10 PC with Arduino IDE the Nodemcu plays up with staying connected to the wifi, even though it tested and uploaded fine, now if I take the same sketch and uploaded it from a Linux PC with Arduino IDE the Nodemcu connects to the wifi and works fine.
PS: I used 2 Windows 10 computers and one was a new build and the other was one that I had and was only work fine weeks before when I up uploaded a sketch.
don't know if its the some problem you are having, but you never know .

regards,
Melissa