macsbug

Just another WordPress.com site

M5STACK WiFiScan

leave a comment »

ESP8266 WiFi Scan を M5STACK に移植しました。     2018.05.03

ORIGINALは oaquimorg/esp8266_wifi_scan です。
joaquim.org : joaquimorg氏の記事:ESP8266 WiFi Scan
joaquimorg氏 に感謝致します。

オリジナルのディスプレーは 2.4 inch 320×240 です。
M5STACK も 320×200 ですが 2.0 inch の為 文字が小さく見づらいですが 良しとします。

Free_Fonts.h は M5STACK Library の中にあります。

SD にアプリを入れて起動する為に SD Update のスケッチが入っています。


スケッチ:WiFiScan:
_ WiFiScan のフォルダーの構成は以下の3つです。
_ icons.h は esp8266_wifi_scan から使用すると良いです。
_ WiFiScan Folder : [ WiFiScan.ino, icons.h, Free_Fonts.h ]

// Scan WiFi networks. (c)joaquim.org
// https://github.com/joaquimorg/ESP8266/tree/master/esp8266_wifi_scan
// https://www.joaquim.org/esp8266-wifi-scan/
// Github:https://macsbug.wordpress.com/2018/05/03/m5stack-wifiscan/
#include "WiFi.h"
#include <M5Stack.h>
#include "M5StackUpdater.h"                          // SD Update
#include "icons.h"
//#include "Free_Fonts.h"

typedef struct {
  String SSID;
  uint8_t encryptionType,Active,Update;
  int32_t RSSI,Channel;
} NetInfo;
NetInfo networkInfo[30];
typedef struct {
  int32_t Total,RSSI;
}ChannelInfo;
ChannelInfo totalChannel[14],totalChannelOld[14];
#define TEXT_SIZE 10
int initclear = 1;

void setup() {
  Serial.begin(115200);delay(500);
  M5.begin();
  Wire.begin(); 
  if(digitalRead(BUTTON_A_PIN) == 0){                // SD Update
     updateFromFS(SD); ESP.restart();                // SD Update
  }                                                  // SD Update
  M5.Lcd.setTextColor(TFT_BLACK, TFT_WHITE);
  M5.Lcd.fillScreen(TFT_WHITE);
  //M5.Lcd.setFreeFont(FSSB12);
  //M5.Lcd.setFreeFont(FSS9);
  M5.Lcd.fillScreen(ILI9341_BLACK);
  M5.Lcd.drawBitmap(110, 10, wifiBitmap, 100, 70, ILI9341_WHITE);
  //M5.Lcd.setFont(&FreeMono9pt7b);
  M5.Lcd.setTextColor(ILI9341_YELLOW, ILI9341_BLACK);
  M5.Lcd.setCursor(105, 100);
  M5.Lcd.print("Wifi Scan");
  M5.Lcd.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
  M5.Lcd.setCursor(95, 130);
  M5.Lcd.print("joaquim.org");
  //M5.Lcd.displayUpdate();
  M5.Lcd.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  Serial.println("esp8266 Wifi Scan ... ");
  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(5000);
  Serial.println("running... ");
  //M5.Lcd.fillScreen(ILI9341_BLACK);
  M5.Lcd.setCursor(75, 200);
  M5.Lcd.println("Scanning WiFi...");
  //M5.Lcd.displayUpdate();
  memset( networkInfo,     0x0, sizeof(networkInfo) );
  memset( totalChannel,    0x0, sizeof(totalChannel) );
  memset( totalChannelOld, 0x0, sizeof(totalChannelOld) );
  M5.Lcd.setTextSize(1);
}

