I am using an ESP32 module for bluetooth connection and scaning wifi network. But I cannot format the json using arduinojson library in the way I want.
void scanWifiNetworks() {
//creating json
DynamicJsonDocument jsonDoc(1024);
char jsonBuffer[512];
JsonObject wifiJsonObject = jsonDoc.createNestedObject("wifiList");
wifiJsonObject["ssid"];
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
// Serial.print(i + 1);
// Serial.print(": ");
// Serial.print(WiFi.SSID(i));
// Serial.print(" (");
// Serial.print(WiFi.RSSI(i));
// Serial.print(")");
// Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
wifiJsonObject["ssid"] = WiFi.SSID(i);
serializeJsonPretty(jsonDoc, SerialBT);
serializeJsonPretty(jsonDoc, Serial);
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
delay(60000);
}
The output is like this
scan start
scan done
5 networks found
{
"wifiList": {
"ssid": "quark"
}
}{
"wifiList": {
"ssid": "Airtel_Zerotouch"
}
}{
"wifiList": {
"ssid": "NETGEAR77"
}
}{
"wifiList": {
"ssid": "Itobuz-technologies"
}
}{
"wifiList": {
"ssid": "Nokia 6.1"
}
}
what I want is something like that
{ "wifiList": { "ssid1": "quark1", "ssid2": "quark2" } }
I know I am looping in a wrong way but could not figure it out. if possible please pointing out the right direction will help a lot.
createNestedArray("wifiList")