diff --git a/src/thing/Portal.cpp b/src/thing/Portal.cpp index 37bfc44d..bcc2a2c1 100644 --- a/src/thing/Portal.cpp +++ b/src/thing/Portal.cpp @@ -3,7 +3,14 @@ namespace thing { -Portal::Portal(const Config& config) : config_(config), callback_([](const Config&){}) {} +Portal::Portal(const Config& config) + : config_(config), + callback_([](const Config&){}), + debug_([](const char*){}) {} + +void Portal::SetDebugHandler(std::function handler) { + debug_ = handler; +} void Portal::Start() { server_.on("/", [&] () { @@ -50,13 +57,44 @@ void Portal::Start() { } } } + function scan() { + document.getElementById('scanning').style.display='inline'; + var xhr = new XMLHttpRequest(); + xhr.open("GET", "/wifi/scan", true); + xhr.onreadystatechange = function () { + if (xhr.readyState==4 && xhr.status==200) { + load_networks(JSON.parse(xhr.responseText)); + document.getElementById('scanning').style.display='none'; + } + } + xhr.send(); + } + function load_networks(networks) { + select = document.getElementById('networks'); + select.innerHtml = ''; + networks.sort(function(a, b) { + return b.rssi - a.rssi; + }); + networks.forEach(function(network) { + option = document.createElement('option'); + option.value = network.ssid; + option.text = network.ssid + ' (' + network.rssi + ')'; + select.add(option); + }); + select.style.display='inline'; + } + function set_network(select) { + document.getElementById('ssid').value = select[select.selectedIndex].value; + }
Host:
Auth:
Path:
-
Wifi SSID:
+
Wifi SSID:
+ +
Wifi Key:
Loading....
@@ -67,6 +105,7 @@ void Portal::Start() { static const PROGMEM char type[] = "text/html"; server_.send_P(200, type, page); + debug_("served root page."); }); server_.on("/config", [&] () { @@ -82,9 +121,11 @@ void Portal::Start() { String buffer; root.printTo(buffer); server_.send(200, "application/json", buffer); + debug_("config retrieved"); } else if (server_.method() == HTTP_POST) { if (!server_.hasArg("config")) { server_.send(500, "text/plain", "Missing config.\r\n"); + debug_("Config updated called without param."); return; } String config = server_.arg("config"); @@ -96,9 +137,28 @@ void Portal::Start() { config_.wifi_key = root["wifi_key"].asString(); callback_(config_); server_.send(200, "text/plain", ""); + debug_("config updated."); + } + }); + + server_.on("/wifi/scan", [&] () { + int net_count = WiFi.scanNetworks(); + DynamicJsonBuffer json_buffer; + JsonArray& data = json_buffer.createArray(); + for (int i=0; i < net_count; i++) { + JsonObject& entry = data.createNestedObject(); + entry["ssid"] = WiFi.SSID(i); + entry["rssi"] = WiFi.RSSI(i); } + + String buffer; + data.printTo(buffer); + server_.send(200, "application/json", buffer); + debug_("served networks."); }); + server_.begin(); + debug_("Portal started."); } void Portal::Loop() { diff --git a/src/thing/Portal.h b/src/thing/Portal.h index 340902e7..7fcd6de1 100644 --- a/src/thing/Portal.h +++ b/src/thing/Portal.h @@ -14,10 +14,13 @@ class Portal { void Loop(); void NotifyOnUpdate(std::function cb); + void SetDebugHandler(std::function handler); + private: Config config_; ESP8266WebServer server_; std::function callback_; + std::function debug_; }; diff --git a/src/thing/Transcriber.cpp b/src/thing/Transcriber.cpp index 4c1e19ea..bd967149 100644 --- a/src/thing/Transcriber.cpp +++ b/src/thing/Transcriber.cpp @@ -1,5 +1,5 @@ - #include "thing/Transcriber.h" +#include namespace thing { namespace { @@ -37,6 +37,10 @@ void Transcriber::SetValue(const std::string& path, const std::string& value) { } void Transcriber::Loop() { + if (WiFi.status() != WL_CONNECTED) { + return; + } + // Read incoming data if (stream_->available()) { std::string event_str;