Skip to content

Commit

Permalink
Add save method and allow JSON on SSID endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma committed Aug 22, 2016
1 parent 3c0f1dc commit 715f571
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 16 deletions.
49 changes: 47 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ void begin(T &config)
> Starts the configuration manager. The config parameter will be saved into
> and retrieved from the EEPROM.
### save
```
void save()
```
> Saves the config passed to the begin function to the EEPROM.
### loop
```
void loop()
Expand All @@ -98,14 +104,53 @@ void loop()

> Gets the HTML page that is used to set the Wifi SSID and password.
+ Response 200 *(text/html)*

### POST /

> Sets the Wifi SSID and password. The form example can be found in the ```data``` directory.
+ Request *(application/x-www-form-urlencoded)*

```
ssid=access point&password=some password
```

+ Request *(application/json)*

```json
{
"ssid": "access point",
"password": "some password"
}
```

### GET /settings

> Gets the settings set in ```addParameter```. The response type is ```application/json```.
> Gets the settings set in ```addParameter```.
+ Response 200 *(application/json)*

```json
{
"enabled": true,
"hour": 3
}
```

### PUT /settings

> Sets the settings set in ```addParameter```. The request type is ```application/json```.
> Sets the settings set in ```addParameter```.
+ Request *(application/json)*

```json
{
"enabled": false,
"hour": 4
}
```

+ Response 400 *(application/json)*

+ Response 204 *(application/json)*
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ setAPFilename KEYWORD2
addParameter KEYWORD2
begin KEYWORD2
loop KEYWORD2
save KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
9 changes: 8 additions & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@
},
"frameworks": "arduino",
"platforms": "espressif",
"version": "0.2"
"version": "0.3",
"dependencies":[
{
"name": "Json",
"frameworks": "arduino",
"platforms": "*",
}
]
}
3 changes: 2 additions & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name=ConfigManager
version=0.2
version=0.3
author=Nick Wiersma <[email protected]>
maintainer=Nick Wiersma <[email protected]>
sentence=ESP8266 WiFi connection manager
paragraph=Library for configuring ESP8266 modules WiFi credentials at runtime.
category=Communication
url=https://github.com/nrwiersma/ConfigManager.git
architectures=esp8266
includes=ConfigManager.h
55 changes: 44 additions & 11 deletions src/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ void ConfigManager::loop() {
}
}

void ConfigManager::save() {
this->writeConfig();
}

JsonObject &ConfigManager::decodeJson(String jsonString)
{
DynamicJsonBuffer jsonBuffer;

if (jsonString.length() == 0) {
return jsonBuffer.createObject();
}

JsonObject& obj = jsonBuffer.parseObject(jsonString);

if (!obj.success()) {
return jsonBuffer.createObject();
}

return obj;
}

void ConfigManager::handleAPGet() {
SPIFFS.begin();

Expand All @@ -33,11 +54,22 @@ void ConfigManager::handleAPGet() {
}

void ConfigManager::handleAPPost() {
String ssid = server->arg("ssid");
String password = server->arg("password");
bool isJson = server->header("Content-Type") == "application/json";
String ssid;
String password;
char ssidChar[32];
char passwordChar[64];

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

ssid = obj.get<String>("ssid");
password = obj.get<String>("password");
} else {
ssid = server->arg("ssid");
password = server->arg("password");
}

if (ssid.length() == 0 || password.length() == 0) {
server->send(400, "text/plain", "Invalid ssid or password.");
return;
Expand Down Expand Up @@ -71,14 +103,7 @@ void ConfigManager::handleRESTGet() {
}

void ConfigManager::handleRESTPut() {
String json = server->arg("plain");
if (json.length() == 0) {
server->send(400, "application/json", "");
return;
}

DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject(json);
JsonObject& obj = this->decodeJson(server->arg("plain"));
if (!obj.success()) {
server->send(400, "application/json", "");
return;
Expand All @@ -91,7 +116,7 @@ void ConfigManager::handleRESTPut() {

writeConfig();

server->send(200, "application/json", "");
server->send(204, "application/json", "");
}

void ConfigManager::handleNotFound() {
Expand Down Expand Up @@ -153,6 +178,9 @@ void ConfigManager::setup() {
}

void ConfigManager::startAP() {
const char* headerKeys[] = {"Content-Type"};
size_t headerKeysSize = sizeof(headerKeys)/sizeof(char*);

Serial.println("Starting Access Point");

IPAddress ip(192, 168, 1, 1);
Expand All @@ -167,6 +195,7 @@ void ConfigManager::startAP() {
dnsServer.start(DNS_PORT, "*", ip);

server.reset(new ESP8266WebServer(80));
server->collectHeaders(headerKeys, headerKeysSize);
server->on("/", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleAPGet, this));
server->on("/", HTTPMethod::HTTP_POST, std::bind(&ConfigManager::handleAPPost, this));
server->onNotFound(std::bind(&ConfigManager::handleNotFound, this));
Expand All @@ -179,7 +208,11 @@ void ConfigManager::startAP() {
}

void ConfigManager::startApi() {
const char* headerKeys[] = {"Content-Type"};
size_t headerKeysSize = sizeof(headerKeys)/sizeof(char*);

server.reset(new ESP8266WebServer(80));
server->collectHeaders(headerKeys, headerKeysSize);
server->on("/", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleAPGet, this));
server->on("/", HTTPMethod::HTTP_POST, std::bind(&ConfigManager::handleAPPost, this));
server->on("/settings", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleRESTGet, this));
Expand Down
5 changes: 4 additions & 1 deletion src/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ConfigParameter : public BaseParameter {
*/
class ConfigManager {
public:
ConfigManager() {};
ConfigManager() {}

void setAPName(const char *name);
void setAPFilename(const char *filename);
Expand All @@ -76,6 +76,7 @@ class ConfigManager {
void addParameter(const char *name, T *variable) {
parameters.push_back(new ConfigParameter<T>(name, variable));
}
void save();

private:
void *config;
Expand All @@ -85,6 +86,8 @@ class ConfigManager {
std::unique_ptr<ESP8266WebServer> server;
std::list<BaseParameter*> parameters;

JsonObject &decodeJson(String jsonString);

void handleAPGet();
void handleAPPost();
void handleRESTGet();
Expand Down

0 comments on commit 715f571

Please sign in to comment.