Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By njnyc
#52731 Hi all,

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:
Code: Select all#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?
User avatar
By Orcanbull
#52828 Hi njnyc,

Try to removing the WiFi.config(////); in my experience this caused an error with the telnet server.
can you run the normal example without the config?
User avatar
By njnyc
#53010 I flashed this exact code onto another module (both are NodeMCU ESP-12) and it worked with no issues. The erroneous unit still works as a webserver, gpio actions work, just not telnet. What could be causing this and how can I fix it?
User avatar
By Orcanbull
#53050 Hi,

Try using :

wifi_station_disconnect();
wifi_set_opmode(0x02); // SETTING IT TO AP MODE

The ESP remembers old settings this might be your problem