void showScreen1( int num ) {
  int pos = 0, col = 0, total = 0;  
  for (int i = 0; i < num; ++i) {
    if (networkInfo[i].Active == 1) {     
      M5.Lcd.fillRect(col, 2 + (TEXT_SIZE * pos),160,TEXT_SIZE,BLACK);      
      M5.Lcd.setCursor(col, 2 + (TEXT_SIZE * pos));       
      /*if (networkInfo[i].encryptionType == ENC_TYPE_NONE) {
        M5.Lcd.drawBitmap(col,(TEXT_SIZE * pos),unlockBitmap10,10,10,GREEN);
      } else {
        M5.Lcd.drawBitmap(col,(TEXT_SIZE * pos),lockBitmap10,10,10,RED);
      }*/    
      M5.Lcd.setCursor(col + 14, 2 + (TEXT_SIZE * pos)); 
      M5.Lcd.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
      if (networkInfo[i].RSSI <= -80) {
        M5.Lcd.setTextColor(ILI9341_RED, ILI9341_BLACK);
      } else if (networkInfo[i].RSSI >= -80 && networkInfo[i].RSSI < -70){
        M5.Lcd.setTextColor(ILI9341_YELLOW, ILI9341_BLACK);
      } else {
        M5.Lcd.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
      }   
      M5.Lcd.print(networkInfo[i].SSID);
      //Serial.print(networkInfo[i].SSID);Serial.print(" : ");
      //Serial.println(networkInfo[i].RSSI);*/
      
      //M5.Lcd.fillRect(0, (TEXT_SIZE * pos), 240, TEXT_SIZE, ILI9341_BLACK);
      /*if (networkInfo[i].RSSI <= -80) {
        M5.Lcd.drawBitmap(120,20 + (TEXT_SIZE * pos),level1Bitmap, 8, 8,RED);
      } else if (networkInfo[i].RSSI >= -80 && networkInfo[i].RSSI < -70){
        M5.Lcd.drawBitmap(120,20 + (TEXT_SIZE * pos),level2Bitmap,8,8,YELLOW);  
      } else {
        M5.Lcd.drawBitmap(120,20 + (TEXT_SIZE * pos),level3Bitmap,8,8,GREEN);
      }*/
      pos++;
      total++;
      if ( total > 21) break;
      if ( total == 11 ) {
        col = 160;
        pos = 0;
      }
    }
  }
}

void BubbleSort(NetInfo netInfo[], int numLength) {
  uint8_t i, j, flag = 1;    // set flag to 1 to start first pass
  NetInfo temp;              // holding variable
  for (i = 1; (i <= numLength) && flag; i++){
    flag = 0;
    for (j = 0; j < (numLength - 1);j++){// ascending order simply changes to
      if (abs(netInfo[j + 1].RSSI) < abs(netInfo[j].RSSI)){ 
        temp = netInfo[j];             // swap elements
        netInfo[j] = netInfo[j + 1];
        netInfo[j + 1] = temp;
        flag = 1;                      // indicates that a swap occurred.
      }
    }
  }
  return;   //arrays are passed to functions by address; nothing is returned
}

void addToArray( NetInfo netInfo ) {
  uint8_t total = 0;
  for (int i = 0; i < 30; ++i) {
   if (networkInfo[i].Active == 1 && (networkInfo[i].SSID == netInfo.SSID)){
     networkInfo[i] = netInfo;
     //Serial.print("Found :" + netInfo.SSID + " ");Serial.println(i);
     return;
   } 
  }
  for (int i = 0; i < 30; ++i) {    
    /*Serial.print(i);Serial.print(" ");    
    Serial.print(networkInfo[i].Active);
    Serial.println(" " + networkInfo[i].SSID + " ");*/      
    if ( networkInfo[i].Active == 1 ) {
      total++;
    } 
  } 
  if ( total < 30 ) {
    networkInfo[total] = netInfo;
    //Serial.print("New :" + netInfo.SSID + " ");Serial.println(total);
  }
}

void drawGraphScreen() {
  M5.Lcd.drawRect(1, 120, 319, 105, ILI9341_DARKGREY);
  M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK); 
  M5.Lcd.drawString("01 02 03 04 05 06 07 08 09 10 11 12 13 14",50,210);
  M5.Lcd.drawFastHLine(40, 205, 265, ILI9341_ORANGE); 
  M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK); 
  M5.Lcd.drawString("-99", 15, 195);
  M5.Lcd.drawString("-80", 15, 185);
  M5.Lcd.drawString("-70", 15, 175);
  M5.Lcd.drawString("-60", 15, 165);
  M5.Lcd.drawString("-50", 15, 155);
  M5.Lcd.drawString("-40", 15, 145);
  M5.Lcd.drawString("-30", 15, 135);
  M5.Lcd.drawString("-20", 15, 125);
  M5.Lcd.drawFastVLine(40, 125, 80, ILI9341_ORANGE);
}

void drawGraph() {
  for (int i = 0; i < 14; ++i) {
    M5.Lcd.drawFastVLine(55 + (i * 18), 125, 75, ILI9341_BLACK);
    if ( totalChannelOld[i].Total > 0) {
      M5.Lcd.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
      M5.Lcd.setCursor(60 + (i * 18), 200 - totalChannelOld[i].RSSI);
      M5.Lcd.print("  ");
    }  
    if ( totalChannel[i].Total > 0) {
      M5.Lcd.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
      M5.Lcd.setCursor(60 + (i * 18), 200 - totalChannel[i].RSSI);
      M5.Lcd.print(totalChannel[i].Total);     
      M5.Lcd.drawFastVLine(55 + (i * 18), 200 - totalChannel[i].RSSI,
                          totalChannel[i].RSSI, ILI9341_GREEN);
    } 
    totalChannelOld[i] = totalChannel[i];
  }
}

