How to "buff" json when device is not connected

At a precise interval in time ( each 15 minutes or so ), the device is sending some data to my device

`function SendNewData(type,origin,data){
json <- {



so for example, I run : SendNewData("Message","Device","The beer is cold");

the function timestamp it with moment ( that have the current local time ).

Problem is when the device is not connected with the agent, lets say for the next 8 hours. where does the message goes? How can I make sure that i’ll not “loose” any message

You can find out whether agent.send() was successful by checking the value it returns (0 on success, or 1-3 on failure), and retrying later if it failed, or taking other action so you don’t lose the message, or at least are notified it has been lost.

result = agent.send("DATA".json); if (result == 0) { // success } else { // let's try that again }

See the agent.send() documentation for details on what the error codes mean.

You might be interested in our new MessageManager library, which is free to use.

One action you can take to prevent data loss, if cannot send after a set number of retries, is to save your data in a nv table then retrieve it when able to send. Description of how to do this, provided here.

NV table stores your data even if device reboots or uploads new firmware etc.

ok, so if I understand ( and i’m not the greatest programmer ever, sorry for that )

As i told, i’m doing, SendNewData("Message","Device","The beer is cold");
function SendNewData(type,origin,data){ json <- { "Node":Node.deviceid, "Type":type, "Origin":origin, "Content":data, "Moment":moment } agent.send("DATA",json); }

When something appen ( but its not a fix interval of time ).

now I just have to do

function SendNewData(type,origin,data){ json <- { "Node":Node.deviceid, "Type":type, "Origin":origin, "Content":data, "Moment":moment } <b>cm.onNextConnect(function() { agent.send("DATA", json);</b> }


the problem from what I understand is that cm.onNextConnect will be executed by itself without SendNewData() being processed, so my json is empty …wich is just useless

Turns out, cm.onNextConnect() can’t receive a json, but work with a table

`function SendNewData(type,origin,data){

    local delayedmsg = "";
    if( ! cm.isConnected() ) delayedmsg = " OFFLINE ";
    local table = { };
    table["Node"] <- Node.deviceid;
    table["Type"] <- type + delayedmsg;
    table["Origin"] <- origin;
    table["Content"] <- data;
    table["Moment"] <- moment;

    cm.onNextConnect(function() {
        agent.send("DATA", table);

Does anyone know a way to know the quantity of “task” that are buffed? and know the memory depth left ?

The code below:

function SendNewData(type,origin,data) {
    json <- { 
      // ...
    cm.onNextConnect(function() {
        agent.send("DATA", json);

should work as expected. Ie. even if the callback provided to the onNextConnect is executed asynchronously, it captures the contexts that it’s invoked at.

In this case the statement:

    json <- { }

defines a global variable. So it’s shared between all the scopes (functions/classes) in your code. If you set it to null somewhere you’ll get what you see.

To avoid this effect (assuming that’s the case), you could just change it slightly to:

    local json = { 
      // ...

to make it local and to avoid further “race condition”. Please give it a try.