|
@ -25,6 +25,7 @@ void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb) { |
|
|
mStaConn = DISCONNECTED; |
|
|
mStaConn = DISCONNECTED; |
|
|
mCnt = 0; |
|
|
mCnt = 0; |
|
|
mScanActive = false; |
|
|
mScanActive = false; |
|
|
|
|
|
mLastApClients = 0; |
|
|
|
|
|
|
|
|
#if defined(ESP8266) |
|
|
#if defined(ESP8266) |
|
|
wifiConnectHandler = WiFi.onStationModeConnected(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1)); |
|
|
wifiConnectHandler = WiFi.onStationModeConnected(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1)); |
|
@ -64,21 +65,44 @@ void ahoywifi::tickWifiLoop() { |
|
|
#if !defined(AP_ONLY) |
|
|
#if !defined(AP_ONLY) |
|
|
if(mStaConn != GOT_IP) { |
|
|
if(mStaConn != GOT_IP) { |
|
|
if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists
|
|
|
if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists
|
|
|
mDns.processNextRequest(); |
|
|
if(WIFI_AP_STA == WiFi.getMode()) { |
|
|
if((WIFI_AP_STA == WiFi.getMode()) && !mScanActive) { |
|
|
if(mScanActive && (mLastApClients != WiFi.softAPgetStationNum())) |
|
|
DBGPRINTLN(F("AP client connected")); |
|
|
mScanActive = false; |
|
|
welcome(mApIp.toString()); |
|
|
|
|
|
|
|
|
if(mLastApClients != WiFi.softAPgetStationNum()) { |
|
|
|
|
|
mLastApClients = WiFi.softAPgetStationNum(); |
|
|
|
|
|
WiFi.scanDelete(); |
|
|
WiFi.mode(WIFI_AP); |
|
|
WiFi.mode(WIFI_AP); |
|
|
|
|
|
//mDns.start(53, "*", mApIp);
|
|
|
mAppWifiCb(true); |
|
|
mAppWifiCb(true); |
|
|
|
|
|
DBGPRINTLN(F("AP client connected")); |
|
|
|
|
|
welcome(mApIp.toString()); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
mDns.processNextRequest(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
else if(WIFI_AP == WiFi.getMode()) { |
|
|
else if(WIFI_AP == WiFi.getMode()) { |
|
|
|
|
|
mLastApClients = 0; |
|
|
mCnt = 0; |
|
|
mCnt = 0; |
|
|
|
|
|
DPRINTLN(DBG_INFO, "DNS stop"); |
|
|
|
|
|
mDns.stop(); |
|
|
WiFi.mode(WIFI_AP_STA); |
|
|
WiFi.mode(WIFI_AP_STA); |
|
|
} |
|
|
} |
|
|
mCnt++; |
|
|
mCnt++; |
|
|
|
|
|
|
|
|
|
|
|
if(!mScanActive && mBSSIDList.empty()) { // start scanning APs with the given SSID
|
|
|
|
|
|
DBGPRINT(F("scanning APs with SSID ")); |
|
|
|
|
|
DBGPRINTLN(String(mConfig->sys.stationSsid)); |
|
|
|
|
|
mScanActive = true; |
|
|
|
|
|
#if defined(ESP8266) |
|
|
|
|
|
WiFi.scanNetworks(true, false, 0U, (uint8_t *)mConfig->sys.stationSsid); |
|
|
|
|
|
#else |
|
|
|
|
|
WiFi.scanNetworks(true, false, false, 300U, 0U, mConfig->sys.stationSsid); |
|
|
|
|
|
#endif |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
uint8_t timeout = 10; // seconds
|
|
|
uint8_t timeout = 10; // seconds
|
|
|
|
|
|
|
|
|
if (mStaConn == CONNECTED) // connected but no ip
|
|
|
if (mStaConn == CONNECTED) // connected but no ip
|
|
@ -87,10 +111,27 @@ void ahoywifi::tickWifiLoop() { |
|
|
DBGPRINT(F("reconnect in ")); |
|
|
DBGPRINT(F("reconnect in ")); |
|
|
DBGPRINT(String(timeout-mCnt)); |
|
|
DBGPRINT(String(timeout-mCnt)); |
|
|
DBGPRINTLN(F(" seconds")); |
|
|
DBGPRINTLN(F(" seconds")); |
|
|
|
|
|
if(mScanActive) { |
|
|
|
|
|
getBSSIDs(); |
|
|
|
|
|
if(!mScanActive) // scan completed
|
|
|
|
|
|
if ((mCnt % timeout) < 8) |
|
|
|
|
|
mCnt = timeout - 2; |
|
|
|
|
|
} |
|
|
if((mCnt % timeout) == 0) { // try to reconnect after x sec without connection
|
|
|
if((mCnt % timeout) == 0) { // try to reconnect after x sec without connection
|
|
|
if(mStaConn != CONNECTED) |
|
|
if(mStaConn != CONNECTED) |
|
|
mStaConn = CONNECTING; |
|
|
mStaConn = CONNECTING; |
|
|
WiFi.reconnect(); |
|
|
if(mBSSIDList.size() > 0) { // get first BSSID in list
|
|
|
|
|
|
DBGPRINT("try to connect to AP with BSSID:"); |
|
|
|
|
|
uint8_t bssid[6]; |
|
|
|
|
|
for (int j = 0; j < 6; j++) { |
|
|
|
|
|
bssid[j] = mBSSIDList.front(); |
|
|
|
|
|
mBSSIDList.pop_front(); |
|
|
|
|
|
DBGPRINT(" " + String(bssid[j], HEX)); |
|
|
|
|
|
} |
|
|
|
|
|
DBGPRINTLN(""); |
|
|
|
|
|
WiFi.disconnect(); |
|
|
|
|
|
WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd, 0, &bssid[0]); |
|
|
|
|
|
} |
|
|
mCnt = 0; |
|
|
mCnt = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -135,7 +176,7 @@ void ahoywifi::setupStation(void) { |
|
|
if(!WiFi.config(ip, gateway, mask, dns1, dns2)) |
|
|
if(!WiFi.config(ip, gateway, mask, dns1, dns2)) |
|
|
DPRINTLN(DBG_ERROR, F("failed to set static IP!")); |
|
|
DPRINTLN(DBG_ERROR, F("failed to set static IP!")); |
|
|
} |
|
|
} |
|
|
mStaConn = (WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd) != WL_CONNECTED) ? DISCONNECTED : CONNECTED; |
|
|
mBSSIDList.clear(); |
|
|
if(String(mConfig->sys.deviceName) != "") |
|
|
if(String(mConfig->sys.deviceName) != "") |
|
|
WiFi.hostname(mConfig->sys.deviceName); |
|
|
WiFi.hostname(mConfig->sys.deviceName); |
|
|
WiFi.mode(WIFI_AP_STA); |
|
|
WiFi.mode(WIFI_AP_STA); |
|
@ -206,40 +247,72 @@ void ahoywifi::sendNTPpacket(IPAddress& address) { |
|
|
mUdp.endPacket(); |
|
|
mUdp.endPacket(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
void ahoywifi::sortRSSI(int *sort, int n) { |
|
|
|
|
|
for (int i = 0; i < n; i++) |
|
|
|
|
|
sort[i] = i; |
|
|
|
|
|
for (int i = 0; i < n; i++) |
|
|
|
|
|
for (int j = i + 1; j < n; j++) |
|
|
|
|
|
if (WiFi.RSSI(sort[j]) > WiFi.RSSI(sort[i])) |
|
|
|
|
|
std::swap(sort[i], sort[j]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
//-----------------------------------------------------------------------------
|
|
|
void ahoywifi::scanAvailNetworks(void) { |
|
|
void ahoywifi::scanAvailNetworks(void) { |
|
|
|
|
|
DPRINTLN(DBG_INFO, "start scan"); |
|
|
if(-2 == WiFi.scanComplete()) { |
|
|
if(-2 == WiFi.scanComplete()) { |
|
|
mScanActive = true; |
|
|
mScanActive = true; |
|
|
if(WIFI_AP == WiFi.getMode()) |
|
|
if(WIFI_AP == WiFi.getMode()) |
|
|
WiFi.mode(WIFI_AP_STA); |
|
|
WiFi.mode(WIFI_AP_STA); |
|
|
WiFi.scanNetworks(true); |
|
|
WiFi.scanNetworks(true); |
|
|
} |
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
DPRINTLN(DBG_INFO, "complete!"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
//-----------------------------------------------------------------------------
|
|
|
void ahoywifi::getAvailNetworks(JsonObject obj) { |
|
|
void ahoywifi::getAvailNetworks(JsonObject obj) { |
|
|
JsonArray nets = obj.createNestedArray("networks"); |
|
|
JsonArray nets = obj.createNestedArray("networks"); |
|
|
|
|
|
DPRINTLN(DBG_INFO, "getAvailNetworks"); |
|
|
|
|
|
|
|
|
int n = WiFi.scanComplete(); |
|
|
int n = WiFi.scanComplete(); |
|
|
|
|
|
if (n < 0) |
|
|
|
|
|
return; |
|
|
if(n > 0) { |
|
|
if(n > 0) { |
|
|
int sort[n]; |
|
|
int sort[n]; |
|
|
for (int i = 0; i < n; i++) |
|
|
sortRSSI(&sort[0], n); |
|
|
sort[i] = i; |
|
|
|
|
|
for (int i = 0; i < n; i++) |
|
|
|
|
|
for (int j = i + 1; j < n; j++) |
|
|
|
|
|
if (WiFi.RSSI(sort[j]) > WiFi.RSSI(sort[i])) |
|
|
|
|
|
std::swap(sort[i], sort[j]); |
|
|
|
|
|
for (int i = 0; i < n; ++i) { |
|
|
for (int i = 0; i < n; ++i) { |
|
|
|
|
|
DPRINTLN(DBG_INFO, "found network: " + WiFi.SSID(sort[i])); |
|
|
nets[i]["ssid"] = WiFi.SSID(sort[i]); |
|
|
nets[i]["ssid"] = WiFi.SSID(sort[i]); |
|
|
nets[i]["rssi"] = WiFi.RSSI(sort[i]); |
|
|
nets[i]["rssi"] = WiFi.RSSI(sort[i]); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
mScanActive = false; |
|
|
mScanActive = false; |
|
|
WiFi.scanDelete(); |
|
|
WiFi.scanDelete(); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
void ahoywifi::getBSSIDs() { |
|
|
|
|
|
int n = WiFi.scanComplete(); |
|
|
|
|
|
if (n < 0) |
|
|
|
|
|
return; |
|
|
|
|
|
if(n > 0) { |
|
|
|
|
|
mBSSIDList.clear(); |
|
|
|
|
|
int sort[n]; |
|
|
|
|
|
sortRSSI(&sort[0], n); |
|
|
|
|
|
for (int i = 0; i < n; i++) { |
|
|
|
|
|
DBGPRINT("BSSID " + String(i) + ":"); |
|
|
|
|
|
uint8_t *bssid = WiFi.BSSID(sort[i]); |
|
|
|
|
|
for (int j = 0; j < 6; j++){ |
|
|
|
|
|
DBGPRINT(" " + String(bssid[j], HEX)); |
|
|
|
|
|
mBSSIDList.push_back(bssid[j]); |
|
|
|
|
|
} |
|
|
|
|
|
DBGPRINTLN(""); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
mScanActive = false; |
|
|
|
|
|
WiFi.scanDelete(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
//-----------------------------------------------------------------------------
|
|
|
void ahoywifi::connectionEvent(WiFiStatus_t status) { |
|
|
void ahoywifi::connectionEvent(WiFiStatus_t status) { |
|
@ -248,15 +321,19 @@ void ahoywifi::connectionEvent(WiFiStatus_t status) { |
|
|
if(mStaConn != CONNECTED) { |
|
|
if(mStaConn != CONNECTED) { |
|
|
mStaConn = CONNECTED; |
|
|
mStaConn = CONNECTED; |
|
|
DBGPRINTLN(F("\n[WiFi] Connected")); |
|
|
DBGPRINTLN(F("\n[WiFi] Connected")); |
|
|
WiFi.mode(WIFI_STA); |
|
|
|
|
|
DBGPRINTLN(F("[WiFi] AP disabled")); |
|
|
|
|
|
mDns.stop(); |
|
|
|
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case GOT_IP: |
|
|
case GOT_IP: |
|
|
mStaConn = GOT_IP; |
|
|
mStaConn = GOT_IP; |
|
|
|
|
|
if (mScanActive) { // maybe another scan has started
|
|
|
|
|
|
WiFi.scanDelete(); |
|
|
|
|
|
mScanActive = false; |
|
|
|
|
|
} |
|
|
welcome(WiFi.localIP().toString() + F(" (Station)")); |
|
|
welcome(WiFi.localIP().toString() + F(" (Station)")); |
|
|
|
|
|
mDns.stop(); |
|
|
|
|
|
WiFi.mode(WIFI_STA); |
|
|
|
|
|
DBGPRINTLN(F("[WiFi] AP disabled")); |
|
|
mAppWifiCb(true); |
|
|
mAppWifiCb(true); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|