void loop() {
  int n = WiFi.scanNetworks();
  int m = n;
  NetInfo netInfo;
  for (int i = 0; i < 30; ++i) {
    networkInfo[i].Update = 0;
  }
  for (int i = 0; i < 14; ++i) {
   totalChannel[i].Total = 0;
   totalChannel[i].RSSI = 0;
  }
  if ( initclear == 1 ) {
    initclear = 0;
    M5.Lcd.fillScreen(ILI9341_BLACK);
    drawGraphScreen();    
  }
  if (n == 0) {
    //M5.Lcd.setTextSize(2);
    M5.Lcd.fillScreen(ILI9341_BLACK);
    M5.Lcd.setTextColor(ILI9341_BLUE, ILI9341_BLACK);
    M5.Lcd.setCursor(0, 10);
    M5.Lcd.println();M5.Lcd.println();
    M5.Lcd.print("No networks found...");M5.Lcd.println();
    M5.Lcd.println("Scanning...");
    //M5.Lcd.setTextSize(1);
  } else {
    if ( m > 30 ) m = 30;
    for (int i = 0; i < m; ++i) {
      netInfo.SSID = WiFi.SSID(i);
      netInfo.encryptionType = WiFi.encryptionType(i);
      netInfo.RSSI = WiFi.RSSI(i);
      netInfo.Channel = WiFi.channel(i);
      netInfo.Active = 1;
      netInfo.Update = 1;
      addToArray(netInfo);
      totalChannel[netInfo.Channel - 1].Total = 
      totalChannel[netInfo.Channel - 1].Total + 1;
      if ((netInfo.RSSI + 100) > totalChannel[netInfo.Channel - 1].RSSI){
        totalChannel[netInfo.Channel - 1].RSSI = (netInfo.RSSI + 100);
      }
    }
    for (int i = 0; i < 30; ++i) {
      if ( networkInfo[i].Update == 0 ) {
           networkInfo[i].SSID   = "";
           networkInfo[i].RSSI   = 255;
           networkInfo[i].Active = 0;
      }
    }
    BubbleSort(networkInfo, 30);
    M5.Lcd.setCursor(5, 230);
    M5.Lcd.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
    M5.Lcd.print(n);
    M5.Lcd.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
    M5.Lcd.print(" networks found");   
    //M5.Lcd.fillRect(0, 20, 240, 320, ILI9341_BLACK);
    showScreen1(30);
    drawGraph();   
  }
}

