forming JSON string, pushing onto vector

Home Forums mDot/xDot forming JSON string, pushing onto vector

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #12430
    Eric Tsai
    Participant

    I want to compose a JSON string from sensor data (say a temperature and a humidity), so I’ll have a JSON string that looks something like this:

    {“temperature”:”32″, “humidity”:”57″}

    The variable values are from int32_t or maybe float. I’m having trouble composing the string and pushing it onto the vector. After some compile errors, I settled on using char[] rather than strings, because it seemed easier to concatenate with char[] and convert int32_t. Code looks like this:

    std::vector<uint8_t> data;
    char str1[] = "temperature:";
    char str2[] = "humidity";
    char str3[] = "other";
    char buf[60];
    //just trying to compose a string, not worried if not json format for now
    snprintf(buf, sizeof buf, "%s%d%s%s", str1, counter_test, str2, str3);
    ...
    ...
    for (int i=0; i<59; i++){
      //data.push_back(buf[i]);    //doesn't make a difference
      data.push_back((uint8_t)buf[i]);
    }
    
    ...
    ...
    if ((ret = dot->send(data)) != mDot::MDOT_OK) {
        logError("failed to send %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
    } else {
        logInfo("successfully sent data to gateway");
    }
    ...
    logInfo("The String is = %s", buf);

    I don’t see any data sent over, and the log file saids:

    [INFO] network not joined, joining network
    [INFO] TX Frequency: 912300000 SF: 10 BW: 125 kHz POW: 11 dBm
    [ERROR] Data exceeds datarate max payload
    [ERROR] failed to send -1:Invalid Parameter
    [INFO] The String is = temperature:3humidityother

    The string looks right (I know it’s not the correct JSON format, but I’m not worried about that now). The way I'm pushing it onto the vector seems incorrect. I'm not sure how to do this with char[]. I'm comparing the my char method to the String examples from Multitech.

    for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
       data.push_back((uint8_t) *it);

    Thanks,
    Eric

    • This topic was modified 8 years ago by Eric Tsai.
    • This topic was modified 8 years ago by Eric Tsai.
    • This topic was modified 8 years ago by Eric Tsai. Reason: clarify
    • This topic was modified 8 years ago by Eric Tsai. Reason: html tags
    #12435
    Jason Reiss
    Keymaster

    When building strings stringstream is also very helpful
    http://www.cplusplus.com/reference/sstream/stringstream/

    Using DR0 the max payload is 11 bytes.
    http://www.multitech.net/developer/software/lora/introduction-to-lora/

    Sending JSON maybe overkill for data being sent from a known endpoint. It would be best to pack it as small as possible.

    “t:32,h:57” -> 9 bytes

    A binary format with predefined types for each data item
    temp: 0x01
    value: 0x20 -> 32
    humidity: 0x02
    value: 0x39 -> 57

    0x01200239 -> 4 bytes

    If you know this sensor is only sending temp and humidity then you could just send the values in two bytes.

    Here is a binary format we use for the EVB:
    http://www.multitech.net/developer/software/dot-box-and-evb-software/data-packet-format/

    #12438
    Eric Tsai
    Participant

    Oh, thanks for the links Jason. That makes a lot more sense than what I was trying to do.

    #13287
    Ashu Joshi
    Participant

    Jason – for the Binary Format you use for EVB – do you have a code sample that I could re-use? I am trying to read a Temp & Light sensor and send that to the Gateway.

    Here is a binary format we use for the EVB:

    http://www.multitech.net/developer/software/dot-box-and-evb-software/data-packet-format/

    I tried going through the EVB FW code on mBed but it wasn’t obvious where and how you are packing the sensor data?

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.