Average rainfall 2001-2016, global tropics

Map: Average rainfall 2001-2016, global tropics

ide: Arduinojson

Introduction

Arduinojson is a library for serialization and deserialization of json structured documents in Arduino IDE. This post outlines how to install and use Arduinojson. The post also contains the complete json code for version 0.78 of xSpectre handheld spectrometer.

Arduinojson

Arduinojson is an evolving project and widely used as a standard container for transferring data to and from Arduino microcontrollers. When receiving data to the Arduino microcontroller, arduinojson deserializes the content to ordinary data and when packaging data for sending from the microcontroller, arduinojson serializes the data.

Add Arduinojson library

Open Arduino IDE, from the menu select:

Tools -> Manage Libraries…

Type arduinojeson in the search box. The json library you want to install is the one by Benoit Blanchon, Arduinojson. At time of writing this in October 2022 the version available is 6.19.4. Go ahead and install. Accept to install additional libraries required.

Deserialize (decode) json

Start a new Arduino IDE sketch and import Arduinojson.

/*
 Test for json handling in Arduino using Arduinojson
 Thomas Gumbricht 28 October 2022
 *
 */

#include <ArduinoJson.h>

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Arduino JSON uses a preallocated memory pool to store the JsonObject tree, this is done by the StaticJsonBuffer. You can use ArduinoJson Assistant to compute the exact buffer size, but for this example 200 is enough.

StaticJsonBuffer<200> jsonBuffer;

Create a char array called json[] to store a sample JSON string, you have to escape (“/”) for passing the syntax demands of both Arduino and Arduinojson:

StaticJsonBuffer<200> jsonBuffer;
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";

JsonObject& root = jsonBuffer.parseObject(json);

if(!root.success()) {
  Serial.println("parseObject() failed");
  return false;
}

const char* sensor = root["sensor"];
long time = root["time"];
double latitude = root["data"][0];
double longitude = root["data"][1];