Thursday, December 1, 2016

Portable ESP8266 WiFi Sniffer (Arduino IDE)

-----
The ESP8266 modules are so cheap who could resist experimenting with one.  For about $8USD a few things you get are 9 GPIOs, I2C/SPI support, an ADC, and on board WiFi or other goodies.  In addition it's all programmable in the Arduino IDE that is familiar to many and has a good user support network.

This example application shows a quick and easy way to get a portable 'WarDriver' with the WiFi ESP8266 and an OLED display.

No resistors, etc. needed; connect it up like this:
https://www.adafruit.com/product/2821https://www.amazon.com/Diymall-Yellow-Serial-Arduino-Display/dp/B00O2LLT30/ref=sr_1_1?ie=UTF8&qid=1480612090&sr=8-1&keywords=diymall+oled

    Pinout Connections
ESP8266                  OLED
3VDC <<==+==>> Vcc
  GND <<==+==>> GND
 SCL(5) <<==+==>> SCL
 SDA(4) <<==+==>> SDA



Take a look at the source code below for the links on installing ESP8266 capability to the Arduino IDE.  Chances are if you are reading this you already have the Arduino IDE installed; just make sure you are running at least Rev 1.6.8.  Then upload the source code to the ESP8266 and your up.
-----
Here is a sample of the rig running in a random parking lot a fair distance from an apartment complex.  Eight networks were found; all encrypted.  The OLED displays the number of networks, SSID name, signal strength (dBm), and if the network is OPEN or Encrypted.
One thing that was a surprise is how many cars have OPEN WiFi running.  Also, pretty much every long haul 18 wheeler heading down the interstate is a rolling WiFi hotspot, but most (not all) are Encrypted.
-----
Here is the source code to push into the ESP8266 via the Arduino IDE:

/*
 * WhiskeyTangoHotel.Com /  NOV2016
 * 'WarDriver' ESP8266 Adafruit HUZZAH w/ WiFi and 32 line OLED
 *   
 *   Scan WiFi networks leverages from:
 *   https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino
 *   
 *   OLED Driver: Thanks, adafruit.com
 *   
 *   Compile for 80MHz with Arduino IDE
 *      Arduino IDE 1.6.8 or greater
 *      http://www.arduino.cc/en/Main/Software
 *      
 *      ESP8266 Board Package
 *      Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manage
 *      Restart, select your ESP8266 from the Tools->Board dropdown
 *   
 */
#include "ESP8266WiFi.h"  // API for the ESP8266 WiFi

// Setup the 32 line OLED 
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET LED_BUILTIN  // 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 32)  // change to 64 for larger OLED. See error trap next line.
#error("Height incorrect, please fix Adafruit_SSD1306.h!"); 
#endif

int screen_roll_delay = 800; // How long to leave Network info on OLED. Delay is executed four times (for LED blink)

void setup() {
  // Setup and test writes to the OLED and Serial Monitor (ESP8266 expects Serial Monitor at 115200 baud)
  
  // Some more OLED setup
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();    // Clear the buffer.
  display.display();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  
  display.setCursor(0,0);
  display.println("2.4GHz WiFi Scanner");
  Serial.begin(115200); // Display to serial monitor as well as OLED
  Serial.println("Setup begins....");

  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  Serial.println("Setup completes!!!");
  display.println("------------------");
  display.println("WhiskeyTangoHotel");
  display.println("      .Com");
  display.display();
  delay(5000);  // Welcome/Test screen delay.
}  // end void setup

void loop() {
  Serial.println("Scan starts...");

  // update OLED
  display.clearDisplay();
  display.display();
  display.setCursor(0,0);
  display.println("Scanning...");
  display.display();

  // WiFi.scanNetworks will return the number of networks found as variable "n"
  digitalWrite(0, HIGH);  // On board LED ON
  int n = WiFi.scanNetworks();
  Serial.println(" and completes!!!");
  
  if (n == 0) {  // No WiFi found.  Update the Serial Monitor and the OLED
    Serial.println("No WiFi found!!!");

    // update OLED
    display.clearDisplay();
    display.display();
    display.setCursor(0,0);
    display.println("Scanning...");
    display.println("No WiFi found!!!");
    display.display();
    digitalWrite(0, LOW);  // On board LED OFF
  }      // endif n=0 (no wifi found)
  else  // wifi found
  {
    Serial.print(n);
    Serial.println(" Networks found:");
    Serial.println("----------------");

    for (int i = 0; i < n; ++i)
    {
      digitalWrite(0, HIGH);  // On board LED ON
      
      // Print SSID and RSSI for each network found to Serial Monitor. Show SSID, Signal strenght, and OPEN or Encrypted
      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)?"Not Encrypted":"Encrypted");

      //update OLED with found WiFi. Show SSID, Signal strenght, and OPEN or Encrypted
      display.clearDisplay();
      display.display();
      display.setCursor(0,0);
      display.print("Network ");
      display.print(i+1);
      display.print(" of ");
      display.println(n);
      display.display();
      display.println(WiFi.SSID(i));
      display.println("-------------------");
      display.print(WiFi.RSSI(i));
      display.print("dBm | ");
      display.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?"OPEN":"Encrypted");
      display.display();

      delay(screen_roll_delay);  // Little delay to allow time to read OLED. Flash the on board LED just for fun.
      digitalWrite(0, LOW);   // On board LED OFF
      delay(screen_roll_delay); 
      digitalWrite(0, HIGH);   // On board LED ON
      delay(screen_roll_delay);  
      digitalWrite(0, LOW);   // On board LED OFF
      delay(screen_roll_delay); 
    }  // end for/next loop for n# of wifi networks found
  }   // endif wifi found (n was <> 0)
  Serial.println("");
}  // end void loop (endless)
-----
Thanks for the visit.