Accessing inbound messages

Hi,

I’m trying to test out whether I can change the sampling interval of an MCU connected to a sensor. I can see on the debug console that we got the MCU/carrier to pick up a number from the server (in this test case ‘456’ which I put in ‘env.sampling_interval’), but I haven’t been able to figure out how to get that number into a variable so that I can use it in my code. I’m pretty sure I’m just not understanding the API correctly.

This is what I have that isn’t working:

if (J *req = (notecard.newRequest("env.get"))) {
      J *rsp = notecard.requestAndResponse(req);
      notecard.deleteResponse(rsp);

      int sampling_interval = JGetNumber(rsp, "sampling_interval");
      Serial.print("sampling interval: ");
      Serial.println(sampling_interval); 
 }

output:
15:25:47.895 → {“req”:“env.get”}
15:25:48.029 → {“body”:{“sampling_interval”:“456”,"_<…>"}}
15:25:48.029 → sampling interval: 0

Hi,

This is what I’m using. “envVariable” is the name of the variable and “defaultValue” is returned if the variable is not found or an error occurred.

float ncGetEnv(char* envVariable, float defaultValue) {
  float rValue=defaultValue;
  J *req = notecard.newRequest("env.get");
  if (req != NULL) {
    JAddStringToObject(req, "name", envVariable);
    J *rsp = notecard.requestAndResponse(req);
    if (rsp != NULL) {
      if (!notecard.responseError(rsp)) {
        if (JIsPresent(rsp, "text")) rValue=JAtoN(JGetString(rsp, "text"),NULL);
      }
      notecard.deleteResponse(rsp);
    }
  }
  return rValue;
}

That works perfectly!

Thank you so much,
Brecky