-->
Page 1 of 1

Every so often ESP8266 hangs... WifiClient at fault? (yes)

PostPosted: Sat Aug 04, 2018 12:42 am
by Charles Cunningham
I use this code sometimes with a Java program to give my ESP an output console for debugging and such. When I include this code in my loop, however, sometimes my code will freeze for a few seconds right after the device is powered on. Sometimes the hang does not go away until you reset or cycle the power to the device. When I comment out the bit of code in the loop below, the hangs never happen.. Is there any way this code could be causing random hangs in the loop? (Setup completes fine as is indicated by printouts below.)

:!: A key clue: I know for sure that when the hangs happen client.connected() is true because I made the device turn on a debugging led inside the client.connected() if statement and that led only goes on when the hangs happen. BUT I'm not running my java program when this happens -- could another device on my network be interfering with this code somehow? Sorry for noob question but I'm new to networking.

Code: Select all 
#include <WiFiClient.h>
WiFiClient client;

void loop{

//do other stuff

 if (client.connected()) {//Websocket communication with java program running on computer

    if (millis() - tsBroadcast > 500) {
      tsBroadcast = millis();
      sendMessage("\n\nRG: "); sendMessage(String(red - green));
      sendMessage("  B: "); sendMessage(String(blue));
      sendMessage("  encPos-csd-cdd: "); sendMessage(String(encPos - colorStartDist - colorDelayDist));
      sendMessage("  INMARKER: "); sendMessage(String(inMarker()));
      sendMessage("\n");
    }

    Serial.print(getMessage());
  }
  else {
    if (!client.connect(host, 3000)) {
      //Client won't connect -- at least we tried
    }
  }
}

void sendMessage(String msg) {
  String out = msg + "\nend";
  client.println(msg);
}

String getMessage() {
  if (client.available()) {
    String line = "";
    line = client.readStringUntil('\n');
    return line;
  }
  return "";
}





Below are two serial outputs from the same code -- the only difference is that the first output had no hangs and the second had a permanent hang that didn't go away until I reset the device:

rl⸮⸮r⸮⸮#⸮n⸮⸮⸮ ⸮ ⸮⸮p⸮<⸮⸮⸮⸮8⸮⸮ǒ⸮⸮p  ⸮nn⸮⸮;⸮nĒ⸮⸮ ⸮ b⸮$⸮rrp⸮n⸮⸮⸮ ⸮⸮⸮⸮l⸮⸮⸮⸮ ⸮ b⸮n⸮⸮n⸮$⸮⸮ ⸮ b⸮⸮>~⸮n⸮⸮⸮⸮⸮l`⸮⸮⸮#⸮n⸮rnr⸮⸮⸮;⸮⸮ ?⸮ ⸮;r⸮ےn⸮⸮⸮b⸮⸮`Ğ⸮l⸮⸮⸮⸮⸮8⸮⸮r⸮⸮n⸮b⸮scandone
........scandone
.scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 3
cnt

connected with DVW3201BE8, channel 6
dhcp client start...
ip:###.###.#.###,mask:###.###.###.#,gw:###.###.#.# [# are actual numbers in the real printout]
OTA ready


rl⸮⸮r⸮⸮#⸮n⸮⸮⸮ ⸮ ⸮⸮p⸮<⸮⸮⸮⸮8⸮⸮ǒ⸮⸮p  ⸮nn⸮⸮;⸮nĒ⸮⸮ ⸮ b⸮$⸮rrp⸮n⸮⸮⸮ ⸮⸮⸮⸮l⸮⸮⸮⸮ ⸮ b⸮n⸮⸮n⸮$⸮⸮ ⸮ b⸮ľ~⸮n⸮⸮⸮⸮⸮l`⸮⸮⸮#⸮n⸮rnr⸮⸮⸮;⸮⸮ ?⸮ ⸮;r⸮ےn⸮⸮b⸮⸮`Ğ⸮l⸮⸮⸮⸮⸮8⸮⸮r⸮⸮n⸮b⸮scandone
........scandone
.scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 4
cnt

connected with DVW3201BE8, channel 1
dhcp client start...
ip:same,mask:same,gw:same
OTA ready


Only difference is aid 3 in the first vs aid 4 in the second ... not sure what this means/if it is just coincidental.
Network channel number is also different.