MsgPackParser.ino 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. //
  5. // This example shows how to deserialize a MessagePack document with
  6. // ArduinoJson.
  7. //
  8. // https://arduinojson.org/v7/example/msgpack-parser/
  9. #include <ArduinoJson.h>
  10. void setup() {
  11. // Initialize serial port
  12. Serial.begin(9600);
  13. while (!Serial)
  14. continue;
  15. // Allocate the JSON document
  16. JsonDocument doc;
  17. // The MessagePack input string
  18. uint8_t input[] = {131, 166, 115, 101, 110, 115, 111, 114, 163, 103, 112, 115,
  19. 164, 116, 105, 109, 101, 206, 80, 147, 50, 248, 164, 100,
  20. 97, 116, 97, 146, 203, 64, 72, 96, 199, 58, 188, 148,
  21. 112, 203, 64, 2, 106, 146, 230, 33, 49, 169};
  22. // This MessagePack document contains:
  23. // {
  24. // "sensor": "gps",
  25. // "time": 1351824120,
  26. // "data": [48.75608, 2.302038]
  27. // }
  28. // Parse the input
  29. DeserializationError error = deserializeMsgPack(doc, input);
  30. // Test if parsing succeeded
  31. if (error) {
  32. Serial.print("deserializeMsgPack() failed: ");
  33. Serial.println(error.f_str());
  34. return;
  35. }
  36. // Fetch the values
  37. //
  38. // Most of the time, you can rely on the implicit casts.
  39. // In other case, you can do doc["time"].as<long>();
  40. const char* sensor = doc["sensor"];
  41. long time = doc["time"];
  42. double latitude = doc["data"][0];
  43. double longitude = doc["data"][1];
  44. // Print the values
  45. Serial.println(sensor);
  46. Serial.println(time);
  47. Serial.println(latitude, 6);
  48. Serial.println(longitude, 6);
  49. }
  50. void loop() {
  51. // not used in this example
  52. }