Notecard template formatting

Dear friends,
Can anyone please help me fix a template issue? I had something working but decided to add a new field “key” to my template and body of my note.add json. Now I get an error:
{“err”:“error adding note: body fields not found in template: key {template-incompatible}”}
Clearly I have something wrong with my template formatting, but I don’t know what?


#include <Notecard.h>
#include <Wire.h>
#define serialDebugOut Serial
#define myProductID "com.gmail.rberkelm:berkelbees1"
Notecard notecard;

void setup()
{
#ifdef serialDebugOut
    serialDebugOut.begin(115200);
    notecard.setDebugOutputStream(serialDebugOut);
#endif

    // Initialize the physical I/O channel to the Notecard
#ifdef serialNotecard
    notecard.begin(serialNotecard, 9600);
#else
    Wire.begin();
    notecard.begin();
#endif
  pinMode(LED_BUILTIN, OUTPUT);


    J *req = notecard.newRequest("hub.set");
    JAddStringToObject(req, "product", myProductID);
    JAddStringToObject(req, "mode", "periodic"); 
    JAddStringToObject(req, "file", "data.qos");
    notecard.sendRequest(req);

  // Optimize voltage variable behaviors for LiPo battery
  {
    J * req = notecard.newRequest("card.voltage");
    JAddStringToObject(req, "mode", "lipo");
    notecard.sendRequest(req);
  }

    // Establish a template to optimize queue size and data usage
  {
    J * req = notecard.newRequest("note.template");
    JAddStringToObject(req, "file", "data.qos");
    J * body = JAddObjectToObject(req, "body");
    JAddNumberToObject(body, "weight", 12.1);
    JAddNumberToObject(body, "temp", 12.1);
    JAddNumberToObject(body, "bat", 12.1);
    JAddNumberToObject(body, "netrawwt", 14);
    JAddStringToObject(body, "key", "AAAAAAAAAAAAAAAA");
    notecard.sendRequest(req);
  }
  
  serialDebugOut.println ("end Setup");
}
void loop()
{
  serialDebugOut.println ("Starting loop");
  digitalWrite(LED_BUILTIN, HIGH);
//temp from Notecard
    double temperature = 0;
    J *rsp = notecard.requestAndResponse(notecard.newRequest("card.temp"));
    if (rsp != NULL) {
        temperature = JGetNumber(rsp, "value");
        notecard.deleteResponse(rsp);
    }

 // Voltage from Notecard on its V+ pin.
    double voltage = 0;
    rsp = notecard.requestAndResponse(notecard.newRequest("card.voltage"));
    if (rsp != NULL) {
        voltage = JGetNumber(rsp, "value");
        notecard.deleteResponse(rsp);
    }

    float weight = (double) random(800, 850) / 10.0; 
    long netrawwt = (double) random(410000, 460000);
    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", temperature);  
            JAddNumberToObject(body, "bat", voltage);
            JAddNumberToObject(body, "weight", weight);
            JAddNumberToObject(body, "netrawwt", netrawwt);
            JAddStringToObject(body, "key","ABCDABCDABCDABCD");
        }
        notecard.sendRequest(req);
    }
digitalWrite(LED_BUILTIN, LOW);   
serialDebugOut.println ("Going to sleep");
ESP.deepSleep(900*1000*1000, WAKE_RF_DEFAULT); //sec to sleep
}

Hi Ray! Thanks for posting on the forum.

I don’t see anything jumping out at me.

Can you send the JSON messages that appear in the Serial Monitor? (I’m assuming you are using the Arduino IDE or something similar) Since the culprit may include the setup function, it is best to start the serial monitor, and then reset the MCU.

Also, can you provide the firmware version running on Notecard? You can do this using the Notecard Playground if you plug in the Notecard via USB to your laptop.

{"req":"card.version"}

Or you can check the Devices list in your Notehub project, and select the “Notecard Firmware” tab

Thanks for the quick response! The Notecard firmware version is: 1.5.6.13782.
The output from the serial monitor is:

