Device going offline, but not reconnecting

Hi,

I am hoping that someone is able to help me.

I am very new to this, so if I don’t get the terminology correct, please accept my apologies.

I have the imp set up within my PC to enable me to use it as a physical switch to turn it on and off as required from away from my home.
This is using pin8 on the device, a transistor, and connecting this to the motherboard of my PC.
The device is powered using an internal motherboard to mini B USB cable.

The device disconnects shortly after I send the relevant command to turn the PC on.

The log shows the “Received ON/OFF command” log entry, and then just under 2 minutes later it shows the “Device Disconnected” log entry - after which the only way I can get the imp online again is to physically disconnect the device from the power, and then re-connect.

Is my code correct to allow the device to re-connect by itself, without requiring me to physically disconnect the power, or is there a “reset” function that I haven’t discovered yet?

[I even tried to get the imp to deep sleep for 3 minutes as soon as it receives a command in order to get it to wake up and connect again, but that didn’t work!]

I have the following agent code:

`
// Log the URLs we need
server.log("Turn PC On: " + http.agenturl() + “?PCSwitch=1”);
server.log("Turn PC Off: " + http.agenturl() + “?PCSwitch=0”);

function requestHandler(request, response) {
try {
// check if the user sent PCSwitch as a query parameter
if (“PCSwitch” in request.query) {

  // if they did, and PCSwitch=1.. set our variable to 1
  if (request.query.PCSwitch == "1" || request.query.PCSwitch == "0") {
    // convert the PCSwitch query parameter to an integer
    local httpVal = request.query.PCSwitch.tointeger();

    // send "PCSwitch" message to device, and send httpVal as the data
    device.send("PCSwitch", httpVal); 
  }
}
// send a response back saying everything was OK.
response.send(200, "OK");

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);
`

And the following device code:

`
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30)
PCSwitch <- hardware.pin8;
PCSwitch.configure(DIGITAL_OUT_OD_PULLUP);

// Event handler when HTTP command is received
function setPinValue(httpVal)
{
// If HTTP receives a “1” hold switch on for 1 second
// If HTTP receives a “0” hold switch on for 6 seconds, forcing a
// hard shutdown
if (httpVal == 1)
{
server.log(“Received ON/OFF command”);
PCSwitch.write(0);
imp.sleep(0.01);
PCSwitch.write(1);
imp.sleep(0.75);
PCSwitch.write(0);
imp.sleep(0.5);
} else if (httpVal == 0)
{
server.log(“Received HARD OFF command”);
PCSwitch.write(0);
imp.sleep(0.01);
PCSwitch.write(1);
imp.sleep(6);
PCSwitch.write(0);
imp.sleep(0.5);
}
server.expectonlinein(180)
imp.deepsleepfor(180)

}

function RecoverFromLostConn(reason)
{
if (reason != SERVER_CONNECTED)
{
server.connect(RecoverFromLostConn, 180)
}
}

agent.on(“PCSwitch”, setPinValue)
server.onunexpecteddisconnect(RecoverFromLostConn)
`
:frowning: :frowning:

FYI, when an imp wakes from deep sleep (after three mins in your code’s case), it doesn’t connect to the server immediately, only when it contacts the server itself by writing to the log, sending a message to the agent or some such.

The other point to note is that the imp reboots when it wakes, so Squirrel starts afresh. Squirrel doesn’t run when the imp is in deep sleep, which means it ignores messages from the server. So if your imp is asleep, it won’t be able to turn the PC on or off because an incoming message from the agent won’t wake it.

I would therefore remove the server.expectedonlinein() and imp.deepsleepfor)() calls.

If you want to keep them, use server.sleepfor() (it combines the above calls) instead. However, this (and the others) should only be called when you’re sure the imp is idle, ie. it has no wireless management or other housekeeping to do. So use:

imp.onidle( function(){server.sleepfor(180)} )

This code runs the function when the imp goes idle; the function puts the imp into deep sleep for three minutes.

