Arduinojson: 馃敟 JsonBuffer was not declared in this scope / does not name a type

Created on 14 Jun 2018  路  27Comments  路  Source: bblanchon/ArduinoJson

After updating from 5.13.1 to 5.13.2 neither StaticJsonBuffer nor DynamicJsonBuffer work.
Compiler gives error StaticJsonBuffer/DynamicJsonBuffer' was not declared in this scope. Downgrading back to 5.13.1 and using the same code compiles.

question

Most helpful comment

Hi @teemue,

I'm pretty sure everything is okay with 5.13.2.
However, version 6 replaces JsonBuffer with JsonDocument.

Are you sure that it was 5.13.2, not 6.0?

Regards
Benoit

All 27 comments

Hi @teemue,

I'm pretty sure everything is okay with 5.13.2.
However, version 6 replaces JsonBuffer with JsonDocument.

Are you sure that it was 5.13.2, not 6.0?

Regards
Benoit

Got the same error after upgrading the library from the library manager in the Arduino IDE.

error: 'StaticJsonBuffer' was not declared in this scope

Rolling back to the previous version solved it.

@bblanchon is there any way to detect the library version and have a chance to use JsonDocument instead of JsonBuffer while maintaining a single codebase in external projects that use your library?

Hi @tobozo

Yes, you can check the value of ARDUINOJSON_VERSION_MAJOR, it's available in version 5.13.2 and 6.0.0
However, since ArduinoJson 6 is currently in beta, I would recommend only to support version 5 and use the macro to check that the right version is installed.

BTW, I tried to reproduce this bug (error: 'StaticJsonBuffer' was not declared in this scope) but I could not; can you provide the steps so I can understand what's going on?
I guess that version 6 was installed instead of version 5.13.2, but I don't understand why...

Regards,
Benoit

@bblanchon thanks for that, I've patched accordingly 馃憤

[edit] patch applied

@tobozo, any idea why version 6 was installed?
Was it a human mistake or did the Arduino Library Manager install the wrong one?

@bblanchon every time you tag a release, it appears in the Arduino Library Manager, which prompts the user into updating without specifying it's beta.

version - version of the library. Version should be semver compliant. 
1.2.0 is correct; 1.2 is accepted; r5, 003, 1.1c are invalid

suggestion: keep on doing beta on the relative branch but don't tag your beta releases

Just wanted to add I got bitten too by the automatic update to 6 beta.
I was getting the error "DynamicJsonBuffer does not name a type".
The cause is obviously the same (and a rollback restored functionality) but the message is somewhat different, so pasting it here to help people seraching for it.

Thanks, @vdeconinck.
I added this message to the FAQ, so it should be easier to find.

Today, got the same error after upgrading the library from the library manager in the Arduino IDE to 6.2.0-beta.

Rolling back to the previous version 5.13.2 solved it.

@christophs70 you can implement both syntaxes in your code

#if ARDUINOJSON_VERSION_MAJOR==6
  // use new syntax
#else
  // use old syntax
#endif

... and never worry about your IDE upgrading the library, see this example

@bblanchon I'm not good enough in C++ to know the drawbacks or even the feasability, but still I'm wondering if some kind of compatibility layer with the previous version is possible.

If StaticJsonBuffer existed and would resolve to StaticJsonDocument, the message error: 'StaticJsonBuffer' was not declared in this scope would go away.

If parseObject() would statically call deserializeJson() + as() (and ignore the DeserializationError error) that would fix the second compilation error while keeping the initial behaviour.

Or maybe that would just open a pandora box :-)

@tobozo If that was possible I would have done it, but unfortunately, there are many fundamental differences between 6 and 5.

The example in the link is not working anymore, you need to remove the references and success() is now isNull(); so you need to wrap the whole function in the #if.
Don't worry, I don't plan on doing any other big breaking change.

This caught me off-guard. The problem is the Arduino IDE now defaults to v6 beta, while the documentation defaults to v5. If its not possible to make the IDE default to v5 instead, it would be better to make the online documentation default to v6, or atleast make it more clear what is going on from the front page.

@fluentart, I added the following warning on the documentation:

image

I tried the entire range of 6.x ones until I found this thread. 5.13.2 on Arduino 1.8.5 on OSX compiled

@bblanchon , Sorry sir. I have a problem about "JsonBuffer was not declared in this scope" then i follow your solution (Downgrade ArduinoJson to 5.x (recommended as v6 is still in beta))

image

But version is still 6
Serial.print("ARDUINOJSON_VERSION_MAJOR = ");
Serial.println(ARDUINOJSON_VERSION_MAJOR);
image

so could you please give me an advice.

sorry for my poor english skill.
Best regard

@TonJimFit, there is another ArduinoJson.h on your computer.

5.13.3 compiles nicely on Arduino IDE 1.8.7.
The Library Manager installed 6.5.0-beta!
Thank you ;) ;)

Just Use version 9.5.0 this helped for me.

Its not corrent but it works!

5.13.4 is currently the latest stable.

For some reason Arduino updates to 6-beta even when you select the 5.x version in the lib manager. Reverting back to 5 fixed the issue for me!

It didn't for me.
It will update to the latest if you press "Update".

Guys the code is still not compiling for me even after I downgrade the version on arduinojson library. Can anyone help me out? I tried version 5.12.1, 5.12.2 and even 5.13

/*
Version 0.3 - March 06 2018
*/

include

include

include

include // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries

include // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries

include

int device_1 = 2;
int device_2 = 0;
int device_3 = 12;
int device_4 = 4;
int device_5 = 13;

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;

