forming JSON string, pushing onto vector
Tagged: mDot, multiple sensors, sensor data
- This topic has 3 replies, 3 voices, and was last updated 8 years, 9 months ago by
Ashu Joshi.
-
AuthorPosts
-
May 6, 2016 at 10:49 pm #12430
Eric Tsai
ParticipantI 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:3humidityotherThe 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,
EricMay 7, 2016 at 8:34 am #12435Jason Reiss
KeymasterWhen building strings stringstream is also very helpful
http://www.cplusplus.com/reference/sstream/stringstream/Using DR0 the max payload is 11 bytes.
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 -> 570x01200239 -> 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:
May 7, 2016 at 3:32 pm #12438Eric Tsai
ParticipantOh, thanks for the links Jason. That makes a lot more sense than what I was trying to do.
June 15, 2016 at 1:36 pm #13287Ashu Joshi
ParticipantJason – 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:
I tried going through the EVB FW code on mBed but it wasn’t obvious where and how you are packing the sensor data?
-
AuthorPosts
- You must be logged in to reply to this topic.