LMIC-node JSON payload

I am using the LMIC-node example for a node I created. Everything was going smoothly sensing bytes down. I decided I wanted to send downlink in JSON format. I am using the ArduinoJson library in Platform IO to parse the JSON message. However, I am having some issues getting my JSON message to print in the terminal.

Here is my ProcessDownlink function

void processDownlink(ostime_t txCompleteTimestamp, uint8_t fPort, uint8_t *data, uint8_t dataLength)
{

    char holder[] = {data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11], data[12],
                       data[13], data[14], data[15], data[16], data[17], data[18], data[19], data[20], data[21], data[22], data[23], data[24],
                       data[25], data[26], data[27], data[28], data[29], data[30], data[31], data[32], data[33], data[34], data[35], data[36],
                       data[37], data[38], data[39], data[40], data[41], data[42], data[43], data[44], data[45], data[46]};

    String message;
    message = String(holder);
    serial.println(message);
    StaticJsonDocument<200> doc;

    // Check for NUL characters in the JSON string.
    char *nullCharacter = static_cast<char *>(memchr(holder, 0, dataLength));
    if (nullCharacter)
    {
        Serial.print("There is a NUL character in the JSON string.");
        *nullCharacter = ' ';
        return;
    }

    DeserializationError error = deserializeJson(doc, holder, dataLength);

    if (error)
    {
        Serial.print("deserializeJson() failed: ");
        Serial.println(error.c_str());
        return;
    }

    long Sensitivity = doc["Sensitivity"]; // "750"
    const char *Light = doc["Light"];      // "OFF"
    const char *Mode = doc["Mode"];        // "C"
    Serial.println(Sensitivity);
    Serial.println(Light);
    Serial.println(Mode);
}

Here is the JSON message

{
  "Sensitivity": "750",
  "Light": "OFF",
  "Mode": "C"
}

And here is my Downlink payload formatter for encoding.

function encodeDownlink(input) {
  return {
    bytes: [input.data.Sensitivity, input.data.Light, input.data.Mode],
    fPort: 10,
  };
}

Can anyone help me resolve this issue.

Your downlink is only sending the values in the JSON message, not the complete JSON string.
So the data you are receiving on the node only contains the actual values - the names are not included. Hopefully that’ll help enough!

What do I need to add in order to have it send the full JSON string?

Please don’t send strings of any variety - it’s hugely inefficient and increases the time the gateway is deaf to all uplinks from all devices in the area.

Your JSON is 59 bytes, you only need to send 4.

However the good news is that you are only send four bytes, ish, the Light may well be 3 bytes, but I’m not totally sure what the Downlink PF will do with your pushing a mix of numbers (or is that a number as a string) and text. But easily resolved.

This should help: https://www.thethingsnetwork.org/docs/devices/bytes/

Mostly you need to work on the Arduino code to take four bytes, convert the first two in to an integer (how to do it in the link above), use an if for the ON/OFF and possibly a switch for the Mode.

So if I’m understanding you correctly, I should just send down the values as bytes instead of as a JSON string?

Yes

But you can send JSON to the Downlink encoder that turns it in to bytes.

And then you process the bytes in the reverse of what you will be doing in the Arduino C code for the uplink.

This actually helps me alot, I worked with sending Bytes first and was trying use the JSON format because I thought it would e easier to organize. I am going to try this method when I return home. Thank You!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.