Is there a way to stop the imp from disconnecting the wifi?

I’ve seen a few hacks to ping the server every now and again to keep the wifi alive but is there a better way, some kind of setting? The thing I’m building is mains powered so doesn’t really matter about saving power.


I don’t think that the Imp turns off WiFi unless you explicitly tell it to. You have to disconnect from the server, deep sleep or similar to disconnect WiFi.

Well that’s weird as I keep getting disconnection messages if it doesn’t poll the agent within a certain number of times. I’m using quite a few sleep calls. Maybe that’s why?

If you’re using sleep (or have yielding code), it’s possible your imp will disconnect. The imp only has a single thread of execution, and right now, user code takes priority over everything else (including the wifi stack).

As a result, if you have code that steals the thread of execution for long enough, your imp won’t get a chance to respond to wifi messages, and end up disconnected.

Sleeps, and not yielding execution (eg infinite loops) will cause the imp to be marked offline, because it has not been able to run the network stack as your code has priority.

In terms of staying online, you don’t have to do anything. A blank squirrel program will stay online. In the past there had been issues with certain network types, but these days we deal pretty automatically with network hiccups/timeouts/etc.

Thanks everyone - makes sense. I’m using a loop to make some less “breathe” using PWM so I just need to rework it a little.

Here’s some fancy non-blocking code I wrote for fading between colors on an RGB LED… you should be able to modify it pretty easily :slight_smile:


I see this is an old Thread, should i start a new one or should we keep this one?

Here is my question, my imp001 is keep going offline very often, is there anything i can do to prevent this or understand why this is happening?

|2018-10-26 14:11:24 -04:00|[Status]|Device disconnected|
|2018-10-26 14:11:24 -04:00|[Status]|Device connected|
|2018-10-26 14:13:27 -04:00|[Status]|Device disconnected|
|2018-10-26 14:13:27 -04:00|[Status]|Device connected|
|2018-10-26 14:15:30 -04:00|[Status]|Device disconnected|
|2018-10-26 14:15:30 -04:00|[Status]|Device connected|
|2018-10-26 14:17:37 -04:00|[Status]|Device disconnected|
|2018-10-26 14:17:37 -04:00|[Status]|Device connected|
|2018-10-26 14:19:39 -04:00|[Status]|Device disconnected|
|2018-10-26 14:19:39 -04:00|[Status]|Device connected|
|2018-10-26 14:21:41 -04:00|[Status]|Device disconnected|
|2018-10-26 14:21:41 -04:00|[Status]|Device connected|
|2018-10-26 14:23:43 -04:00|[Status]|Device disconnected|
|2018-10-26 14:23:43 -04:00|[Status]|Device connected|

I also notice that every time it come back online, it reload the device code from the beginning…

Really you need to post your code. I suspect you have an infinite loop in there, which prevents processing of network data (your code runs as the highest priority thread in the system).

Hi Hugo,
Thanks for your response… here you have the code:

#require "MessageManager.lib.nut:2.2.0"

class Application {
    cm = null;
    mm = null;
    door = null;
    disconection_reason = null;
    constructor() {
        door = hardware.pin1; // Alias 'door' as the imp001 pin to which the output is connected
        door.configure(DIGITAL_OUT); // Configure the pin
        // Initialise ConnectionManager
        cm = ConnectionManager({
          "startBehavior": CM_START_CONNECTED,
          "blinkupBehavior": CM_BLINK_ALWAYS,
          "stayConnected": true,
          "checkTimeout": 5,
          "connectTimeout": 60.0,
          "ackTimeout": 5.0,
          "errorPolicy": RETURN_ON_ERROR
     // Initialise MessageManager
        mm = MessageManager({
            "connectionManager": cm
        mm = MessageManager()

        // Setup event handlers
        mm.on("open_door", open_the_door.bindenv(this));
        mm.on("get_info", get_dev_info.bindenv(this));
        // Notify the agent that we are online
        mm.send("boot", { "mac":[0].macaddress, "rssi":[0].rssi } );
    function open_the_door(message = null, reply = null) {
        cm.log("opening the door");
        reply({"data":"aaaaand.... Opened", "rssi":[0].rssi});
    function get_dev_info(message = null, reply = null) {
        cm.log("Gathering Device info");
        reply({"mac":[0].macaddress, "rssi":[0].rssi});
// Bootstrap the application
application <- Application();```


So, My guess here is that this is a local network issue - the code looks fine. The only thing I’d change is here:

function open_the_door(message = null, reply = null) {
    cm.log("opening the door");
    imp.wakeup(1, function() { door.write(0); });
    reply({"data":"aaaaand.... Opened", "rssi":[0].rssi});

…using imp.wakeup to schedule the turning off of the door control instead of sleeping. This prevents blocking, you’ll get the response back quicker, it’s more power efficient, etc etc.

If you PM me your device ID I can look and see what’s happening on the local network that’s kicking the device off. I suspect it’s a NAT timeout or maximum session length setting in your router though.

Thanks for the advice. I applied your suggestions and sent the device by PM. :smile:

Had a look at your device and it’s reporting a clean powerup every couple of minutes. How exactly is it powered? Seems like maybe a power issue.

Hi Hugo,

Thanks again!

I am using a power supply (with many cables, kind of dirty :woozy_face:). This might explain why I am seeing "“reloading” the agent after each disconnection.

I will check today/tomorrow and I will let you know!

Aside question, is there any way to have access to this kind of logs?


Depends on the power supply, voltage, connectors, length of wires, etc. WiFi radios have very fast current transients so you may just be browning out - describing the setup would help!

Hi Hugo,

I just connected it with a USB and I don’t see any disconnections… I will re-work my power supply.

Thanks for your support again!