Run code, if device is not connected

Hi,

im really new to electricimp, but i like it a lot till now.
I made this code that sends out DMX-Data to LED-Light (uart), triggered by http-request send to the agent.
So when starting up the Imp (giving power), the device is sending out smth, that the light connected flickering in all kind of colors etc.

Is there a way to run code on the device till the connection with the agent is established (so sending out 0 as Values through uart, so the light stays off till the connection is done)

device code:

rts <- hardware.pinC;
uart <- hardware.uartABCD;
tx <- hardware.pinA;

local led = 0;


// initialise a 512-device DMX frame
local outBlob = blob(512);
for (local i = 0; i < outBlob.len(); i++) {
  outBlob.writen(0, 'b');
}


function setLedState(state) {
    server.log("Set LED to state: " + state);
    if (state == 1){
       setLevel(1,0); 
        setLevel(2,0); 
        setLevel(3,255); 
        setLevel(4,255);
        setLevel(5,0);
        setLevel(6,255);
        setLevel(7,0);
        setLevel(8,0); 
        
    }
    else {
        setLevel(1,0); 
        setLevel(2,0); 
        setLevel(3,255); 
        setLevel(4,255);
        setLevel(5,0);
        setLevel(6,0);
        setLevel(7,0);
        setLevel(8,0);
        
    }
    
}



function setLevel(addr, level) {
  // send DMX512 command to set device at "addr"
  outBlob.seek(addr);
  outBlob.writen(level, 'b');
  // the frame will automatically be sent on the next refresh
}

function refresh() {
  // manually send out the break and mark-after-break
  tx.configure(DIGITAL_OUT, 0);
  imp.sleep(0.0001);

  // mark-after-break is implicitly sent here; bus idles high while we configure the UART in SW
  uart.configure(250000, 8, PARITY_NONE, 2, NO_CTSRTS);
  
  uart.write(outBlob); // send the frame
  imp.wakeup(0.1, refresh); // schedule next refresh
}

// ----- main program -----
server.log("DMX - " + imp.getsoftwareversion());
server.log(imp.net.info());

rts.configure(DIGITAL_OUT);
rts.write(1);
refresh();

//Green
/*
setLevel(1,0); 
setLevel(2,0); 
setLevel(3,0); 
setLevel(4,255);
setLevel(5,255);
setLevel(6,0);
setLevel(7,0);
setLevel(8,0);
*/

// Register a handler for incoming "set.led" messages from the agent
agent.on("set.led", setLedState);

agent code:
// Log the URLs we need
server.log("Turn LED On: " + http.agenturl() + “?led=1”);
server.log("Turn LED Off: " + http.agenturl() + “?led=0”);

function requestHandler(request, response) {
   
        // Check if the user sent led as a query parameter
        if ("led" in request.query) {
            // If they did, and led=1.. set our variable to 1
            //if (request.query.led == "1" || request.query.led == "0") {
                // Convert the led query parameter to an integer
                //local ledState = request.query.led.tointeger();
                response.send(200, "OK");
                imp.sleep(0.1);
                // Send "set.led" message to device, and send ledState as the data
                device.send("set.led", 1);
                imp.sleep(10);
                device.send("set.led", 0);
            }
        
    
        // Send a response back to the browser saying everything was OK.
        
    } 


// Register the HTTP handler to begin watching for HTTP requests from your browser
http.onrequest(requestHandler);

I hope you understand what i want to do :slight_smile:

thx

greetings
Julius

Essentially, the default state - SUSPEND_ON_ERROR - is such that if your device code attempts to contact the cloud (server.log or agent.send) then code will block until the connection has been established. If there’s no network available, it’ll block forever.

For simple code like yours, you likely have an even easier path - just comment out your server.log statements (or wrap them in “if (server.connected()) then server.log(…)”). As your code is then not trying to send anything to the cloud, it’ll just run uninterrupted - if the network is available, then the connection will be established and your agent.on() will fire when a message arrives from the cloud.

On a cold boot, wifi/ethernet imps will spend up to 10 seconds trying to verify that they have the latest code version in flash - by connecting to the server and checking - before running your application: this is so, if you deploy bad code (maybe something which disconnected immediately), you can recover by deploying a fixed version and power cycling.

Commercial devices can get round this by assigning a “rescue pin” - if one of these is configured, then code will run instantly on powerup (well, a small number of milliseconds after the device comes out of reset). If the reset pin is asserted at powerup, the local code is discarded and the device connects to fetch the latest version.

For more info on running offline, see https://developer.electricimp.com/resources/offline though note that using the connection manager library makes this easier (we could do with updating that guide to recommend that approach!).

This topic was automatically closed after 60 days. New replies are no longer allowed.