Skip to content

Instantly share code, notes, and snippets.

@designer2k2
Last active November 15, 2024 21:33
Show Gist options
  • Save designer2k2/2dc8c4a06394fdba91f3655dc9be9728 to your computer and use it in GitHub Desktop.
Save designer2k2/2dc8c4a06394fdba91f3655dc9be9728 to your computer and use it in GitHub Desktop.
Realtek BW16 2.4GHz and 5GHz WiFi scanner
/*
Advanced dual channel WiFi Scan example for Ameba
This example scans for available Wifi networks using the build in functionality.
Every 3 seconds, it scans again.
It doesn't actually connect to any network.
based on the work of https://gist.github.com/EstebanFuentealba/3da9ccecefa7e1b44d84e7cfaad2f35f
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org/>
*/
#include <WiFi.h>
#include <wifi_conf.h>
#define SCAN_INTERVAL 3000
static uint8_t _networkCount;
static char _networkSsid[WL_NETWORKS_LIST_MAXNUM][WL_SSID_MAX_LENGTH];
static int32_t _networkRssi[WL_NETWORKS_LIST_MAXNUM];
static uint32_t _networkEncr[WL_NETWORKS_LIST_MAXNUM];
static uint8_t _networkChannel[WL_NETWORKS_LIST_MAXNUM];
static uint8_t _networkBand[WL_NETWORKS_LIST_MAXNUM];
static char _networkMac[WL_NETWORKS_LIST_MAXNUM][18];
static rtw_result_t wifidrv_scan_result_handler(rtw_scan_handler_result_t *malloced_scan_result) {
rtw_scan_result_t *record;
if (malloced_scan_result->scan_complete != RTW_TRUE) {
record = &malloced_scan_result->ap_details;
record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
if (_networkCount < WL_NETWORKS_LIST_MAXNUM) {
strcpy(_networkSsid[_networkCount], (char *)record->SSID.val);
_networkRssi[_networkCount] = record->signal_strength;
_networkEncr[_networkCount] = record->security;
_networkChannel[_networkCount] = record->channel;
_networkBand[_networkCount] = record->band;
sprintf(_networkMac[_networkCount], "%02X:%02X:%02X:%02X:%02X:%02X",
record->BSSID.octet[0], record->BSSID.octet[1], record->BSSID.octet[2],
record->BSSID.octet[3], record->BSSID.octet[4], record->BSSID.octet[5]);
_networkCount++;
}
}
return RTW_SUCCESS;
}
void printEncryptionTypeEx(uint32_t thisType) {
switch (thisType) {
case RTW_SECURITY_OPEN:
Serial.print("Open");
break;
case RTW_SECURITY_WEP_PSK:
Serial.print("WEP");
break;
case RTW_SECURITY_WPA_TKIP_PSK:
Serial.print("WPA TKIP");
break;
case RTW_SECURITY_WPA_AES_PSK:
Serial.print("WPA AES");
break;
case RTW_SECURITY_WPA2_AES_PSK:
Serial.print("WPA2 AES");
break;
case RTW_SECURITY_WPA2_TKIP_PSK:
Serial.print("WPA2 TKIP");
break;
case RTW_SECURITY_WPA2_MIXED_PSK:
Serial.print("WPA2 Mixed");
break;
case RTW_SECURITY_WPA_WPA2_MIXED_PSK:
Serial.print("WPA/WPA2 AES");
break;
case RTW_SECURITY_WPA3_AES_PSK:
Serial.print("WPA3 AES");
break;
case RTW_SECURITY_WPA2_WPA3_MIXED:
Serial.print("WPA2/WPA3");
}
}
static int8_t scanNetworks() {
_networkCount = 0;
if (wifi_scan_networks(wifidrv_scan_result_handler, NULL) != RTW_SUCCESS) {
return WL_FAILURE;
}
return _networkCount;
}
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Initialize the onboard WiFi:
WiFi.status();
}
void loop() {
// scan for existing networks:
Serial.println("Scanning available networks...");
int n = scanNetworks();
if (n == 0) {
Serial.println("No networks found");
} else {
for (int network = 1; network < n; network++) {
Serial.print(_networkSsid[network]);
Serial.print("\tSignal: ");
Serial.print(_networkRssi[network]);
Serial.print(" dBm");
Serial.print("\tEncryptionRaw: ");
printEncryptionTypeEx(_networkEncr[network]);
Serial.print("\tBand: ");
// Serial.print(_networkBand[network]);
if (_networkChannel[network] < 14) {
Serial.print("2.4 Ghz");
} else {
Serial.print("5 Ghz");
}
Serial.print("\tChannel: ");
Serial.print(_networkChannel[network]);
Serial.print("\tMac: ");
Serial.print(_networkMac[network]);
Serial.println("");
}
}
Serial.println("");
delay(SCAN_INTERVAL);
}
@CoD-Segfault
Copy link

Hi, I would like to use portions of this in an open source project, but I don't see a license attached to this. Can you please state how this code is licensed?

@designer2k2
Copy link
Author

Hi, I would like to use portions of this in an open source project, but I don't see a license attached to this. Can you please state how this code is licensed?

Hello i added the unlicense, do whatever you like :-)

@CoD-Segfault
Copy link

Great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment