Arduino JsonObject MQTT sub&pub command

MQTTデバイスのサブスクライブとパブリッシュ双方のデータを全てJSONフォーマットに統一。

フロントエンドであるHome Assistantの照明インターフェイスもJSONフォーマットに対応しているため、調光機能付LED照明の調光レベルとON/OFFコマンドもJSONフォーマットで統一した。

ex)

char* json[50] = "{"brightness":50, "state":"ON"}"

Example

Create an object and serialize it

// allocate the memory for the document
const size_t CAPACITY = JSON_OBJECT_SIZE(1);
StaticJsonDocument<CAPACITY> doc;

// create an object
JsonObject object = doc.to<JsonObject>();
object["hello"] = "world";

// serialize the object and send the result to Serial
serializeJson(doc, Serial);

Deserialize a object

// allocate the memory for the document
const size_t CAPACITY = JSON_OBJECT_SIZE(1);
StaticJsonDocument<CAPACITY> doc;

// deserialize the object
char json[] = "{\"hello\":\"world\"}"; 
deserializeJson(doc, json);

// extract the data
JsonObject object = doc.as<JsonObject>();
const char* world = object["hello"];

オンラインアシスタント

以下オンライン上のツールで、任意のJSONフォーマットデータ入力すると、Arduinoへ導入するための最適なパラメータを算出してくれます。

以下Output typeの指定は任意です。

以下Step2:JSONのOutputの欄に、導入したいJSONデータを入力して下さい。今回は入れ子データを入力。

最適なパラメータが出力されます。

以下コードをArduinoスケッチに記述して下さい。

StaticJsonDocument<96> doc;

JsonObject id1 = doc.createNestedObject("id1");
id1["hr"] = "65";
id1["spo2"] = "98";
id1["temp"] = "36.56";

serializeJson(doc, output);

JsonDocument::createNestedObject() 入れ子データ