Recording a created_at date for each point

Hi. Thank you all for the help so far. My goal is to record some data with timestamps, having the device sleep, do it again, and then periodically send it off to Adafruit. I think I have most elements down. But, I’m trying to get the data timestamped at the time it was taken (not the transmitted time). I can turn on the GPS and that looks great. But, if I want to add a created time?

long UNIXtime = 0;
rsp = notecard.requestAndResponse(notecard.newRequest(“card.time”));
if (rsp != NULL) {
UNIXtime = JGetNumber(rsp, “value”);
notecard.deleteResponse(rsp);
}

Not surprised that it doesn’t work, but it was my best guess. I’m also concerned that I get the current time, not the time of the last contact with the GPS.

Thanks,
Brecky

Hi @brecky,

If you look in the JSON of a given event in Notehub, you’ll see three timestamps automatically added:

"received": 1646767303.647652,
"routed": 1646767303,
"when": 1646767301,

when is the timestamp you want - this is when the Note was created on the Notecard.

Hi,
I’m looking to do the same thing with ThingSpeak but I don’t know how to construct the JSONata expression to include the “when” timestamp. I’ve tried this but it doesn’t work.
{
“created_at”: when,
“api_key”: body.key,
“field1”: body.weight,
“field2”: body.temp,
“field3”: body.bat,
“field4”: body.soc,
“field5”: body.netrawwt
}
Without the [“created_at”: when] line, the route works fine but the timestamp becomes the default “routed” time. Can someone please set me straight?

hey @rberkelm that should work, but a couple of questions:

  1. What is the response message your get from ThingSpeak when “created_at” is included?
  2. Is your route set-up to only send events from the notefile you are using with that body above?

In my setup there are 6 notefiles that get sync’d to Notehub once per hour. Out of those 6 notefiles, only one gets routed successfully to Thingspeak. The rest don’t make it. The route response for those is: {“status”:“200”,“text”:“0”}. The time stamp on the successfully routed note is always the “routed” timestamp, not the “when” timestamp. Yes, to your second question.
There may be two issues at play here. One is the timestamp that remains the “routed” timestamp. The other is the frequency of routes to Thingspeak which has a limit of 1 per 20s. Batch uploads are allowed but the format needs to be like a csv table from what I can tell.

Ok, just an update on the “created_at” timestamp issue. It turns our that ThinkSpeak ignores the “when” timestamp because it is in epoch format, not the ISO8601 format (e.g. 2014-12-31 23:59:59) that it wants. It then creates its own timestamp which is close to the “routed” timestamp. So, unfortunately we can’t use the automatically generated timestamp. Instead we have to create our own.
In my case:


#include <TimeLib.h>
char timeString[32];

void setup() {


   //time from Notecard
      {
    JTIME ncTime = 0;
    J *rsp = notecard.requestAndResponse(notecard.newRequest("card.time"));
    if (rsp != NULL) {
       ncTime = JGetNumber(rsp, "time");
       notecard.deleteResponse(rsp);
    } 
    //print long time
    sprintf(timeString, "%02d-%02d-%02d %02d:%02d:%02d", year(ncTime), month(ncTime), day(ncTime), hour(ncTime), minute(ncTime), second(ncTime));
    serialDebugOut.println(timeString); 
      }

    // add note
    J *req = notecard.newRequest("note.add");
    if (req != NULL) {
        JAddBoolToObject(req, "sync", true);
        J *body = JCreateObject();
        if (body != NULL) {
            JAddItemToObject(req, "body", body);
            JAddNumberToObject(body, "temp", air_temp);  
            JAddNumberToObject(body, "bat", voltage);
            JAddNumberToObject(body, "soc", soc);
            JAddNumberToObject(body, "weight", Weight);
            JAddNumberToObject(body, "netrawwt", NetRawWt);
            JAddStringToObject(body, "key",api_key);
            JAddStringToObject(body, "createdTime",timeString);
        }
      notecard.sendRequest(req); // send and sync
    }

}

void loop() {
}

And changed my JSONata to:
{
“created_at”: body.createdTime,
“api_key”: body.key,
“field1”: body.weight,
“field2”: body.temp,
“field3”: body.bat,
“field4”: body.soc,
“field5”: body.netrawwt
}

Hope this helps anyone else stuck on this. :slight_smile:

Hey @rberkelm, it’s actually possible to do a conversion of the when field in JSONata to get an ISO date, like so:

{
  “created_at”: $fromMillis(when*1000),
  “api_key”: body.key,
  “field1”: body.weight,
  “field2”: body.temp,
  “field3”: body.bat,
  “field4”: body.soc,
  “field5”: body.netrawwt
}

Hope that helps!

Brandon

1 Like

That is excellent! Much more elegant - thank you!