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);
}
2 Answers 2
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)
-
1your 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 toward
192.168.4.1
, so that's the expected server address... Also I don't think he ever sets the client address to192.168.0.1
, that was a statement from me, but I was probably looking at the wrong lib.Roberto Lo Giacco– Roberto Lo Giacco2016年09月19日 09:01:04 +00:00Commented Sep 19, 2016 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– Code Gorilla2016年09月19日 09:14:25 +00:00Commented Sep 19, 2016 at 9:14
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.
Client
sketch you are setting the IP address of your host to"192.168.4.1"
, but in yourServer
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...192.168.0.1
.#include <ESP8266WiFi.h>
isnt that visible through the sketch code? I'm gonna try to not rely on a default IP mybe that works.