I have reported a bug before here, but apparently it cannot be confirmed.
Now I have set up a minimal example to reproduce it.
I have this code on the Photon:
It listens on TCP for ‘t’ and responds with ‘toc’. Every second it prints WiFi status and IP.
#include "application.h"
SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);
void setup() {
Serial.begin(115200);
}
void loop() {
static uint32_t last_update = millis();
static TCPServer tcpServer = TCPServer(6666);
static TCPClient tcpClient;
static bool tcpServerRunning = false;
// Tic/toc on WiFi over TCP
// reply to 't' with 'toc' over TCP, print other characters to serial
if (WiFi.ready()) {
if(!tcpServerRunning) {
tcpServer.begin();
tcpServerRunning = true;
}
// if a new client appears, drop the old one
TCPClient newClient = tcpServer.available();
if(newClient) {
tcpClient.stop();
tcpClient = newClient;
}
}
else {
tcpServer.stop();
tcpClient.stop();
tcpServerRunning = false;
if (WiFi.hasCredentials() && !WiFi.connecting()) {
WiFi.connect();
}
}
while (tcpClient.available() > 0) {
char inByte = tcpClient.read();
switch(inByte){
case ' ':
case '\n':
case '\r':
break;
case 't':
tcpClient.write("toc");
break;
default:
Serial.printf("<-%c", inByte);
}
}
// print status on serial every second
if ( millis() - last_update > 1000UL ) {
last_update = millis();
bool wifiReady = WiFi.ready();
IPAddress ip = WiFi.localIP();
Serial.printf("WiFi.ready(): %d - IP: %d.%d.%d.%d\n", wifiReady, ip[0],ip[1],ip[2],ip[3]);
}
}
I run this python script to communicate with the Photon:
It tic/tocs with the Photon over TCP and prints what it receives on Serial.
import time;
import serial;
tcp = None
ser = serial.serial_for_url('/dev/ttyACM0',baudrate=256000, timeout=0.1, write_timeout=0)
timer = time.time()
while True:
# send something to the photon via TCP every 0.2 sec
try:
if tcp is None:
time.sleep(1)
tcp = serial.serial_for_url("socket://192.168.1.62:6666", baudrate=57600, timeout=0.1, write_timeout=0)
if time.time() - timer > 0.2:
tcp.write("t")
print "->tic"
timer = time.time()
# read something back via TCP
new_tcp_data = ""
while tcp.in_waiting > 0:
# for sockets, in_waiting returns 1 instead of the actual number of bytes
# this is a workaround for that
new_tcp_data = new_tcp_data + tcp.read(tcp.in_waiting)
if len(new_tcp_data) > 0:
print "<-" + new_tcp_data
except (IOError, OSError, serial.SerialException) as e:
print 'Serial Error: {0})'.format(str(e))
if tcp:
tcp.close()
tcp = None
time.sleep(1)
# read serial and print it
new_serial_data = ""
while ser.in_waiting > 0:
new_serial_data = new_serial_data + ser.read(ser.in_waiting)
time.sleep(0.1)
if len(new_serial_data) > 0:
print "**" + new_serial_data
When the WiFi channel is switched on the router, it crashes and doesn’t recover. You can see that in this test it recovered the first time, but it more often doesn’t:
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
<-toc
->tic
<-toctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoc
<-toctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoctoc
<-toctoctoc
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 0.0.0.0
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 0.0.0.0
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.1.62
Serial Error: read failed: [Errno 104] Connection reset by peer)
**WiFi.ready(): 1 - IP: 192.168.1.62
Serial Error: Could not open port socket://192.168.1.62:6666: [Errno 111] Connection refused)
**WiFi.ready(): 1 - IP: 192.168.1.62
WiFi.ready(): 1 - IP: 192.168.1.62
Serial Error: Could not open port socket://192.168.1.62:6666: [Errno 111] Connection refused)
**WiFi.ready(): 1 - IP: 192.168.1.62
WiFi.ready(): 1 - IP: 192.168.1.62
Serial Error: Could not open port socket://192.168.1.62:6666: [Errno 111] Connection refused)
**WiFi.ready(): 1 - IP: 192.168.1.62
WiFi.ready(): 1 - IP: 192.168.1.62
Serial Error: Could not open port socket://192.168.1.62:6666: [Errno 111] Connection refused)
**WiFi.ready(): 1 - IP: 192.168.1.62
WiFi.ready(): 1 - IP: 192.168.1.62
Serial Error: Could not open port socket://192.168.1.62:6666: [Errno 111] Connection refused)
**WiFi.ready(): 1 - IP: 192.168.1.62
WiFi.ready(): 1 - IP: 192.168.1.62
WiFi.ready(): 1 - IP: 192.168.1.62
Serial Error: Could not open port socket://192.168.1.62:6666: [Errno 111] Connection refused)
**WiFi.ready(): 1 - IP: 192.168.1.62
WiFi.ready(): 1 - IP: 192.168.1.62
Only rebooting the photon helps to recover.
Note that in the log above, this occurred:
**WiFi.ready(): 1 - IP: 0.0.0.0
That is clearly a bug!
Please give fixing this some priority or help me find a workaround to recover.