Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/.pio
.pioenvs
.pio

.clang_complete
.gcc-flags.json
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sudo: false

language: python
python:
- "2.7"

cache:
directories:
- "~/.platformio"

install:
- pip install -U platformio
- platformio update

script:
- platformio ci -c platformio.ini --lib="./src" --lib="./lib/*" ./examples/simple/

notifications:
email:
on_success: never
on_failure: always
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
![Logo](http://svg.wiersma.co.za/github/project?lang=cpp&title=ConfigManager&tag=wifi%20configuration%20manager)

[![Build Status](https://travis-ci.com/nrwiersma/ConfigManager.svg?branch=master)](https://travis-ci.com/nrwiersma/ConfigManager)
[![arduino-library-badge](http://www.ardu-badge.com/badge/ConfigManager.svg)](http://www.ardu-badge.com/ConfigManager)

Wifi connection and configuration manager for ESP8266 and ESP32.
Expand Down Expand Up @@ -109,7 +110,7 @@ on the most common IDEs:

### Enabling

By default, ConfigManager runs in `DEBUG_MODE` off. This is to allow the serial iterface to communicate as needed.
By default, ConfigManager runs in `DEBUG_MODE` off. This is to allow the serial iterface to communicate as needed.
To turn on debugging, add the following line inside your `setup` routine:

```
Expand Down Expand Up @@ -295,7 +296,7 @@ ssid=access point&password=some password
```json
{
"ssid": "access point name",
"strength": *int*,
"strength": *int*,
"security": *bool*
}
```
Expand Down
20 changes: 20 additions & 0 deletions lib/ConfigParameter/BaseParameter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __BASE_PARAMETER_H__
#define __BASE_PARAMETER_H__

#include <ArduinoJson.h>
#include <DebugPrint.h>

enum ParameterMode { get, set, both};

/**
* Base Parameter
*/
class BaseParameter {
public:
virtual ParameterMode getMode() = 0;
virtual void fromJson(JsonObject *json) = 0;
virtual void toJson(JsonObject *json) = 0;
virtual void clearData() = 0;
};

#endif /* __BASE_PARAMETER_H__ */
44 changes: 44 additions & 0 deletions lib/ConfigParameter/ConfigParameter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef __CONFIG_PARAMETER_H__
#define __CONFIG_PARAMETER_H__

#include <BaseParameter.h>

/**
* Config Parameter
*/
template<typename T>
class ConfigParameter : public BaseParameter {
public:
ConfigParameter(const char *name, T *ptr, ParameterMode mode = both) {
this->name = name;
this->ptr = ptr;
this->mode = mode;
}

ParameterMode getMode() {
return this->mode;
}

void fromJson(JsonObject *json) {
if (json->containsKey(name) && json->is<T>(name)) {
*ptr = json->get<T>(name);
}
}

void toJson(JsonObject *json) {
json->set(name, *ptr);
}

void clearData() {
DebugPrint("Clearing: ");
DebugPrintln(name);
*ptr = T();
}

private:
const char *name;
T *ptr;
ParameterMode mode;
};

#endif /* __CONFIG_PARAMETER_H__ */
50 changes: 50 additions & 0 deletions lib/ConfigParameter/ConfigStringParameter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef __CONFIG_STRING_PARAMETER_H__
#define __CONFIG_STRING_PARAMETER_H__

#include <BaseParameter.h>

/**
* Config String Parameter
*/
class ConfigStringParameter : public BaseParameter {
public:
ConfigStringParameter(const char *name, char *ptr, size_t length, ParameterMode mode = both) {
this->name = name;
this->ptr = ptr;
this->length = length;
this->mode = mode;
}

ParameterMode getMode() {
return this->mode;
}

void fromJson(JsonObject *json) {
if (json->containsKey(name) && json->is<char *>(name)) {
const char * value = json->get<const char *>(name);

memset(ptr, NULL, length);
strncpy(ptr, const_cast<char*>(value), length - 1);
}
}

void toJson(JsonObject *json) {
json->set(name, ptr);
}

void clearData() {
DebugPrint("Clearing: ");
DebugPrintln(name);
memset(ptr, NULL, length);
}


private:
const char *name;
char *ptr;
size_t length;
ParameterMode mode;
};


#endif /* __CONFIG_STRING_PARAMETER_H__ */
9 changes: 9 additions & 0 deletions lib/DebugPrint/DebugPrint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __DEBUG_PRINT_H__
#define __DEBUG_PRINT_H__

extern bool DEBUG_MODE;

#define DebugPrintln(a) (DEBUG_MODE ? Serial.println(a) : false)
#define DebugPrint(a) (DEBUG_MODE ? Serial.print(a) : false)

#endif /* __DEBUG_PRINT_H__ */
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ConfigManager",
"version": "1.5",
"version": "1.5.1",
"keywords": "wifi, wi-fi, config",
"description": "WiFi connection manager for ESP8266 and ESP32",
"repository":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ConfigManager
version=1.5
version=1.5.1
author=Nick Wiersma <nick@wiersma.co.za>
maintainer=Nick Wiersma <nick@wiersma.co.za>
sentence=WiFi connection manager for ESP8266 and ESP32
Expand Down
28 changes: 28 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[platformio]
default_envs = nodemcuv2

[common]
lib_deps =
ArduinoJson@5.13.4

[env:nodemcuv2]
platform = https://github.com/platformio/platform-espressif8266.git
board = d1_mini
framework = arduino
monitor_speed = 115200
build_flags = -std=c++11, -Wl,-Teagle.flash.4m3m.ld, -D PIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH, -DARDUINO_ARCH_ESP8266
lib_ldf_mode = deep
board_build.f_flash = 80000000L
upload_speed = 115200
lib_deps = ${common.lib_deps}
test_filter = embedded

[env:esp32]
platform = https://github.com/platformio/platform-espressif32.git
board = esp-wrover-kit
framework = arduino
board_build.partitions = noota_3g.csv
build_flags = -std=c++11, -DARDUINO_ARCH_ESP32
lib_ldf_mode = deep
lib_deps = ${common.lib_deps}
test_filter = embedded
24 changes: 12 additions & 12 deletions src/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const byte DNS_PORT = 53;
const char magicBytes[MAGIC_LENGTH] = {'C', 'M'};
const char magicBytesEmpty[MAGIC_LENGTH] = {NULL, NULL};
const char magicBytesEmpty[MAGIC_LENGTH] = {'\0', '\0'};

const char mimeHTML[] PROGMEM = "text/html";
const char mimeJSON[] PROGMEM = "application/json";
Expand Down Expand Up @@ -128,7 +128,7 @@ void ConfigManager::handleAPPost() {
storeWifiSettings(ssid, password, false);

server->send(204, FPSTR(mimePlain), F("Saved. Will attempt to reboot."));

ESP.restart();
}

Expand All @@ -148,7 +148,7 @@ void ConfigManager::handleScanGet() {
for (int i = 0; i < n; ++i) {
String ssid = WiFi.SSID(i);
int rssi = WiFi.RSSI(i);
String security = WiFi.encryptionType(i) == ENC_TYPE_NONE ? "none" : "enabled";
String security = WiFi.encryptionType(i) == WIFI_OPEN ? "none" : "enabled";

DebugPrint("Name: ");
DebugPrint(ssid);
Expand Down Expand Up @@ -215,12 +215,12 @@ void ConfigManager::handleRESTPut() {
void ConfigManager::handleNotFound() {
String URI = toStringIP(server->client().localIP()) + String(":") + String(webPort);
String header = server->hostHeader();

if ( !isIp(header) && header != URI) {
DebugPrint(F("Unknown URL: "));
DebugPrintln(header);
server->sendHeader("Location", String("http://") + URI, true);
server->send(302, FPSTR(mimePlain), "");
server->send(302, FPSTR(mimePlain), "");
// Empty content inhibits Content-length header so we have to close the socket ourselves.
server->client().stop();
return;
Expand Down Expand Up @@ -298,13 +298,13 @@ void ConfigManager::startAP() {

WiFi.mode(WIFI_AP);
WiFi.softAP(apName, apPassword);

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

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

DebugPrint("AP Name: ");
DebugPrintln(apName);

Expand Down Expand Up @@ -348,7 +348,7 @@ void ConfigManager::createBaseWebServer() {
server.reset(new WebServer(this->webPort));
DebugPrint(F("Webserver enabled on port: "));
DebugPrintln(webPort);

server->collectHeaders(headerKeys, headerKeysSize);

server->on("/", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleAPGet, this));
Expand All @@ -361,8 +361,8 @@ void ConfigManager::createBaseWebServer() {
void ConfigManager::clearWifiSettings(bool reboot) {
char ssid[SSID_LENGTH];
char password[PASSWORD_LENGTH];
memset(ssid, NULL, SSID_LENGTH);
memset(password, NULL, PASSWORD_LENGTH);
memset(ssid, 0, SSID_LENGTH);
memset(password, 0, PASSWORD_LENGTH);

DebugPrintln(F("Clearing WiFi connection."));
storeWifiSettings(ssid, password, true);
Expand Down Expand Up @@ -429,7 +429,7 @@ void ConfigManager::writeConfig() {
}

boolean ConfigManager::isIp(String str) {
for (int i = 0; i < str.length(); i++) {
for (uint i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (c != '.' && (c < '0' || c > '9')) {
return false;
Expand Down
Loading