mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-12 08:26:38 +02:00
* PR #76 updated debug messages: now 5 different levels are available
* fixed CRC loop issue
This commit is contained in:
parent
7e2df351d9
commit
0347a3df44
10 changed files with 245 additions and 196 deletions
|
@ -31,7 +31,7 @@ Main::Main(void) {
|
|||
|
||||
mEep = new eep();
|
||||
Serial.begin(115200);
|
||||
DBGMAIN(F("Main::Main"));
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::Main"));
|
||||
|
||||
mUptimeSecs = 0;
|
||||
mUptimeTicker = 0xffffffff;
|
||||
|
@ -45,7 +45,7 @@ Main::Main(void) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::setup(uint32_t timeout) {
|
||||
DBGMAIN(F("Main::setup"));
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::setup"));
|
||||
bool startAp = mApActive;
|
||||
mLimit = timeout;
|
||||
|
||||
|
@ -74,7 +74,7 @@ void Main::setup(uint32_t timeout) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::loop(void) {
|
||||
//DBGMAIN(F("M"));
|
||||
//DPRINTLN(DBG_VERBOSE, F("M"));
|
||||
if(mApActive) {
|
||||
mDns->processNextRequest();
|
||||
#ifndef AP_ONLY
|
||||
|
@ -83,7 +83,7 @@ void Main::loop(void) {
|
|||
mApActive = setupStation(mLimit);
|
||||
if(mApActive) {
|
||||
if(strlen(WIFI_AP_PWD) < 8)
|
||||
DPRINTLN(F("ERROR: password must be at least 8 characters long"));
|
||||
DPRINTLN(DBG_ERROR, F("password must be at least 8 characters long"));
|
||||
setupAp(WIFI_AP_SSID, WIFI_AP_PWD);
|
||||
}
|
||||
}
|
||||
|
@ -91,11 +91,11 @@ void Main::loop(void) {
|
|||
if(millis() - mApLastTick > 10000) {
|
||||
uint8_t cnt = WiFi.softAPgetStationNum();
|
||||
if(cnt > 0) {
|
||||
DPRINTLN(String(cnt) + F(" clients connected, resetting AP timeout"));
|
||||
DPRINTLN(DBG_INFO, String(cnt) + F(" clients connected, resetting AP timeout"));
|
||||
mNextTryTs = (millis() + (WIFI_AP_ACTIVE_TIME * 1000));
|
||||
}
|
||||
mApLastTick = millis();
|
||||
DPRINTLN(F("AP will be closed in ") + String((mNextTryTs - mApLastTick) / 1000) + F(" seconds"));
|
||||
DPRINTLN(DBG_INFO, F("AP will be closed in ") + String((mNextTryTs - mApLastTick) / 1000) + F(" seconds"));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -109,7 +109,7 @@ void Main::loop(void) {
|
|||
else {
|
||||
if(!mApActive) {
|
||||
mTimestamp = getNtpTime();
|
||||
DPRINTLN("[NTP]: " + getDateTimeStr(mTimestamp));
|
||||
DPRINTLN(DBG_INFO, "[NTP]: " + getDateTimeStr(mTimestamp));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -123,11 +123,11 @@ void Main::loop(void) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
bool Main::getConfig(void) {
|
||||
DBGMAIN(F("Main::getConfig"));
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::getConfig"));
|
||||
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);
|
||||
mSettingsValid = checkEEpCrc(ADDR_START_SETTINGS, ((ADDR_NEXT)-(ADDR_START_SETTINGS)), ADDR_SETTINGS_CRC);
|
||||
|
||||
if(mWifiSettingsValid) {
|
||||
mEep->read(ADDR_SSID, mStationSsid, SSID_LEN);
|
||||
|
@ -146,15 +146,15 @@ bool Main::getConfig(void) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::setupAp(const char *ssid, const char *pwd) {
|
||||
DBGMAIN(F("Main::setupAp"));
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::setupAp"));
|
||||
IPAddress apIp(192, 168, 1, 1);
|
||||
|
||||
DPRINTLN(F("\n---------\nAP MODE\nSSDI: ")
|
||||
DPRINTLN(DBG_INFO, F("\n---------\nAP MODE\nSSID: ")
|
||||
+ String(ssid) + F("\nPWD: ")
|
||||
+ String(pwd) + F("\nActive for: ")
|
||||
+ String(WIFI_AP_ACTIVE_TIME) + F(" seconds")
|
||||
+ F("\n---------\n"));
|
||||
DBGMAIN("DBG: " + String(mNextTryTs));
|
||||
DPRINTLN(DBG_DEBUG, String(mNextTryTs));
|
||||
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAPConfig(apIp, apIp, IPAddress(255, 255, 255, 0));
|
||||
|
@ -173,7 +173,7 @@ void Main::setupAp(const char *ssid, const char *pwd) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
bool Main::setupStation(uint32_t timeout) {
|
||||
DBGMAIN(F("Main::setupStation"));
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::setupStation"));
|
||||
int32_t cnt;
|
||||
bool startAp = false;
|
||||
|
||||
|
@ -190,7 +190,7 @@ bool Main::setupStation(uint32_t timeout) {
|
|||
WiFi.hostname(mDeviceName);
|
||||
|
||||
delay(2000);
|
||||
DPRINTLN(F("connect to network '") + String(mStationSsid) + F("' ..."));
|
||||
DPRINTLN(DBG_INFO, F("connect to network '") + String(mStationSsid) + F("' ..."));
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(100);
|
||||
if(cnt % 100 == 0)
|
||||
|
@ -223,7 +223,7 @@ bool Main::setupStation(uint32_t timeout) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::showSetup(void) {
|
||||
DBGMAIN(F("Main::showSetup"));
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::showSetup"));
|
||||
String html = FPSTR(setup_html);
|
||||
html.replace(F("{SSID}"), mStationSsid);
|
||||
// PWD will be left at the default value (for protection)
|
||||
|
@ -241,23 +241,21 @@ void Main::showSetup(void) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::showCss(void) {
|
||||
DBGMAIN(F("Main::showCss"));
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::showCss"));
|
||||
mWeb->send(200, "text/css", FPSTR(style_css));
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::showSave(void) {
|
||||
#ifdef DEBUG_HMMAIN
|
||||
DPRINTLN(F("Main::showSave"));
|
||||
#endif
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::showSave"));
|
||||
saveValues(true);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::saveValues(bool webSend = true) {
|
||||
DBGMAIN(F("Main::saveValues"));
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::saveValues"));
|
||||
if(mWeb->args() > 0) {
|
||||
if(mWeb->arg("ssid") != "") {
|
||||
memset(mStationSsid, 0, SSID_LEN);
|
||||
|
@ -291,7 +289,7 @@ void Main::saveValues(bool webSend = true) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::updateCrc(void) {
|
||||
DBGMAIN(F("Main::updateCrc"));
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::updateCrc"));
|
||||
uint16_t crc;
|
||||
crc = buildEEpCrc(ADDR_START, ADDR_WIFI_CRC);
|
||||
//Serial.println("new CRC: " + String(crc, HEX));
|
||||
|
@ -302,7 +300,7 @@ void Main::updateCrc(void) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::showUptime(void) {
|
||||
//DBGMAIN(F("Main::showUptime"));
|
||||
//DPRINTLN(DBG_VERBOSE, F("Main::showUptime"));
|
||||
char time[20] = {0};
|
||||
|
||||
int upTimeSc = uint32_t((mUptimeSecs) % 60);
|
||||
|
@ -318,14 +316,14 @@ void Main::showUptime(void) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::showTime(void) {
|
||||
//DBGMAIN(F("Main::showTime"));
|
||||
//DPRINTLN(DBG_VERBOSE, F("Main::showTime"));
|
||||
mWeb->send(200, "text/plain", getDateTimeStr(mTimestamp));
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::showNotFound(void) {
|
||||
DBGMAIN(F("Main::showNotFound - ") + mWeb->uri());
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::showNotFound - ") + mWeb->uri());
|
||||
String msg = F("File Not Found\n\nURI: ");
|
||||
msg += mWeb->uri();
|
||||
msg += F("\nMethod: ");
|
||||
|
@ -344,7 +342,7 @@ void Main::showNotFound(void) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::showReboot(void) {
|
||||
DBGMAIN(F("Main::showReboot"));
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::showReboot"));
|
||||
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Rebooting ...</title><meta http-equiv=\"refresh\" content=\"10; URL=/\"></head><body>rebooting ... auto reload after 10s</body></html>"));
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
|
@ -354,7 +352,7 @@ void Main::showReboot(void) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::showFactoryRst(void) {
|
||||
DBGMAIN(F("Main::showFactoryRst"));
|
||||
DPRINTLN(DBG_VERBOSE, F("Main::showFactoryRst"));
|
||||
String content = "";
|
||||
int refresh = 3;
|
||||
if(mWeb->args() > 0) {
|
||||
|
@ -383,7 +381,7 @@ void Main::showFactoryRst(void) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
time_t Main::getNtpTime(void) {
|
||||
//DBGMAIN(F("Main::getNtpTime"));
|
||||
//DPRINTLN(DBG_VERBOSE, F("Main::getNtpTime"));
|
||||
time_t date = 0;
|
||||
IPAddress timeServer;
|
||||
uint8_t buf[NTP_PACKET_SIZE];
|
||||
|
@ -421,7 +419,7 @@ time_t Main::getNtpTime(void) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Main::sendNTPpacket(IPAddress& address) {
|
||||
//DBGMAIN(F("Main::sendNTPpacket"));
|
||||
//DPRINTLN(DBG_VERBOSE, F("Main::sendNTPpacket"));
|
||||
uint8_t buf[NTP_PACKET_SIZE] = {0};
|
||||
|
||||
buf[0] = B11100011; // LI, Version, Mode
|
||||
|
@ -442,7 +440,7 @@ void Main::sendNTPpacket(IPAddress& address) {
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
String Main::getDateTimeStr(time_t t) {
|
||||
//DBGMAIN(F("Main::getDateTimeStr"));
|
||||
//DPRINTLN(DBG_VERBOSE, F("Main::getDateTimeStr"));
|
||||
char str[20] = {0};
|
||||
if(0 == t)
|
||||
sprintf(str, "n/a");
|
||||
|
@ -456,7 +454,7 @@ String Main::getDateTimeStr(time_t t) {
|
|||
// calculates the daylight saving time for middle Europe. Input: Unixtime in UTC
|
||||
// from: https://forum.arduino.cc/index.php?topic=172044.msg1278536#msg1278536
|
||||
time_t Main::offsetDayLightSaving (uint32_t local_t) {
|
||||
//DBGMAIN(F("Main::offsetDayLightSaving"));
|
||||
//DPRINTLN(DBG_VERBOSE, F("Main::offsetDayLightSaving"));
|
||||
int m = month (local_t);
|
||||
if(m < 3 || m > 10) return 0; // no DSL in Jan, Feb, Nov, Dez
|
||||
if(m > 3 && m < 10) return 1; // DSL in Apr, May, Jun, Jul, Aug, Sep
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue