2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2018-01-05 09:20:01 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2018
|
2014-11-03 21:29:55 +01:00
|
|
|
// MIT License
|
2017-12-11 15:19:28 +01:00
|
|
|
//
|
|
|
|
// This example shows how to generate a JSON document with ArduinoJson.
|
2014-11-03 21:29:55 +01:00
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
void setup() {
|
2017-12-11 15:19:28 +01:00
|
|
|
// Initialize Serial port
|
2014-11-03 21:29:55 +01:00
|
|
|
Serial.begin(9600);
|
2017-12-11 15:19:28 +01:00
|
|
|
while (!Serial) continue;
|
2014-11-03 21:29:55 +01:00
|
|
|
|
2018-06-07 11:21:54 +02:00
|
|
|
// Allocate the JSON document
|
2016-02-01 13:31:07 +01:00
|
|
|
//
|
2018-04-17 21:27:45 +02:00
|
|
|
// Inside the brackets, 200 is the RAM allocated to this document.
|
|
|
|
// Don't forget to change this value to match your requirement.
|
2019-01-29 14:09:09 +01:00
|
|
|
// Use arduinojson.org/v6/assistant to compute the capacity.
|
2018-04-17 21:27:45 +02:00
|
|
|
StaticJsonDocument<200> doc;
|
2014-11-03 21:29:55 +01:00
|
|
|
|
2018-02-26 16:05:16 +01:00
|
|
|
// StaticJsonObject allocates memory on the stack, it can be
|
2018-04-17 21:27:45 +02:00
|
|
|
// replaced by DynamicJsonDocument which allocates in the heap.
|
2016-02-01 13:31:07 +01:00
|
|
|
//
|
2018-04-17 21:27:45 +02:00
|
|
|
// DynamicJsonDocument doc(200);
|
|
|
|
|
2019-01-29 14:09:09 +01:00
|
|
|
// Add values in the document
|
2016-02-01 13:31:07 +01:00
|
|
|
//
|
2019-01-29 14:09:09 +01:00
|
|
|
doc["sensor"] = "gps";
|
|
|
|
doc["time"] = 1351824120;
|
2014-11-07 16:23:21 +01:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
// Add an array.
|
2016-02-01 13:31:07 +01:00
|
|
|
//
|
2019-01-29 14:09:09 +01:00
|
|
|
JsonArray data = doc.createNestedArray("data");
|
2017-05-20 09:06:53 +02:00
|
|
|
data.add(48.756080);
|
|
|
|
data.add(2.302038);
|
2014-11-03 21:29:55 +01:00
|
|
|
|
2019-01-29 14:09:09 +01:00
|
|
|
// Generate the minified JSON and send it to the Serial port.
|
|
|
|
//
|
2019-01-23 18:19:24 +01:00
|
|
|
serializeJson(doc, Serial);
|
2019-01-29 14:09:09 +01:00
|
|
|
// The above line prints:
|
2014-11-03 21:29:55 +01:00
|
|
|
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
|
|
|
|
|
2019-01-29 14:09:09 +01:00
|
|
|
// Start a new line
|
2014-11-03 21:29:55 +01:00
|
|
|
Serial.println();
|
|
|
|
|
2019-01-29 14:09:09 +01:00
|
|
|
// Generate the prettified JSON and send it to the Serial port.
|
|
|
|
//
|
2019-01-23 18:19:24 +01:00
|
|
|
serializeJsonPretty(doc, Serial);
|
2019-01-29 14:09:09 +01:00
|
|
|
// The above line prints:
|
2014-11-03 21:29:55 +01:00
|
|
|
// {
|
|
|
|
// "sensor": "gps",
|
|
|
|
// "time": 1351824120,
|
|
|
|
// "data": [
|
|
|
|
// 48.756080,
|
|
|
|
// 2.302038
|
|
|
|
// ]
|
|
|
|
// }
|
2014-07-01 14:08:15 +02:00
|
|
|
}
|
|
|
|
|
2014-11-03 21:29:55 +01:00
|
|
|
void loop() {
|
|
|
|
// not used in this example
|
2015-08-20 15:15:59 +02:00
|
|
|
}
|
2017-12-11 15:19:28 +01:00
|
|
|
|
2018-07-02 09:35:21 +02:00
|
|
|
// Visit https://arduinojson.org/v6/example/generator/ for more.
|