Skip to content

Commit

Permalink
scanning for available networks before going to AP mode
Browse files Browse the repository at this point in the history
scan for wifi networks before going to config mode to avoid wifi mode changes
select channel with lowest signal for AP mode
  • Loading branch information
ricki-z committed Sep 12, 2018
1 parent 2593e49 commit 7c7f0c2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
4 changes: 4 additions & 0 deletions airrohr-firmware/Versions.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
NRZ-2018-110-B4
* scan for wifi networks before going to config mode to avoid wifi mode changes
* select channel with lowest signal for AP mode

NRZ-2018-110-B3
* cosmetic changes to get rid of some compiler warnings

Expand Down
76 changes: 56 additions & 20 deletions airrohr-firmware/airrohr-firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
*
************************************************************************/
// increment on change
#define SOFTWARE_VERSION "NRZ-2018-110-B3"
#define SOFTWARE_VERSION "NRZ-2018-110-B4"

/*****************************************************************
* Includes *
Expand Down Expand Up @@ -418,6 +418,18 @@ unsigned long count_sends = 0;
unsigned long next_display_millis = 0;
unsigned long next_display_count = 0;

struct struct_wifiInfo {

This comment has been minimized.

Copy link
@Informatic

Informatic Sep 13, 2018

Contributor

I don't think you need to copy this to user code.
https:/esp8266/Arduino/blob/74819a763bfb6e9890a57411dcea4aba221a778d/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp#L71
Arduino already keeps a copy of that in its own allocated memory, and this, actually, is never freed, unless you run another scan, as far as I understand.

This comment has been minimized.

Copy link
@ricki-z

ricki-z Sep 13, 2018

Author Member

The developers of the ESP8266 library have changed their code in version 2.4.x to save as much RAM as possible. So they may change this in future releases and may free the allocated memory. With that code we are independent from such possible changes.

char ssid[35];
uint8_t encryptionType;
int32_t RSSI;
int32_t channel;
bool isHidden;
};
uint8_t size_wifiInfo = 35 + sizeof(uint8_t) + sizeof(int32_t) + sizeof(uint8_t) + sizeof(bool);

struct struct_wifiInfo *wifiInfo;
uint8_t count_wifiInfo;

#define data_first_part "{\"software_version\": \"{v}\", \"sensordatavalues\":["

/*****************************************************************
Expand Down Expand Up @@ -1349,7 +1361,7 @@ void webserver_config() {
page_content += F("</table><br/>");
page_content += F("<br/></form>");
if (WiFi.status() != WL_CONNECTED) { // scan for wlan ssids
page_content += F("<script>window.setTimeout(load_wifi_list,3000);</script>");
page_content += F("<script>window.setTimeout(load_wifi_list,1000);</script>");
}
} else {

Expand Down Expand Up @@ -1478,53 +1490,50 @@ void webserver_config() {
* Webserver wifi: show available wifi networks *
*****************************************************************/
void webserver_wifi() {
WiFi.disconnect();
debug_out(F("scan for wifi networks..."), DEBUG_MIN_INFO, 1);
int n = WiFi.scanNetworks();
debug_out(F("wifi networks found: "), DEBUG_MIN_INFO, 0);
debug_out(String(n), DEBUG_MIN_INFO, 1);
debug_out(String(count_wifiInfo), DEBUG_MIN_INFO, 1);
String page_content = "";
if (n == 0) {
if (count_wifiInfo == 0) {
page_content += F("<br/>");
page_content += FPSTR(INTL_KEINE_NETZWERKE);
page_content += F("<br/>");
} else {
page_content += FPSTR(INTL_NETZWERKE_GEFUNDEN);
page_content += String(n);
page_content += String(count_wifiInfo);
page_content += F("<br/>");
int indices[n];
int indices[count_wifiInfo];
debug_out(F("output config page 2"), DEBUG_MIN_INFO, 1);
for (int i = 0; i < n; i++) {
for (int i = 0; i < count_wifiInfo; i++) {
indices[i] = i;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) {
for (int i = 0; i < count_wifiInfo; i++) {
for (int j = i + 1; j < count_wifiInfo; j++) {
if (wifiInfo[indices[j]].RSSI > wifiInfo[indices[i]].RSSI) {
std::swap(indices[i], indices[j]);
}
}
}
String cssid;
debug_out(F("output config page 3"), DEBUG_MIN_INFO, 1);
for (int i = 0; i < n; i++) {
for (int i = 0; i < count_wifiInfo; i++) {
if (indices[i] == -1) {
continue;
}
cssid = WiFi.SSID(indices[i]);
for (int j = i + 1; j < n; j++) {
if (cssid == WiFi.SSID(indices[j])) {
cssid = wifiInfo[indices[i]].ssid;
for (int j = i + 1; j < count_wifiInfo; j++) {
if (cssid == wifiInfo[indices[j]].ssid) {
indices[j] = -1; // set dup aps to index -1
}
}
}
page_content += F("<br/><table>");
//if(n > 30) n=30;
for (int i = 0; i < n; ++i) {
for (int i = 0; i < count_wifiInfo; ++i) {
if (indices[i] == -1) {
continue;
}
// Print SSID and RSSI for each network found
page_content += wlan_ssid_to_table_row(WiFi.SSID(indices[i]), ((WiFi.encryptionType(indices[i]) == ENC_TYPE_NONE) ? " " : "*"), WiFi.RSSI(indices[i]));
page_content += wlan_ssid_to_table_row(wifiInfo[indices[i]].ssid, ((wifiInfo[indices[i]].encryptionType == ENC_TYPE_NONE) ? " " : "*"), wifiInfo[indices[i]].RSSI);
}
page_content += F("</table><br/><br/>");
}
Expand Down Expand Up @@ -1865,6 +1874,10 @@ void setup_webserver() {
void wifiConfig() {
const byte DNS_PORT = 53;
int retry_count = 0;
String SSID;
uint8_t* BSSID;
int channels_rssi[14];
uint8_t AP_channel = 1;
DNSServer dnsServer;
IPAddress apIP(192, 168, 4, 1);
IPAddress netMsk(255, 255, 255, 0);
Expand All @@ -1878,9 +1891,30 @@ void wifiConfig() {
wificonfig_loop = true;

WiFi.disconnect(true);
debug_out(F("scan for wifi networks..."), DEBUG_MIN_INFO, 1);
count_wifiInfo = WiFi.scanNetworks(false, true);
wifiInfo = (struct_wifiInfo *) malloc(count_wifiInfo * 100);
for (int i = 0; i < 14; i++) {
channels_rssi[i] = -100;
}
for (int i = 0; i < count_wifiInfo; i++) {
WiFi.getNetworkInfo(i, SSID, wifiInfo[i].encryptionType, wifiInfo[i].RSSI, BSSID, wifiInfo[i].channel, wifiInfo[i].isHidden);
SSID.toCharArray(wifiInfo[i].ssid, 35);
if (wifiInfo[i].RSSI > channels_rssi[wifiInfo[i].channel]) {
channels_rssi[wifiInfo[i].channel] = wifiInfo[i].RSSI;
}
}
if ((channels_rssi[1] < channels_rssi[6]) && (channels_rssi[1] < channels_rssi[11])) {
AP_channel = 1;
} else if ((channels_rssi[6] < channels_rssi[1]) && (channels_rssi[6] < channels_rssi[11])) {
AP_channel = 6;
} else {
AP_channel = 11;
}

WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, netMsk);
WiFi.softAP(fs_ssid, fs_pwd);
WiFi.softAP(fs_ssid, fs_pwd, AP_channel);
debug_out(String(WLANPWD), DEBUG_MIN_INFO, 1);

dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
Expand All @@ -1907,6 +1941,8 @@ void wifiConfig() {
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);

free(wifiInfo);

dnsServer.stop();

delay(100);
Expand Down

0 comments on commit 7c7f0c2

Please sign in to comment.