// // // #include "accesspoint.h" ESP8266WebServer accesspoint::server(80); Stream* accesspoint::debugger; bool accesspoint::hasConfig() { return config.hasConfig(); } void accesspoint::setup(int accessPointButtonPin, Stream& debugger) { this->debugger = &debugger; // Test if we're missing configuration if (!config.hasConfig()) { print("No config. We're booting as AP. Look for SSID "); println(this->AP_SSID); isActivated = true; } else { // Load the configuration config.load(); if (this->debugger) config.print(debugger); // Test if we're holding down the AP pin, over 5 seconds int time = millis() + 5000; print("Press the AP button now to boot as access point"); while (millis() < time) { print("."); if (digitalRead(accessPointButtonPin) == LOW) { println(""); print("AP button was pressed. Booting as access point now. Look for SSID "); println(this->AP_SSID); isActivated = true; break; } delay(100); } } if (isActivated) { // Setup AP WiFi.disconnect(true); WiFi.softAPdisconnect(true); WiFi.mode(WIFI_OFF); delay(2000); WiFi.softAP(AP_SSID); WiFi.mode(WIFI_AP); /* Setup the DNS server redirecting all the domains to this IP */ dnsServer.setErrorReplyCode(DNSReplyCode::NoError); dnsServer.start(DNS_PORT, "*", WiFi.softAPIP()); server.on("/", handleRoot); server.on("/save", handleSave); server.begin(); // Web server start print("Web server is ready for config at http://"); print(WiFi.softAPIP()); println("/"); } } bool accesspoint::loop() { if (isActivated) { //DNS dnsServer.processNextRequest(); //HTTP server.handleClient(); return true; } else { return false; } } /** Handle root or redirect to captive portal */ void accesspoint::handleRoot() { println("Serving / over http..."); server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. String html = String("\r\n\r\n
\r\n\t\r\n\r\n\r\n\r\n\t\r\n\r\n\t\r\n\r\n"); server.sendContent(html); server.client().stop(); // Stop is needed because we sent no content length } void accesspoint::handleSave() { configuration *config = new configuration(); String temp; temp = server.arg("ssid"); config->ssid = new char[temp.length() + 1]; temp.toCharArray(config->ssid, temp.length() + 1, 0); temp = server.arg("ssidPassword"); config->ssidPassword = new char[temp.length() + 1]; temp.toCharArray(config->ssidPassword, temp.length() + 1, 0); config->meterType = (byte)server.arg("meterType").toInt(); temp = server.arg("mqtt"); config->mqtt = new char[temp.length() + 1]; temp.toCharArray(config->mqtt, temp.length() + 1, 0); config->mqttPort = (int)server.arg("mqttPort").toInt(); temp = server.arg("mqttClientID"); config->mqttClientID = new char[temp.length() + 1]; temp.toCharArray(config->mqttClientID, temp.length() + 1, 0); temp = server.arg("mqttPublishTopic"); config->mqttPublishTopic = new char[temp.length() + 1]; temp.toCharArray(config->mqttPublishTopic, temp.length() + 1, 0); temp = server.arg("mqttSubscribeTopic"); config->mqttSubscribeTopic = new char[temp.length() + 1]; temp.toCharArray(config->mqttSubscribeTopic, temp.length() + 1, 0); temp = server.arg("mqttUser"); config->mqttUser = new char[temp.length() + 1]; temp.toCharArray(config->mqttUser, temp.length() + 1, 0); temp = server.arg("mqttPass"); config->mqttPass = new char[temp.length() + 1]; temp.toCharArray(config->mqttPass, temp.length() + 1, 0); println("Saving configuration now..."); if (accesspoint::debugger) config->print(*accesspoint::debugger); if (config->save()) { println("Successfully saved. Will roboot now."); String html = "