Running in manual - Core loses connection to WiFi at some point in the night but never reconnects

So I’m running a Core in manual mode. I’m trying to run it as a IoT rosetta stone - an intermediary between the Particle Cloud Pub/Sub to MQTT.

I’m running it in manual mode because at one point that seemed more reliable than automatic.

“My” code does this in loop

	if (WiFi.ready() && init) {         // Wait it out until we have a network to talk to
        client.connect(CLIENT_NAME);    // connect to the broker as CLIENT_NAME
        delay(500);
        setMqtt();                      // Perform MQTT pub/sub
        delay(200);
        init = false;
		Particle.connect();
        delay(200);
        Particle.subscribe(DEVICE_NAME, eventHandler, MY_DEVICES);
	}

	if (client.isConnected()) {
        client.loop();     
        Particle.process();             
    } else if (WiFi.ready()) {          
        if (millis() - lastConnect > RECONNECT) {   
            lastConnect = millis();
            WiFi.off();                 
            delay(1000);
            WiFi.on();
            delay(500);
            WiFi.connect();
            init = true;               
        }
    }
}

to try to work around any WiFi/cloud/MQTT dropouts but it’s not 100%. It can work for days and then suddenly stop working in the middle of the night (why then?!).
I’m at a bit of a loss as to why the above code doesn’t ALWAYS resurrect my Core (seems to work most of the time, but then when it doesn’t, seems to stop trying!) when either WiFi or the Cloud connection drops. Is there something I could do to make it more robust? Build in a full system reset maybe? Thanks!

Just a thought…maybe there’s a condition the above code doesn’t deal with? WiFi/Network there, MQTT connection there, but Cloud connection not there?

Particle.process() is in your client.isConnected() condition. That means that it won’t be called (anymore) if client.isConnected() turns false at some point. Particle.process() is needed to do all the system processes (including reconnects) so if you don’t have Particle.process() somewhere else in your code, in the loop() this might explain why once it disconnects, it never reconnects.

1 Like

That only is true for SYSTEM_MODE(MANUAL) without SYSTEM_THREAD(ENABLED) tho’
But since we don’t know whether that’s the case or not, it’s still a good call

Thanks @ScruffR and @joost - I am not using SYSTEM_THREAD(ENABLED)…
Does that mean I should include SYSTEM_THREAD(ENABLED) or leave it out (as it is now?)
Was a bit unsure.

SYSTEM_THREAD(ENABLED) would help keeping the cloud connection even if you happen to have too little Particle.process() calls in your code.

With SYSTEM_MODE(MANUAL) you should keep calling Particle.process() as often as possible best every 1~10ms but at least once every 5 sec.

@ScruffR, I believe @netpex is using a Core, not a Photon so SYSTEM_THREAD() will not apply.

I missed that :blush:
In that case calling Particle.process() reliably and regularly is key and up to the application code.

1 Like

Thanks, @ScruffR, this is driving me nuts. I inserted lots of particle.process() and recompiled and reflashed. Ran fine for an hour and a half, then started breathing green. I want to weep!
I’m going to go back to automatic mode and see if that helps.
Arrrrrrrrrrrrrrrrrrrrrrrrr

Unfortunately I don’t own any Cores anymore, so I can’t really reproduce your issue.

Thanks. I’ve done some more testing and set that Core to ONE WiFi network (I thought having a choice would be beneficial, turns out it’s not?). It’s more reliable now. Seems like a DHCP client joining or leaving that WiFi AP sometimes (not always) knocks the Core offline :frowning:
Were Cores REALLY this unreliable? I have other Cores connected to the same AP doing different (not MQTT) stuff and they’re a lot more stable. Not bulletproof, but a lot more stable than this.
EDIT Scrap that, it’s not DHCP clients I don’t think, it’s happening far too often for that. Anyone using the @hirotakaster on a Core with success?

1 Like

Just to report that MQTT does not work reliably on a Core. I transferred the same code to a Photon and it’s now rock solid. Posting this for future forum searchers!!
Thanks for all help @ScruffR, @hirotakaster, @peekay123, @joost :slight_smile: :slight_smile:

1 Like

@netpex
Unluckily my CORE can’t work anymore now, but I was testing MQTT source code on CORE from the begining.
Not using MQTT, simple TCP client could works on your CORE environment?
You could break down the issue into parts in order to find the real cause(your WiFi or TCP and MQTT).

Hi @hirotakaster - Thanks - but I don’t have the time (or skill, come to that) to find out where the bug is. I’m a working Dad and a IoT hobbyist. It’s all I can do to find a bit of time to “play”, so when I do get chance to “play”, I like to just find a stack that works without bugs and run with it.
I’ve already sort of wasted a week and £40 (buying a new router) before I realised the problem was the Core, not the library or my WiFi.
I’ve found that working stack now: my existing WiFi, your library and the Photon (not the Core!)
Thanks again

MQTT users: would you recommend increasing the MQTT keep alive or does that just mask network problems rather than help mitigate them? As in, I could be missing messages and whereas now at least I know when I’m missing them from checking server logs, if I increase my keep alive, I could be missing them and not even know it?