ArduinoJson5 containsKey to 6
Created by: surapol4345
I am using the Arduino esp32 dev board on BLE server and complier error at doc.containsKey("xxx"). Error message:'class ArduinoJson691_000::StaticJsonDocument<200u>' has no member named 'containsKey'. Please help. Thank you. // Default Arduino includes #include <Arduino.h> #include <WiFi.h> #include <nvs.h> #include <nvs_flash.h>
// Includes for JSON object handling // Requires ArduinoJson library // https://arduinojson.org // https://github.com/bblanchon/ArduinoJson #include <ArduinoJson.h>
// Includes for BLE #include <BLEUtils.h> #include <BLEServer.h> #include <BLEDevice.h> #include <BLEAdvertising.h> #include <Preferences.h> /** Build time */ const char compileDate[] = DATE " " TIME;
/** Unique device name / char apName[] = "ESP32-xxxxxxxxxxxx"; /* Selected network true = use primary network false = use secondary network / bool usePrimAP = true; /* Flag if stored AP credentials are available / bool hasCredentials = false; /* Connection status / volatile bool isConnected = false; /* Connection change status / bool connStatusChanged = false; /*
- Create unique device name from MAC address / void createName() { uint8_t baseMac[6]; // Get MAC address for WiFi station esp_read_mac(baseMac, ESP_MAC_WIFI_STA); // Write unique name into apName sprintf(apName, "ESP32-%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]); } // List of Service and Characteristic UUIDs #define SERVICE_UUID "0000aaaa-ead2-11e7-80c1-9a214cf093ae" #define WIFI_UUID "00005555-ead2-11e7-80c1-9a214cf093ae" / SSIDs of local WiFi networks / String ssidPrim; String ssidSec; /* Password for local WiFi network */ String pwPrim; String pwSec;
/** Characteristic for digital output / BLECharacteristic pCharacteristicWiFi; / BLE Advertiser / BLEAdvertising pAdvertising; /** BLE Service / BLEService pService; / BLE Server */ BLEServer *pServer;
/** Buffer for JSON string / // MAx size is 51 bytes for frame: // {"ssidPrim":"","pwPrim":"","ssidSec":"","pwSec":""} // + 4 x 32 bytes for 2 SSID's and 2 passwords //StaticJsonBuffer<200> jsonBuffer;//v5 StaticJsonDocument<200> doc;//Document:Buffer+root(doc) v6 /*
- MyServerCallbacks
- Callbacks for client connection and disconnection / class MyServerCallbacks: public BLEServerCallbacks { // TODO this doesn't take into account several clients being connected void onConnect(BLEServer pServer) { Serial.println("BLE client connected"); };
void onDisconnect(BLEServer* pServer) { Serial.println("BLE client disconnected"); pAdvertising->start(); } }; /**
-
MyCallbackHandler
-
Callbacks for BLE client read/write requests */ class MyCallbackHandler: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string value = pCharacteristic->getValue(); if (value.length() == 0) { return; } Serial.println("Received over BLE: " + String((char *)&value[0]));
// Decode data int keyIndex = 0; for (int index = 0; index < value.length(); index ++) { value[index] = (char) value[index] ^ (char) apName[keyIndex]; keyIndex++; if (keyIndex >= strlen(apName)) keyIndex = 0; }
/** Json object for incoming data */ //JsonObject& jsonIn = doc.parseObject((char *)&value[0]); DeserializationError jsonIn=deserializeJson(doc,(char *)&value[0]); if (jsonIn) { if (doc.containsKey("ssidPrim") && doc.containsKey("pwPrim") && doc.containsKey("ssidSec") && doc.containsKey("pwSec")) { ssidPrim = doc["ssidPrim"].as(); pwPrim = doc["pwPrim"].as(); ssidSec = doc["ssidSec"].as(); pwSec = doc["pwSec"].as();
Preferences preferences; preferences.begin("WiFiCred", false); preferences.putString("ssidPrim", ssidPrim); preferences.putString("ssidSec", ssidSec); preferences.putString("pwPrim", pwPrim); preferences.putString("pwSec", pwSec); preferences.putBool("valid", true); preferences.end(); Serial.println("Received over bluetooth:"); Serial.println("primary SSID: "+ssidPrim+" password: "+pwPrim); Serial.println("secondary SSID: "+ssidSec+" password: "+pwSec); connStatusChanged = true; hasCredentials = true;
} else if (doc.containsKey("erase")) { Serial.println("Received erase command"); Preferences preferences; preferences.begin("WiFiCred", false); preferences.clear(); preferences.end(); connStatusChanged = true; hasCredentials = false; ssidPrim = ""; pwPrim = ""; ssidSec = ""; pwSec = "";
int err; err=nvs_flash_init(); Serial.println("nvs_flash_init: " + err); err=nvs_flash_erase(); Serial.println("nvs_flash_erase: " + err);
} else if (doc.containsKey("reset")) { WiFi.disconnect(); esp_restart(); } } else { Serial.println("Received invalid JSON"); } jsonBuffer.clear(); };
void onRead(BLECharacteristic *pCharacteristic) { Serial.println("BLE onRead request"); String wifiCredentials;
/** Json object for outgoing data */
JsonObject& jsonOut = doc.createObject();
jsonOut["ssidPrim"] = ssidPrim;
jsonOut["pwPrim"] = pwPrim;
jsonOut["ssidSec"] = ssidSec;
jsonOut["pwSec"] = pwSec;
// Convert JSON object into a string
jsonOut.printTo(wifiCredentials);
// encode the data
int keyIndex = 0;
Serial.println("Stored settings: " + wifiCredentials);
for (int index = 0; index < wifiCredentials.length(); index ++) {
wifiCredentials[index] = (char) wifiCredentials[index] ^ (char) apName[keyIndex];
keyIndex++;
if (keyIndex >= strlen(apName)) keyIndex = 0;
}
pCharacteristicWiFi->setValue((uint8_t*)&wifiCredentials[0],wifiCredentials.length());
doc.clear();
} }; void setup() { // put your setup code here, to run once:
}
void loop() { // put your main code here, to run repeatedly:
}