Server.onunexpecteddisconnect()

Hey!

After reading the docs I still have a few questions regarding the unexpected disconnect behavior:

What happens with the events which have been set up with imp.wakeup? Is it needed to re-assign these in order for them to run in ACTIVE-OFFLINE mode? Specifically I have a set of timers that needs to turn off or proceed timing, as they are controlling water valves.

Should the server.connect() be called in a recursive loop to ensure future connection? In general, what is the best practice of reconnecting from RETURN_ON_ERROR mode?

I see that uploading new code doesn’t trigger unexpected disconnect. Can I emulate the behavior in some way for development? Right now I’m trying calling server.disconnect() in the start of my program and right after calling the unexpected disconnect handler - is there a better way?

Thanks a lot in advance!

Best, bobby :slight_smile:

server.disconnect() is not an unexpected disconnect, so it will not trigger the handler.

This is a Very Important thing to take note of - the server.onunexpecteddisconnect() handler does not trigger when the imp disconnects, it triggers when the imp disconnects unexpectedly.

I usually test by blinking up my imp to a mobile hotspot, then turning the hotspot off. (Note - it takes 1 minute for the unexpecteddisconnect handler to fire after loss of connection, as it tries to reconnect during that period in case it was just a ‘blip’).

Okay it makes sense. Now i tried different disconnection handlers, and it’s beginning to work like it should.

During my work with the offline mode i still have some doubts:

When cutting the power source during active-offline mode (or some squirrel error occured while parsing a string from arduino), it seems the imp is going with the normal boot process when i put the power back - therefore not starting squirrel when it can’t find a connection, but instead it’s going to snooze mode? Shouldn’t it start my program when it’s not finding anything as I have timeout policy to RETURN_ON_ERROR? And is there a way to lower the timeout in the start, so that my water valves can open sooner after power is up?

When I cut the wifi i’m not getting any feedback from my device.ondisconnect handler before it’s actually online again? Can I do my own agent ping to the imp to get more immediate disconnection feedback. Really, I don’t see the point in a disconnection handler that triggers on connection - there must be something i didn’t see :smiley:

And did I understand it correctly, that I cant use server.log during offline mode? Is there any other restrictions that are crucial in offline mode?

Thanks for your patience and answers :slight_smile:

Also I’m beginning to have problems with uploading new code to the imp after cutting the WiFi and it reconnected.
According to my onconnect and ondisconnect handlers it’s disconnecting and connecting, and not downloading the new model?

I’ll try to provide some more in depth answers soon, but in the meantime, I highly encourage you to read the How to run an imp offline doc, as well as take a look at the WiFi state diagram.

This is one of the harder flows to understand and master with the imp (or any device that connects to the internet).

Thank you! I’m reading the documentation again and again!

I think now I’m having the wifi on/off working properly. But still have the problem to bypass the normal boot-up process and go straight to squirrel running after the 60 seconds of blinkup/connect. I’ll try to provide the code that I’m running:

After reading this offline guide, it sais:
“If the imp is set to RETURN_ON_ERROR, however, it will continue to run its code. Because disconnection was deliberate, any callback function registered using the server.onunexpecteddisconnect() method will not be called.”

An example code is provided below, and I’m copying that to accomplish deliberate disconnection and run my startup script which will check for timer interrupts and re-trigger or re-assign depending on their state and time since last run:

`function disconnection_handler(reason)
{

if (reason != SERVER_CONNECTED) {
    DISCONNECTED <- true;
    MAIN_INTERVAL <- 1.0;
} else { 
    DISCONNECTED <- false;
    MAIN_INTERVAL <- 60;
}

}

server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30);
server.onunexpecteddisconnect(disconnection_handler);

// according to the example i’m disabling blink-yp
imp.enableblinkup(false);

// deliberate disconnection
server.disconnect();

// main interval to 5 seconds
MAIN_INTERVAL <- 5;

// tell my program that now im disconnected
DISCONNECTED <- true;`

The main “loop” or sensor post:
`// send sensor values
function send_sensors_to_agent()
{

if(!DISCONNECTED) server.log("sending values...");

if(DISCONNECTED) {
    
    
    server.connect(disconnection_handler, 30);
    
} else {
    
   
    send values to db . . .
      
} 

imp.wakeup(MAIN_INTERVAL, send_sensors_to_agent);

}`

And my start up function which will start send_sensors_to_agent() and load saved perferences table is called in the buttom of the code (any issue with that?).

So upon connection, I’ll put my interval to 60 seconds of sensor posting, and when disonnected 5 seconds for checking the connection (i want to be online, and have power - so i want to get back online fast!)

The problem right now is, that cutting the wifi and putting it back again works perfectly fine, as it’s handled by the server.onunexpecteddisconnect method. But when cutting the wifi AND the power, it seems like it’s following the SUSPEND_ON_ERROR flow, and not starting squirrel.

Okay. I found out that actually it was still running, but didn’t get access to my preferences table i saved using server.save(). Am I right that there’s no way to save these data across power cycles AND wifi unavailable? :slight_smile:

There’s no built-in way, no. If you need to do that, one way would be to add an external SPI flash chip, or similar, if you can spare the pins.

Peter