@ -10,11 +10,26 @@ Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
# include <Ticker.h>
# include <Ticker.h>
uint32_t _wifi_scan_client_id = 0 ;
uint32_t _wifi_scan_client_id = 0 ;
bool _wifi_wps_running = false ;
bool _wifi_smartconfig_running = false ;
uint8_t _wifi_ap_mode = WIFI_AP_FALLBACK ;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// PRIVATE
// PRIVATE
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void _wifiCheckAP ( ) {
if ( ( WIFI_AP_FALLBACK = = _wifi_ap_mode ) & &
( jw . connected ( ) ) & &
( ( WiFi . getMode ( ) & WIFI_AP ) > 0 ) & &
( WiFi . softAPgetStationNum ( ) = = 0 )
) {
jw . enableAP ( false ) ;
}
}
void _wifiConfigure ( ) {
void _wifiConfigure ( ) {
jw . setHostname ( getSetting ( " hostname " ) . c_str ( ) ) ;
jw . setHostname ( getSetting ( " hostname " ) . c_str ( ) ) ;
@ -25,9 +40,11 @@ void _wifiConfigure() {
# endif
# endif
jw . setConnectTimeout ( WIFI_CONNECT_TIMEOUT ) ;
jw . setConnectTimeout ( WIFI_CONNECT_TIMEOUT ) ;
wifiReconnectCheck ( ) ;
wifiReconnectCheck ( ) ;
jw . setAPMode ( WIFI_AP_MODE ) ;
jw . enableAPFallback ( true ) ;
jw . cleanNetworks ( ) ;
jw . cleanNetworks ( ) ;
_wifi_ap_mode = getSetting ( " apmode " , WIFI_AP_FALLBACK ) . toInt ( ) ;
// If system is flagged unstable we do not init wifi networks
// If system is flagged unstable we do not init wifi networks
# if SYSTEM_CHECK_ENABLED
# if SYSTEM_CHECK_ENABLED
if ( ! systemCheck ( ) ) return ;
if ( ! systemCheck ( ) ) return ;
@ -56,7 +73,7 @@ void _wifiConfigure() {
}
}
}
}
jw . scanNetworks ( getSetting ( " wifiScan " , WIFI_SCAN_NETWORKS ) . toInt ( ) = = 1 ) ;
jw . enableScan ( getSetting ( " wifiScan " , WIFI_SCAN_NETWORKS ) . toInt ( ) = = 1 ) ;
}
}
@ -196,6 +213,47 @@ void _wifiInject() {
}
}
}
}
void _wifiCallback ( justwifi_messages_t code , char * parameter ) {
if ( MESSAGE_WPS_START = = code ) {
_wifi_wps_running = true ;
}
if ( MESSAGE_SMARTCONFIG_START = = code ) {
_wifi_smartconfig_running = true ;
}
if ( MESSAGE_WPS_ERROR = = code | | MESSAGE_SMARTCONFIG_ERROR = = code ) {
_wifi_wps_running = false ;
_wifi_smartconfig_running = false ;
}
if ( MESSAGE_WPS_SUCCESS = = code | | MESSAGE_SMARTCONFIG_SUCCESS = = code ) {
String ssid = WiFi . SSID ( ) ;
String pass = WiFi . psk ( ) ;
// Look for the same SSID
uint8_t count = 0 ;
while ( count < WIFI_MAX_NETWORKS ) {
if ( ! hasSetting ( " ssid " , count ) ) break ;
if ( ssid . equals ( getSetting ( " ssid " , count , " " ) ) ) break ;
count + + ;
}
// If we have reached the max we overwrite the first one
if ( WIFI_MAX_NETWORKS = = count ) count = 0 ;
setSetting ( " ssid " , count , ssid ) ;
setSetting ( " pass " , count , pass ) ;
_wifi_wps_running = false ;
_wifi_smartconfig_running = false ;
}
}
# if WIFI_AP_CAPTIVE
# if WIFI_AP_CAPTIVE
# include "DNSServer.h"
# include "DNSServer.h"
@ -221,7 +279,9 @@ void _wifiCaptivePortal(justwifi_messages_t code, char * parameter) {
# if DEBUG_SUPPORT
# if DEBUG_SUPPORT
void _wifiDebug ( justwifi_messages_t code , char * parameter ) {
void _wifiDebugCallback ( justwifi_messages_t code , char * parameter ) {
// -------------------------------------------------------------------------
if ( code = = MESSAGE_SCANNING ) {
if ( code = = MESSAGE_SCANNING ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] Scanning \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] Scanning \n " ) ) ;
@ -243,6 +303,8 @@ void _wifiDebug(justwifi_messages_t code, char * parameter) {
DEBUG_MSG_P ( PSTR ( " [WIFI] %s \n " ) , parameter ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] %s \n " ) , parameter ) ;
}
}
// -------------------------------------------------------------------------
if ( code = = MESSAGE_CONNECTING ) {
if ( code = = MESSAGE_CONNECTING ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] Connecting to %s \n " ) , parameter ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] Connecting to %s \n " ) , parameter ) ;
}
}
@ -256,25 +318,59 @@ void _wifiDebug(justwifi_messages_t code, char * parameter) {
}
}
if ( code = = MESSAGE_CONNECTED ) {
if ( code = = MESSAGE_CONNECTED ) {
wifiStatus ( ) ;
}
if ( code = = MESSAGE_ACCESSPOINT_CREATED ) {
wifiStatus ( ) ;
wifiDebug ( WIFI_STA ) ;
}
}
if ( code = = MESSAGE_DISCONNECTED ) {
if ( code = = MESSAGE_DISCONNECTED ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] Disconnected \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] Disconnected \n " ) ) ;
}
}
// -------------------------------------------------------------------------
if ( code = = MESSAGE_ACCESSPOINT_CREATING ) {
if ( code = = MESSAGE_ACCESSPOINT_CREATING ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] Creating access point \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] Creating access point \n " ) ) ;
}
}
if ( code = = MESSAGE_ACCESSPOINT_CREATED ) {
wifiDebug ( WIFI_AP ) ;
}
if ( code = = MESSAGE_ACCESSPOINT_FAILED ) {
if ( code = = MESSAGE_ACCESSPOINT_FAILED ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] Could not create access point \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] Could not create access point \n " ) ) ;
}
}
if ( code = = MESSAGE_ACCESSPOINT_DESTROYED ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] Access point destroyed \n " ) ) ;
}
// -------------------------------------------------------------------------
if ( code = = MESSAGE_WPS_START ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] WPS started \n " ) ) ;
}
if ( code = = MESSAGE_WPS_SUCCESS ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] WPS succeded! \n " ) ) ;
}
if ( code = = MESSAGE_WPS_ERROR ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] WPS failed \n " ) ) ;
}
// ------------------------------------------------------------------------
if ( code = = MESSAGE_SMARTCONFIG_START ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] Smart Config started \n " ) ) ;
}
if ( code = = MESSAGE_SMARTCONFIG_SUCCESS ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] Smart Config succeded! \n " ) ) ;
}
if ( code = = MESSAGE_SMARTCONFIG_ERROR ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] Smart Config failed \n " ) ) ;
}
}
}
# endif // DEBUG_SUPPORT
# endif // DEBUG_SUPPORT
@ -294,10 +390,24 @@ void _wifiInitCommands() {
} ) ;
} ) ;
settingsRegisterCommand ( F ( " WIFI.AP " ) , [ ] ( Embedis * e ) {
settingsRegisterCommand ( F ( " WIFI.AP " ) , [ ] ( Embedis * e ) {
create AP( ) ;
wifiStart AP( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
} ) ;
# if defined(JUSTWIFI_ENABLE_WPS)
settingsRegisterCommand ( F ( " WIFI.WPS " ) , [ ] ( Embedis * e ) {
wifiStartWPS ( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
# endif // defined(JUSTWIFI_ENABLE_WPS)
# if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
settingsRegisterCommand ( F ( " WIFI.SMARTCONFIG " ) , [ ] ( Embedis * e ) {
wifiStartSmartConfig ( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
# endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
settingsRegisterCommand ( F ( " WIFI.SCAN " ) , [ ] ( Embedis * e ) {
settingsRegisterCommand ( F ( " WIFI.SCAN " ) , [ ] ( Embedis * e ) {
_wifiScan ( ) ;
_wifiScan ( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
@ -346,6 +456,59 @@ void _wifiWebSocketOnAction(uint32_t client_id, const char * action, JsonObject&
# endif
# endif
// -----------------------------------------------------------------------------
// INFO
// -----------------------------------------------------------------------------
void wifiDebug ( WiFiMode_t modes ) {
bool footer = false ;
if ( ( ( modes & WIFI_STA ) > 0 ) & & ( ( WiFi . getMode ( ) & WIFI_STA ) > 0 ) ) {
uint8_t * bssid = WiFi . BSSID ( ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] ------------------------------------- MODE STA \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] SSID %s \n " ) , WiFi . SSID ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] IP %s \n " ) , WiFi . localIP ( ) . toString ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] MAC %s \n " ) , WiFi . macAddress ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] GW %s \n " ) , WiFi . gatewayIP ( ) . toString ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] DNS %s \n " ) , WiFi . dnsIP ( ) . toString ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] MASK %s \n " ) , WiFi . subnetMask ( ) . toString ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] HOST http://%s.local \n " ) , WiFi . hostname ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] BSSID %02X:%02X:%02X:%02X:%02X:%02X \n " ) ,
bssid [ 0 ] , bssid [ 1 ] , bssid [ 2 ] , bssid [ 3 ] , bssid [ 4 ] , bssid [ 5 ] , bssid [ 6 ]
) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] CH %d \n " ) , WiFi . channel ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] RSSI %d \n " ) , WiFi . RSSI ( ) ) ;
footer = true ;
}
if ( ( ( modes & WIFI_AP ) > 0 ) & & ( ( WiFi . getMode ( ) & WIFI_AP ) > 0 ) ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] -------------------------------------- MODE AP \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] SSID %s \n " ) , getSetting ( " hostname " ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] PASS %s \n " ) , getSetting ( " adminPass " , ADMIN_PASS ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] IP %s \n " ) , WiFi . softAPIP ( ) . toString ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] MAC %s \n " ) , WiFi . softAPmacAddress ( ) . c_str ( ) ) ;
footer = true ;
}
if ( WiFi . getMode ( ) = = 0 ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] ------------------------------------- MODE OFF \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] No connection \n " ) ) ;
footer = true ;
}
if ( footer ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] ---------------------------------------------- \n " ) ) ;
}
}
void wifiDebug ( ) {
wifiDebug ( WIFI_AP_STA ) ;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// API
// API
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
@ -372,11 +535,30 @@ void wifiDisconnect() {
jw . disconnect ( ) ;
jw . disconnect ( ) ;
}
}
bool createAP ( ) {
jw . disconnect ( ) ;
jw . resetReconnectTimeout ( ) ;
return jw . createAP ( ) ;
void wifiStartAP ( bool only ) {
if ( only ) {
jw . enableSTA ( false ) ;
jw . disconnect ( ) ;
jw . resetReconnectTimeout ( ) ;
}
jw . enableAP ( true ) ;
}
void wifiStartAP ( ) {
wifiStartAP ( true ) ;
}
# if defined(JUSTWIFI_ENABLE_WPS)
void wifiStartWPS ( ) {
jw . startWPS ( ) ;
}
}
# endif // defined(JUSTWIFI_ENABLE_WPS)
# if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
void wifiStartSmartConfig ( ) {
jw . startSmartConfig ( ) ;
}
# endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
void wifiReconnectCheck ( ) {
void wifiReconnectCheck ( ) {
bool connected = false ;
bool connected = false ;
@ -389,44 +571,13 @@ void wifiReconnectCheck() {
jw . setReconnectTimeout ( connected ? 0 : WIFI_RECONNECT_INTERVAL ) ;
jw . setReconnectTimeout ( connected ? 0 : WIFI_RECONNECT_INTERVAL ) ;
}
}
void wifiStatus ( ) {
if ( WiFi . getMode ( ) = = WIFI_AP_STA ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] MODE AP + STA -------------------------------- \n " ) ) ;
} else if ( WiFi . getMode ( ) = = WIFI_AP ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] MODE AP -------------------------------------- \n " ) ) ;
} else if ( WiFi . getMode ( ) = = WIFI_STA ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] MODE STA ------------------------------------- \n " ) ) ;
} else {
DEBUG_MSG_P ( PSTR ( " [WIFI] MODE OFF ------------------------------------- \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] No connection \n " ) ) ;
}
if ( ( WiFi . getMode ( ) & WIFI_AP ) = = WIFI_AP ) {
DEBUG_MSG_P ( PSTR ( " [WIFI] SSID %s \n " ) , jw . getAPSSID ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] PASS %s \n " ) , getSetting ( " adminPass " , ADMIN_PASS ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] IP %s \n " ) , WiFi . softAPIP ( ) . toString ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] MAC %s \n " ) , WiFi . softAPmacAddress ( ) . c_str ( ) ) ;
}
if ( ( WiFi . getMode ( ) & WIFI_STA ) = = WIFI_STA ) {
uint8_t * bssid = WiFi . BSSID ( ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] SSID %s \n " ) , WiFi . SSID ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] IP %s \n " ) , WiFi . localIP ( ) . toString ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] MAC %s \n " ) , WiFi . macAddress ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] GW %s \n " ) , WiFi . gatewayIP ( ) . toString ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] DNS %s \n " ) , WiFi . dnsIP ( ) . toString ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] MASK %s \n " ) , WiFi . subnetMask ( ) . toString ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] HOST http://%s.local \n " ) , WiFi . hostname ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] BSSID %02X:%02X:%02X:%02X:%02X:%02X \n " ) ,
bssid [ 0 ] , bssid [ 1 ] , bssid [ 2 ] , bssid [ 3 ] , bssid [ 4 ] , bssid [ 5 ] , bssid [ 6 ]
) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] CH %d \n " ) , WiFi . channel ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [WIFI] RSSI %d \n " ) , WiFi . RSSI ( ) ) ;
}
DEBUG_MSG_P ( PSTR ( " [WIFI] ---------------------------------------------- \n " ) ) ;
uint8_t wifiState ( ) {
uint8_t state = 0 ;
if ( jw . connected ( ) ) state + = WIFI_STATE_STA ;
if ( jw . connectable ( ) ) state + = WIFI_STATE_AP ;
if ( _wifi_wps_running ) state + = WIFI_STATE_WPS ;
if ( _wifi_smartconfig_running ) state + = WIFI_STATE_SMARTCONFIG ;
return state ;
}
}
void wifiRegister ( wifi_callback_f callback ) {
void wifiRegister ( wifi_callback_f callback ) {
@ -445,11 +596,12 @@ void wifiSetup() {
_wifiConfigure ( ) ;
_wifiConfigure ( ) ;
// Message callbacks
// Message callbacks
wifiRegister ( _wifiCallback ) ;
# if WIFI_AP_CAPTIVE
# if WIFI_AP_CAPTIVE
wifiRegister ( _wifiCaptivePortal ) ;
wifiRegister ( _wifiCaptivePortal ) ;
# endif
# endif
# if DEBUG_SUPPORT
# if DEBUG_SUPPORT
wifiRegister ( _wifiDebug ) ;
wifiRegister ( _wifiDebugCallback ) ;
# endif
# endif
# if WEB_SUPPORT
# if WEB_SUPPORT
@ -470,17 +622,27 @@ void wifiSetup() {
void wifiLoop ( ) {
void wifiLoop ( ) {
// Main wifi loop
jw . loop ( ) ;
jw . loop ( ) ;
// Process captrive portal DNS queries if in AP mode only
# if WIFI_AP_CAPTIVE
# if WIFI_AP_CAPTIVE
if ( ( WiFi . getMode ( ) & WIFI_AP ) = = WIFI_AP ) {
if ( ( WiFi . getMode ( ) & WIFI_AP ) = = WIFI_AP ) {
_wifi_dnsServer . processNextRequest ( ) ;
_wifi_dnsServer . processNextRequest ( ) ;
}
}
# endif
# endif
// Do we have a pending scan?
if ( _wifi_scan_client_id > 0 ) {
if ( _wifi_scan_client_id > 0 ) {
_wifiScan ( _wifi_scan_client_id ) ;
_wifiScan ( _wifi_scan_client_id ) ;
_wifi_scan_client_id = 0 ;
_wifi_scan_client_id = 0 ;
}
}
// Check if we should disable AP
static unsigned long last = 0 ;
if ( millis ( ) - last > 60000 ) {
last = millis ( ) ;
_wifiCheckAP ( ) ;
}
}
}