Skip to main content

Assuming that String response will contain the json {"light": "off"} and your program is ready to use the ArduinoJson libraryArduinoJson library (#include <ArduinoJson.h> on top), a simple solution could be:

Step1 - declare and initialize StaticJsonBuffer object.

StaticJsonBuffer<80> jsonBuffer;

Step2 - deserialize the json from response.

JsonObject& root = jsonBuffer.parseObject(response);

Step3 - if the deserialization doesn't succeed, warn and break,

if (!root.success())
{
  Serial.print("parseObject(");
  Serial.print(response);
  Serial.println(") failed");
  break;
}

Step4 - otherwise, use json object to get the LED_Control.

Is the json will really contain {"light": "onn"} or is a typo ?

String LED_Control = root["light"];

if (LED_Control == "onn") {  // 'on' or 'onn' ??
    digitalWrite(pin, HIGH);
    Serial.println("LED ON");
}
else if (LED_Control == "off"){
    digitalWrite(pin, LOW);
    Serial.println("led OFF");
}

Assuming that String response will contain the json {"light": "off"} and your program is ready to use the ArduinoJson library (#include <ArduinoJson.h> on top), a simple solution could be:

Step1 - declare and initialize StaticJsonBuffer object.

StaticJsonBuffer<80> jsonBuffer;

Step2 - deserialize the json from response.

JsonObject& root = jsonBuffer.parseObject(response);

Step3 - if the deserialization doesn't succeed, warn and break,

if (!root.success())
{
  Serial.print("parseObject(");
  Serial.print(response);
  Serial.println(") failed");
  break;
}

Step4 - otherwise, use json object to get the LED_Control.

Is the json will really contain {"light": "onn"} or is a typo ?

String LED_Control = root["light"];

if (LED_Control == "onn") {  // 'on' or 'onn' ??
    digitalWrite(pin, HIGH);
    Serial.println("LED ON");
}
else if (LED_Control == "off"){
    digitalWrite(pin, LOW);
    Serial.println("led OFF");
}

Assuming that String response will contain the json {"light": "off"} and your program is ready to use the ArduinoJson library (#include <ArduinoJson.h> on top), a simple solution could be:

Step1 - declare and initialize StaticJsonBuffer object.

StaticJsonBuffer<80> jsonBuffer;

Step2 - deserialize the json from response.

JsonObject& root = jsonBuffer.parseObject(response);

Step3 - if the deserialization doesn't succeed, warn and break,

if (!root.success())
{
  Serial.print("parseObject(");
  Serial.print(response);
  Serial.println(") failed");
  break;
}

Step4 - otherwise, use json object to get the LED_Control.

Is the json will really contain {"light": "onn"} or is a typo ?

String LED_Control = root["light"];

if (LED_Control == "onn") {  // 'on' or 'onn' ??
    digitalWrite(pin, HIGH);
    Serial.println("LED ON");
}
else if (LED_Control == "off"){
    digitalWrite(pin, LOW);
    Serial.println("led OFF");
}
Source Link
J. Piquard
  • 322
  • 1
  • 8

Assuming that String response will contain the json {"light": "off"} and your program is ready to use the ArduinoJson library (#include <ArduinoJson.h> on top), a simple solution could be:

Step1 - declare and initialize StaticJsonBuffer object.

StaticJsonBuffer<80> jsonBuffer;

Step2 - deserialize the json from response.

JsonObject& root = jsonBuffer.parseObject(response);

Step3 - if the deserialization doesn't succeed, warn and break,

if (!root.success())
{
  Serial.print("parseObject(");
  Serial.print(response);
  Serial.println(") failed");
  break;
}

Step4 - otherwise, use json object to get the LED_Control.

Is the json will really contain {"light": "onn"} or is a typo ?

String LED_Control = root["light"];

if (LED_Control == "onn") {  // 'on' or 'onn' ??
    digitalWrite(pin, HIGH);
    Serial.println("LED ON");
}
else if (LED_Control == "off"){
    digitalWrite(pin, LOW);
    Serial.println("led OFF");
}