define MyApiKey "66c7262d-e0ca-4441-bd17-e87e9fe628aa" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard

define MySSID "RTGOMES" // TODO: Change to your Wifi network SSID

define MyWifiPassword "9846680691" // TODO: Change to your Wifi network password

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

uint64_t heartbeatTimestamp = 0;
bool isConnected = false;

void setPowerStateOnServer(String deviceId, String value);
void setTargetTemperatureOnServer(String deviceId, String value, String scale);

// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here

void turnOn(String deviceId) {
if (deviceId == "5c3d91921834ae3082fc3966") // Device ID of first device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device_1, HIGH);
}
else if (deviceId == "5c3d91b41834ae3082fc3968") // Device ID of second device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device_2, HIGH);
}
else if (deviceId == "5c3d91ca1834ae3082fc396a") // Device ID of third device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device_3, HIGH);
}
else if (deviceId == "5c3d91de1834ae3082fc396c") // Device ID of fourth device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device_4, HIGH);
}
else if (deviceId == "5c3d920f1834ae3082fc396e") // Device ID of fifth device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device_5, HIGH);
}

else {
Serial.print("Turn on for unknown device id: ");
Serial.println(deviceId);
}
}

void turnOff(String deviceId) {
if (deviceId == "5c3d91921834ae3082fc3966") // Device ID of first device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device_1, LOW);
}
else if (deviceId == "5c3d91b41834ae3082fc3968") // Device ID of second device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device_2, LOW);
}
else if (deviceId == "5c3d91ca1834ae3082fc396a") // Device ID of third device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device_3, LOW);
}
else if (deviceId == "5c3d91de1834ae3082fc396c") // Device ID of fourth device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device_4, LOW);
}
else if (deviceId == "5c3d920f1834ae3082fc396e") // Device ID of fifth device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device_5, LOW);
}
else {
Serial.print("Turn off for unknown device id: ");
Serial.println(deviceId);
}
}

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
isConnected = false;
Serial.printf("[WSc] Webservice disconnected from sinric.com!n");
break;
case WStype_CONNECTED: {
isConnected = true;
Serial.printf("[WSc] Service connected to sinric.com at url: %sn", payload);
Serial.printf("Waiting for commands from sinric.com ...n");
}
break;
case WStype_TEXT: {
Serial.printf("[WSc] get text: %sn", payload);
// Example payloads

    // For Switch or Light device types
    // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html

    // For Light device type
    // Look at the light example in github

    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.parseObject((char*)payload); 
    String deviceId = json ["deviceId"];     
    String action = json ["action"];

    if(action == "setPowerState") { // Switch or Light
        String value = json ["value"];
        if(value == "ON") {
            turnOn(deviceId);
        } else {
            turnOff(deviceId);
        }
    }
    else if (action == "SetTargetTemperature") {
        String deviceId = json ["deviceId"];     
        String action = json ["action"];
        String value = json ["value"];
    }
    else if (action == "test") {
        Serial.println("[WSc] received test command from sinric.com");
    }
  }
  break;
case WStype_BIN:
  Serial.printf("[WSc] get binary length: %u\n", length);
  break;

}
}

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

pinMode (device_1, OUTPUT);
pinMode (device_2, OUTPUT);
pinMode (device_3, OUTPUT);
pinMode (device_4, OUTPUT);
pinMode (device_5, OUTPUT);

WiFiMulti.addAP(MySSID, MyWifiPassword);
Serial.println();
Serial.print("Connecting to Wifi: ");
Serial.println(MySSID);

// Waiting for Wifi connect
while(WiFiMulti.run() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if(WiFiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.print("WiFi connected. ");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}

// server address, port and URL
webSocket.begin("iot.sinric.com", 80, "/");

// event handler
webSocket.onEvent(webSocketEvent);
webSocket.setAuthorization("apikey", MyApiKey);

// try again every 5000ms if connection has failed
webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
}

void loop() {
webSocket.loop();

if(isConnected) {
uint64_t now = millis();

  // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
  if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
      heartbeatTimestamp = now;
      webSocket.sendTXT("H");          
  }

}
}

// If you are going to use a push button to on/off the switch manually, use this function to update the status on the server
// so it will reflect on Alexa app.
// eg: setPowerStateOnServer("deviceid", "ON")

// Call ONLY If status changed. DO NOT CALL THIS IN loop() and overload the server.

void setPowerStateOnServer(String deviceId, String value) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["deviceId"] = deviceId;
root["action"] = "setPowerState";
root["value"] = value;
StreamString databuf;
root.printTo(databuf);

webSocket.sendTXT(databuf);
}

//eg: setPowerStateOnServer("deviceid", "CELSIUS", "25.0")

// Call ONLY If status changed. DO NOT CALL THIS IN loop() and overload the server.

void setTargetTemperatureOnServer(String deviceId, String value, String scale) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["action"] = "SetTargetTemperature";
root["deviceId"] = deviceId;

JsonObject& valueObj = root.createNestedObject("value");
JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint");
targetSetpoint["value"] = value;
targetSetpoint["scale"] = scale;

StreamString databuf;
root.printTo(databuf);

webSocket.sendTXT(databuf);
}

@LiamGomes27 Please open a new issue.

ArduinoJson 6 finally got out of beta; it's time to close this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

loewekordel picture loewekordel  路  4Comments

timpur picture timpur  路  5Comments

zixzixzix picture zixzixzix  路  5Comments

ghost picture ghost  路  7Comments

sorokolokarim picture sorokolokarim  路  5Comments