From e6b5761b9560802a3d9bd6db518c5fa188fde13f Mon Sep 17 00:00:00 2001 From: Nicholas Wiersma Date: Thu, 12 Dec 2019 06:55:30 +0200 Subject: [PATCH] Fix ConfigManager Bugs (#66) --- .gitignore | 1 + .travis.yml | 25 ++++++++----------------- README.md | 5 +++-- src/ConfigManager.cpp | 24 ++++++++++++------------ src/ConfigManager.h | 12 +++++++++--- 5 files changed, 33 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 7558808..cc2351c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/.pio .pioenvs .pio diff --git a/.travis.yml b/.travis.yml index 785345f..1222d6c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,23 +1,9 @@ -# Continuous Integration (CI) is the practice, in software -# engineering, of merging all developer working copies with a shared mainline -# several times a day < https://docs.platformio.org/page/ci/index.html > -# -# Documentation: -# -# * Travis CI Embedded Builds with PlatformIO -# < https://docs.travis-ci.com/user/integration/platformio/ > -# -# * PlatformIO integration with Travis CI -# < https://docs.platformio.org/page/ci/travis.html > -# -# * User Guide for `platformio ci` command -# < https://docs.platformio.org/page/userguide/cmd_ci.html > -# +sudo: false + language: python python: - - "3.7.5" + - "2.7" -sudo: false cache: directories: - "~/.platformio" @@ -28,3 +14,8 @@ install: script: - platformio ci --lib="./src" -c platformio.ini ./examples/simple/ + +notifications: + email: + on_success: never + on_failure: always diff --git a/README.md b/README.md index fcbf443..ee7d96e 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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: ``` @@ -295,7 +296,7 @@ ssid=access point&password=some password ```json { "ssid": "access point name", - "strength": *int*, + "strength": *int*, "security": *bool* } ``` diff --git a/src/ConfigManager.cpp b/src/ConfigManager.cpp index ff024c6..c56f07f 100644 --- a/src/ConfigManager.cpp +++ b/src/ConfigManager.cpp @@ -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"; @@ -128,7 +128,7 @@ void ConfigManager::handleAPPost() { storeWifiSettings(ssid, password, false); server->send(204, FPSTR(mimePlain), F("Saved. Will attempt to reboot.")); - + ESP.restart(); } @@ -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); @@ -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; @@ -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); @@ -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)); @@ -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); @@ -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; diff --git a/src/ConfigManager.h b/src/ConfigManager.h index 25aa2d7..5363a51 100644 --- a/src/ConfigManager.h +++ b/src/ConfigManager.h @@ -18,6 +18,12 @@ #include #include "ArduinoJson.h" +#if defined(ARDUINO_ARCH_ESP8266) //ESP8266 + #define WIFI_OPEN ENC_TYPE_NONE +#elif defined(ARDUINO_ARCH_ESP32) //ESP32 + #define WIFI_OPEN WIFI_AUTH_OPEN +#endif + #define MAGIC_LENGTH 2 #define SSID_LENGTH 32 #define PASSWORD_LENGTH 64 @@ -111,7 +117,7 @@ class ConfigStringParameter : public BaseParameter { if (json->containsKey(name) && json->is(name)) { const char * value = json->get(name); - memset(ptr, NULL, length); + memset(ptr, 0, length); strncpy(ptr, const_cast(value), length - 1); } } @@ -123,9 +129,9 @@ class ConfigStringParameter : public BaseParameter { void clearData() { DebugPrint("Clearing: "); DebugPrintln(name); - memset(ptr, NULL, length); + memset(ptr, 0, length); } - + private: const char *name;