arduino-libraries / WiFiLink-Library

Arduino Library WiFi Link
14 stars 26 forks source link

Wifi Chatserver stop responding after first connection #11

Open jonportus opened 7 years ago

jonportus commented 7 years ago

Using the latest library and firmware when i run the chatserver sample it works perfectly on an UNO Wifi for the first connection if I then close and open a new connection it does not respond. If i then press reset and try again it still does not respond , I have to physically remove the power i.e USB and then on restart it works fine again for one connection. If i force a close in code using client.stop; this allows re-connections but if the client closes the connection it is game over. Any ideas ?

JAndrassy commented 7 years ago

test if the state of existing connection is ESTABLISHED. if not stop it. const int ESTABLISHED = 4; // tcp state

  if (telnetClient) {
    if (telnetClient.status() != ESTABLISHED) {
      telnetClient.stop();
    } else {
...
jonportus commented 7 years ago

Hi , Thank you for you quick response , I've added what I think you meant but it just disconnects immediately however it does allow a reconnect . my code is as per below is this correct

include

char ssid[] = "xxx"; // your network SSID (name) char pass[] = "xxx"; // your network password (use for WPA, or use as key for WEP)

int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

const int ESTABLISHED = 4;

WiFiServer server(23);

boolean alreadyConnected = false; // whether or not the client was connected previously

void setup() { //Initialize serial and wait for port to open: Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only }

//Check if communication with wifi module has been established if (WiFi.status() == WL_NO_WIFI_MODULE_COMM) { Serial.println("Communication with WiFi module not established."); while (true); // don't continue: }

// attempt to connect to Wifi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);

}

// start the server: server.begin(); // you're connected now, so print out the status: printWifiStatus(); }

void loop() { // wait for a new client: WiFiClient client = server.available();

// when the client sends the first byte, say hello: if (client) { if (client.status() != ESTABLISHED) { client.stop(); alreadyConnected = false; } else {
if (!alreadyConnected) { // clead out the input buffer: client.flush(); Serial.println("We have a new client"); client.println("Hello, client!"); alreadyConnected = true; }

if (client.available() > 0) {
  // read the bytes incoming from the client:
  char thisChar = client.read();
  // echo the bytes back to the client:
  server.write(thisChar);
  // echo the bytes to the server as well:
  Serial.write(thisChar);
}
else

{ // close the connection: if (alreadyConnected) { client.stop();
Serial.println("client disonnected"); alreadyConnected = false;
} } } } }

void printWifiStatus() { // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID());

// print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip);

// print the received signal strength: long rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI):"); Serial.print(rssi); Serial.println(" dBm"); }

JAndrassy commented 7 years ago

try only inserting

if (client.status() != ESTABLISHED) {
  client.stop();
  alreadyConnected = false;
  return;
}

after if (client) {

jonportus commented 7 years ago

Hi , many thanks for the suggestion , this has improved things in that it sometimes works 3 or 4 times in a row but after that it stops responding again. My code is now as per below , Do you have any other suggestions.

Jonathan

void loop() { // wait for a new client: WiFiClient client = server.available();

if (client.status() != ESTABLISHED) { client.stop(); if (alreadyConnected) Serial.println("Client disconnected"); alreadyConnected = false; return; }

// when the client sends the first byte, say hello: if (client) {
if (!alreadyConnected) { // clead out the input buffer: client.flush(); Serial.println("New client"); client.println("Hello, client!"); alreadyConnected = true; }

if (client.available() > 0) {
  // read the bytes incoming from the client:
  char thisChar = client.read();
  // echo the bytes back to the client:
  server.write(thisChar);
  // echo the bytes to the server as well:
  Serial.write(thisChar);
}

} }

JAndrassy commented 7 years ago

there are more tcp states of the connection. function client.connected() tests them, but returns true for wrong set of states (I think). WiFi Link library is limited to 4 sockets all together (servers, clients and udp). If a client 'hangs', the socket stays blocked. try testing other states. they are defined in libraries/WiFi_Link/src/utility/wifi_spi.h.

wcpettus commented 6 years ago

I think I have a comparable problem, if a little more extreme. Also running on an Uno WiFi, first telnet connection works fine, after disconnecting no other connections work (telnet "connects", but the serial monitor shows that the arduino doesn't recognize it).

The code I'm using is just the WiFiChatServer, but with the loop stripped out for debugging.

void loop() {
  // wait for a new client:
  WiFiClient client = server.available();
  Serial.print("Starting loop: ");
  Serial.print(client);
  Serial.print(" - ");
  Serial.println(client.status());

  // when the client sends the first byte, say hello:
  if (client) {
    Serial.println("it works!");
  }
  else {
    Serial.println("resetting");
    client.stop();
    alreadyConnected = false;
  }
  delay(2000);
}

Before connecting, the return is predictably 0 - 0. After connecting with telnet, the return goes to 1 - 4. Once that telnet is disconnected, it is stuck at 0 - 0 forever.

Starting loop: 0 - 0
resetting
Starting loop: 1 - 4
it works!
Starting loop: 1 - 4
it works!
Starting loop: 0 - 0
resetting

Then if I re-upload the sketch, I get the same behavior as jonportus, never able to connect a telnet. I then tried a slightly more nuclear option - clearing the DHCP lease for the arduino from the router. Surprisingly, the arduino still claims to have the same IP, but the router does not agree (it claims the arduino never bothered to connect).

Then if I add a WiFi.disconnect() to the setup before the while loop with WiFi.begin(ssid,pass), the first connection fails (maybe the WiFi takes a bit to actually reset), but then the second one succeeds. Then each re-upload of the sketch produces a successful telnet connection.

I'd like a less nuclear option that forcibly resetting the wifi, any advice?

JAndrassy commented 6 years ago

this version 1.0.1 of WiFi Link has unbelievable bugs. I made bug-fixes in my fork of WiFi Link library and firmware. use versions from my repositories. for Uno WiFi with my Uno WiFi Serial1 library