@montu17 smittytone’s code appears to show this working. Have you tried running this yourself?
The device side code he posted is correct, if slightly hard to follow because after the imp wakes from sleep, the wifi isn’t connected. It’s not until the onidle() handler fires, whose main job it is to go back to sleep, that the connection comes up - forced by the server.log() - and that then causes the pending messages to be delivered.
This might be a little clearer:
agent:
`function connected()
{
server.log(“Connected”);
device.send(“do.something”, saved_messages);
}
saved_messages <- [];
message1 <- {};
message1.a_key <- 999;
message1.b_key <- true;
message2 <- {};
message2.a_key <- “Hello”;
saved_messages.append(message1);
saved_messages.append(message2);
// when we get this from the device, clear our pending queue and queue another message for its next wake
device.on(“ack”, function(v) {
saved_messages <- [];
saved_messages.append(message1);
});
// when the device connects, send the saved messages to it
device.onconnect(connected);
`
`function zizz()
{
server.log(“Sleeping”);
server.sleepfor(15.0);
server.log(“Asleep”); // Should never appear in the log
}
function process_messages(messages)
{
server.log(“Messages received”);
foreach (i, item in messages)
{
server.log("Message " + (i + 1) + " processed: " + item.a_key);
}
// ask server for some more
agent.send("ack", 0);
}
// set up handler to deal with messages from the server
agent.on(“do.something”, process_messages);
// force the connection up (if we’ve woken from sleep, wifi isn’t up yet)
server.log(“imp online”);
// Wait online a couple of seconds to ensure any pending messages arrive, then go to sleep
imp.wakeup(2, function() {
imp.onidle(zizz);
});
`
…this may be a bit more explicit.