Wi-Fi doesn't reconnect until reset


I’ve written a small application for my Particle Photon to control some RGB LED lights over UDP using either Art-Net or Kinet. All is well until the Photon loses Wi-Fi. Once Wi-Fi is restored, the Photon never reconnects. If I hit the reset button on the Photon, the Wi-Fi reconnects immediately.

My question is how do I get the Photon to automatically reconnect to the Wi-Fi network once the Wi-Fi is restored without having to do a power cycle or hitting the reset button? For a network connected device, the ability to automatically reconnect to the network once the network is restored is essential.

I have the system thread enabled. I’m using the internal antenna.

Here’s the relevant section of code. This code is called 50 times a second. timer3s is 0 once every three seconds.

    // check WiFi status and re-init udp routines if needed
    if (!wiFiReady && WiFi.ready ()) {
        wiFiReady = true;
        Serial.printf ("Connected to WiFi.\n\r");
#ifdef KINET
        udp.begin (KINET_PORT);
#ifdef ARTNET
        udp.begin (ARTNET_PORT);
    } else if (wiFiReady && !WiFi.ready ()) {
        wiFiReady = false;
        WiFi.disconnect ();
        Serial.printf ("Lost WiFi connection.\n\r");
    } else if (timer3s == 0) {
        if (WiFi.connecting ()) {
            Serial.printf ("C");
        } else if (WiFi.ready ()) {
            Serial.printf ("R");
        } else {
            Serial.printf ("X");
            WiFi.connect ();

Complete code is here in github: https://github.com/bikerglen/particle/tree/master/ck-scene-controller

The output looks something like this:

Hello, world!
Connected to WiFi.
Connected to the Particle Cloud.
.R.R.R.R.R.R.R.R.RDisconnected from the Particle Cloud.
Lost WiFi connection.

Right now, I’m blocking/unblocking the Photon by MAC address to simulate a drop/restore in the Wi-Fi network.

Firmware version is 0.8.0.rc11


You may be running out of free sockets since you don’t seem to return the socket to the system once it’s not needed anymore or gone stale.
So you should call udp.stop() to free unused sockets and if you happen to still run out of free sockets, try WiFi.off() to reclaim lost sockets.

Thanks, but I don’t think that’s it. udp.begin is only called once because once the device is disconnected from the network, it never reconnects and hence udp.begin is not called a 2nd time. Also, it’s not dropping off the network on its own. I’m perturbing it so that it loses connectivity via either 1) blocking by MAC on the AP, 2) shutting down the AP radios, or 3) wandering out of range.

I also just tried a version of the code where I never call udp.begin. It still sends UDP packets and controls the lights but once off the network, it still won’t reconnect. Also tried disabling 5GHz radios on WLAN, disabling 2.4GHz radios, then re-enabling only 2.4GHz radios so this isn’t the problem others have seen where the module (which only has 2.4GHz radios) gets confused by an AP with a common SSID assigned to both radios.

Well, I have no idea what happened, but now if I force the Photon to disconnect by turning off the AP’s 2.4GHz radio then turning the AP’s radio back on again, the Photon reconnects and everything works as expected. I updated the code in github with the working version. I don’t really see any changes that should make a difference.

Also, now I see WiFi.connecting () is asserted (i.e., I get ‘C’ on the serial port instead of ‘X’ when disconnecting the Wi-Fi and waiting for it to reconnect.

Another edit: Before the status light was blinking cyan fast while waiting to reconnect. Now that it reconnects, the status light blinks green fast while waiting to reconnect.

I ran a few more experiments. Here’s my theory.

  1. The following will never recover. You’ll be stuck with a fast cyan blink until a power cycle or pushbutton reset:
udp.begin ()
lose Wi-Fi
udp.stop ()
  1. The following will always recover. The status light will fast blink green then turn to breathing cyan once the Wi-Fi and cloud are reconnected:
udp.begin ()
udp.stop ()
lose Wi-Fi

In other words, if you lose Wi-Fi while a udp socket is open, the module will never recover. I’d rank this as a solid bug in the 0.8.0-rc.11 firmware.