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.
#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.