I'm trying to connect one ESP8266 to WiFi as a WebServer in station mode and to another ESP8266 as a server (that has no web pages) in AP mode.
The problem is both servers are being created in AP mode, between the ESP's, while one of them (the webserver) should be created in station mode and accessible from WiFi. The only way I can reach the webserver is connecting to the AP and accessing its IP (as access point) from my computer.
What I wanted was to access the webserver connected to my WiFi using the IP that my router gave to the hosting ESP, while the two of them would communicate using the server without web pages.
I've already tried using the same IP for both servers, inverting the order of creation of the servers and using both ESP8266WiFi.h
and ESP8266WebServer.h
. I've read something about the later not working with too much code inside the loop and, since the web page I want is pretty simple, I'm using ESP8266WiFi.h
. Here's my code:
#include <ESP8266WiFi.h>
//#include <ESP8266WebServer.h>
WiFiServer internalServer(81);
WiFiServer externalServer(80);
//ESP8266WebServer externalServer(80);
IPAddress IP(192, 168, 4, 15); // Default IP for the AP mode, the one I'm using.
IPAddress mask = (255, 255, 255, 0);
WiFiClient internalClient;
WiFiClient externalClient;
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
WiFi.mode(WIFI_AP_STA);
// Config. AP mode:
WiFi.softAP("my-access-point", "my-password");
WiFi.softAPConfig (IP, IP, mask);
// This connection is ok:
internalServer.begin();
Serial.println("Internal server created.");
while (!internalClient) { // waiting for the internal client to connect before continuing. This works fine.
internalClient = internalServer.available();
}
Serial.println("Internal client found.");
// Config. STA mode:
Serial.print("Connecting to Wi-Fi");
WiFi.begin("my-wifi", "my-wifi-password");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(" Ready.");
// Config. external server.
// This connection isn't working
externalServer.begin();
Serial.print("External server created with IP: ");
Serial.println(WiFi.localIP()); // This is the IP I expected to use to connect to the external server.
// But it is accessible at 192.168.4.15.
void loop() {
// handling external client:
externalClient = externalServer.available();
if(!externalClient) { return; } // proceed only when there is a request.
String request = externalClient.readStringUntil('\r');
Serial.println("Request: " + request);
// The answer is just a hello world:
String answer = "..."
externalClient.println(answer)
Serial.println("Answer: " + answer);
handleInternalClient();
}
The internal client is supposed to turn on the builtin led when it receives "on" from the server and turn it off when it receives "off", and that is working fine so I think the code for it isn't necessary. If you need it, please ask.
What I want is the other ESP talking to this one through 192.168.4.15 in the AP network and my computer talking to it through whatever IP my router gives to it, in my WiFi.