0
\$\begingroup\$

I am trying to connect between two ESP8266-01 chips using one as an access point and another in the station mode connecting to the AP. I am using a simple WiFiClient (from the examples) to create a connection to an access point hosted by another ESP01. It fails to connect to it.

I have tried using my router WiFi as an access point and the ESP8266 client connects to it fine. It is able to request URLs and response from the same but when I just change the settings to an access point hosted by a ESP01, it fails with an error. Can you please help me identify why does it fail only for the ESP8266-01 access point?

Please note that I am able to connect to this access point ESP02 using my browser and request for a page. It works well with no issues but not with another ESP Client.

  #include <ESP8266WiFi.h>

  const char* ssid       = "ESP02";
  const char* password   = "pswdesp002";
  //const char* ssid     = "EAGLE_EXT";
  //const char* password = "xxxxxxxxxx";

  const char* host = "192.168.4.1";
  //const char* host = "192.168.1.1";
  const char* streamId   = "";
  const char* privateKey = "";

  void setup() {
    Serial.begin(115200);
    delay(10);
    monitorWiFi();
  }

  void monitorWiFi()
  {
    // We start by connecting to a WiFi network
    if(WiFi.status() != WL_CONNECTED){
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);

      WiFi.begin(ssid, password);

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

      Serial.println("");
      Serial.println("WiFi connected");  
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    }
  }

  int value = 0;

  void loop() {
    delay(5000);
    ++value;

    monitorWiFi();
    Serial.print("connecting to ");
    Serial.println(host);

    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
      Serial.println("connection failed");
      return;
    }

    // We now create a URI for the request
    String url = "/setlevel/";
    url += "&value=";
    url += "50";

    Serial.print("Requesting URL: ");
    Serial.println(url);

    // This will send the request to the server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" + 
                 "Connection: close\r\n\r\n");
    unsigned long timeout = millis();
    while (client.available() == 0) {
      if (millis() - timeout > 5000) {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
      }
    }

    // Read all the lines of the reply from server and print them to Serial
    while(client.available()){
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }

    Serial.println();
    Serial.println("closing connection");
  }

Debug messages:

Connecting to ESP02 ....... WiFi connected IP address: 192.168.4.2 connecting to 192.168.4.1 :err -8 connection failed connecting to 192.168.4.1 :err -8 connection failed connecting to 192.168.4.1 :err -8 connection failed connecting to 192.168.4.1 :err -8 connection failed chg_B1:-40 connecting to 192.168.4.1 :err -8 connection failed chg_B2:0 connecting to 192.168.4.1

\$\endgroup\$
2
\$\begingroup\$

I was able to solve this issue by including the line WiFi.mode(WIFI_STA); before WiFi.begin(ssid, password); and whola! the problem solved. Although if you go into the code of WiFi.begin(ssid, password); , I think it sets the station mode to STA but not sure why does it need to be set explicitly. I got this suggestion from another website where people faced the same issue.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.