From d6b7742cbca35fd0c6272203bf6c96eb9066752d Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Fri, 11 Nov 2016 16:41:27 -0800 Subject: [PATCH] Initial implementation of WiFiManager --- src/Portal.h | 1 - src/Thing.h | 3 ++ src/Transcriber.h | 1 - src/thing/Portal.cpp | 4 ++- src/thing/WiFiManager.cpp | 69 +++++++++++++++++++++++++++++++++++++++ src/thing/WiFiManager.h | 31 ++++++++++++++++++ 6 files changed, 106 insertions(+), 3 deletions(-) delete mode 100644 src/Portal.h create mode 100644 src/Thing.h delete mode 100644 src/Transcriber.h create mode 100644 src/thing/WiFiManager.cpp create mode 100644 src/thing/WiFiManager.h diff --git a/src/Portal.h b/src/Portal.h deleted file mode 100644 index 90a29176..00000000 --- a/src/Portal.h +++ /dev/null @@ -1 +0,0 @@ -#include "thing/Portal.h" diff --git a/src/Thing.h b/src/Thing.h new file mode 100644 index 00000000..6e4e09ca --- /dev/null +++ b/src/Thing.h @@ -0,0 +1,3 @@ +#include "thing/Transcriber.h" +#include "thing/Portal.h" +#include "thing/WiFiManager.h" diff --git a/src/Transcriber.h b/src/Transcriber.h deleted file mode 100644 index 0fd0ece7..00000000 --- a/src/Transcriber.h +++ /dev/null @@ -1 +0,0 @@ -#include "thing/Transcriber.h" diff --git a/src/thing/Portal.cpp b/src/thing/Portal.cpp index bcc2a2c1..8135ba8c 100644 --- a/src/thing/Portal.cpp +++ b/src/thing/Portal.cpp @@ -9,7 +9,7 @@ Portal::Portal(const Config& config) debug_([](const char*){}) {} void Portal::SetDebugHandler(std::function handler) { - debug_ = handler; + debug_ = std::move(handler); } void Portal::Start() { @@ -150,6 +150,8 @@ void Portal::Start() { entry["ssid"] = WiFi.SSID(i); entry["rssi"] = WiFi.RSSI(i); } + // Free station info from memory. + WiFi.scanDelete(); String buffer; data.printTo(buffer); diff --git a/src/thing/WiFiManager.cpp b/src/thing/WiFiManager.cpp new file mode 100644 index 00000000..763c06ce --- /dev/null +++ b/src/thing/WiFiManager.cpp @@ -0,0 +1,69 @@ +#include "thing/WiFiManager.h" + +#include +#include + + +namespace thing { +namespace { + +const short kDnsPort = 53; + +} // namespace + +WiFiManager::WiFiManager() : debug_([](const char*){}) {} + +bool WiFiManager::StartAP() { + // Use arduino string, casting an int to std::string with arduino C++ support + // isn't easy. + String ssid("FireThing-"); + ssid += ESP.getChipId(); + + WiFi.mode(WIFI_AP_STA); + if (!WiFi.softAP(ssid.c_str())) { + return false; + } + + debug_((String("WiFi AP : ") + ssid).c_str()); + debug_((String("Wifi AP IP : ") + WiFi.softAPIP().toString()).c_str()); + + dns_.reset(new DNSServer()); + dns_->start(kDnsPort, "*", WiFi.softAPIP()); + dns_->setTTL(30); + + ap_up_ = true; + return true; +} + +bool WiFiManager::StopAP() { + if (!WiFi.softAPdisconnect()) { + return false; + } + dns_->stop(); + dns_.reset(nullptr); + + ap_up_ = false; + return true; +} + +void WiFiManager::Loop() { + if (dns_) { + dns_->processNextRequest(); + } +} + +bool WiFiManager::Connect(const std::string& ssid, const std::string& auth) { + auto status = WiFi.begin(ssid.c_str(), auth.c_str()); + + return status != WL_CONNECT_FAILED; +} + +bool WiFiManager::connected() const { + return WiFi.status() == WL_CONNECTED; +} + +void WiFiManager::SetDebugHandler(std::function handler) { + debug_ = std::move(handler); +} + +}; diff --git a/src/thing/WiFiManager.h b/src/thing/WiFiManager.h new file mode 100644 index 00000000..b8f63d7e --- /dev/null +++ b/src/thing/WiFiManager.h @@ -0,0 +1,31 @@ +#ifndef THING_WIFI_MANAGER_H +#define THING_WIFI_MANAGER_H + +#include +#include + +namespace thing { + +class WiFiManager { + public: + WiFiManager(); + + bool StartAP(); + bool StopAP(); + bool Connect(const std::string& ssid, const std::string& auth); + + void Loop(); + + bool connected() const; + bool ap_up() const { return ap_up_; } + + void SetDebugHandler(std::function handler); + private: + bool ap_up_ = false; + std::unique_ptr dns_; + std::function debug_; +}; + +}; + +#endif // THING_WIFI_MANAGER_H