Skip to content

Commit

Permalink
Merge pull request #3 from leegang/master
Browse files Browse the repository at this point in the history
Upgrade to ArduinoJson V6.
  • Loading branch information
snakeye authored Sep 10, 2020
2 parents 8ae7bb6 + 15ce915 commit 119bf38
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 109 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ and REST variable configuration.

## Requires

* [ArduinoJson 5.x](https://github.com/bblanchon/ArduinoJson)
* [ArduinoJson 6.x](https://github.com/bblanchon/ArduinoJson)

## Quick Start

Expand Down Expand Up @@ -97,7 +97,7 @@ automatically by the JavaScript application.

## Things TODO / Roadmap

* [ ] Migrate to ArduinoJSON v6
* [x] Migrate to ArduinoJSON v6
* [ ] Eliminate parallel HTTP requests
* [ ] Proper WiFi connection handling
* [ ] HTTP Authentication
Expand Down
10 changes: 5 additions & 5 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "WiFiConfig",
"version": "2.1.0",
"version": "2.2.0",
"keywords": "wifi, wi-fi, config, rest, vuejs",
"description": "WiFi connection and configuration manager for ESP8266 and ESP32",
"repository": {
"type": "git",
"url": "https://github.com/snakeye/WifiConfig.git"
"url": "https://github.com/leegang/WifiConfig.git"
},
"authors": {
"name": "Andrey Ovcharov",
"email": "andrew.ovcharov@gmail.com"
"name": "leegang",
"email": "bestmrlee@gmail.com"
},
"frameworks": "arduino",
"platforms": [
Expand All @@ -18,7 +18,7 @@
],
"dependencies": [{
"name": "ArduinoJson",
"version": "^5.0.0",
"version": "^6.0.0",
"frameworks": "arduino"
}]
}
24 changes: 14 additions & 10 deletions src/ConfigParameterGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,48 @@

void ConfigParameterGroup::toJson(JsonObject *json)
{
JsonObject &obj = json->createNestedObject(this->name);

std::list<ConfigParameterInterface *>::iterator it;
for (it = parameters.begin(); it != parameters.end(); ++it)
{
(*it)->toJson(&obj);
(*it)->toJson(json);
}
}

void ConfigParameterGroup::toJsonSchema(JsonObject *json)
{
json->set("name", name);
json->getOrAddMember(name).set((const char*) name);
if (this->metadata != NULL)
{
json->set("label", this->metadata->label());
// json->set("label", this->metadata->label());
json->getOrAddMember('label').set((const char*) this->metadata->label());

if (this->metadata->description() != NULL)
{
json->set("description", this->metadata->description());
// json->set("description", this->metadata->description());
json->getOrAddMember('description').set((const char*) this->metadata->description());

}
}

JsonArray &params = json->createNestedArray("params");
JsonArray params = json->createNestedArray("params");
std::list<ConfigParameterInterface *>::iterator it;
for (it = parameters.begin(); it != parameters.end(); ++it)
{
JsonObject &obj = params.createNestedObject();
JsonObject obj = params.createNestedObject();
(*it)->toJsonSchema(&obj);
}
}

void ConfigParameterGroup::fromJson(JsonObject *json)
{
if (json->containsKey(name) && json->is<JsonObject>(name))
if (json->containsKey(name) && json->getMember(name).is<JsonObject>())
{
JsonVariant var = json->get<JsonVariant>(name);
// JsonVariant var = json->get<JsonVariant>(name);
JsonVariant var = json->getMember(name).as<JsonVariant>();

JsonObject &group = var.asObject();
JsonObject group;
serializeJsonPretty(var,group);

std::list<ConfigParameterInterface *>::iterator it;
for (it = parameters.begin(); it != parameters.end(); ++it)
Expand Down
109 changes: 63 additions & 46 deletions src/WiFiConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,24 @@ void ConfigManager::save()
* @param jsonString
* @return JsonObject&
*/
JsonObject &ConfigManager::decodeJson(String jsonString)
JsonObject ConfigManager::decodeJson(String jsonString)
{
DynamicJsonBuffer jsonBuffer;
DynamicJsonDocument doc(1024);

if (jsonString.length() == 0)
{
return jsonBuffer.createObject();
return doc.as<JsonObject>();
}

JsonObject &obj = jsonBuffer.parseObject(jsonString);

if (!obj.success())
auto error = deserializeJson(doc, jsonString);
if (error)
{
return jsonBuffer.createObject();
Serial.println(F("deserializeJson() failed with code "));
Serial.println(error.c_str());
return doc.as<JsonObject>();
}

return obj;
return doc.as<JsonObject>();
}

/**
Expand Down Expand Up @@ -146,34 +147,41 @@ void ConfigManager::handleReboot()
*/
void ConfigManager::handleGetWifi()
{
DynamicJsonBuffer jsonBuffer;
JsonObject &res = jsonBuffer.createObject();
DynamicJsonDocument jsonBuffer(1024);
JsonObject res = jsonBuffer.as<JsonObject>();

// WiFi mode
switch (WiFi.getMode())
{
case WIFI_OFF:
res.set("mode", "off");
// res.set("mode", "off");
res.getOrAddMember("mode").set("off");
break;
case WIFI_STA:
res.set("mode", "sta");
// res.set("mode", "sta");
res.getOrAddMember("mode").set("sta");
break;
case WIFI_AP:
res.set("mode", "ap");
// res.set("mode", "ap");
res.getOrAddMember("mode").set("ap");
break;
case WIFI_AP_STA:
res.set("mode", "ap_sta");
// res.set("mode", "ap_sta");
res.getOrAddMember("mode").set("ap_sta");
break;
default:
res.set("mode", "");
// res.set("mode", "");
res.getOrAddMember("mode").set("");
break;
}

// connection status
res.set("connected", WiFi.isConnected());
// res.set("connected", WiFi.isConnected());
res.getOrAddMember("connected").set(WiFi.isConnected());

String body;
res.printTo(body);
// res.printTo(body);
serializeJson(res, body);

server->send(200, FPSTR(mimeJSON), body);
}
Expand All @@ -184,8 +192,9 @@ void ConfigManager::handleGetWifi()
*/
void ConfigManager::handleGetWifiScan()
{
DynamicJsonBuffer jsonBuffer;
JsonArray &res = jsonBuffer.createArray();
DynamicJsonDocument doc(1024);
JsonArray jsonArray = doc.createNestedArray();
// JsonObject res = jsonBuffer.as<JsonObject>();

static long lastScan = 0;
const long scanPeriod = 5000;
Expand All @@ -203,18 +212,18 @@ void ConfigManager::handleGetWifiScan()
{
for (int i = 0; i < n; i++)
{
JsonObject &obj = res.createNestedObject();

obj.set("ssid", WiFi.SSID(i));
obj.set("channel", WiFi.channel(i));
obj.set("rssi", WiFi.RSSI(i));
obj.set("open", WiFi.encryptionType(i) == ENC_TYPE_NONE);
JsonObject obj = doc.createNestedObject();
obj["ssid"] = WiFi.SSID(i);
obj["channel"] = WiFi.channel(i);
obj["strength"] = WiFi.RSSI(i);
obj["open"] = (WiFi.encryptionType(i) == WIFI_AUTH_OPEN);
jsonArray.add(obj);
}
}

String body;
res.printTo(body);

// res.printTo(body);
serializeJson(jsonArray, body);
server->send(200, FPSTR(mimeJSON), body);
}

Expand All @@ -232,10 +241,10 @@ void ConfigManager::handlePostConnect()

if (isJson)
{
JsonObject &obj = this->decodeJson(server->arg("plain"));
JsonObject obj = decodeJson(server->arg("plain"));

ssid = obj.get<String>("ssid");
password = obj.get<String>("password");
ssid = obj.getMember("ssid").as<String>();
password = obj.getMember("password").as<String>();
}
else
{
Expand Down Expand Up @@ -292,18 +301,19 @@ void ConfigManager::handlePostDisconnect()
*/
void ConfigManager::handleGetSettingsSchema()
{
DynamicJsonBuffer jsonBuffer;
JsonArray &res = jsonBuffer.createArray();
DynamicJsonDocument jsonBuffer(1024);
JsonArray res = jsonBuffer.createNestedArray();

std::list<ConfigParameterGroup *>::iterator it;
for (it = groups.begin(); it != groups.end(); ++it)
{
JsonObject &obj = res.createNestedObject();
JsonObject obj = res.createNestedObject();
(*it)->toJsonSchema(&obj);
}

String body;
res.printTo(body);
// res.printTo(body);
serializeJson(res, body);

server->send(200, FPSTR(mimeJSON), body);
}
Expand All @@ -313,8 +323,8 @@ void ConfigManager::handleGetSettingsSchema()
*/
void ConfigManager::handleGetSettings()
{
DynamicJsonBuffer jsonBuffer;
JsonObject &res = jsonBuffer.createObject();
DynamicJsonDocument jsonBuffer(1024);
JsonObject res = jsonBuffer.as<JsonObject>();

std::list<ConfigParameterGroup *>::iterator it;
for (it = groups.begin(); it != groups.end(); ++it)
Expand All @@ -323,8 +333,8 @@ void ConfigManager::handleGetSettings()
}

String body;
res.printTo(body);

// res.printTo(body);
serializeJson(res, body);
server->send(200, FPSTR(mimeJSON), body);
}

Expand All @@ -334,8 +344,10 @@ void ConfigManager::handleGetSettings()
*/
void ConfigManager::handlePostSettings()
{
JsonObject &obj = this->decodeJson(server->arg("plain"));
if (!obj.success())
DynamicJsonDocument doc(1024);
JsonObject obj = this->decodeJson(server->arg("plain"));
auto error = deserializeJson(doc, server->arg("plain"));
if (error)
{
server->send(400, FPSTR(mimeJSON), "");
return;
Expand Down Expand Up @@ -534,13 +546,12 @@ void ConfigManager::startAP()
delay(500);

WiFi.mode(WIFI_AP);

WiFi.softAPConfig(IPAddress(192, 168, 1, 1), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));

IPAddress ip(192, 168, 1, 1);
IPAddress NMask(255, 255, 255, 0);
WiFi.softAP(apName, apPassword);

delay(1000); // Need to wait to get IP

WiFi.softAPConfig(ip, ip, NMask);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Expand All @@ -560,15 +571,21 @@ void ConfigManager::startAP()
void ConfigManager::startApi(const char *ssid)
{
mode = MODE_API;

char hname[19];
Serial.print(F("Connected to "));
Serial.print(ssid);
Serial.print(F(" with IP "));
Serial.println(WiFi.localIP());

WiFi.mode(WIFI_STA);
WiFi.setHostname(hname);
// //
WiFi.config(0u, 0u, 0u);
WiFi.begin();

// (void)wifi_station_dhcpc_start();


wifi_station_dhcpc_start();
}

/**
Expand Down
Loading

0 comments on commit 119bf38

Please sign in to comment.