#include "main.h" #include "version.h" #include "html/h/style_css.h" #include "html/h/setup_html.h" //----------------------------------------------------------------------------- Main::Main(void) { mDns = new DNSServer(); mWeb = new ESP8266WebServer(80); mUpdater = new ESP8266HTTPUpdateServer(); mUdp = new WiFiUDP(); mApActive = true; mWifiSettingsValid = false; mSettingsValid = false; snprintf(mVersion, 12, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); memset(&mDeviceName, 0, DEVNAME_LEN); mEep = new eep(); Serial.begin(115200); mUptimeSecs = 0; mUptimeTicker = new Ticker(); mUptimeTicker->attach(1, std::bind(&Main::uptimeTicker, this)); } //----------------------------------------------------------------------------- void Main::setup(const char *ssid, const char *pwd, uint32_t timeout) { bool startAp = mApActive; mWeb->on("/setup", std::bind(&Main::showSetup, this)); mWeb->on("/save", std::bind(&Main::showSave, this)); mWeb->on("/uptime", std::bind(&Main::showUptime, this)); mWeb->on("/time", std::bind(&Main::showTime, this)); mWeb->on("/style.css", std::bind(&Main::showCss, this)); mWeb->on("/reboot", std::bind(&Main::showReboot, this)); mWeb->onNotFound (std::bind(&Main::showNotFound, this)); startAp = getConfig(); if(String(mDeviceName) != "") WiFi.hostname(mDeviceName); if(false == startAp) startAp = setupStation(timeout); if(true == startAp) { if(strlen(pwd) < 8) Serial.println("password must be at least 8 characters long"); setupAp(ssid, pwd); } mUpdater->setup(mWeb); mApActive = startAp; mTimestamp = getNtpTime(); //Serial.println("[NTP]: " + getDateTimeStr(getNtpTime())); } //----------------------------------------------------------------------------- void Main::loop(void) { if(mApActive) mDns->processNextRequest(); mWeb->handleClient(); } //----------------------------------------------------------------------------- bool Main::getConfig(void) { bool mApActive = false; mWifiSettingsValid = checkEEpCrc(ADDR_START, ADDR_WIFI_CRC, ADDR_WIFI_CRC); mSettingsValid = checkEEpCrc(ADDR_START_SETTINGS, (ADDR_NEXT-ADDR_START_SETTINGS), ADDR_SETTINGS_CRC); if(mWifiSettingsValid) { mEep->read(ADDR_SSID, mStationSsid, SSID_LEN); mEep->read(ADDR_PWD, mStationPwd, PWD_LEN); mEep->read(ADDR_DEVNAME, mDeviceName, DEVNAME_LEN); } else { mApActive = true; memset(mStationSsid, 0, SSID_LEN); memset(mStationPwd, 0, PWD_LEN); memset(mDeviceName, 0, DEVNAME_LEN); // erase eeprom uint8_t buf[ADDR_NEXT-ADDR_START_SETTINGS] = {0}; mEep->write(ADDR_START_SETTINGS, buf, (ADDR_NEXT-ADDR_START_SETTINGS)); } return mApActive; } //----------------------------------------------------------------------------- void Main::setupAp(const char *ssid, const char *pwd) { IPAddress apIp(192, 168, 1, 1); Serial.println("\n---------\nAP MODE\nSSDI: "+ String(ssid) + "\nPWD: " + String(pwd) + "\n---------\n"); WiFi.mode(WIFI_AP); WiFi.softAPConfig(apIp, apIp, IPAddress(255, 255, 255, 0)); WiFi.softAP(ssid, pwd); mDns->start(mDnsPort, "*", apIp); mWeb->onNotFound([&]() { showSetup(); }); mWeb->on("/", std::bind(&Main::showSetup, this)); mWeb->begin(); } //----------------------------------------------------------------------------- bool Main::setupStation(uint32_t timeout) { int32_t cnt = timeout * 10; bool startAp = false; WiFi.mode(WIFI_STA); WiFi.begin(mStationSsid, mStationPwd); delay(5000); Serial.println("wait for network"); while (WiFi.status() != WL_CONNECTED) { delay(100); if(cnt % 100 == 0) Serial.println("."); else Serial.print("."); if(timeout > 0) { // limit == 0 -> no limit if(--cnt <= 0) { startAp = true; WiFi.disconnect(); delay(100); break; } } } Serial.println("."); if(false == startAp) { mWeb->begin(); } delay(1000); return startAp; } //----------------------------------------------------------------------------- void Main::showSetup(void) { String html = FPSTR(setup_html); html.replace("{SSID}", mStationSsid); // PWD will be left at the default value (for protection) // -> the PWD will only be changed if it does not match the default "{PWD}" html.replace("{DEVICE}", String(mDeviceName)); html.replace("{VERSION}", String(mVersion)); mWeb->send(200, "text/html", html); } //----------------------------------------------------------------------------- void Main::showCss(void) { mWeb->send(200, "text/css", FPSTR(style_css)); } //----------------------------------------------------------------------------- void Main::showSave(void) { saveValues(true); } //----------------------------------------------------------------------------- void Main::saveValues(bool webSend = true) { if(mWeb->args() > 0) { if(mWeb->arg("ssid") != "") { memset(mStationSsid, 0, SSID_LEN); mWeb->arg("ssid").toCharArray(mStationSsid, SSID_LEN); mEep->write(ADDR_SSID, mStationSsid, SSID_LEN); if(mWeb->arg("pwd") != "{PWD}") { memset(mStationPwd, 0, PWD_LEN); mWeb->arg("pwd").toCharArray(mStationPwd, PWD_LEN); mEep->write(ADDR_PWD, mStationPwd, PWD_LEN); } } memset(mDeviceName, 0, DEVNAME_LEN); mWeb->arg("device").toCharArray(mDeviceName, DEVNAME_LEN); mEep->write(ADDR_DEVNAME, mDeviceName, DEVNAME_LEN); updateCrc(); if(webSend) { if(mWeb->arg("reboot") == "on") showReboot(); else mWeb->send(200, "text/html", "
saved
"); } } } //----------------------------------------------------------------------------- void Main::updateCrc(void) { uint16_t crc; crc = buildEEpCrc(ADDR_START, ADDR_WIFI_CRC); //Serial.println("new CRC: " + String(crc, HEX)); mEep->write(ADDR_WIFI_CRC, crc); } //----------------------------------------------------------------------------- void Main::showUptime(void) { char time[20] = {0}; int upTimeSc = uint32_t((mUptimeSecs) % 60); int upTimeMn = uint32_t((mUptimeSecs / (60)) % 60); int upTimeHr = uint32_t((mUptimeSecs / (60 * 60)) % 24); int upTimeDy = uint32_t((mUptimeSecs / (60 * 60 * 24)) % 365); snprintf(time, 20, "%d Tage, %02d:%02d:%02d", upTimeDy, upTimeHr, upTimeMn, upTimeSc); mWeb->send(200, "text/plain", String(time)); } //----------------------------------------------------------------------------- void Main::showTime(void) { mWeb->send(200, "text/plain", getDateTimeStr(mTimestamp)); } //----------------------------------------------------------------------------- void Main::showNotFound(void) { String msg = "File Not Found\n\n"; msg += "URI: "; msg += mWeb->uri(); msg += "\nMethod: "; msg += ( mWeb->method() == HTTP_GET ) ? "GET" : "POST"; msg += "\nArguments: "; msg += mWeb->args(); msg += "\n"; for(uint8_t i = 0; i < mWeb->args(); i++ ) { msg += " " + mWeb->argName(i) + ": " + mWeb->arg(i) + "\n"; } mWeb->send(404, "text/plain", msg); } //----------------------------------------------------------------------------- void Main::showReboot(void) { mWeb->send(200, "text/html", "