12:49:00.260 -> {}
12:49:00.260 -> {"req":"card.voltage","mode":"lipo"}
12:49:00.401 -> {"minutes":7,"hours":649,"mode":"normal","value":3.796010154485245,"vmin":3.79,"vmax":4.9400000000000007,"vavg":4.556425269645609,"daily":-0.02,"weekly":-0.91}
12:49:00.401 -> {"req":"note.template","file":"data.qos","body":{"weight":12.1,"temp":12.1,"bat":12.1,"netrawwt":14,"key":"AAAAAAAAAAAAAAAA"}}
12:49:00.590 -> {"bytes":42}
12:49:00.590 -> end Setup
12:49:00.590 -> Starting loop
12:49:00.590 -> {"req":"card.temp"}
12:49:00.637 -> {"value":31.5,"calibration":-1.0}
12:49:00.637 -> {"req":"card.voltage"}
12:49:00.731 -> {"minutes":7,"hours":649,"mode":"normal","value":3.7982544713494845,"vmin":3.79,"vmax":4.9400000000000007,"vavg":4.556425269645609,"daily":-0.02,"weekly":-0.91}
12:49:00.731 -> {"req":"note.add","sync":true,"body":{"temp":31.5,"bat":3.798254471349485,"weight":81,"netrawwt":422311,"key":"ABCABCABCABCABCA"}}
12:49:00.952 -> {"err":"error adding note: body fields not found in template: key {template-incompatible}"}
12:49:00.952 -> Going to sleep

When I remove the line “JAddStringToObject(body, “key”,“ABCABCABCABCABCA”);” from my note.add json, the error disappears and the Notecard syncs fine. However when I check what is received on Notehub it appears that the new template has not “taken” because the last note.add data transaction still shows “file”: “data.qo” instead of “file”: “data.qos” (another change I made to the template).

"event": "482be34a-4c61-494b-b64e-893cc049faa1",
    "session": "025e3fb1-67f6-49c5-862a-64c21d1cdf7f",
    "best_id": "dev:864475040536624",
    "device": "dev:864475040536624",
    "product": "product:com.gmail.rberkelm:berkelbees1",
    "received": 1646448672.313646,
    "routed": 1646448675,
    "req": "note.add",
    "when": 1646448633,
    "file": "data.qo",
    "updates": 1,
    "body": {
        "bat": 3.796875,
        "netrawwt": 423221,
        "temp": 31.5,
        "weight": 80.5
    },
    "best_location_type": "tower",
    "best_lat": -28.0311625,
    "best_lon": 153.268578125,
    "best_location": "Lower Beechmont",
    "best_country": "AU",
    "best_timezone": "Australia/Brisbane",
    "tower_when": 1646448671,
    "tower_lat": -28.0311625,
    "tower_lon": 153.268578125,
    "tower_country": "AU",
    "tower_location": "Lower Beechmont",
    "tower_timezone": "Australia/Brisbane",
    "tower_id": "505,1,28687,147283213"

So as not to leave this thread hanging, I managed to overcome my template issue by hooking the Notecard up to the in-browser terminal and clearing the template followed by a sync. Now it will run my MCU code and send the “key” field. I guess the Notecard just got itself into a bad state?
However it still sends the Note as a “.qo” rather than a “.qos”. It doesn’t particularly worry me as I understand that “.qo” Notes are still secure in that they go through a VPN? All the same, if anyone can identify what I’m doing wrong, I’d love to hear it!

In the logs you provided, it doesn’t look like you specified a file parameter to the note.add operation.

{"req":"note.add","sync":true,"body":{"temp":31.5,"bat":3.798254471349485,"weight":81,"netrawwt":422311,"key":"ABCABCABCABCABCA"}}

When this occurs, it will pick the default Notefile, data.qo, for you.

Try using this request instead…

{"req":"note.add","file":"data.qos", "sync":true,"body":{"temp":31.5,"bat":3.798254471349485,"weight":81,"netrawwt":422311,"key":"ABCABCABCABCABCA"}}

Aahh… yep, that will do it! Thanks for pointing this out. Another lesson under the belt…

1 Like