Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to encode & decode JSON data in Arduino for IoT

JSON and XML are the most common data serialization formats. The Internet is a hub of billions of different devices, applications, and services. These devices and applications are built in different programming languages and around diverse platforms. For all these entirely different devices and applications to effectively communicate with each other, data is serialized and de-serialized at the endpoints in a format that is shareable. JSON and XML are the two most important markup languages in the world of the internet that let exchange data across different platforms, devices, and applications in a standardized manner. The devices and applications serialize the data into JSON or XML format before transmitting it over to the internet. The receiving device or application at the other end of the internet de-serializes the JSON/XML data and extracts the required information as per its user code.

All IoT devices are also part of the same internet universe. Most IoT devices use JSON format for communicating data with a server, gateway, or other devices (in the same network). JSON is preferred over XML because it is simpler and less verbose. For IoT devices, even a little overhead can be a great burden for microcontroller-run devices. That is why most IoT devices use JSON to exchange data over the internet. Most IoT platforms and services also essentially use JSON, while they may also be using XML format.

In this article, we will discuss how Arduino and Arduino-compatible microcontrollers serialize and de-serialize JSON data for standard universal communication in the realms of the IoT. Arduino and its compatible boards rely on the ArduinoJSON library to encode and decode JSON data for IoT and the internet.

Read the entire project on EngineersGarage.com
 
1. Install the ArduinoJSON Library:

Download the latest version from https://arduinojson.org/ or use the Library Manager in the Arduino IDE.

2. Include the Library in Your Sketch:
C++

#include <ArduinoJson.h>



3. Decoding JSON Data:

Use the deserializeJson() function to parse a JSON string into a JSON object or array.

C++

const size_t capacity = JSON_OBJECT_SIZE(3) + 40; // Estimate memory for the JSON object
DynamicJsonDocument doc(capacity);

deserializeJson(doc, jsonString);

int sensorValue = doc["sensorValue"];
const char* sensorName = doc["sensorName"];



4. Encoding JSON Data:

Use the serializeJson() function to convert a JSON object or array into a JSON string.

C++

DynamicJsonDocument doc(capacity);

doc["sensorValue"] = 123;
doc["sensorName"] = "temperature";

String jsonString;
serializeJson(doc, jsonString);



Key Points:

Memory Management: JSON objects can consume significant memory. Use DynamicJsonDocument with an estimated capacity to allocate memory efficiently.
String Size: Consider the length of the JSON string when allocating memory for it.
Nesting: ArduinoJSON supports nested objects and arrays.
Error Handling: Check for errors using DeserializationError or SerializationError.

Additional Tips:

Use descriptive variable names to improve code readability.
Comment your code to explain its logic.
Test your code with various JSON data inputs.
Explore the ArduinoJSON library's documentation for advanced features and examples.
 
I'm currently doing a project using a Pic. I have an object that is in a union with a byte array so I can read/write the object to EEPROM. I send coma separated data (via bluetooth) and write it to the same byte array. When doing internet stuff (ESP) I use the JSON library and found it no problem.However, can't find a JSON library for Pics (anyone know of one) hence the CSV approach. Also XC8 doesn't seem particularly friendly towards JSON.

Mike.
 
For a small memory PIC, I'd expect converting binary value storage for INTs or FLOATs to and from their text representations to be slow and CPU intensive - plus needing quite a bit of RAM for storing the JSON text in the first place. No problem on a faster & larger memory MCU or CPU.

I have an object that is in a union with a byte array so I can read/write the object to EEPROM. I send coma separated data (via bluetooth) and write it to the same byte array.

I've used Base64 encoding to transfer binary data, that's mostly just bit-shifting so is quick and simple. Three bytes converted to four, using six bits of each to put them in the printable ASCII region. It's a lot faster and more compact than converting to & from readable text, if that is not required for the application.


It could be ideal for transferring and storing your byte array?
 
I'm currently doing a project using a Pic. I have an object that is in a union with a byte array so I can read/write the object to EEPROM. I send coma separated data (via bluetooth) and write it to the same byte array. When doing internet stuff (ESP) I use the JSON library and found it no problem.However, can't find a JSON library for Pics (anyone know of one) hence the CSV approach. Also XC8 doesn't seem particularly friendly towards JSON.

Mike.
There are some PIC JSON examples out there, the main 'problem' is that the Arduino ones are written using C++ rather than C.

There's even a MicroChip application note with code:

 

Latest threads

New Articles From Microcontroller Tips

Back
Top