ESP8266 NodeMCU ssd1306 WiFi scanner

Long time since I posted my last one. Meanwhile I went into programming arduino and trying out some sensors and displays. Here I am presensting you the source code to make yourself a portable wifi scanner with the tiny OLED ssd1306.

Check out a short video to understand what we are talking about.

This article is not for the beginners but indeed easy to make.

Get your hands on one of the convinient developer boards with ESP8266 such as NodeMCU and a ssd1306 OLED with I2C interface and 128×64 pixels.

display VCC –> 3.3V
display GND –> GND
display SCL –> D1
display SDA –> D2

You will need to install the board libraries for the ESP8266 and the adafruit_gfx and adafruit_ssd1306 library.

#include <ESP8266WiFi.h>
//#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET LED_BUILTIN // 4 // GPIO4
Adafruit_SSD1306 display(OLED_RESET);

#define BLINK (millis()%1000<500)?WHITE:BLACK

#define BLINKING_RATE 3 // in Hertz
#define BLINKING (millis()%(1000/BLINKING_RATE)<(500/BLINKING_RATE))?WHITE:BLACK

/* inspired by the example code and this post
* http://arduinom.org/kablosuz-aglarin-oled-ekranda-listelenmesi/
* SCL --> PIN D1 GPIO 5
* SDA --> PIN D2 GPIO 4
*/

int dB2y(int32_t in) {
// transform Signal Strength into vertical Pixel Position
int a = 40; // strong signal limit
int b = 90; // low signal limit
int outmin = 11; // upper pixel {0..
int outmax = display.height()-1; // lower pixel ..63}
return map(constrain(abs(in), a, b), a, b, outmin, outmax);
}

int ch2x(int32_t ch) {
// transform channel into horizontal pixel position
int a = -1; // low channel limit {1..
int b = 15; // high channel limit ..13}
int outmin = 0; // upper pixel {0..
int outmax = display.width(); // lower pixel ..127}
return map(constrain(ch, a, b), a, b, outmin, outmax);
}

int sortBySignal() {

}

void drawInit() {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Scanning Networks");
display.display();
}

void drawNoNetwork() {
// update OLED
display.clearDisplay();
display.setCursor(0, 0);
display.println("No Network found");
display.display();
}

void drawBand(int32_t rssi, int32_t ch, bool color) {
//int x0 = ch2x(ch);
int x1 = ch2x(ch-2);
int x2 = ch2x(ch+2);
int y0 = dB2y(rssi);
int y1 = dB2y(-200);
display.drawLine(x1-3, y1, x1+4, y0, color); // left boundery
display.drawLine(x2+3, y1, x2-4, y0, color); // right boundery
display.drawLine(x1+4, y0, x2-4, y0, color); // upper line
}

void drawSelection(int32_t rssi, int32_t ch) {
int x = ch2x(ch);
int y = dB2y(rssi);
display.drawRect(x-3, y-3, 7, 7, BLACK);
display.drawRect(x-2, y-2, 5, 5, WHITE);
display.drawRect(x-1, y-1, 3, 3, BLACK);
display.drawPixel(x, y, BLACK);
}

void drawAxis() {
for (int x = 1; x <= 13; x++) {
display.drawPixel(ch2x(x), 63, WHITE);
}
}

void drawNetworks(int i, int n) {
long int timestamp = millis();
long int duration = 2000;
while ((millis()-timestamp < duration) && (millis() >= timestamp)) {
display.clearDisplay();
display.setCursor(0, 0);
//display.print(i + 1);
//display.print(" / ");
//display.println(n);

//display.print((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "e ");
display.print(WiFi.SSID(i));
//display.print(WiFi.BSSIDstr(i));
//display.print(WiFi.RSSI(i));
//display.print(WiFi.channel(i));

display.setCursor(display.width()-24,0);
display.setTextColor(BLACK, WHITE);
display.print("ch");
if (WiFi.channel(i) < 10) display.print(" "); // align right
display.print(WiFi.channel(i));
display.setTextColor(WHITE, BLACK);

//int enc = WiFi.encryptionType(i);
//drawAxis();

for (int net = 0; net < n; net++) {
if (net!=i) drawBand(WiFi.RSSI(net), WiFi.channel(net), WHITE);
}
drawBand(WiFi.RSSI(i), WiFi.channel(i), BLINKING);
drawSelection(WiFi.RSSI(i), WiFi.channel(i));

display.display();
delay(100); // minimum frame rate 50ms
}
}

// read the encryption type and print out the name:
void printEncryptionType(int thisType) {
switch (thisType) {
case ENC_TYPE_WEP:
Serial.print("WEP");
break;
case ENC_TYPE_TKIP:
Serial.print("WPA");
break;
case ENC_TYPE_CCMP:
Serial.print("WPA2");
break;
case ENC_TYPE_NONE:
Serial.print("None");
break;
case ENC_TYPE_AUTO:
Serial.print("Auto");
break;
}
}

void SerialPrintNetworks(int i) {
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print("; (");
Serial.print(WiFi.RSSI(i));
Serial.print(" dBm) ");
//Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? "open " : "enc ");
printEncryptionType(WiFi.encryptionType(i));
Serial.println("");
}

void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay(); // delete Adafruit Splash Screen
display.display();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setTextWrap(0);
display.setCursor(0, 0);
display.println("ESP8266 WiFi Scanner");
display.display();

Serial.begin(115200);
Serial.println("Setup ESP8266");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

delay(2000);
}

void loop() {
Serial.println("Scanning Networks");
drawInit();
int n = WiFi.scanNetworks();

if (n == 0) {
Serial.println("No Network found");
drawNoNetwork();
}
else {
Serial.print(n);
Serial.println(" Networks found");
for (int i = 0; i < n; ++i) {
SerialPrintNetworks(i);
drawNetworks(i, n);
}
}
Serial.println("");
}

 

Advertisements