Where does the device code runs?

Hello, i ´m confused.

I thought that the device code runs in my breakout.

I went with my breakout until i completely lost my wireless connection.
At this point my test of blinking a led stopped.

So can i assume that the device code is not runnnig in my device=“breakout board” and in the cloud?

Thank you

Device code does run on the device itself. The breakout board usually just provides power and access to the IO pins. Agent code runs in the cloud.

So why did the led stopped blinking?

Can you reproduce the problem by unplugging your access point?

When the imp loses it’s network connection it will block on calls to the server. This behaviour can be changed with the server.setsendtimeoutpolicy api. In fact, the first example in the server.setsendtimeoutpolicy page describes the difference between SUSPEND_ON_ERROR and RETURN_ON_ERROR using a blinking LED :slight_smile:

Alternatively, if you remove any calls that talk to the server (server.log, agent.send, imp.configure), your code should continue running when it disconnects.

Its my case. I have only a blinking led with no server calls.
Going away from my access point the led stops blinking and coming back in range didnt make it blink again. I removed the imp 2 times to see it connecting again

Can you share your code (either here, or in a PM to myself) and/or the entries from what’s going on when this happens?

`local ledState <-0;
function blink()
{
// Change state

ledState = ledState?0:1;

// Reflect state to the pin
hardware.pin9.write(ledState);

// Schedule the next state change
imp.wakeup(1, blink);

}

hardware.pin9.configure(DIGITAL_OUT);
blink();`

edit: wrapped in < code > </ code > tag for formatting.

I couldn’t get your code to run - the following line is invalid:

local ledState <-0;

It should be:

ledState <- 0;

I’m trying to reproduce it stop running code when the connection is lost.

Ok - it turns out I was incorrect in what I said before. Regardless of your code - if you have not set server.setsendtimeoutpolicy(RETURN_ON_ERROR, …) your code will stop executing.

Here’s the bit from the documentation you really need to care about:

If SUSPEND_ON_ERROR is in effect (this is the default behavior), the imp will stop executing code, attempt to reconnect to the server for 1 minute, then go to sleep. Every 9 minutes the imp will wake up and try to connect for 1 minute, and then go back to sleep if it cannot establish a connection.

If RETURN_ON_ERROR is in effect, the call will return a send error code to Squirrel and code will continue to run. The onunexpecteddisconnect callback, if any, will be called.

If RETURN_ON_ERROR is in effect, the only way for your imp to reconnect is to explicitly call server.connect() – so a common pattern is to make that call in the unexpected-disconnect callback.

Edit: Here is some sample code to show you the basics:
`hardware.pin9.configure(DIGITAL_OUT);

state <- 1;
blinking <- true;

function blink() {
if (blinking) {
state = 1-state;
hardware.pin9.write(state);
}
imp.wakeup(1.0, blink);
}

blink();

function connect() {
// if we’re connected, drop out of loop
if(server.isconnected()) return;

server.connect(function(result) {
    // log that we connected I guess
    blinking = true;
    server.log("reconnected");
}, 30);

// call wakeup loop again in 30 seconds
imp.wakeup(30, connect);

}

server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30);
server.onunexpecteddisconnect(function(reason) {
// stop blinking
hardware.pin9.write(0);
blinking = false;

// start connection loop
connect();

});`

That code will blink while the device is connected, stop blinking when it disconnects, and start blinking again when it reconnects. You could easily have the “program continue” (ie - the blinking not stop) when disconnecting by removing the blinking=false; statement in server.onunexpecteddisconnect.

I will try.
Another question , the device code runs in the device or in the imp?

device = imp, when connected, code has no errors and you have pushed “Build and Run”

@ramstein: in the imp. It’s downloaded and written to flash within the imp so with teh right code, you can use it like an MCU with no network connection available at all.