mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-21 04:46:10 +02:00
fix #472 refactored ahoyWifi class completely
-> now Ahoy opens an AP during boot. This will be closed once a station WiFi connection is established improved NTP after boot, will be synced immediately after successful WiFi connection
This commit is contained in:
parent
614af26cd9
commit
7c6ab6792b
9 changed files with 129 additions and 204 deletions
16
src/app.cpp
16
src/app.cpp
|
@ -42,8 +42,7 @@ void app::setup(uint32_t timeout) {
|
||||||
mMqtt.setup(&mConfig->mqtt, mConfig->sys.deviceName, mVersion, mSys, &mUtcTimestamp, &mSunrise, &mSunset);
|
mMqtt.setup(&mConfig->mqtt, mConfig->sys.deviceName, mVersion, mSys, &mUtcTimestamp, &mSunrise, &mSunset);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
mWifi = new ahoywifi(mConfig);
|
mWifi.setup(mConfig, &mUtcTimestamp);
|
||||||
mWifi->setup(timeout, mSettings.getValid());
|
|
||||||
|
|
||||||
mPayload.setup(mSys);
|
mPayload.setup(mSys);
|
||||||
mPayload.enableSerialDebug(mConfig->serial.debug);
|
mPayload.enableSerialDebug(mConfig->serial.debug);
|
||||||
|
@ -72,6 +71,10 @@ void app::loop(void) {
|
||||||
|
|
||||||
ah::Scheduler::loop();
|
ah::Scheduler::loop();
|
||||||
|
|
||||||
|
#if !defined(AP_ONLY)
|
||||||
|
mWifi.loop();
|
||||||
|
#endif
|
||||||
|
|
||||||
mWeb->loop();
|
mWeb->loop();
|
||||||
|
|
||||||
if (mFlagSendDiscoveryConfig) {
|
if (mFlagSendDiscoveryConfig) {
|
||||||
|
@ -198,19 +201,14 @@ void app::handleIntr(void) {
|
||||||
mSys->Radio.handleIntr();
|
mSys->Radio.handleIntr();
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
bool app::getWifiApActive(void) {
|
|
||||||
return mWifi->getApActive();
|
|
||||||
}
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void app::scanAvailNetworks(void) {
|
void app::scanAvailNetworks(void) {
|
||||||
mWifi->scanAvailNetworks();
|
mWifi.scanAvailNetworks();
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void app::getAvailNetworks(JsonObject obj) {
|
void app::getAvailNetworks(JsonObject obj) {
|
||||||
mWifi->getAvailNetworks(obj);
|
mWifi.getAvailNetworks(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
22
src/app.h
22
src/app.h
|
@ -84,15 +84,6 @@ class app : public ah::Scheduler {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
String getDateTimeStr(time_t t) {
|
|
||||||
char str[20];
|
|
||||||
if(0 == t)
|
|
||||||
sprintf(str, "n/a");
|
|
||||||
else
|
|
||||||
sprintf(str, "%04d-%02d-%02d %02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
|
|
||||||
return String(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
String getTimeStr(uint32_t offset = 0) {
|
String getTimeStr(uint32_t offset = 0) {
|
||||||
char str[10];
|
char str[10];
|
||||||
if(0 == mUtcTimestamp)
|
if(0 == mUtcTimestamp)
|
||||||
|
@ -155,25 +146,20 @@ class app : public ah::Scheduler {
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (mUpdateNtp) {
|
if (mUpdateNtp) {
|
||||||
mUpdateNtp = false;
|
mUpdateNtp = false;
|
||||||
mUtcTimestamp = mWifi->getNtpTime();
|
mWifi.getNtpTime();
|
||||||
DPRINTLN(DBG_INFO, F("[NTP]: ") + getDateTimeStr(mUtcTimestamp) + F(" UTC"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void minuteTick(void) {
|
void minuteTick(void) {
|
||||||
if(0 == mUtcTimestamp) {
|
if(0 == mUtcTimestamp) {
|
||||||
if(!mWifi->getApActive())
|
mUpdateNtp = true;
|
||||||
mUpdateNtp = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ntpUpdateTick(void) {
|
void ntpUpdateTick(void) {
|
||||||
if (!mWifi->getApActive())
|
mUpdateNtp = true;
|
||||||
mUpdateNtp = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void stats(void) {
|
void stats(void) {
|
||||||
|
@ -203,7 +189,7 @@ class app : public ah::Scheduler {
|
||||||
|
|
||||||
bool mShowRebootRequest;
|
bool mShowRebootRequest;
|
||||||
|
|
||||||
ahoywifi *mWifi;
|
ahoywifi mWifi;
|
||||||
web *mWeb;
|
web *mWeb;
|
||||||
PayloadType mPayload;
|
PayloadType mPayload;
|
||||||
PubSerialType mPubSerial;
|
PubSerialType mPubSerial;
|
||||||
|
|
|
@ -226,7 +226,7 @@ class settings {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void loadDefaults(bool wifi = true) {
|
void loadDefaults(bool wifi = true) {
|
||||||
DPRINTLN(DBG_INFO, F("loadDefaults"));
|
DPRINTLN(DBG_VERBOSE, F("loadDefaults"));
|
||||||
if(wifi) {
|
if(wifi) {
|
||||||
snprintf(mCfg.sys.stationSsid, SSID_LEN, FB_WIFI_SSID);
|
snprintf(mCfg.sys.stationSsid, SSID_LEN, FB_WIFI_SSID);
|
||||||
snprintf(mCfg.sys.stationPwd, PWD_LEN, FB_WIFI_PWD);
|
snprintf(mCfg.sys.stationPwd, PWD_LEN, FB_WIFI_PWD);
|
||||||
|
|
|
@ -139,14 +139,15 @@ class HmRadio {
|
||||||
mNrf24.setPALevel(ampPwr & 0x03);
|
mNrf24.setPALevel(ampPwr & 0x03);
|
||||||
mNrf24.startListening();
|
mNrf24.startListening();
|
||||||
|
|
||||||
DPRINTLN(DBG_INFO, F("Radio Config:"));
|
|
||||||
mNrf24.printPrettyDetails();
|
|
||||||
|
|
||||||
mTxCh = setDefaultChannels();
|
mTxCh = setDefaultChannels();
|
||||||
|
|
||||||
if(!mNrf24.isChipConnected()) {
|
if(mNrf24.isChipConnected()) {
|
||||||
DPRINTLN(DBG_WARN, F("WARNING! your NRF24 module can't be reached, check the wiring"));
|
DPRINTLN(DBG_INFO, F("Radio Config:"));
|
||||||
|
mNrf24.printPrettyDetails();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
DPRINTLN(DBG_WARN, F("WARNING! your NRF24 module can't be reached, check the wiring"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(void) {
|
void loop(void) {
|
||||||
|
|
|
@ -30,4 +30,13 @@ namespace ah {
|
||||||
double round3(double value) {
|
double round3(double value) {
|
||||||
return (int)(value * 1000 + 0.5) / 1000.0;
|
return (int)(value * 1000 + 0.5) / 1000.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String getDateTimeStr(time_t t) {
|
||||||
|
char str[20];
|
||||||
|
if(0 == t)
|
||||||
|
sprintf(str, "n/a");
|
||||||
|
else
|
||||||
|
sprintf(str, "%04d-%02d-%02d %02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
|
||||||
|
return String(str);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,15 +6,18 @@
|
||||||
#ifndef __HELPER_H__
|
#ifndef __HELPER_H__
|
||||||
#define __HELPER_H__
|
#define __HELPER_H__
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <TimeLib.h>
|
||||||
|
|
||||||
namespace ah {
|
namespace ah {
|
||||||
void ip2Arr(uint8_t ip[], const char *ipStr);
|
void ip2Arr(uint8_t ip[], const char *ipStr);
|
||||||
void ip2Char(uint8_t ip[], char *str);
|
void ip2Char(uint8_t ip[], char *str);
|
||||||
double round3(double value);
|
double round3(double value);
|
||||||
|
String getDateTimeStr(time_t t);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /*__HELPER_H__*/
|
#endif /*__HELPER_H__*/
|
||||||
|
|
|
@ -664,10 +664,10 @@ void web::showJson(void) {
|
||||||
snprintf(val, 25, "[%.3f, \"%s\"]", iv->getValue(i), iv->getUnit(i));
|
snprintf(val, 25, "[%.3f, \"%s\"]", iv->getValue(i), iv->getUnit(i));
|
||||||
modJson += String(topic) + ": " + String(val) + F(",\n");
|
modJson += String(topic) + ": " + String(val) + F(",\n");
|
||||||
}
|
}
|
||||||
modJson += F("\t\"last_msg\": \"") + mMain->getDateTimeStr(iv->ts) + F("\"\n\t},\n");
|
modJson += F("\t\"last_msg\": \"") + ah::getDateTimeStr(iv->ts) + F("\"\n\t},\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
modJson += F("\"json_ts\": \"") + String(mMain->getDateTimeStr(mMain->mTimestamp)) + F("\"\n}\n");
|
modJson += F("\"json_ts\": \"") + String(ah::getDateTimeStr(mMain->mTimestamp)) + F("\"\n}\n");
|
||||||
|
|
||||||
mWeb->send(200, F("application/json"), modJson);
|
mWeb->send(200, F("application/json"), modJson);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,144 +16,84 @@
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
ahoywifi::ahoywifi(settings_t *config) {
|
ahoywifi::ahoywifi() {
|
||||||
mConfig = config;
|
mCnt = 0;
|
||||||
|
mConnected = false;
|
||||||
mDns = new DNSServer();
|
mInitNtp = true;
|
||||||
mUdp = new WiFiUDP();
|
|
||||||
|
|
||||||
mWifiStationTimeout = 10;
|
|
||||||
wifiWasEstablished = false;
|
|
||||||
mNextTryTs = 0;
|
|
||||||
mApLastTick = 0;
|
|
||||||
mApActive = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void ahoywifi::setup(uint32_t timeout, bool settingValid) {
|
void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp) {
|
||||||
//wifiConnectHandler = WiFi.onStationModeGotIP(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1));
|
char ipSta[16];
|
||||||
//wifiDisconnectHandler = WiFi.onStationModeDisconnected(std::bind(&ahoywifi::onDisconnect, this, std::placeholders::_1));
|
mConfig = config;
|
||||||
|
mUtcTimestamp = utcTimestamp;
|
||||||
|
|
||||||
#ifdef FB_WIFI_OVERRIDDEN
|
#if !defined(FB_WIFI_OVERRIDDEN)
|
||||||
mStationWifiIsDef = false;
|
if(strncmp(mConfig->sys.stationSsid, FB_WIFI_SSID, 14) != 0)
|
||||||
#else
|
setupAp();
|
||||||
mStationWifiIsDef = (strncmp(mConfig->sys.stationSsid, FB_WIFI_SSID, 14) == 0);
|
|
||||||
#endif
|
#endif
|
||||||
mWifiStationTimeout = timeout;
|
#if !defined(AP_ONLY)
|
||||||
#ifndef AP_ONLY
|
setupStation();
|
||||||
if(false == mApActive)
|
ah::ip2Char(mConfig->sys.ip.ip, ipSta);
|
||||||
mApActive = (mStationWifiIsDef) ? true : setupStation(mWifiStationTimeout);
|
|
||||||
#endif
|
#endif
|
||||||
if(!settingValid) {
|
|
||||||
DPRINTLN(DBG_WARN, F("your settings are not valid! check [IP]/setup"));
|
wifiConnectHandler = WiFi.onStationModeGotIP(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1));
|
||||||
mApActive = true;
|
wifiDisconnectHandler = WiFi.onStationModeDisconnected(std::bind(&ahoywifi::onDisconnect, this, std::placeholders::_1));
|
||||||
mApLastTick = millis();
|
|
||||||
mNextTryTs = (millis() + (WIFI_AP_ACTIVE_TIME * 1000));
|
|
||||||
setupAp(WIFI_AP_SSID, WIFI_AP_PWD);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
DPRINTLN(DBG_INFO, F("\n\n----------------------------------------"));
|
|
||||||
DPRINTLN(DBG_INFO, F("Welcome to AHOY!"));
|
|
||||||
DPRINT(DBG_INFO, F("\npoint your browser to http://"));
|
|
||||||
if(mApActive)
|
|
||||||
DBGPRINTLN(F("192.168.4.1"));
|
|
||||||
else
|
|
||||||
DBGPRINTLN(WiFi.localIP().toString());
|
|
||||||
DPRINTLN(DBG_INFO, F("to configure your device"));
|
|
||||||
DPRINTLN(DBG_INFO, F("----------------------------------------\n"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
bool ahoywifi::loop(void) {
|
void ahoywifi::loop() {
|
||||||
if(mApActive) {
|
#if !defined(AP_ONLY)
|
||||||
mDns->processNextRequest();
|
if(!mConnected) {
|
||||||
#ifndef AP_ONLY
|
delay(100);
|
||||||
if(ah::checkTicker(&mNextTryTs, (WIFI_AP_ACTIVE_TIME * 1000))) {
|
mCnt++;
|
||||||
mApActive = (mStationWifiIsDef) ? true : setupStation(mWifiStationTimeout);
|
if((mCnt % 50) == 0)
|
||||||
if(mApActive) {
|
WiFi.disconnect();
|
||||||
if(strlen(WIFI_AP_PWD) < 8)
|
else if((mCnt % 60) == 0) {
|
||||||
DPRINTLN(DBG_ERROR, F("password must be at least 8 characters long"));
|
WiFi.reconnect();
|
||||||
mApLastTick = millis();
|
mCnt = 0;
|
||||||
mNextTryTs = (millis() + (WIFI_AP_ACTIVE_TIME * 1000));
|
|
||||||
setupAp(WIFI_AP_SSID, WIFI_AP_PWD);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
} else if(mInitNtp) {
|
||||||
if(millis() - mApLastTick > 10000) {
|
getNtpTime();
|
||||||
mApLastTick = millis();
|
mInitNtp = false;
|
||||||
uint8_t cnt = WiFi.softAPgetStationNum();
|
|
||||||
if(cnt > 0) {
|
|
||||||
DPRINTLN(DBG_INFO, String(cnt) + F(" client connected (no timeout)"));
|
|
||||||
mNextTryTs = (millis() + (WIFI_AP_ACTIVE_TIME * 1000));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
DBGPRINT(F("AP will be closed in "));
|
|
||||||
DBGPRINT(String((mNextTryTs - mApLastTick) / 1000));
|
|
||||||
DBGPRINTLN(F(" seconds"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
mCnt = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
if((WiFi.status() != WL_CONNECTED) && wifiWasEstablished) {
|
|
||||||
if(!mApActive) {
|
|
||||||
DPRINTLN(DBG_INFO, "[WiFi]: Connection Lost");
|
|
||||||
mApActive = (mStationWifiIsDef) ? true : setupStation(mWifiStationTimeout);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return mApActive;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void ahoywifi::setupAp(const char *ssid, const char *pwd) {
|
void ahoywifi::setupAp(void) {
|
||||||
DPRINTLN(DBG_VERBOSE, F("app::setupAp"));
|
DPRINTLN(DBG_VERBOSE, F("wifi::setupAp"));
|
||||||
IPAddress apIp(192, 168, 4, 1);
|
IPAddress apIp(192, 168, 4, 1);
|
||||||
|
|
||||||
DBGPRINTLN(F("\n---------\nAhoy Info:"));
|
DBGPRINTLN(F("\n---------\nAhoyDTU Info:"));
|
||||||
DBGPRINT(F("Version: "));
|
DBGPRINT(F("Version: "));
|
||||||
DBGPRINTLN(String(VERSION_MAJOR) + F(".") + String(VERSION_MINOR) + F(".") + String(VERSION_PATCH));
|
DBGPRINTLN(String(VERSION_MAJOR) + F(".") + String(VERSION_MINOR) + F(".") + String(VERSION_PATCH));
|
||||||
DBGPRINT(F("Github Hash: "));
|
DBGPRINT(F("Github Hash: "));
|
||||||
DBGPRINTLN(String(AUTO_GIT_HASH));
|
DBGPRINTLN(String(AUTO_GIT_HASH));
|
||||||
|
|
||||||
DBGPRINT(F("\n---------\nAP MODE\nSSID: "));
|
DBGPRINT(F("\n---------\nAP MODE\nSSID: "));
|
||||||
DBGPRINTLN(ssid);
|
DBGPRINTLN(WIFI_AP_SSID);
|
||||||
DBGPRINT(F("PWD: "));
|
DBGPRINT(F("PWD: "));
|
||||||
DBGPRINTLN(pwd);
|
DBGPRINTLN(WIFI_AP_PWD);
|
||||||
DBGPRINT(F("\nActive for: "));
|
DBGPRINTLN("IP Address: http://" + apIp.toString());
|
||||||
DBGPRINT(String(WIFI_AP_ACTIVE_TIME));
|
DBGPRINTLN(F("---------\n"));
|
||||||
DBGPRINTLN(F(" seconds"));
|
|
||||||
|
|
||||||
DBGPRINTLN("\nIp Address: " + apIp[0] + apIp[1] + apIp[2] + apIp[3]);
|
WiFi.mode(WIFI_AP_STA);
|
||||||
DBGPRINTLN(F("\n---------\n"));
|
|
||||||
|
|
||||||
WiFi.mode(WIFI_AP);
|
|
||||||
WiFi.softAPConfig(apIp, apIp, IPAddress(255, 255, 255, 0));
|
WiFi.softAPConfig(apIp, apIp, IPAddress(255, 255, 255, 0));
|
||||||
WiFi.softAP(ssid, pwd);
|
WiFi.softAP(WIFI_AP_SSID, WIFI_AP_PWD);
|
||||||
|
|
||||||
mDns->start(53, "*", apIp);
|
mDns.start(53, "*", apIp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
bool ahoywifi::setupStation(uint32_t timeout) {
|
void ahoywifi::setupStation(void) {
|
||||||
DPRINTLN(DBG_VERBOSE, F("app::setupStation"));
|
DPRINTLN(DBG_VERBOSE, F("wifi::setupStation"));
|
||||||
int32_t cnt;
|
|
||||||
bool startAp = false;
|
|
||||||
|
|
||||||
if(timeout >= 3)
|
|
||||||
cnt = (timeout - 3) / 2 * 10;
|
|
||||||
else {
|
|
||||||
timeout = 1;
|
|
||||||
cnt = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
|
||||||
if(mConfig->sys.ip.ip[0] != 0) {
|
if(mConfig->sys.ip.ip[0] != 0) {
|
||||||
IPAddress ip(mConfig->sys.ip.ip);
|
IPAddress ip(mConfig->sys.ip.ip);
|
||||||
IPAddress mask(mConfig->sys.ip.mask);
|
IPAddress mask(mConfig->sys.ip.mask);
|
||||||
|
@ -167,45 +107,14 @@ bool ahoywifi::setupStation(uint32_t timeout) {
|
||||||
if(String(mConfig->sys.deviceName) != "")
|
if(String(mConfig->sys.deviceName) != "")
|
||||||
WiFi.hostname(mConfig->sys.deviceName);
|
WiFi.hostname(mConfig->sys.deviceName);
|
||||||
|
|
||||||
delay(2000);
|
|
||||||
DBGPRINT(F("connect to network '"));
|
DBGPRINT(F("connect to network '"));
|
||||||
DBGPRINT(mConfig->sys.stationSsid);
|
DBGPRINT(mConfig->sys.stationSsid);
|
||||||
DBGPRINTLN(F("' ..."));
|
DBGPRINTLN(F("' ..."));
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
|
||||||
delay(100);
|
|
||||||
if(cnt % 40 == 0)
|
|
||||||
DBGPRINTLN(".");
|
|
||||||
else
|
|
||||||
DBGPRINT(".");
|
|
||||||
|
|
||||||
if(timeout > 0) { // limit == 0 -> no limit
|
|
||||||
if(--cnt <= 0) {
|
|
||||||
if(WiFi.status() != WL_CONNECTED) {
|
|
||||||
startAp = true;
|
|
||||||
WiFi.disconnect();
|
|
||||||
}
|
|
||||||
delay(100);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Serial.println(".");
|
|
||||||
|
|
||||||
if(false == startAp)
|
|
||||||
wifiWasEstablished = true;
|
|
||||||
|
|
||||||
delay(1000);
|
|
||||||
return startAp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
bool ahoywifi::getApActive(void) {
|
void ahoywifi::getNtpTime(void) {
|
||||||
return mApActive;
|
|
||||||
}
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
time_t ahoywifi::getNtpTime(void) {
|
|
||||||
//DPRINTLN(DBG_VERBOSE, F("wifi::getNtpTime"));
|
//DPRINTLN(DBG_VERBOSE, F("wifi::getNtpTime"));
|
||||||
time_t date = 0;
|
time_t date = 0;
|
||||||
IPAddress timeServer;
|
IPAddress timeServer;
|
||||||
|
@ -213,16 +122,16 @@ time_t ahoywifi::getNtpTime(void) {
|
||||||
uint8_t retry = 0;
|
uint8_t retry = 0;
|
||||||
|
|
||||||
WiFi.hostByName(mConfig->ntp.addr, timeServer);
|
WiFi.hostByName(mConfig->ntp.addr, timeServer);
|
||||||
mUdp->begin(mConfig->ntp.port);
|
mUdp.begin(mConfig->ntp.port);
|
||||||
|
|
||||||
sendNTPpacket(timeServer);
|
sendNTPpacket(timeServer);
|
||||||
|
|
||||||
while(retry++ < 5) {
|
while(retry++ < 5) {
|
||||||
int wait = 150;
|
int wait = 150;
|
||||||
while(--wait) {
|
while(--wait) {
|
||||||
if(NTP_PACKET_SIZE <= mUdp->parsePacket()) {
|
if(NTP_PACKET_SIZE <= mUdp.parsePacket()) {
|
||||||
uint64_t secsSince1900;
|
uint64_t secsSince1900;
|
||||||
mUdp->read(buf, NTP_PACKET_SIZE);
|
mUdp.read(buf, NTP_PACKET_SIZE);
|
||||||
secsSince1900 = (buf[40] << 24);
|
secsSince1900 = (buf[40] << 24);
|
||||||
secsSince1900 |= (buf[41] << 16);
|
secsSince1900 |= (buf[41] << 16);
|
||||||
secsSince1900 |= (buf[42] << 8);
|
secsSince1900 |= (buf[42] << 8);
|
||||||
|
@ -230,13 +139,14 @@ time_t ahoywifi::getNtpTime(void) {
|
||||||
|
|
||||||
date = secsSince1900 - 2208988800UL; // UTC time
|
date = secsSince1900 - 2208988800UL; // UTC time
|
||||||
break;
|
break;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
delay(10);
|
delay(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return date;
|
*mUtcTimestamp = date;
|
||||||
|
|
||||||
|
DPRINTLN(DBG_INFO, F("[NTP]: ") + ah::getDateTimeStr(*mUtcTimestamp) + F(" UTC"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -285,19 +195,42 @@ void ahoywifi::sendNTPpacket(IPAddress& address) {
|
||||||
buf[14] = 49;
|
buf[14] = 49;
|
||||||
buf[15] = 52;
|
buf[15] = 52;
|
||||||
|
|
||||||
mUdp->beginPacket(address, 123); // NTP request, port 123
|
mUdp.beginPacket(address, 123); // NTP request, port 123
|
||||||
mUdp->write(buf, NTP_PACKET_SIZE);
|
mUdp.write(buf, NTP_PACKET_SIZE);
|
||||||
mUdp->endPacket();
|
mUdp.endPacket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
/*void ahoywifi::onConnect(const WiFiEventStationModeGotIP& event) {
|
void ahoywifi::onConnect(const WiFiEventStationModeGotIP& event) {
|
||||||
Serial.println("Connected to Wi-Fi.");
|
if(!mConnected) {
|
||||||
|
mConnected = true;
|
||||||
|
DBGPRINTLN(F("\n[WiFi] Connected"));
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.begin();
|
||||||
|
DBGPRINTLN(F("[WiFi] AP disabled"));
|
||||||
|
mDns.stop();
|
||||||
|
|
||||||
|
welcome(WiFi.localIP().toString() + F(" (Station)"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) {
|
void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||||
Serial.println("Disconnected from Wi-Fi.");
|
if(mConnected) {
|
||||||
}*/
|
mConnected = false;
|
||||||
|
DPRINTLN(DBG_INFO, "[WiFi] Connection Lost");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
void ahoywifi::welcome(String msg) {
|
||||||
|
DBGPRINTLN(F("\n\n--------------------------------"));
|
||||||
|
DBGPRINTLN(F("Welcome to AHOY!"));
|
||||||
|
DBGPRINT(F("\npoint your browser to http://"));
|
||||||
|
DBGPRINTLN(msg);
|
||||||
|
DBGPRINTLN(F("to configure your device"));
|
||||||
|
DBGPRINTLN(F("--------------------------------\n"));
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
#include "../utils/dbg.h"
|
#include "../utils/dbg.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <WiFiUdp.h>
|
#include <WiFiUdp.h>
|
||||||
#include <TimeLib.h>
|
|
||||||
#include <DNSServer.h>
|
#include <DNSServer.h>
|
||||||
#include "ESPAsyncWebServer.h"
|
#include "ESPAsyncWebServer.h"
|
||||||
|
|
||||||
|
@ -19,36 +18,32 @@ class app;
|
||||||
|
|
||||||
class ahoywifi {
|
class ahoywifi {
|
||||||
public:
|
public:
|
||||||
ahoywifi(settings_t *config);
|
ahoywifi();
|
||||||
~ahoywifi() {}
|
|
||||||
|
|
||||||
void setup(uint32_t timeout, bool settingValid);
|
void setup(settings_t *config, uint32_t *utcTimestamp);
|
||||||
bool loop(void);
|
void loop(void);
|
||||||
void setupAp(const char *ssid, const char *pwd);
|
void getNtpTime(void);
|
||||||
bool setupStation(uint32_t timeout);
|
|
||||||
bool getApActive(void);
|
|
||||||
time_t getNtpTime(void);
|
|
||||||
void scanAvailNetworks(void);
|
void scanAvailNetworks(void);
|
||||||
void getAvailNetworks(JsonObject obj);
|
void getAvailNetworks(JsonObject obj);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void setupAp(void);
|
||||||
|
void setupStation(void);
|
||||||
void sendNTPpacket(IPAddress& address);
|
void sendNTPpacket(IPAddress& address);
|
||||||
//void onConnect(const WiFiEventStationModeGotIP& event);
|
void onConnect(const WiFiEventStationModeGotIP& event);
|
||||||
//void onDisconnect(const WiFiEventStationModeDisconnected& event);
|
void onDisconnect(const WiFiEventStationModeDisconnected& event);
|
||||||
|
void welcome(String msg);
|
||||||
|
|
||||||
settings_t *mConfig;
|
settings_t *mConfig;
|
||||||
|
|
||||||
DNSServer *mDns;
|
DNSServer mDns;
|
||||||
WiFiUDP *mUdp; // for time server
|
WiFiUDP mUdp; // for time server
|
||||||
//WiFiEventHandler wifiConnectHandler;
|
WiFiEventHandler wifiConnectHandler;
|
||||||
//WiFiEventHandler wifiDisconnectHandler;
|
WiFiEventHandler wifiDisconnectHandler;
|
||||||
|
|
||||||
uint32_t mWifiStationTimeout;
|
bool mConnected, mInitNtp;
|
||||||
uint32_t mNextTryTs;
|
uint8_t mCnt;
|
||||||
uint32_t mApLastTick;
|
uint32_t *mUtcTimestamp;
|
||||||
bool mApActive;
|
|
||||||
bool wifiWasEstablished;
|
|
||||||
bool mStationWifiIsDef;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /*__AHOYWIFI_H__*/
|
#endif /*__AHOYWIFI_H__*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue