Making ArduinoJson work when compiling with CLI

Getting ArduinoJson to work from the Web or Atom IDE is no problem, but I want to have all my source locally and compile via CLI. I can make it work by adding it to the project.properties file, but I’d like to have all the files locally.

I guess this one’s for @bblanchon: What hoops do I need to jump through to make ArduinoJson (and any other library using .hpp extensions) work when compiling from CLI?

I’ve found several tips, but none of them work well. I’m using CLI 1.29.0.

To be honest, I’m not very familiar with the Particle CLI.
But, as far as ArduinoJson is concerned, you just need to copy one file to your source directory and you’re done.
See https://arduinojson.org/doc/installation/

No worries Benoit and thanks for your great work on the library!

I guess the problem is that it’s rather undocumented how you make the cloud compiler accept .hpp files. I have a workaround by adding it using Library Manager so that it’s “automagically” written into the project.properties file, but I’d love to see better documentation on both .hpp files and “project.properties”.

This is one way to handle it. Use the extended project format, which means you have a project.properties file and a src directory.

This is my src/ArduinoJsonTest.cpp file:

#include "Particle.h"

#include "ArduinoJson.h"

StaticJsonBuffer<200> jsonBuffer;

void setup() {
	Serial.begin(9600);

	char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";

	// Root of the object tree.
	//
	// It's a reference to the JsonObject, the actual bytes are inside the
	// JsonBuffer with all the other nodes of the object tree.
	// Memory is freed when jsonBuffer goes out of scope.
	JsonObject& root = jsonBuffer.parseObject(json);
}

void loop() {
}

My project.properties file was empty. In the top level directory, add the library:

particle library add ArduinoJson

Now you can build using the library:

particle compile photon . --saveTo firmware.bin

To use a local copy of the library so you can modify it:

particle library copy ArduinoJson

This downloads the library source into the lib directory in the top level of your project directory.

You must also also comment out the library from project.properties, otherwise it will still use the official version:

project.properties:

# dependencies.ArduinoJson=5.11.2

Now when you compile you’ll see it upload all of the files:

$ particle compile photon . --saveTo firmware.bin

Compiling code for photon

