WiFi channel switching stops WiFi connectivity and is only recoverable by reboot (BUG)

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.

How does this behave when adding a waitUntil(WiFi.ready) after your WiFi.connect() statement? Does this change anything?

I’d also swap around the xxxx.stop() instructions as the client object would be supplied by the server object, I’d stop/dispose the “inner” object before the outer.
This might not be necessarey, but I’d just consider it logical order.

You could also add a line

SerialLogHandler traceLog(LOG_LEVEL_TRACE);

to get some output what’s happening in the system in case of hangs.

2 Likes

Thanks for the suggestions. I have updated my test code to this:

#include "application.h"

SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);
SerialLogHandler traceLog(LOG_LEVEL_TRACE);

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() && WiFi.localIP()[0] != 0) {
       if(!tcpServerRunning) {
           tcpServer.begin();
           tcpServerRunning = true;
       }
       else {
		   // if a new client appears, drop the old one
		   TCPClient newClient = tcpServer.available();
		   if(newClient) {
			   tcpClient.stop();
			   tcpClient = newClient;
		   }
       }
    }
    else {
       tcpClient.stop();
       tcpServer.stop();
       tcpServerRunning = false;

       if (WiFi.hasCredentials() && !WiFi.connecting()) {
           WiFi.connect();
           waitUntil(WiFi.ready);
       }
    }
    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]);
    }
}

There is still a short period where WiFi.ready() returns true, even though the IP address is 0.0.0.0.
Note that I have added an extra check to this line:

 if (WiFi.ready() && WiFi.localIP()[0] != 0) {

With the IP address check, I can survive a channel switch. The result I get with without the IP != 0 check is that after a WiFi channel switch, the P1 actively refuses new connections.

I might have missed it but what system version are you running?

And merely adding the tracer logger is only half the way - you may also post the log output in order to get the full picture.

I am using 7.0 RC6.

WiFi.connect() seems to be a blocking call, even with SYSTEM_THREAD(ENABLED). This means that if WiFi is disconnected, the application will spend almost all of its time in WiFi.connect(). My previous understanding is that WiFi would be handled in the background thread?

This is the result I get when I disable WiFi on the router and enable it again:

**WiFi.ready(): 1 - IP: 192.168.2.108

<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
<-toc
<-toctoctoc
->tic
<-toctoctoctoctoc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
<-toc
<-toctoctoctoctoctoctoctoc
->tic
<-toctoc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
<-toc
<-toctoctoctoc
->tic
<-toctoc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108
0000032556 [hal.wlan] INFO: Joining brewpi-subnet
0000032556 [hal.wlan] TRACE: Free RAM connect: 44472

->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
**0000039571 [hal.wlan] ERROR: wiced_join_ap_specific(), result: 1024
0000039571 [hal.wlan] INFO: Joining brewpi-subnet
0000039571 [hal.wlan] TRACE: Free RAM connect: 44472

->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
**0000046578 [hal.wlan] ERROR: wiced_join_ap_specific(), result: 1024
0000046578 [hal.wlan] INFO: Joining brewpi-subnet
0000046578 [hal.wlan] TRACE: Free RAM connect: 44472

->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
Serial Error: read failed: [Errno 10054] An existing connection was forcibly closed by the remote host)
**0000053664 [hal.wlan] ERROR: wiced_join_ap_specific(), result: 1024

Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**0000055771 [hal.wlan] TRACE: connect cancel
0000056342 [hal.wlan] INFO: Using internal antenna
0000056367 [hal.wlan] INFO: Joining brewpi-subnet
0000056367 [hal.wlan] TRACE: Free RAM connect: 47016

Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**0000063374 [hal.wlan] ERROR: wiced_join_ap_specific(), result: 1024
0000063374 [hal.wlan] INFO: Joining brewpi-subnet
0000063374 [hal.wlan] TRACE: Free RAM connect: 47016

->tic
**0000070461 [hal.wlan] ERROR: wiced_join_ap_specific(), result: 1024
0000070461 [hal.wlan] INFO: Joining brewpi-subnet
0000070461 [hal.wlan] TRACE: Free RAM connect: 47016
0000071193 [hal.wlan] ERROR: wiced_join_ap_specific(), result: 1025
0000073293 [hal.wlan] TRACE: connect cancel
0000073866 [hal.wlan] INFO: Using internal antenna
0000073890 [hal.wlan] INFO: Joining brewpi-subnet
0000073890 [hal.wlan] TRACE: Free RAM connect: 47016
0000074607 [hal.wlan] INFO: Bringing WiFi interface up with DHCP
WiFi.ready(): 1 - IP: 192.168.2.108