//icons.h
const unsigned char wifiBitmap [] PROGMEM = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 252, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
3, 255, 255, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 255, 255, 248, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 31, 255, 255, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 255, 
255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 128, 0, 0, 0, 0, 
0, 0, 0, 1, 255, 255, 255, 255, 192, 0, 0, 0, 0, 0, 0, 0, 3, 255, 255, 
255, 255, 224, 0, 0, 0, 0, 0, 0, 0, 7, 255, 255, 255, 255, 240, 0, 0, 0, 
0, 0, 0, 0, 15, 255, 255, 255, 255, 248, 0, 0, 0, 0, 0, 0, 0, 15, 255, 
255, 255, 255, 252, 0, 0, 0, 0, 0, 0, 7, 255, 255, 255, 255, 255, 255, 
248, 0, 0, 0, 0, 0, 127, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 
0, 255, 255, 255, 255, 255, 255, 255, 255, 192, 0, 0, 0, 3, 255, 255, 
255, 255, 255, 255, 255, 255, 224, 0, 0, 0, 7, 255, 255, 255, 255, 255, 
128, 0, 1, 240, 0, 0, 0, 7, 255, 255, 255, 255, 255, 0, 0, 0, 120, 0, 0,
0, 15, 255, 255, 255, 255, 254, 0, 0, 0, 60, 0, 0, 0, 31, 255,255, 255, 
255, 252, 0, 0, 0, 28, 0, 0, 0, 31, 255,255, 255, 255, 248, 0, 0, 0, 30, 
0, 0, 0, 63, 255,255, 255, 31, 240, 0, 0, 30, 14, 0, 0, 0, 63, 7, 7, 6, 
15, 240, 255, 255, 62, 14, 0, 0, 0, 63, 7, 7, 6, 15, 224, 255, 255, 62, 
7, 0, 0, 0, 127, 7, 7, 14, 15, 224, 255, 255, 62, 7, 0, 0, 0, 127, 135, 
7, 15, 31, 224, 255, 255, 30, 7, 0, 0, 0, 127, 135, 7, 15, 255, 224, 
255, 255, 0, 3, 0, 0, 0, 127, 134, 6, 15, 255, 224, 252, 0, 30, 3, 0, 0, 
0, 127, 130, 2, 14, 31, 224, 252, 0, 62, 3, 0, 0, 0, 127, 130, 2, 30, 
31, 224, 252, 0, 62, 3, 0, 0, 0, 127, 194, 34, 30, 31, 224, 252, 0, 62, 
3, 0, 0, 0, 127, 194, 34, 30, 31, 224, 255, 254, 62, 3, 0, 0, 0, 127, 
194, 34, 30, 31, 224, 255, 254, 62, 3, 0, 0, 0, 127, 194, 34, 30, 31, 
224, 255, 254, 62, 3, 0, 0, 0, 127, 192, 32, 62, 31, 224, 255, 254, 62, 
3, 0, 0, 0, 127, 224, 32, 62, 31, 224, 255, 254, 62, 3, 0, 0, 0, 127, 
224, 96, 62, 31, 224, 255, 254, 62, 3, 0, 0, 0, 127, 224, 112, 62, 31, 
224, 252, 0, 62, 3, 0, 0, 0, 127, 224, 112, 62, 31, 224, 252, 0, 62, 3, 
0, 0, 0, 127, 224, 112, 126, 31, 224, 252, 0, 62, 3, 0, 0, 0, 127, 240, 
112, 126, 31, 192, 252, 0, 62, 7, 0, 0, 0, 127, 240, 240, 126, 31, 192, 
252, 0, 62, 7, 0, 0, 0, 63, 255, 255, 255, 255, 192, 248, 0, 62, 7, 0, 
0, 0, 63, 255, 255, 255, 255, 192, 0, 0, 0, 14, 0, 0, 0, 63, 255, 255, 
255, 255, 128, 0, 0, 0, 14, 0, 0, 0, 31, 255, 255, 255, 255, 128, 0, 0, 
0, 30, 0, 0, 0, 31, 255, 255, 255, 255, 0, 0, 0, 0, 28, 0, 0, 0, 15, 255, 
255, 255, 255, 0, 0, 0, 0, 56, 0, 0, 0, 7, 255, 255, 255, 254, 0, 0, 0, 
0, 248, 0, 0, 0, 7, 255, 255, 255, 252, 0, 0, 0, 1, 240, 0, 0, 0, 1, 255, 
255, 255, 255, 255, 255, 255, 255, 224, 0, 0, 0, 0, 255, 255, 255, 255, 
255, 255, 255, 255, 192, 0, 0, 0, 0, 63, 255, 255, 255, 255, 255, 255, 
255, 0, 0, 0, 0, 0, 7, 255, 255, 255, 255, 255, 255, 240, 0, 0, 0, 0, 0, 
0, 7, 255, 255, 255, 255, 248, 0, 0, 0, 0, 0, 0, 0, 3, 255, 255, 255, 255, 
240, 0, 0, 0, 0, 0, 0, 0, 3, 255, 255, 255, 255, 224, 0, 0, 0, 0, 0, 0, 
0, 1, 255, 255, 255, 255, 192, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 
128, 0, 0, 0, 0, 0, 0, 0, 0, 63, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
31, 255, 255, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 255, 255, 248, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 3, 255, 255, 224, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 255, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

const unsigned char battBitmap10 [] PROGMEM = {
0, 0, 0, 0, 0, 0, 127, 255, 0, 127, 193, 192, 127, 129, 192, 127, 1, 192, 
126, 1, 192, 127, 255, 0, 0, 0, 0, 0, 0, 0 };
 
const unsigned char lockBitmap10 [] PROGMEM = {
0, 0, 30, 0, 51, 0, 33, 0, 33, 0, 127, 128, 115, 128, 115, 128, 63, 0, 0, 
0 };
        
const unsigned char unlockBitmap10 [] PROGMEM = {
0, 0, 30, 0, 51, 0, 1, 0, 1, 0, 127, 128, 127, 128, 127, 128, 63, 0, 0, 0 };

const unsigned char level1Bitmap [] PROGMEM = {
7, 128, 63, 240, 240, 60, 199, 140, 31, 224, 48, 48, 7, 128, 12, 192, 0, 
0, 3, 0, 3, 0 };

const unsigned char level2Bitmap [] PROGMEM = {
0x00, 0x00, 0x00, 0x18, 0x18, 0xd8, 0xd8, 0x00 };

const unsigned char level3Bitmap [] PROGMEM = {
0x00, 0x03, 0x03, 0x1b, 0x1b, 0xdb, 0xdb, 0x00 };

const unsigned char openBitmap [] PROGMEM = {
0x00, 0x18, 0x24, 0x42, 0x42, 0x24, 0x18, 0x00 };

const unsigned char closeBitmap [] PROGMEM = {
0x00, 0x18, 0x3C, 0x7E, 0x7E, 0x3C, 0x18, 0x00 };

広告

Written by macsbug

5月 3, 2018 @ 7:00 pm

カテゴリー: ESP32, M5STACK

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Google+ フォト

Google+ アカウントを使ってコメントしています。 ログアウト /  変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中

%d人のブロガーが「いいね」をつけました。