-->
Page 1 of 3

LOSS PROBLEM OF WIRELESS CONNECTION ON ESP, WEMOS D1,NodeMCU

PostPosted: Sun Jan 28, 2018 3:24 pm
by pascal69
Hello to all.
It's about 3 days that I'm going crazy and I can not get over it. Any ESP WiFi module I connect to my static IP router, after a few minutes I lose my connection. I tried everything and more and read a few thousand pages in all languages but the problem persists. The modules I tested are: ESP8266-07, Wemos D1 and NodeMCU with an ESP8266-F on board. When they lose the connection, I use the arduino serial monitor to see the status, give me the OK connection, the IP address to which they are connected and the status of the active server, but in reality there is no way to contact them via wireless request, until I reset them. I can not believe this is my only problem and it never happened to anyone ...

Here is one of the sketches used:

Code: Select all
#include <ESP8266WiFi.h>
IPAddress ip(192, 168, 1, 222);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

const char* ssid     = "TP-LINK_123A47";
const char* password = "mypassword";


//#define DEBUG true
//#define Serial if(DEBUG)Serial
//#define DEBUG_OUTPUT Serial
//extern "C" {
//  #include "user_interface.h"
//}

WiFiServer server(80);
WiFiClient client;

/********************************************************
/*  Debug Print                                         *
/********************************************************/
void dbg_printf ( const char *format, ... )
{

    static char sbuf[1400];                                                     // For debug lines
    va_list varArgs;                                                            // For variable number of params

    va_start ( varArgs, format );                                               // Prepare parameters
    vsnprintf ( sbuf, sizeof ( sbuf ), format, varArgs );                       // Format the message
    va_end ( varArgs );                                                         // End of using parameters

    Serial.print ( sbuf );

}


/********************************************************
/*  Handle WiFi events                                  *
/********************************************************/
void eventWiFi(WiFiEvent_t event) {
     
  switch(event) {
    case WIFI_EVENT_STAMODE_CONNECTED:
      dbg_printf("[WiFi] %d, Connected\n", event);
    break;
   
    case WIFI_EVENT_STAMODE_DISCONNECTED:
      dbg_printf("[WiFi] %d, Disconnected - Status %d, %s\n", event, WiFi.status(), connectionStatus( WiFi.status() ).c_str() );     
    break;
   
     case WIFI_EVENT_STAMODE_AUTHMODE_CHANGE:
      dbg_printf("[WiFi] %d, AuthMode Change\n", event);
    break;
   
    case WIFI_EVENT_STAMODE_GOT_IP:
      dbg_printf("[WiFi] %d, Got IP\n", event);
     // setupOTA();
    break;
   
    case WIFI_EVENT_STAMODE_DHCP_TIMEOUT:
      dbg_printf("[WiFi] %d, DHCP Timeout\n", event);
    break;
   
    case WIFI_EVENT_SOFTAPMODE_STACONNECTED:
      dbg_printf("[AP] %d, Client Connected\n", event);
    break;
   
    case WIFI_EVENT_SOFTAPMODE_STADISCONNECTED:
      dbg_printf("[AP] %d, Client Disconnected\n", event);
    break;
   
    case WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED:
      dbg_printf("[AP] %d, Probe Request Recieved\n", event);
    break;
   
    default:
        dbg_printf("[GENERIC EVENT] %d\n", event);
    break;
  }
 
}



/********************************************************
/*  WiFi Connection Status                              *
/********************************************************/
String connectionStatus ( int which )
{
    switch ( which )
    {
        case WL_CONNECTED:
            return "Connected";
            break;

        case WL_NO_SSID_AVAIL:
            return "Network not availible";
            break;

        case WL_CONNECT_FAILED:
            return "Wrong password";
            break;

        case WL_IDLE_STATUS:
            return "Idle status";
            break;

        case WL_DISCONNECTED:
            return "Disconnected";
            break;

        default:
            return "Unknown";
            break;
    }
}


void setup(){
  Serial.begin(9600);
  WiFi.config(ip, gateway, subnet);
  delay(100);

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

  WiFi.setAutoConnect ( true );                                             // Autoconnect to last known Wifi on startup
  WiFi.setAutoReconnect ( true );                                             
  WiFi.onEvent(eventWiFi);                                                    // Handle WiFi event
  WiFi.mode(WIFI_STA);                                                        // Station mode

 
  WiFi.begin(ssid, password);
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
      Serial.print(".");
      delay(200);
  }
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println();
    Serial.println("Fail connecting");
    delay(5000);
    ESP.restart();
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
 // server.setNoDelay(true);
  Serial.println("Server started");

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

void loop() {

if (Serial.available()>0) {
  String r = Serial.readStringUntil('\r');
  Serial.print("OK: ");
  Serial.println(r);
  //Serial.flush();

  r = r.substring(0,3);
 
  if (r == "000") {
   if (WiFi.status() != WL_CONNECTED) {
     Serial.println("CONNECTION...");
     ESP.restart();
     return;
   } else {
     Serial.println("WIFI IS CONNECT");
   }
 
  } else if (r == "001") {
   // Print the IP address
   Serial.print("IP ADDR: ");
   Serial.println(WiFi.localIP());   
 
  } else if (r == "002") {
   // RESTART ESP
   Serial.println("RESTART ESP");
   ESP.restart();
   return;
   
  } else if (r == "003") {
   
   Serial.println(server.status());
  }
 
 
}

// Check if a client has connected
client = server.available();
if (!client) {
return;
}

// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}

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


   // Match the request
   int val;
   if (req.indexOf("/gpio/0") != -1) {
     //digitalWrite(2, LOW);
     Serial.println("PIN GO TO LOW");
     val = 0;
   } else if (req.indexOf("/gpio/1") != -1) {
     //digitalWrite(2,HIGH);
     Serial.println("PIN GO TO HIGHT");
     val = 1;
   } else {
     Serial.println("invalid request");
     //client.stop();
     //return;
     val = 2;
   }

// Set GPIO2 according to the request
//digitalWrite(2, val);

//client.flush();

// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now ";
if (val != 2) {
  s += (val)?"high":"low";
} else s += "undefined";
s += "\n";

// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");

// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}



Finally, if I connect to the wifi router of the phone with the ip address assigned by dhcp and without a password, everything works wonderfully without any kind of problem.

Does anyone have any solution or tell me how can I detect the connection drop by software?

Thank's
Pascal

Re: LOSS PROBLEM OF WIRELESS CONNECTION ON ESP, WEMOS D1,Nod

PostPosted: Mon Jan 29, 2018 12:34 am
by htwtsang
I tried playing around with connecting to a static IP router and had similar problems, from emailing the people at espressif they said as long as you turn off the DHCP server function and set the IP address beforehand it should work but I havent had time to go back and try again.

I suggesting checking with your router to see if you can see the device's hostname and if the IP address is correct.

Re: LOSS PROBLEM OF WIRELESS CONNECTION ON ESP, WEMOS D1,Nod

PostPosted: Mon Jan 29, 2018 7:49 am
by pascal69
If you enable the debugging function, everything seems to work properly. The problem is the ESP8266WiFi library and other libraries that refer to it.

Pascal

Re: LOSS PROBLEM OF WIRELESS CONNECTION ON ESP, WEMOS D1,Nod

PostPosted: Wed Jan 31, 2018 1:52 am
by htwtsang
I had some time and went back for another look, this time I got the IP and it connects fine but it seems to only work when im in station only mode.