kakopappa / sinric

Amazon Alexa Smart home skill / Google Home Action for ESP8266 / ESP32 / Arduino
https://sinric.com
285 stars 166 forks source link

DynamicJsonBuffer error #336

Closed Likhith152 closed 4 years ago

Likhith152 commented 4 years ago

I'm getting this DynamiJsonBuffer error. Tried to upgrade from v5 to v6, but no use. Will you please help me out in this topic,. thanks Capture

kakopappa commented 4 years ago

you have two options

  1. downgrade to 5 2 change the code to support 6 https://arduinojson.org/v6/doc/upgrade/
Likhith152 commented 4 years ago

Sorry, Will you please change the code to support 6 for me! Thanks in advANCE

Likhith152 commented 4 years ago

And now Im getting this error 'ArduinoJson6120_000001::DynamicJsonDocument' has no member named 'parseObject'

Capture2

thegoliathgeek commented 4 years ago

Current websocket library works fine with 5th verison of JSON library. So it's better to downgrade to 5 .

emnik commented 4 years ago

See an example code gere:

//#define DEBUG

include

include

include

include

include

include // get it from

https://github.com/Links2004/arduinoWebSockets/releases

include // get it from https://arduinojson.org/ or install

via Arduino library manager SOS Version 6

WebSocketsClient webSocket;

const char MyApiKey = "xxxxxxxxxxxxxxxxxxx"; // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard const char MySSID = "xxxxxxxxx"; // TODO: Change to your Wifi network SSID const char* MyWifiPassword = "xxxxxxxxxxx"; // TODO: Change to your Wifi network password const String MyDeviceID = "xxxxxxxxxxxxxxxxxxxxxxx"; // TODO: Change to your deviceID (get it from sinric)

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

uint64_t heartbeatTimestamp = 0; bool isConnected = false;

void setPowerStateOnServer(String deviceId, String value) { char databuf[128]; StaticJsonDocument<128> root; root["deviceId"] = deviceId; root["action"] = "setPowerState"; root["value"] = value; serializeJson(root, databuf); webSocket.sendTXT(databuf); }

//Define the commands for device control void turnOn(String deviceId) { if (deviceId==MyDeviceID) // Device ID of first device {

ifdef DEBUG

Serial.print("Turn on device id: ");
Serial.println(deviceId);
#endif
setPowerStateOnServer(deviceId, "ON");

} }

void turnOff(String deviceId) { if (deviceId==MyDeviceID) // Device ID of first device {

ifdef DEBUG

 Serial.print("Turn off Device ID: ");
 Serial.println(deviceId);
 #endif
 setPowerStateOnServer(deviceId, "OFF");

} }

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { switch(type) { case WStype_DISCONNECTED: isConnected = false;

ifdef DEBUG

  Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
  #endif
  break;
case WStype_CONNECTED: {
  isConnected = true;
  #ifdef DEBUG
  Serial.printf("[WSc] Service connected to sinric.com at url: %s\n",

payload); Serial.printf("Waiting for commands from sinric.com ...\n");

endif

  }
  break;
case WStype_TEXT: {
    #ifdef DEBUG
    Serial.printf("[WSc] get text: %s\n", payload);
    #endif
    // 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
    // {"deviceId": xxxx, "action": "AdjustBrightness", value: 3} //

https://developer.amazon.com/docs/device-apis/alexa-brightnesscontroller.html // {"deviceId": xxxx, "action": "setBrightness", value: 42} // https://developer.amazon.com/docs/device-apis/alexa-brightnesscontroller.html // {"deviceId": xxxx, "action": "SetColor", value: {"hue": 350.5, "saturation": 0.7138, "brightness": 0.6501}} // https://developer.amazon.com/docs/device-apis/alexa-colorcontroller.html // {"deviceId": xxxx, "action": "DecreaseColorTemperature", value: 0} // https://developer.amazon.com/docs/device-apis/alexa-colortemperaturecontroller.html // {"deviceId": xxxx, "action": "IncreaseColorTemperature", value: 0} // https://developer.amazon.com/docs/device-apis/alexa-colortemperaturecontroller.html // {"deviceId": xxxx, "action": "SetColorTemperature", value: 2200} // https://developer.amazon.com/docs/device-apis/alexa-colortemperaturecontroller.html

    DynamicJsonDocument json(1024);
    deserializeJson(json, (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 == "test") {
        #ifdef DEBUG
        Serial.println("[WSc] received test command from sinric.com");
        #endif
    }
  }
  break;
case WStype_BIN:
  #ifdef DEBUG
  Serial.printf("[WSc] get binary length: %u\n", length);
  #endif
  break;

} }

void setup() {

ifdef DEBUG

Serial.begin(115200);

endif

WiFi.mode(WIFI_STA); WiFi.begin(MySSID, MyWifiPassword); while (WiFi.waitForConnectResult() != WL_CONNECTED) {

ifdef DEBUG

Serial.println("Connection Failed! Rebooting...");
#endif
delay(5000);
ESP.restart();

}

ArduinoOTA.setHostname("xxxxxxxxx"); //TODO change to your OTA hostname ArduinoOTA.onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) type = "sketch"; else // U_SPIFFS type = "filesystem";

// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS

using SPIFFS.end()

ifdef DEBUG

Serial.println("Start updating " + type);
#endif

});

