@ -16,144 +16,84 @@
//-----------------------------------------------------------------------------
ahoywifi : : ahoywifi ( settings_t * config ) {
mConfig = config ;
mDns = new DNSServer ( ) ;
mUdp = new WiFiUDP ( ) ;
mWifiStationTimeout = 10 ;
wifiWasEstablished = false ;
mNextTryTs = 0 ;
mApLastTick = 0 ;
mApActive = false ;
ahoywifi : : ahoywifi ( ) {
mCnt = 0 ;
mConnected = false ;
mInitNtp = true ;
}
//-----------------------------------------------------------------------------
void ahoywifi : : setup ( uint32_t timeout , bool settingValid ) {
//wifiConnectHandler = WiFi.onStationModeGotIP(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1));
//wifiDisconnectHandler = WiFi.onStationModeDisconnected(std::bind(&ahoywifi::onDisconnect, this, std::placeholders::_1));
# ifdef FB_WIFI_OVERRIDDEN
mStationWifiIsDef = false ;
# else
mStationWifiIsDef = ( strncmp ( mConfig - > sys . stationSsid , FB_WIFI_SSID , 14 ) = = 0 ) ;
void ahoywifi : : setup ( settings_t * config , uint32_t * utcTimestamp ) {
char ipSta [ 16 ] ;
mConfig = config ;
mUtcTimestamp = utcTimestamp ;
# if !defined(FB_WIFI_OVERRIDDEN)
if ( strncmp ( mConfig - > sys . stationSsid , FB_WIFI_SSID , 14 ) ! = 0 )
setupAp ( ) ;
# endif
mWifiStationTimeout = timeout ;
# ifndef AP_ONLY
if ( false = = mApActive )
mApActive = ( mStationWifiIsDef ) ? true : setupStation ( mWifiStationTimeout ) ;
# if !defined(AP_ONLY)
setupStation ( ) ;
ah : : ip2Char ( mConfig - > sys . ip . ip , ipSta ) ;
# endif
if ( ! settingValid ) {
DPRINTLN ( DBG_WARN , F ( " your settings are not valid! check [IP]/setup " ) ) ;
mApActive = true ;
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 ( " \n point 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 " ) ) ;
}
wifiConnectHandler = WiFi . onStationModeGotIP ( std : : bind ( & ahoywifi : : onConnect , this , std : : placeholders : : _1 ) ) ;
wifiDisconnectHandler = WiFi . onStationModeDisconnected ( std : : bind ( & ahoywifi : : onDisconnect , this , std : : placeholders : : _1 ) ) ;
}
//-----------------------------------------------------------------------------
bool ahoywifi : : loop ( void ) {
if ( mApActive ) {
mDns - > processNextRequest ( ) ;
# ifndef AP_ONLY
if ( ah : : checkTicker ( & mNextTryTs , ( WIFI_AP_ACTIVE_TIME * 1000 ) ) ) {
mApActive = ( mStationWifiIsDef ) ? true : setupStation ( mWifiStationTimeout ) ;
if ( mApActive ) {
if ( strlen ( WIFI_AP_PWD ) < 8 )
DPRINTLN ( DBG_ERROR , F ( " password must be at least 8 characters long " ) ) ;
mApLastTick = millis ( ) ;
mNextTryTs = ( millis ( ) + ( WIFI_AP_ACTIVE_TIME * 1000 ) ) ;
setupAp ( WIFI_AP_SSID , WIFI_AP_PWD ) ;
}
}
else {
if ( millis ( ) - mApLastTick > 10000 ) {
mApLastTick = millis ( ) ;
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 " ) ) ;
}
void ahoywifi : : loop ( ) {
# if !defined(AP_ONLY)
if ( ! mConnected ) {
delay ( 100 ) ;
mCnt + + ;
if ( ( mCnt % 50 ) = = 0 )
WiFi . disconnect ( ) ;
else if ( ( mCnt % 60 ) = = 0 ) {
WiFi . reconnect ( ) ;
mCnt = 0 ;
}
} else if ( mInitNtp ) {
getNtpTime ( ) ;
mInitNtp = false ;
}
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 * pw d) {
DPRINTLN ( DBG_VERBOSE , F ( " app ::setupAp" ) ) ;
void ahoywifi : : setupAp ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " wifi::setupAp " ) ) ;
IPAddress apIp ( 192 , 168 , 4 , 1 ) ;
DBGPRINTLN ( F ( " \n --------- \n Ahoy Info: " ) ) ;
DBGPRINTLN ( F ( " \n --------- \n AhoyDTU Info: " ) ) ;
DBGPRINT ( F ( " Version: " ) ) ;
DBGPRINTLN ( String ( VERSION_MAJOR ) + F ( " . " ) + String ( VERSION_MINOR ) + F ( " . " ) + String ( VERSION_PATCH ) ) ;
DBGPRINT ( F ( " Github Hash: " ) ) ;
DBGPRINTLN ( String ( AUTO_GIT_HASH ) ) ;
DBGPRINT ( F ( " \n --------- \n AP MODE \n SSID: " ) ) ;
DBGPRINTLN ( ssid ) ;
DBGPRINTLN ( WIFI_AP_SSID ) ;
DBGPRINT ( F ( " PWD: " ) ) ;
DBGPRINTLN ( pwd ) ;
DBGPRINT ( F ( " \n Active for: " ) ) ;
DBGPRINT ( String ( WIFI_AP_ACTIVE_TIME ) ) ;
DBGPRINTLN ( F ( " seconds " ) ) ;
DBGPRINTLN ( WIFI_AP_PWD ) ;
DBGPRINTLN ( " IP Address: http:// " + apIp . toString ( ) ) ;
DBGPRINTLN ( F ( " --------- \n " ) ) ;
DBGPRINTLN ( " \n Ip Address: " + apIp [ 0 ] + apIp [ 1 ] + apIp [ 2 ] + apIp [ 3 ] ) ;
DBGPRINTLN ( F ( " \n --------- \n " ) ) ;
WiFi . mode ( WIFI_AP ) ;
WiFi . mode ( WIFI_AP_STA ) ;
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 ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::setupStation " ) ) ;
int32_t cnt ;
bool startAp = false ;
if ( timeout > = 3 )
cnt = ( timeout - 3 ) / 2 * 10 ;
else {
timeout = 1 ;
cnt = 1 ;
}
WiFi . mode ( WIFI_STA ) ;
void ahoywifi : : setupStation ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " wifi::setupStation " ) ) ;
if ( mConfig - > sys . ip . ip [ 0 ] ! = 0 ) {
IPAddress ip ( mConfig - > sys . ip . ip ) ;
IPAddress mask ( mConfig - > sys . ip . mask ) ;
@ -167,45 +107,14 @@ bool ahoywifi::setupStation(uint32_t timeout) {
if ( String ( mConfig - > sys . deviceName ) ! = " " )
WiFi . hostname ( mConfig - > sys . deviceName ) ;
delay ( 2000 ) ;
DBGPRINT ( F ( " connect to network ' " ) ) ;
DBGPRINT ( mConfig - > sys . stationSsid ) ;
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 ) {
return mApActive ;
}
//-----------------------------------------------------------------------------
time_t ahoywifi : : getNtpTime ( void ) {
void ahoywifi : : getNtpTime ( void ) {
//DPRINTLN(DBG_VERBOSE, F("wifi::getNtpTime"));
time_t date = 0 ;
IPAddress timeServer ;
@ -213,16 +122,16 @@ time_t ahoywifi::getNtpTime(void) {
uint8_t retry = 0 ;
WiFi . hostByName ( mConfig - > ntp . addr , timeServer ) ;
mUdp - > begin ( mConfig - > ntp . port ) ;
mUdp . begin ( mConfig - > ntp . port ) ;
sendNTPpacket ( timeServer ) ;
while ( retry + + < 5 ) {
int wait = 150 ;
while ( - - wait ) {
if ( NTP_PACKET_SIZE < = mUdp - > parsePacket ( ) ) {
if ( NTP_PACKET_SIZE < = mUdp . parsePacket ( ) ) {
uint64_t secsSince1900 ;
mUdp - > read ( buf , NTP_PACKET_SIZE ) ;
mUdp . read ( buf , NTP_PACKET_SIZE ) ;
secsSince1900 = ( buf [ 40 ] < < 24 ) ;
secsSince1900 | = ( buf [ 41 ] < < 16 ) ;
secsSince1900 | = ( buf [ 42 ] < < 8 ) ;
@ -230,13 +139,14 @@ time_t ahoywifi::getNtpTime(void) {
date = secsSince1900 - 2208988800UL ; // UTC time
break ;
}
else
} else
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 [ 15 ] = 52 ;
mUdp - > beginPacket ( address , 123 ) ; // NTP request, port 123
mUdp - > write ( buf , NTP_PACKET_SIZE ) ;
mUdp - > endPacket ( ) ;
mUdp . beginPacket ( address , 123 ) ; // NTP request, port 123
mUdp . write ( buf , NTP_PACKET_SIZE ) ;
mUdp . endPacket ( ) ;
}
//-----------------------------------------------------------------------------
/*void ahoywifi::onConnect(const WiFiEventStationModeGotIP& event) {
Serial . println ( " Connected to Wi-Fi. " ) ;
void ahoywifi : : onConnect ( const WiFiEventStationModeGotIP & event ) {
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 ) {
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 ( " \n point your browser to http:// " ) ) ;
DBGPRINTLN ( msg ) ;
DBGPRINTLN ( F ( " to configure your device " ) ) ;
DBGPRINTLN ( F ( " -------------------------------- \n " ) ) ;
}