Photon crashing on entry/exit to listening mode

I have some photon devices which are located in areas with poor wifi stability. I have attempted to make the devices resilient by detecting if the server is no longer available, entering listen mode (in case this was a deliberate change to the wifi credentials which could happen when devices are moved between locations) and exit after a minute, re-trying server access again. This works very well for most of the devices. All except one which sometimes enters listening mode e.g. flashing blue but no softAP is present or if it is present it does not allow a connection to it.

Relevant code snippets:


TCPClient client;
byte server[] = { 192, 168, 0, 1 };

void setup() {
    waitFor(WiFi.ready, 10000);

void loop() {
    unsigned long time;
    if (client.connect(server, 80)) {
    } else {
        time = millis();
        while (WiFi.listening() && time + 60000 > millis() && time <= millis()) {
        waitFor(WiFi.ready, 10000);

I have tried it on different firmware versions from 0.7.0 to the latest 0.8.0-rc.10 but all show this kind of behaviour e.g. either crashing on entry or exit to listening mode. Do I have a dodgy device or is there something I can do with the code?



Just a few comments which might not have anything to do with your issue

  • Why are you switching antenna after you got a connection? If you are certain to have the external antenna connected, having the switch happen in STARTUP() would seem to be more logical.
  • Instead of time + 60000 > millis() && time <= millis() you should rather go with millis() - time < 60000 - unsigned maths works somewhat counterintuitive, but better this way
  • Using byte server[] = { 192, 168, 0, 1 }; might make you fall victim to this issue - rather use IPAddress server(192, 168, 0, 1);
  • You may want to explicitly call client.stop() if the connection check fails to make sure the socket is freed.

Thanks ScruffR! Some good suggestions there and I’ve implemented them all. I’ll see if it has any effect, usually takes a few days for it to fail though.

I also put a 10 second delay between the listen(false) and the connect(). Watching the led it seemed to be fighting between listen mode and connecting to wifi. I think that may be the root cause of the crashes.

1 Like