Multiple sensors, output parameters and events

I need some guidance on design and code structure from you. Below are some basic requirements:
I am working on the project where:

  1. The electric imp will be in"deep sleep" mode most of the time to conserve the battery.
  2. Sensors will be logging data locally and data will be saved when the imps are awake.
  3. There will be an “emergency wake up” events from some sensors to awake the imp.
  4. If the data does not raise any events over time the imps would need to wake up once in awhile to poke around and check that everything is ok with the sensors.

Here is what I think my code structure might look like
local output = OutputPort(“Result”, “number”);

function doCheckForData()
{
Check the sensors and output the data. Can I output multiple streams of data here?
}

function whenChanged()
{
Emergency event was raised do event checking and log the data
doCheckForData();
}

hardware.pin1.configure(DIGITAL_IN_WAKEUP, whenChanged);
//define input pin
hardware.pin2.configure(DIGITAL_IN_PULLUP, doCheckForData);

// Register with the server
imp.configure(“sleep_wake_node”, [], [output]);
server.sleepfor(9999999999999.0);

// End of code.
My questions:
a. does code structure makes sense? could anyone offer some advice on how to improve it?
b. could an impee have multiple output parameters that connected to different nodes?

I’m afraid it doesn’t work quite like this…

In deep sleep mode (server.sleepfor/sleepuntil) the only things that will wake the imp are the RTC and a rising edge on pin 1.

When it wakes on either of those events, the code starts from the top (like a clean boot). You can use the nv table to store state so that it know whether this is a cold boot or a wake.

Right now, there’s no way to reliably determine a pin 1 wake (vs an RTC wake) - you’ll just need to read pin1 at boot and if it’s high it was a pin 1 wake. This means you’ll need a long pulse on pin 1… seconds, really. Sounds like you’re using a secondary MCU so that may not be an issue. We’ll have something that means you only need a pulse of tens of milliseconds in the future, it’s just not there yet.

So: your code should just do:

if (hardware.pin1.read()) { // pin1 high, was a pin1 wake whenChanged(); } else { // was an RTC wake doCheckForData(); }

You’d then use server.sleepfor() with the wake period that you want for when there is no pin1 activity. Maybe you send a heartbeat signal, do a server.log(), etc.

No problem with multiple outputs going to different nodes, no.

Hugo, thank you for the detailed explanation! All makes sense for now. I am going to try and ask more questions as they come up. Do you have any examples of how to direct multiple outputs to to different nodes? Thanks!

Just create multiple OutputPorts and list them in the output array when you do imp.configure. Ensure they all have useful names, then when you drag noodles on the planner you’ll see the multiple sources listed in the popup.