Created
December 19, 2018 06:08
-
-
Save staberas/a2eba2ccd9a24e63c75b83424717c8ff to your computer and use it in GitHub Desktop.
WEMOS WIFI monitor with oled screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This sketch demonstrates how to scan WiFi networks. | |
The API is almost the same as with the WiFi Shield library, | |
the most obvious difference being the different file you need to include: | |
resources: https://github.com/wemos/D1_mini_Examples & https://wiki.wemos.cc/products:d1:d1_mini_lite | |
*/ | |
#include "ESP8266WiFi.h" | |
#include <SPI.h> | |
#include "SSD1306Ascii.h" | |
#include "SSD1306AsciiWire.h" | |
// 0X3C+SA0 - 0x3C or 0x3D | |
#define I2C_ADDRESS 0x3C | |
// Define proper RST_PIN if required. | |
#define RST_PIN 0 | |
SSD1306AsciiWire oled; | |
int buttonState = 0; | |
const int buttonPin = 14; | |
//------------ | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#define OLED_RESET 0 | |
Adafruit_SSD1306 display(OLED_RESET); | |
#define NUMFLAKES 10 | |
#define XPOS 0 | |
#define YPOS 1 | |
#define DELTAY 2 | |
#define LOGO16_GLCD_HEIGHT 16 | |
#define LOGO16_GLCD_WIDTH 16 | |
static const unsigned char PROGMEM logo16_glcd_bmp[] = | |
{ B00000000, B11000000, | |
B00000001, B11000000, | |
B00000001, B11000000, | |
B00000011, B11100000, | |
B11110011, B11100000, | |
B11111110, B11111000, | |
B01111110, B11111111, | |
B00110011, B10011111, | |
B00011111, B11111100, | |
B00001101, B01110000, | |
B00011011, B10100000, | |
B00111111, B11100000, | |
B00111111, B11110000, | |
B01111100, B11110000, | |
B01110000, B01110000, | |
B00000000, B00110000 }; | |
//------------ | |
void setup() { | |
pinMode(buttonPin, INPUT_PULLUP); | |
Serial.begin(9600); | |
Wire.begin(); | |
Wire.setClock(400000L); | |
#if RST_PIN >= 0 | |
oled.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN); | |
#else // RST_PIN >= 0 | |
oled.begin(&Adafruit128x32, I2C_ADDRESS); | |
#endif // RST_PIN >= 0 | |
oled.setFont(Adafruit5x7); | |
oled.clear(); | |
// Set WiFi to station mode and disconnect from an AP if it was previously connected | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
delay(2000); | |
Serial.println("Setup done"); | |
oled.set1X(); | |
oled.println("Press button for WIFI"); | |
delay(1000); | |
buttonState = digitalRead(buttonPin); | |
delay(5000); | |
if (buttonState == HIGH) { | |
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); | |
display.display(); | |
// miniature bitmap display | |
display.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, 1); | |
delay(1000); | |
display.clearDisplay(); | |
// draw a bitmap icon and 'animate' movement | |
testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH); | |
// draw a white circle, 10 pixel radius | |
//display.fillCircle(display.width()/2, display.height()/2, 10, WHITE); | |
display.display(); | |
delay(2000); | |
display.clearDisplay(); | |
} else { | |
oled.println("close"); | |
} | |
delay(3000); | |
oled.clear(); | |
} | |
void loop() { | |
display.clearDisplay(); | |
oled.set1X(); | |
Serial.print("Scan start "); | |
// WiFi.scanNetworks will return the number of networks found | |
int n = WiFi.scanNetworks(); | |
oled.println("Scan done"); | |
if (n == 0) { | |
oled.println("no networks found"); | |
} else { | |
oled.print(n); | |
oled.println(" networks found"); | |
delay(2000); | |
oled.clear(); | |
for (int i = 0; i < n-4; ++i) { | |
// Print SSID and RSSI for each network found | |
oled.print(i + 1); | |
oled.print(": "); | |
oled.print(WiFi.SSID(i)); | |
oled.print(" ("); | |
oled.print(WiFi.RSSI(i)); | |
oled.print(")"); | |
oled.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*"); | |
oled.print(i + 2); | |
oled.print(": "); | |
oled.print(WiFi.SSID(i+1)); | |
oled.print(" ("); | |
oled.print(WiFi.RSSI(i+1)); | |
oled.print(")"); | |
oled.println((WiFi.encryptionType(i+1) == ENC_TYPE_NONE) ? " " : "*"); | |
oled.print(i + 3); | |
oled.print(": "); | |
oled.print(WiFi.SSID(i+2)); | |
oled.print(" ("); | |
oled.print(WiFi.RSSI(i+2)); | |
oled.print(")"); | |
oled.println((WiFi.encryptionType(i+2) == ENC_TYPE_NONE) ? " " : "*"); | |
oled.print(i + 4); | |
oled.print(": "); | |
oled.print(WiFi.SSID(i+3)); | |
oled.print(" ("); | |
oled.print(WiFi.RSSI(i+3)); | |
oled.print(")"); | |
oled.println((WiFi.encryptionType(i+3) == ENC_TYPE_NONE) ? " " : "*"); | |
delay(2500); | |
oled.clear(); | |
} | |
} | |
oled.println(""); | |
delay(2000); | |
buttonState = digitalRead(buttonPin); | |
if (buttonState == HIGH) { | |
display.clearDisplay(); | |
// draw a bitmap icon and 'animate' movement | |
testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH); | |
} | |
// Wait a bit before scanning again | |
delay(8000); | |
} | |
void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) { | |
uint8_t icons[NUMFLAKES][3]; | |
// initialize | |
for (uint8_t f=0; f< NUMFLAKES; f++) { | |
icons[f][XPOS] = random(display.width()); | |
icons[f][YPOS] = 0; | |
icons[f][DELTAY] = random(5) + 1; | |
Serial.print("x: "); | |
Serial.print(icons[f][XPOS], DEC); | |
Serial.print(" y: "); | |
Serial.print(icons[f][YPOS], DEC); | |
Serial.print(" dy: "); | |
Serial.println(icons[f][DELTAY], DEC); | |
} | |
while (1) { | |
// draw each icon | |
for (uint8_t f=0; f< NUMFLAKES; f++) { | |
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE); | |
} | |
display.display(); | |
delay(200); | |
// then erase it + move it | |
for (uint8_t f=0; f< NUMFLAKES; f++) { | |
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK); | |
// move it | |
icons[f][YPOS] += icons[f][DELTAY]; | |
// if its gone, reinit | |
if (icons[f][YPOS] > display.height()) { | |
icons[f][XPOS] = random(display.width()); | |
icons[f][YPOS] = 0; | |
icons[f][DELTAY] = random(5) + 1; | |
} | |
} | |
// delay(2000); | |
buttonState = digitalRead(buttonPin); | |
if (buttonState == LOW) { | |
loop(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment