| | /* |
---|
| | 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: |
---|
| | */ |
---|
| | #include "ESP8266WiFi.h" |
---|
| | #include <Arduino.h> |
---|
| | #include <U8g2lib.h> |
---|
| | |
---|
| | #ifdef U8X8_HAVE_HW_SPI |
---|
| | #include <SPI.h> |
---|
| | #endif |
---|
| | #ifdef U8X8_HAVE_HW_I2C |
---|
| | #include <Wire.h> |
---|
| | #endif |
---|
| | |
---|
| | U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 5, /* data=*/ 4); |
---|
| | ADC_MODE(ADC_VCC); |
---|
| | |
---|
| | void setup() { |
---|
| | Serial.begin(115200); |
---|
| | |
---|
| | // sceen stuff |
---|
| | pinMode(10, OUTPUT); |
---|
| | pinMode(9, OUTPUT); |
---|
| | digitalWrite(10, 0); |
---|
| | digitalWrite(9, 0); |
---|
| | u8g2.begin(); |
---|
| | u8g2.enableUTF8Print(); |
---|
| | |
---|
| | Serial.println("Setup done"); |
---|
| | |
---|
| | getBatteryLevel(); |
---|
| | |
---|
| | // Set WiFi to station mode and disconnect from an AP if it was previously connected |
---|
| | WiFi.mode(WIFI_STA); |
---|
| | WiFi.disconnect(); |
---|
| | delay(100); |
---|
| | } |
---|
| | |
---|
| | void getBatteryLevel() { |
---|
| | u8g2.setFont(u8g2_font_8x13_mf); |
---|
| | |
---|
| | uint32_t getVcc = ESP.getVcc(); |
---|
| | Serial.println(getVcc); |
---|
| | |
---|
| | // printing getVcc to screen |
---|
| | /* |
---|
| | String batvol2 = ""; |
---|
| | batvol2 += getVcc; |
---|
| | char batChr[batvol2.length()+1]; |
---|
| | batvol2.toCharArray(batChr, batvol2.length()+1); |
---|
| | u8g2.drawStr(0,10,batChr); |
---|
| | */ |
---|
| | |
---|
| | float measuredvbat = getVcc; // for actual voltage |
---|
| | measuredvbat /= 1024; // convert to voltage |
---|
| | Serial.println(measuredvbat); |
---|
| | |
---|
| | //printing actual votage to screen |
---|
| | /* |
---|
| | String batvol = ""; |
---|
| | batvol += measuredvbat; |
---|
| | char batChr2[batvol.length()+1]; |
---|
| | batvol.toCharArray(batChr2, batvol.length()+1); |
---|
| | u8g2.drawStr(0,20,batChr2); |
---|
| | */ |
---|
| | |
---|
| | int percent = map(getVcc, 2300, 3000, 1, 11); // turn vcc into batt perentage (yea I know it's not that accurate due to dropoff rate) |
---|
| | if(percent > 11){ percent = 11; } |
---|
| | if(percent < 1){ percent = 1; } |
---|
| | |
---|
| | u8g2.setFont(u8g_font_unifont); |
---|
| | //u8g.setFont(u8g_font_osb21); |
---|
| | u8g2.drawFrame(111,24,15,8); |
---|
| | u8g2.drawBox(113,26,percent,4); //3rd param is %(1-11) |
---|
| | u8g2.drawBox(126,26,2,4); |
---|
| | |
---|
| | } |
---|
| | |
---|
| | void drawScrollString(int16_t offset, const char *s){ |
---|
| | static char buf[36]; // should for screen with up to 256 pixel width |
---|
| | size_t len; |
---|
| | size_t char_offset = 0; |
---|
| | u8g2_uint_t dx = 0; |
---|
| | size_t visible = 0; |
---|
| | |
---|
| | u8g2.setDrawColor(0); // clear the scrolling area |
---|
| | u8g2.drawBox(0, 30, u8g2.getDisplayWidth()-1, u8g2.getDisplayHeight()-1); |
---|
| | u8g2.setDrawColor(1); // set the color for the text |
---|
| | |
---|
| | len = strlen(s); |
---|
| | if ( offset < 0 ) |
---|
| | { |
---|
| | char_offset = (-offset)/8; |
---|
| | dx = offset + char_offset*8; |
---|
| | if ( char_offset >= u8g2.getDisplayWidth()/8 ) |
---|
| | return; |
---|
| | visible = u8g2.getDisplayWidth()/8-char_offset+1; |
---|
| | strncpy(buf, s, visible); |
---|
| | buf[visible] = '\0'; |
---|
| | u8g2.setFont(u8g2_font_6x10_mf); |
---|
| | u8g2.drawStr(char_offset*8-dx, 30, buf); |
---|
| | } |
---|
| | else |
---|
| | { |
---|
| | char_offset = offset / 8; |
---|
| | if ( char_offset >= len ) |
---|
| | return; // nothing visible |
---|
| | dx = offset - char_offset*8; |
---|
| | visible = len - char_offset; |
---|
| | if ( visible > u8g2.getDisplayWidth()/8+1 ) |
---|
| | visible = u8g2.getDisplayWidth()/8+1; |
---|
| | strncpy(buf, s+char_offset, visible); |
---|
| | buf[visible] = '\0'; |
---|
| | u8g2.setFont(u8g2_font_6x10_mf); |
---|
| | u8g2.drawStr(-dx, 30, buf); |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | |
---|
| | void loop() { |
---|
| | //u8g2.clearBuffer(); // only need this if you want "scanning..." on a clear screen! |
---|
| | |
---|
| | int16_t offset = -(int16_t)u8g2.getDisplayWidth(); |
---|
| | int16_t len = strlen("Scanning..."); |
---|
| | for(;;) // then do the scrolling |
---|
| | { |
---|
| | |
---|
| | drawScrollString(offset, "Scanning..."); // no clearBuffer required, screen will be partially cleared here |
---|
| | getBatteryLevel(); |
---|
| | u8g2.sendBuffer(); // transfer internal memory to the display |
---|
| | |
---|
| | delay(20); |
---|
| | offset+=2; |
---|
| | if ( offset > len*8+1 ) |
---|
| | break; |
---|
| | } |
---|
| | u8g2.sendBuffer(); |
---|
| | |
---|
| | Serial.println("scan start"); |
---|
| | |
---|
| | // WiFi.scanNetworks will return the number of networks found |
---|
| | int n = WiFi.scanNetworks(); |
---|
| | Serial.println("scan done"); |
---|
| | if (n == 0) { |
---|
| | Serial.println("no networks found"); |
---|
| | } else { |
---|
| | Serial.print(n); |
---|
| | Serial.println(" networks found"); |
---|
| | |
---|
| | for (int i = 0; i < n; ++i) { |
---|
| | // Print SSID and RSSI for each network found |
---|
| | Serial.print(i + 1); |
---|
| | Serial.print(": "); |
---|
| | Serial.print(WiFi.SSID(i)); |
---|
| | |
---|
| | u8g2.clearBuffer(); |
---|
| | u8g2.setFont(u8g2_font_8x13_mf); |
---|
| | char ssid[WiFi.SSID(i).length()+1]; |
---|
| | WiFi.SSID(i).toCharArray(ssid, WiFi.SSID(i).length()+1); |
---|
| | u8g2.drawStr(0,9,ssid); |
---|
| | u8g2.print(WiFi.SSID(i)); |
---|
| | |
---|
| | Serial.print(" ("); |
---|
| | Serial.print(WiFi.RSSI(i)); |
---|
| | Serial.print(")"); |
---|
| | Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*"); |
---|
| | u8g2.setFont(u8g2_font_6x12_mf); |
---|
| | |
---|
| | switch(WiFi.encryptionType(i)){ |
---|
| | case ENC_TYPE_NONE: |
---|
| | u8g2.drawStr(0,21,"Open"); |
---|
| | break; |
---|
| | case ENC_TYPE_WEP: |
---|
| | u8g2.drawStr(0,21,"WEP"); |
---|
| | break; |
---|
| | case ENC_TYPE_TKIP: |
---|
| | u8g2.drawStr(0,21,"WPA/PSK"); |
---|
| | break; |
---|
| | case ENC_TYPE_CCMP: |
---|
| | u8g2.drawStr(0,21,"WPA2/PSK"); |
---|
| | break; |
---|
| | case ENC_TYPE_AUTO: |
---|
| | u8g2.drawStr(0,21,"WPA/WPA2/PSK"); |
---|
| | break; |
---|
| | default: |
---|
| | u8g2.drawStr(0,21,"Unknown Encryption"); |
---|
| | break; |
---|
| | } |
---|
| | |
---|
| | u8g2.setFont(u8g2_font_6x10_mf); |
---|
| | String chStr = ""; |
---|
| | chStr += WiFi.channel(i); |
---|
| | char chan[chStr.length()+1]; |
---|
| | chStr.toCharArray(chan, chStr.length()+1); |
---|
| | |
---|
| | String rssiStr = ""; |
---|
| | rssiStr += WiFi.RSSI(i); |
---|
| | char rssi[rssiStr.length()+1]; |
---|
| | rssiStr.toCharArray(rssi, rssiStr.length()+1); |
---|
| | String toWrite = "ch: "+chStr+" dB: "+rssiStr; |
---|
| | |
---|
| | char chrWrite[toWrite.length()+1]; |
---|
| | toWrite.toCharArray(chrWrite, toWrite.length()+1); |
---|
| | |
---|
| | u8g2.drawStr(0,32,chrWrite); |
---|
| | |
---|
| | getBatteryLevel(); |
---|
| | |
---|
| | u8g2.sendBuffer(); |
---|
| | delay(2000); // was 2000 |
---|
| | } |
---|
| | } |
---|
| | Serial.println(""); |
---|
| | |
---|
| | // Wait a bit before scanning again |
---|
| | delay(1000); |
---|
| | } |
---|
| | |
---|
| | |