1

I have one ESP(1) opening an AP and running a server on it. The other ESP(2) is running a client on it and is connecting to the AP and after that trying to connect to the host server. The connection to the AP does succeed but the connection to the host always fails and I cannot figure out what the problem is here. Testing the server via netcat and packetsender works fine. Connecting to it via telnet fails because the connections gets closed by the "foreign host".

Server Sketch

#include <ESP8266WiFi.h>

const char* ssid = "artex11";
const char* pw = "getmenow11";
WiFiServer server(80);
WiFiClient client;

void setup() {
  Serial.begin(115200);
  //setup AP
  WiFi.softAP(ssid, pw);
  Serial.println("AP started at: ");
  Serial.print(WiFi.softAPIP());
  server.begin();
  Serial.println("Server started...");
}

void loop() {
  client = server.available();

  if(client){
    Serial.println("Client connected!");
    Serial.print("HasClient: ");
    Serial.println(server.hasClient());
    char message = client.read();
    Serial.printf("Message: %c\n", message);
  }
}

Client Sketch

#include <ESP8266WiFi.h>

const char* ssid = "artex11";
const char* pw = "getmenow11";
const char* host = "192.168.4.1";
const int port = 80;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pw);
  Serial.println("Connecting to AP");
  while(WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print("."); 
   }
  Serial.println(" connected");
}

void loop() {
  WiFiClient client;
  Serial.println("Connecting to host");
  if(!client.connect(host, port)){
    Serial.println("...connection failed!");
    Serial.println("Retrying in 5 seconds...");
    delay(5000);
    return;  
  }
  client.print("A");
  Serial.println("...TCP message fired!");
  delay(2000);
}
  • In your Client sketch you are setting the IP address of your host to "192.168.4.1", but in your Server sketch you never set an IP address, so how do you expect the connection to be established? There are multiple libraries for ESP8266, without clarifying which one you are using there is not much we can do about this... – Roberto Lo Giacco Sep 18 '16 at 11:13
  • This is the default IP. As I said connecting to this IP through telnet, netcat and packetsender works but no from the client ESP – xetra11 Sep 18 '16 at 12:14
  • And as I said, without knowing which library you are using we cannot help because in the library I was opening to solve your issue the default IP is 192.168.0.1. – Roberto Lo Giacco Sep 18 '16 at 12:36
  • The library is #include <ESP8266WiFi.h> isnt that visible through the sketch code? I'm gonna try to not rely on a default IP mybe that works. – xetra11 Sep 18 '16 at 13:49
  • That is an header file, not a library. The same header file is defined in at least 11 libraries I was able to find online... – Roberto Lo Giacco Sep 18 '16 at 18:51
1

If the server has a default IP address of 192.168.0.1 and the client has an IP address of 192.168.4.1 my guess is they won't be able to talk because you are using a 24 bit mask.

The subnet mask defines the number of bits that makes up the subnet, so if you were using the two addresses above and had a mask of 255.255.255.0 or 24 bits then the subnets you were using would be 192.168.0 and 192.168.4. To cross subnets you need some form of routing process (usually a router, switch that can route, etc).

So you need to either change the mask or, stick both devices on the same subnet. (Don't rely on defaults, when they change your code breaks)

  • 1
    your reasoning is correct and possibly applicable, I'm commenting here just because you inverted the IP addresses of client and server... In the client firmware he is opening a connection toward192.168.4.1, so that's the expected server address... Also I don't think he ever sets the client address to 192.168.0.1, that was a statement from me, but I was probably looking at the wrong lib. – Roberto Lo Giacco Sep 19 '16 at 9:01
  • @RobertoLoGiacco - I wasn't awake, I've only just started work, I have to get 3 hours in before I can read :) – Code Gorilla Sep 19 '16 at 9:14
1

The problem was this missing piece

WiFi.mode(WIFI_STA);

If omitted it seems the WiFi is starting in Station & AP mode - so I was able to connect to the other AP but not to connect to the host because of reasons.

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.