<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
Serial Error: read failed: [Errno 10054] An existing connection was forcibly closed by the remote host)
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
Serial Error: read failed: [Errno 10054] An existing connection was forcibly closed by the remote host)
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

Serial Error: [Errno 10054] An existing connection was forcibly closed by the remote host)
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
Serial Error: read failed: [Errno 10054] An existing connection was forcibly closed by the remote host)
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

A strange result here is that after the disconnect and reconnect, it seems that the communication becomes unreliable. A lot of tics go unanswered. What could be the cause of this? If I reboot the P1, the communication is faultless and responsive again.

This is a trace of switching WiFi channel. The IP is valid again, but I get no responses on the socket anymore.


**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
->tic
<-toc
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
<-toc
->tic
<-toc
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
**0000050693 [hal.wlan] INFO: Joining brewpi-subnet
0000050693 [hal.wlan] TRACE: Free RAM connect: 44048

->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
**0000057702 [hal.wlan] ERROR: wiced_join_ap_specific(), result: 1025
0000057702 [hal.wlan] INFO: Joining brewpi-subnet
0000057702 [hal.wlan] TRACE: Free RAM connect: 45336

->tic
->tic
->tic
**0000058431 [hal.wlan] INFO: Bringing WiFi interface up with DHCP
WiFi.ready(): 1 - IP: 0.0.0.0

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 0.0.0.0

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
Serial Error: read failed: [Errno 10054] An existing connection was forcibly closed by the remote host)
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

->tic
->tic
->tic
->tic
->tic
**WiFi.ready(): 1 - IP: 192.168.2.108

Serial Error: [Errno 10054] An existing connection was forcibly closed by the remote host)
**WiFi.ready(): 1 - IP: 192.168.2.108

Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

Traceback (most recent call last):
  File "wifi-test.py", line 13, in <module>
    tcp = serial.serial_for_url("socket://192.168.2.108:6666", baudrate=57600, timeout=0.1, write_timeout=0.1)
  File "C:\Users\Elco\Envs\brewpiv0\lib\site-packages\serial\__init__.py", line 88, in serial_for_url
    instance.open()
  File "C:\Users\Elco\Envs\brewpiv0\lib\site-packages\serial\urlhandler\protocol_socket.py", line 61, in open
    self._socket = socket.create_connection(self.from_url(self.portstr), timeout=POLL_TIMEOUT)
  File "C:\Python27\Lib\socket.py", line 562, in create_connection
    sock.connect(sa)
KeyboardInterrupt

(brewpiv0) Elco@EJOFFICE C:\repos\firmware\app\minimal\python
> python wifi-test.py
Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

Serial Error: Could not open port socket://192.168.2.108:6666: timed out)
**WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108
WiFi.ready(): 1 - IP: 192.168.2.108

Note that after restarting the python script, I still get no successful socket connection. Only a P1 restart fixes it.

Please give this code a try yourself. It requires no additional hardware, just a photon.

This was true for older versions but has got changed for some reason - I'm also not happy with that change.
For my understanding that's just a compromise added for people who don't care to read the docs and hence have run into troubles.
The same is true for Particle.connect() which I "complained" about already :wink:
https://github.com/spark/firmware/issues/1399

3 Likes

Ah, that explains it. I am not happy with that change either.
I use the Photon/P1 for temperature control and I don’t want WiFi issues blocking the temperature control loop.

With waitFor/waitUntil available, it should be up to the user to handle WiFi in foreground or background.

I think Particle is really dropping the ball on this. It’s ridiculous that for a WiFi IOT product connectivity issues are not taken more seriously. I have reported this bug 5 months ago and no one has taken a serious look.

Now I have made it even easier by providing the code to reproduce it, but still it is not acknlowledged. The reason I picked Particle over a bare WiFi module is that I expect the system framework to handle WiFi and stability for me. But the reliability is bad and it is affecting my product.

1 Like

Actually, in the issue you linked you address that WiFi.connect() is blocking without SYSTEM_THREAD(ENABLED). It seems to be blocking now even with SYSTEM_THREAD(ENABLED).

I’ll bring that forward again in one of the next sessions with Particle.