Including:
    lib/ArduinoJson/ArduinoJson.h
    lib/ArduinoJson/src/ArduinoJson.h
    lib/ArduinoJson/examples/JsonGeneratorExample/JsonGeneratorExample.ino
    lib/ArduinoJson/examples/JsonHttpClient/JsonHttpClient.ino
    lib/ArduinoJson/examples/JsonParserExample/JsonParserExample.ino
    lib/ArduinoJson/examples/JsonServer/JsonServer.ino
    lib/ArduinoJson/examples/JsonUdpBeacon/JsonUdpBeacon.ino
    lib/ArduinoJson/examples/ProgmemExample/ProgmemExample.ino
    lib/ArduinoJson/examples/StringExample/StringExample.ino
    lib/ArduinoJson/fuzzing/fuzzer.cpp
    lib/ArduinoJson/test/DynamicJsonBuffer/alloc.cpp
    lib/ArduinoJson/test/DynamicJsonBuffer/createArray.cpp
    lib/ArduinoJson/test/DynamicJsonBuffer/createObject.cpp
    lib/ArduinoJson/test/DynamicJsonBuffer/no_memory.cpp
    lib/ArduinoJson/test/DynamicJsonBuffer/size.cpp
    lib/ArduinoJson/test/DynamicJsonBuffer/startString.cpp
    lib/ArduinoJson/test/DynamicJsonBuffer/strdup.cpp
    lib/ArduinoJson/test/IntegrationTests/gbathree.cpp
    lib/ArduinoJson/test/IntegrationTests/round_trip.cpp
    lib/ArduinoJson/test/JsonArray/add.cpp
    lib/ArduinoJson/test/JsonArray/basics.cpp
    lib/ArduinoJson/test/JsonArray/copyFrom.cpp
    lib/ArduinoJson/test/JsonArray/copyTo.cpp
    lib/ArduinoJson/test/JsonArray/invalid.cpp
    lib/ArduinoJson/test/JsonArray/iterator.cpp
    lib/ArduinoJson/test/JsonArray/prettyPrintTo.cpp
    lib/ArduinoJson/test/JsonArray/printTo.cpp
    lib/ArduinoJson/test/JsonArray/remove.cpp
    lib/ArduinoJson/test/JsonArray/set.cpp
    lib/ArduinoJson/test/JsonArray/subscript.cpp
    lib/ArduinoJson/test/JsonBuffer/nested.cpp
    lib/ArduinoJson/test/JsonBuffer/nestingLimit.cpp
    lib/ArduinoJson/test/JsonBuffer/parse.cpp
    lib/ArduinoJson/test/JsonBuffer/parseArray.cpp
    lib/ArduinoJson/test/JsonBuffer/parseObject.cpp
    lib/ArduinoJson/test/JsonObject/basics.cpp
    lib/ArduinoJson/test/JsonObject/containsKey.cpp
    lib/ArduinoJson/test/JsonObject/get.cpp
    lib/ArduinoJson/test/JsonObject/invalid.cpp
    lib/ArduinoJson/test/JsonObject/iterator.cpp
    lib/ArduinoJson/test/JsonObject/prettyPrintTo.cpp
    lib/ArduinoJson/test/JsonObject/printTo.cpp
    lib/ArduinoJson/test/JsonObject/remove.cpp
    lib/ArduinoJson/test/JsonObject/set.cpp
    lib/ArduinoJson/test/JsonObject/subscript.cpp
    lib/ArduinoJson/test/JsonVariant/as.cpp
    lib/ArduinoJson/test/JsonVariant/compare.cpp
    lib/ArduinoJson/test/JsonVariant/copy.cpp
    lib/ArduinoJson/test/JsonVariant/is.cpp
    lib/ArduinoJson/test/JsonVariant/printTo.cpp
    lib/ArduinoJson/test/JsonVariant/set_get.cpp
    lib/ArduinoJson/test/JsonVariant/subscript.cpp
    lib/ArduinoJson/test/JsonVariant/success.cpp
    lib/ArduinoJson/test/JsonVariant/undefined.cpp
    lib/ArduinoJson/test/JsonWriter/writeFloat.cpp
    lib/ArduinoJson/test/JsonWriter/writeString.cpp
    lib/ArduinoJson/test/Misc/deprecated.cpp
    lib/ArduinoJson/test/Misc/FloatParts.cpp
    lib/ArduinoJson/test/Misc/std_stream.cpp
    lib/ArduinoJson/test/Misc/std_string.cpp
    lib/ArduinoJson/test/Misc/StringBuilder.cpp
    lib/ArduinoJson/test/Misc/TypeTraits.cpp
    lib/ArduinoJson/test/Misc/unsigned_char.cpp
    lib/ArduinoJson/test/Misc/vla.cpp
    lib/ArduinoJson/test/Polyfills/isFloat.cpp
    lib/ArduinoJson/test/Polyfills/isInteger.cpp
    lib/ArduinoJson/test/Polyfills/parseFloat.cpp
    lib/ArduinoJson/test/Polyfills/parseInteger.cpp
    lib/ArduinoJson/test/StaticJsonBuffer/alloc.cpp
    lib/ArduinoJson/test/StaticJsonBuffer/createArray.cpp
    lib/ArduinoJson/test/StaticJsonBuffer/createObject.cpp
    lib/ArduinoJson/test/StaticJsonBuffer/parseArray.cpp
    lib/ArduinoJson/test/StaticJsonBuffer/parseObject.cpp
    lib/ArduinoJson/test/StaticJsonBuffer/size.cpp
    lib/ArduinoJson/test/StaticJsonBuffer/startString.cpp
    lib/ArduinoJson/third-party/catch/catch.cpp
    src/ArduinoJsonTest.cpp
    project.properties

In your particle compile command make sure use “.”, the current directory, not the filename of your main source file, otherwise it won’t upload the library files.

2 Likes