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
}