And you are right this was a non-threaded issue but off record I had discussed the “escalation” of that issue to multi-threaded scenarios too.
One argument there was that the intended behaviour would be that the blocking should only occure during the transition from setup() to loop() but I have not tested that myself.

2 Likes

I think blocking behavior might be nice for novice users, but for advanced users I think even that doesn’t make much sense.

When an application loses WiFi (channel switch, router going down, etc):

  • it should be able to reconnect without entering setup() again
  • it should be able to perform the application without interruption (temperature control in my case) while trying to reconnect. A simple example is taking a sensor reading every second. When the WiFi goes down, the application could store these values until WiFi is back, but it should still read every second! With a blocking WiFi reconnect, this is not possible.

If the SYSTEM_THREAD is going to block the entire application, what’s the point of having a system thread?

1 Like

I’m absolutely with you. I think a consistent and fully flexible, well documented behaviour would be the way to go and special treatment of the transition from setup() to loop() can’t be called consistent IMO.

When I mentioned that I didn’t mean that the code would need to go through setup() again in order to connect but that in the current use of setup() and loop() any call of Wifi.connect()/Particle.connect() issued in setup() would prevent the flow from entering loop() till the connection is actually established. But when called early in setup() the rest of setup() should still execute and when called in loop() no blocking should happen.
While this would - if actually working that way - address some issues, it’s still inconsequent IMO and also not address all issues but rather create confusion. So getting the “non-blocking” behaviour back would be desirable.

I set “non-blocking” in double quotes since the extra low level work for the task will inevitably impact the execution of the application thread and hence may cause some delay but attending to application code whenever possible is still better than a hard block IMO.

2 Likes

I have tried removing all calls to connect() from my loop() and just leave that to the particle system thread.

I have changed it to a single WiFi.connect(WIFI_CONNECT_SKIP_LISTEN) in setup().

This seems to have resolved the issues with blocking the main loop. In my investigation I found that WiFi.connect() did not block (it is correctly run on the background thread), but it seems that the main loop execution is blocked when it is called for the second time. How this is possible with the check for WiFi.connecting() is unclear to me.

I’m going to do some more testing, but it seems that this is how it should be handled:

  • Call WiFi.connect() just once.
  • Leave it to particle to reconnect if needed and don’t interfere.

This is a pretty good conclusion for Particle I guess. The system thread seems to work fine too. There might be a bug in connect() or isConnecting() that caused this behavior.

2 Likes

I’m probably not understanding this completely, but would it be possible to ping the wifi router and only call WiFi.connect if the ping succeeds (in MANUAL and with SYSTEM_THREAD)?

Am wondering if the @rickkas7 solution is the way to go for you @Elco:

I was complaining specifically about TCPCLIENT, but the general technique could be useful to your situation too.

1 Like

That is worth a shot. What I currently have is that the P1 responds to ping, but refuses to handle TCP requests.

Any downsides to having the cloud connection when it is not used at all? What would be the effect of having no internet connection, just local LAN?

Connecting to Particle instead of WiFi only seems to help stability of the TCP server indeed! Thanks @UMD

1 Like

@Elco, @UMD this is a very interesting finding. @Elco, it would be good if you could report on your stability after some operating hours so we can report back to the Particle folks.

@Elco, thanks should go to @rickkas7 !!

@Elco confirming that using the technique, you don't need to have connection with Particle to use TCPClient, just WiFi (of course).

@peekay123, the blocking nature of TCPClient() and the like should be looked at - perhaps do what Windows does and have Async versions of things, eg "TCPClientAsync" which incorporates call backs?

For example, I have this "gotcha" work around (note this @Elco):

        // This check cures the tcpClient.connected() hanging problem
        if (!WiFi.ready())
        {
            Log.warn("*NOT* WiFi.ready()");
            continue;  // looping
        }
        // We have WiFi
        // -------------
        bOk = tcpClient.connected();            // HANGING problem here if WiFi has been lost

@peekay123, would be nice to fix issues like excessive timeouts :

        // This call can take upto 5 seconds if the other end is not listening
        // (This many not be be true post v0.6.2 firmware?????)
        bOk = tcpClient.connect(ipAddressServer, nPort);

I ran into a situation where I wanted the TCPClient connect() call to be asynchronous so I wrote an alternate implementation. I haven’t used it in a long time, but I’m pretty sure it should still work.

2 Likes

System seems to be stable now that it is connected to the cloud. Even when the connection is lost, it is restored without problems. I have one test system with terrible reception.

This is what I have (it switches automatically between USB and TCP):