Arduinojson: need help with serialising uint16_t array

Created on 26 Dec 2020  路  9Comments  路  Source: bblanchon/ArduinoJson

hi im using esp8266 ir library to retreieve raw data into an array but im not sure how to convert that into a json array to send that to my server, ive googled two days continuously but no much results so confused with json5 ad json6 codes
`StaticJsonDocument<1024> doc;
doc["result"] = resultToHumanReadableBasic(&results);
String description = IRAcUtils::resultAcToString(&results);
doc["Ac results"] = description;
uint16_t *raw_array = resultToRawArray(&results);
JsonArray array = doc.to();
copyArray(raw_array, array);

yield();
char buffer[1024];
size_t n = serializeJson(doc, buffer);
client.publish("/test/light1", buffer,n);`

the error that im getting is
no matching function for call to 'copyArray(uint16_t*&, ArduinoJson6141_0000010::VariantTo<ArduinoJson6141_0000010::ArrayRef>::type)'

question

All 9 comments

Hello, caster
you share only String value or you want send whole file on Server ??

only string values of that array so i can deserialise the same on another module . sorry I'm a newbie so this is how I think its gonna work

Hi @caster1,

no matching function for call to 'copyArray(uint16_t*&, ArduinoJson6141_0000010::VariantTo::type)'

The compiler tells you that there is no function copyArray(uint16_t*, JsonArray).
Indeed, a quick look at the documentation for copyArray() reveals that this function expects three parameters when called with a pointer:

bool copyArray(uint16_t* src, size_t len, JsonArray dst);

As you can see, you must specify the length of the array.

Best regards,
Benoit

(@Yash1903, I had to delete an unrelated message you wrote in another thread. Please open a new issue if you need assistance, but don't hijack existing conversations).

thank you my problem solved partially now im trying to send it to mysever whenever i receive a signal but its publishing 2 bytes of empty array
StaticJsonDocument<1024> doc; JsonArray array = doc.to<JsonArray>(); `uint16_t` *raw_array = resultToRawArray(&results); uint16_t length = getCorrectedRawLength(&results); bool copyArray(uint16_t* raw_array, size_t length, JsonArray array); yield(); char buffer[1024]; size_t n = serializeJson(doc, buffer); client.publish("/test/light1", buffer,n);

https://imgur.com/myLtFQn

Hi @caster1,

It looks like you wanted to write:

StaticJsonDocument<1024> doc;
uint16_t *raw_array = resultToRawArray(&results); // memory leak?
uint16_t length = getCorrectedRawLength(&results);
copyArray(raw_array, length, doc);
char buffer[1024];
size_t n = serializeJson(doc, buffer);
client.publish("/test/light1", buffer,n);

Also, remember that the maximum message size for PubSubClient is 256 by default.

Best regards,
Benoit

(@Yash1903, I deleted the message you wrote because it didn't belong here; please open a new issue)

hi when i replaced my ode with yours i.e
StaticJsonDocument<1024> doc; uint16_t *raw_array = resultToRawArray(&results); // memory leak? uint16_t length = getCorrectedRawLength(&results); copyArray(raw_array, length, doc); char buffer[1024]; size_t n = serializeJson(doc, buffer); client.publish("/test/light1", buffer,n);

and I'm getting this error
no matching function for call to 'copyArray(uint16_t*&, uint16_t&, ArduinoJson6141_0000010::StaticJsonDocument<256u>&)'

Please upgrade ArduinoJson to 6.15.2 or above.

StaticJsonDocument<256> doc;
  deserializeJson(doc, payload, length);

  uint16_t *rarray = doc;
  uint16_t len = length;
 irsend.sendRaw(rarray, len ,38);

im trying to desrialise like this but its throwing me this error.
cannot convert 'ArduinoJson6172_91::StaticJsonDocument<256u>' to 'uint16_t* {aka short unsigned int*}' in initialization

uint16_t *rarray = doc;

To extract a C array from a JsonDocument, you must use copyArray():

uint16_t rarray[64];
uin16_t len = copyArray(doc, rarray);
irsend.sendRaw(rarray, len, 38);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

HilalAH picture HilalAH  路  3Comments

koffienl picture koffienl  路  3Comments

ligantx picture ligantx  路  4Comments

bepursuant picture bepursuant  路  4Comments

dimityrivanov picture dimityrivanov  路  5Comments