Hi was adding some DB logging to some code today and found that output-port set din’t fire on waking from deep sleep.
I wrote the following test script and if imp.configure is placed at line 12 then output-port set works,
If I place as the last line then it’s doesn’t also imp.rssi returns 0.
Are they rules about where imp.configure should be placed?
// Output to web service & planner
local _wsOut = OutputPort(“wsOut”,“string”);
// Works if declared hear //imp.configure(“output.port.set-test LINE 12 works”,[],[_wsOut]);
// Event handler
function swEvent(){
// Idle for 100ms to allow switch to settle
imp.sleep(0.100);
local state = hardware.pin1.read();
if(state==1){
local _httpOut=“s|200|1|”+getDateTime()+"|io-pin|"+imp.rssi()+"|"+imp.getmemoryfree();
_wsOut.set(_httpOut);
server.log("event-handler: "+_httpOut);
}
}
// state handler
if (“nv” in getroottable()){
hardware.pin1.configure(DIGITAL_IN_PULLDOWN, swEvent);
local _httpOut=“s|200|1|”+getDateTime()+"|wake-pin|"+imp.rssi()+"|"+imp.getmemoryfree();
_wsOut.set(_httpOut);
server.log(“wake-pin boot: “+_httpOut);
} else { // cold boot state
nv <- {activitySleep=0,sleepTS=time()};
local _httpOut=“s|200|1|”+getDateTime()+”|cold boot|”+imp.rssi()+"|"+imp.getmemoryfree();
_wsOut.set(_httpOut);
server.log("cold boot: "+_httpOut);
hardware.pin1.configure(DIGITAL_IN_WAKEUP);
imp.onidle(function() {server.sleepfor(30.0);});
}
// Doesn’t work if declared hear
imp.configure(“output.port.set-test LINE 43 - DON’T”,[],[_wsOut]);`
Are they rules about where imp.configure should be placed?
Yes. As you discovered, calling set() on OutputPort objects only works after a call to imp.configure(). (How could it know which channel you were setting otherwise?) In your code above, wsOut.set() is called in both branches of the “if (nv…)”, before the imp.configure() call.
I’ll go and check the documentation of OutputPort.set(). If it doesn’t say you need to have called imp.configure() first, then it soon will…
This configuration problem caused me a fair amount of frustration. Being new to Squirrel, I tried to write a simple OutputPort code example just to make sure I understood how to make a port work:
local port = OutputPort("value","string"); server.show("Setting port..."); port.set("25"); server.show("...port set."); imp.configure("Output Port Test",[],[port]);
This code agrees with the instructions and example code snippets in the “Software for the Imp” sections, and it doesn’t generate any errors. But it also doesn’t work unless you move the imp.configure statement ahead of the set statement. Thankfully I found this thread.
So let me ask a question about style: why is the imp.configure typically at the end of the file instead of the beginning? It seems like good practice, particularly if ports don’t work until imp.configure is called.
The imp.configure tends to be after all the things it relies on have been declared, hence it’s often near the end. You couldn’t put the imp.configure before your local port line…
(you should really be doing “port <- OutputPort()” too - see the section on writing efficient squirrel on the devwiki)
After reading this, and going to the Hannah example, I’ve realised that the wiki output ports page is incorrect (http://devwiki.electricimp.com/doku.php?id=example:outputport) - it misses the imp.configure directive portions, and also that the OutputPort directive requires 2 values. This sure made things difficult for me to diagnose!
You actually don’t need 2 values when instantiating an outputport; if the second one is omitted, it defaults to “number”… but yes, it doesn’t mention imp.configure in that section because it’s all about the ports.