Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By JimDrew
#64482 WiFiClient has to be defined before the setup() routine. I changed the port, and then long after setting up variables (from reading the EEPROM) and other routines, I finally change the client.

Here is the stripped down code in Setup():

Code: Select allWiFiClient tcpClient;              // enable the client
WiFiServer tcpServer(LISTEN_PORT); // enable server listening on port 6400

void setup() {

  Serial.begin(serialspeed);    // enable serial port at specfic baud rate
  WiFi.begin("", "");         // connect to router with stored ssid and password

  tcpServer.close();
  tcpServer.setPort(6969);
  tcpServer.begin();
  tcpServer.setNoDelay(true);
 
  WiFiClient tempClient = tcpServer.available();  // this is the key to keeping the connection open
  tcpClient = tempClient;       // hand over the new connection to the global client
  tempClient.stop();            // stop the temporary one
  tcpClient.flush();            // flush out all data
}
User avatar
By JimDrew
#64483 Can you post you test code that worked? I have tried dozens of variations on this.

The reason why I made this thread is because I had read in several forums (including the Esprif forum) that it was not possible to do this. If you look at the Arduino github you will find this has been a requested feature since early 2015, but has not been implemented because the underlying SDK doesn't have a function for this. That certainly could be wrong, especially since you have stated it works. It's definitely something that people want to be able to do. Thanks!
User avatar
By martinayotte
#64487 I beleave that what you read in different threads is that you can not switch port on existing connections or on existing listening server, that's why we need to do close()/begin(). And also that the setPort() I've provided didn't exist yet in any releases.

Here is the testing sketch :

Code: Select all#include <ESP8266WiFi.h>

WiFiServer tcpserver(23); // Telnet Port
WiFiClient client;
uint current_port = 23;

void setup() {
  int timeout = 60;
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin("Giroles-Salon", "PitchouFacePouding");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    timeout--;
    if (timeout <= 0) {
      Serial.printf("\r\nWiFi connect aborted !\r\n");
      while(1) {};
    }
  }
  String str = "\r\nWiFi connected !\r\nIP = ";
  str += WiFi.localIP().toString();
  Serial.println(str);
  tcpserver.begin();
}

void loop() {
  if (client) {
    if (client.status() == CLOSED) {
      client.stop();
      Serial.println("connection closed !");
    }
    if (client.available()) {
      client.setNoDelay(1);
      String req = client.readStringUntil('\r');
      client.flush();     
      if (req.equals("hello")) {
        client.println("Hello from ESP8266 !");
      }
      else if (req.equals("switch")) {
        tcpserver.close();
        if (current_port == 23)
          current_port = 24;
        else
          current_port = 23;
        tcpserver.setPort(current_port);
        tcpserver.begin();
      }
      else if (req.equals("exit")) {
        client.stop();
      }
      else if (req.length() > 0)
        client.println("Unknown command !");
      else
        client.print(">");
    }
  }
  else {
    client = tcpserver.available();
  }
}


To execute, simply do "telnet <esp_ipaddr> 23", then type command "hello" just for test, and real "switch" command will switch port to 24, then type "exit". Trying again "telnet <esp_ipaddr> 23" will fail, but doing "telnet <esp_ipaddr> 24" will succeed. Doing "switch" again will bring it back to 23...