ESP8266将扫描到的WiFi按信号强度显示在OLED屏幕上
硬件准备ESP8266:NodeMCU WiFi
OLED:1.3’ 控制芯片:SH1106
Arduino IDE
U8g2lib
GND - 地
VCC - 3.3v
OLED SCK - GPIO 14 (NodeMCU D5)
OLED MOSI - GPIO 13(NodeMCU D7)
OLED CS - GPIO 15 (NodeMCU D8)
OLED DC - GPIO 2 (NodeMCU D4)
/*
WiFiScannerOLED.ino -- A simple ESP8266 network scanner.
Scan result will be displayed on OLDE12864.
Written by CNflysky 20.3.10
*/
#include
#include
//OLED12864 constructor;if you are using another display,change it here.
U8G2_SH1106_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0,14,13,15,2,U8X8_PIN_NONE);
void setup() {
u8g2.begin();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
}
struct wifis_t {
String ssid;
String encryption;
int rssi;
};
struct wifis_t *getWiFisInfo(int count){
wifis_t *wifis=new wifis_t[count];
wifis_t switcher;
for(int i=0;i<count;++i){
wifis[i].ssid=WiFi.SSID(i);
wifis[i].rssi=WiFi.RSSI(i);
switch(WiFi.encryptionType(i)){
case ENC_TYPE_NONE:
wifis[i].encryption="Open";
break;
case ENC_TYPE_WEP:
wifis[i].encryption="WEP";
break;
case ENC_TYPE_TKIP:
wifis[i].encryption="WPA";
break;
case ENC_TYPE_CCMP:
wifis[i].encryption="WPA2";
break;
case ENC_TYPE_AUTO:
wifis[i].encryption="WPA/WPA2";
break;
}
}
for(int i=0;i<count;i++){
for(int j=0;j<count;j++){
if(wifis[i].rssi*-1<wifis[j].rssi*-1){
switcher=wifis[i];
wifis[i]=wifis[j];
wifis[j]=switcher;
}
}
}
return wifis;
}
void displaySSIDs(int count,wifis_t *wifis){
u8g2.setFont(u8g2_font_5x7_tr);
int y=u8g2.getMaxCharHeight();
for(int i=0;i0){
wifis_t *wifis=getWiFisInfo(ssids);
u8g2.clearBuffer();
displaySSIDs(ssids,wifis);
u8g2.sendBuffer();
delete [] wifis;
}else{
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_5x7_tr);
u8g2.drawStr(0,u8g2.getMaxCharHeight(),"No WiFi Found");
u8g2.sendBuffer();
}
}
成果图