When the imp awakes, it reboots. But it doesn’t connect to WiFi. It doesn’t do so until Squirrel attempts to contact the server, by writing to the log, using a call like server.save(), sending a message to the agent or using server.connect(). So you’ll need to actively access the server at the start of your code (which, as I say, is run afresh on a post-deep sleep warm boot).

oh!, this is important

the imp reboots when it wakes [from deep sleep], so Squirrel starts afresh

I don’t think the documentation for imp.deepsleepfor() explains that reboot occurs after the sleep period. I’ve been trying to figure out how code resumes after that call.

That fact ought to be there, yes, but I suspect it was left out because it was put in the WiFi State Diagram.

I shall amend accordingly.

Thanks Smittytone. Previously I didn’t have the deepsleep code in there - i added it after i noticed that the device was disconnecting as a way to try and automatically get the device to reconnect.

Interestingly, the logs are showing that the device is still disconnecting, even when it should be in a deepsleep?!?!

This is what it was doing prior to the deepsleep & reconnect code:
2014-11-27 12:48:54 UTC+0 [Device] Received ON/OFF command
2014-11-27 12:50:40 UTC+0 [Status] Device disconnected

This is after this code was added:
2014-11-29 10:58:03 UTC+0 [Device] Received ON/OFF command
2014-11-29 10:58:05 UTC+0 [Device] sleeping until 1417258864000
2014-11-29 10:59:51 UTC+0 [Status] Device disconnected

So even thought the devoce was supposed to be in a deep sleep for 3 minutes, it somehow disconnected after 1 min 46 secs.

From what you’ve said, i suspect that my approach won’t work?

imp.deepsleepfor will turn off wifi in the imp and it will disconnect from the server so what you are seeing is totally normal.

You may have better results if you let the imp OS (and other parts) handle all the wifi.

you could try removing all of these things

onunexpecteddisconnect call RecoverFromLostConn function expectonlinein deepsleepfor and server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30)

The reason I suggest this is that I have an older imp that has been running since before some of these API calls were available and so I did not use any of these calls (there was no wifi connection API) and that code is more stable and the device stays connected.

If you are not running from a battery you don’t need to bother with deep sleep.

Thanks mjkuwp94 - so i’ve now updated the device code as below.

Unfortunately, the device is still randomly disconnecting, and not re-connecting…

Here’s an extract from the log:
2014-11-29 18:45:55 UTC+0 [Device] Imp awake and ready!
2014-11-29 18:46:17 UTC+0 [Device] Received ON/OFF command
2014-11-29 19:34:59 UTC+0 [Device] Received ON/OFF command
2014-11-29 19:40:07 UTC+0 [Device] Received ON/OFF command
2014-11-29 19:46:44 UTC+0 [Status] Device disconnected

Any other hints or tips?

In theory, once the PC has been started i won’t need to use the imp again for at least a couple of hours, (since i’ll be using the PC) therefore if i could schedule the device to reset itself after a period of time i think that may solve my issue.

Thanks for all your help in this.

`server.log(“Imp awake and ready!”)
PCSwitch <- hardware.pin8;
PCSwitch.configure(DIGITAL_OUT_OD_PULLUP,0);

// Event handler when HTTP command is received
function setPinValue(httpVal)
{
PCSwitch.write(0);
imp.sleep(1);
// If HTTP receives a “1” hold switch on for 1 second
// If HTTP receives a “0” hold switch on for 6 seconds, forcing a
// hard shutdown
if (httpVal == 1)
{
server.log(“Received ON/OFF command”);
PCSwitch.write(1);
imp.sleep(1);
} else if (httpVal == 0)
{
server.log(“Received HARD OFF command”);
PCSwitch.write(1);
imp.sleep(6);
}
PCSwitch.write(0);
imp.sleep(1);
}
agent.on(“PCSwitch”, setPinValue);`
:slight_smile: :slight_smile: :slight_smile:

to keep track of your success, try logging to the agent periodically.

Device
nextwakeup <- null; function beat(){ agent.send("hearbeat",1) nextwakeup = imp.wakeup(1200.0, beat); } beat();

Agent
device.on("hearbeat",function (iv){server.log("beat");});

I think your code is good so the main suspect I have left would be…power supply issues. You might try powering the imp board from a different power supply such as a wall wart.

maybe someone else will chime in with ideas…

That’s what i was starting to think as well. I did put a multimeter on the voltage to monitor for this, and the power input seemed to remain stable at 5v.

I will try a fully external power supply and give it a go (fingers crossed)… will post back if i’m successful.

Out of curiosity, wouldn’t a power blip cause the imp to reboot, and therefore re-connect to the server?

Does anyone know if there is any way to get the imp to reboot (& re-connect) itself in the event of a power blip?

Thanks again.

You are turning on your computer with imp? so,… the USB 5V power is on even when your computer is off?

A long, long time ago if I recall correctly… the imp device need to be told to periodically send a message or else it would drop offline. I think that problem had been fixed but the reason I suggested the code above is just in case… Logging each 20 minutes just may coax the imp to stay online. However, I don’t think it is expected behavior that it would drop offline. I don’t have ideas past this. My imps have been quite stable at holding connections - the problems I have had are due to me constantly stirring my code ; )

You don’t need to send messages to prevent a device going offline (ISTR that was release 17?). The imp is generally pretty insistent about staying online, and you don’t need any code to ensure this happens.

If you enable blinkup with imp.enableblinkup(true) - this will keep the leds on - what is the LED doing when the device shows offline?

What network connection are you on?

Ok - so the strangest thing has happened…

Last night i couldn’t be bothered to manually re-connect the imp before i went to bed, so i just left it.

when i woke up this morning, it had reconnected itself with the following log entry:
2014-11-30 08:16:15 UTC+0 [Exit Code] imp restarted, reason: wifi outage

It’s never done that before - even after leaving it for 48 hours…

I added Hugo’s suggested code (thanks Hugo), and now it is just staying connected.

To say i’m totally confused is an understatement.

I will keep an eye on it over the next couple of days, but hopefully it’s resolved.

This has unfortunately now changed - the device disconnected, and the imp is showing no lights on it at all…

I’ve found the issue - and feel rather stupid…

I’ve been putting the device within my PC case (for the sake of tidiness), and it has been sitting with 4 hard drives below it, and a CD drive above it, along with a number of fans.

Turns out (and i’m pretty sure you all knew this already) that all of that hardware interferes with the wifi signal when the PC is on - which was causing the imp to disconnect.

The reason that it “mysteriously” fired up this morning, was that my PC shutdown at 8am - which meant that by 08:16, when the imp connected, everything was powered down so not interrupting with the signal.

Thank you all for your help - and apologies for not mentioning this key bit of information sooner.

PS - if anyone knows if i can add an external antenna to my device any pointers would be interested. I did see another recent thread, but it seemed to relate to older imps.

@bhaveshpatel, looking at the IMP002, there are two options for external antennas. The first is being able to plug in an antenna into the micro-connector on the IMP, just to the right of the label ‘imp’. I am not sure if you also need to remove a micro-resistor, but plugging in should be easy.

The second option is that just next to this where the signal leaves from under the label there are a very very small resistor and capacitor. Looking at the design, I am guessing that you could swap these parts, and add another resistor closer to the antenna logo. This would permit you to connect a larger antenna next to the antenna logo, possibly even directly soldered to the board rather than going through a connector, depending on what your requirements are.

Go for the microconnector with cable… At least until you work out that you need more, then talk to the Imp guys.

As vk2ds says, if you move the “T junction” component to the other side, then the antenna trace goes to a u.FL connector footprint. Fit a u.FL there and then an antenna.

Note this does invalidate approvals, but it’ll work.

The tiny gold connector is indeed an RF connector (switch-over: when you plug in, the antenna is disconnected) but it’s used in factory test where we check power levels, modulation quality and RX sensitivity on every unit … with a $50k Litepoint analyzer. The downside of using this connector is the mating part is $70ish and huge - it’s designed for factory and development use only.