ifdef DEBUG

ArduinoOTA.onEnd([]() { Serial.println("\nEnd"); });

ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); });

ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); });

endif

ArduinoOTA.begin();

ifdef DEBUG

Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP());

endif

// 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 setPowerStateOnServer(MyDeviceID, "ON"); }

void loop() { ArduinoOTA.handle();

//Handle websocket connections 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"); } } }

Στις Πέμ, 12 Σεπ 2019 στις 8:21 π.μ., ο/η Dhanush notifications@github.com έγραψε:

Current websocket library works fine with 5th verison of JSON library. So it's better to downgrade to 5 .

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kakopappa/sinric/issues/336?email_source=notifications&email_token=ABHFAWISQJLW6ZGOKTCYICTQJHGTZA5CNFSM4IVUCCF2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6QV4WA#issuecomment-530669144, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHFAWOH7KK6RRR52DHECI3QJHGTZANCNFSM4IVUCCFQ .

Likhith152 commented 4 years ago

you have two options

  1. downgrade to 5 2 change the code to support 6 https://arduinojson.org/v6/doc/upgrade/

Worked both. Thamks a lot for the support

Likhith152 commented 4 years ago

Thanks guys. Tried both ways. Downgrading the ArduinoJson library & Upgrading the code to v6 by manual. Both worked fine.

MacSass commented 4 years ago

@Likhith152 I´d like to use upgraded code too - would you mind sharing your upgraded sample code, as I´m not too good with coding ...

Thanks - MacSass

PS: Would be nice if @kakopappa could upgrade the examples to v6 as well, so people don´t fall this trap when they try sinric ...

Likhith152 commented 4 years ago

//

//sinric_v6.zip

Give a try here / Version 0.4 - April 26 2019 /

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 (use the correct version)

include

ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;

define MyApiKey "" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard

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

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

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

uint64_t heartbeatTimestamp = 0; bool isConnected = false;

// 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 == "5axxxxxxxxxxxxxxxxxxx") // Device ID of first device {
Serial.print("Turn on device id: "); Serial.println(deviceId); } else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); } else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId);
}
}

void turnOff(String deviceId) { if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of first device {
Serial.print("Turn off Device ID: "); Serial.println(deviceId); } else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); } 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: %s\n", payload); Serial.printf("Waiting for commands from sinric.com ...\n");
} break; case WStype_TEXT: { Serial.printf("[WSc] get text: %s\n", 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

   DynamicJsonDocument doc(200);
    deserializeJson(doc, (char*)payload); 
    String deviceId = doc ["deviceId"];     
    String action = doc ["action"];

    if(action == "setPowerState") { // Switch or Light
        String value = doc ["value"];
        if(value == "ON") {
            turnOn(deviceId);
        } else {
            turnOff(deviceId);
        }
    }
    else if (action == "SetTargetTemperature") {
        String deviceId = doc ["deviceId"];     
        String action = doc ["action"];
        String value = doc ["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);

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 want a push button: https://github.com/kakopappa/sinric/blob/master/arduino_examples/switch_with_push_button.ino

MacSass commented 4 years ago

Hey, thanks - I´ll take a look ...

MacSass commented 4 years ago

Hello, I finally found the time to update my code to v6 using the code samples from @Likhith152 - works great and easy enough when you know what you need to change.

Thanks a lot for the sample code - really appreciated.

I would recommend to change all code samples to v6, so new users have it more easy to get them running.

Regards - MacSass

sivar2311 commented 4 years ago

All Arduino examples (except multi_wifi) are compatible to ArduinoJson 5 and 6 since November 28. Have a look at the new example code.

MacSass commented 4 years ago

Wow - yes, just saw this now ... Sorry I wasn´t following closely lately due to work issues - great job - nice.

Thanks MacSass