I've been at this for days, but cannot successfully communicate with the ESP8266 in softAP mode via telnet. I've modified the WifiTelnetToSerial sketch to work in AP mode but I can't maintain a connection. On the other end, my phone is connected directly to the AP. Using a Telnet app and attempting to connect always yields a connection error. Here's my code:
#include <ESP8266WiFi.h>
IPAddress ip(192,168,0,100); //Node static IP
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);
//how many clients should be able to telnet to this ESP8266
#define MAX_SRV_CLIENTS 1
const char* ssid = "telnet_test";
const char* password = "";
WiFiServer server(23);
WiFiClient serverClients[MAX_SRV_CLIENTS];
void setup() {
WiFi.softAP(ssid, password);
WiFi.config(ip, gateway, subnet);
//start UART and the server
Serial.begin(115200);
server.begin();
server.setNoDelay(true);
Serial.print("\n\n\n");
Serial.print("Ready! Use 'telnet ");
Serial.print(WiFi.softAPIP());
Serial.println(" 23' to connect");
}
void loop() {
uint8_t i;
//check if there are any new clients
if (server.hasClient()){
for(i = 0; i < MAX_SRV_CLIENTS; i++){
//find free/disconnected spot
if (!serverClients[i] || !serverClients[i].connected()){
if(serverClients[i]) serverClients[i].stop();
serverClients[i] = server.available();
Serial.print("New client: "); Serial.print(i);
continue;
}
}
//no free/disconnected spot so reject
WiFiClient serverClient = server.available();
serverClient.stop();
}
//check clients for data
for(i = 0; i < MAX_SRV_CLIENTS; i++){
if (serverClients[i] && serverClients[i].connected()){
if(serverClients[i].available()){
//get data from the telnet client and push it to the UART
while(serverClients[i].available()) Serial.write(serverClients[i].read());
}
}
}
//check UART for data
if(Serial.available()){
size_t len = Serial.available();
uint8_t sbuf[len];
Serial.readBytes(sbuf, len);
//push UART data to all connected telnet clients
for(i = 0; i < MAX_SRV_CLIENTS; i++){
if (serverClients[i] && serverClients[i].connected()){
serverClients[i].write(sbuf, len);
delay(1);
}
}
}
}
Trying repeatedly to connect prints this result:
Ready! Use 'telnet 192.168.4.1 23' to connect
New client: 0New client: 0New client: 0New client: 0
I'm trying to create some wearables that display images and/or return data to a phone based on TCP commands but have not made much progress. Can anyone tell me what I'm doing wrong?