macsbug

M5STACK WiFiAnalyzer

leave a comment »

ESP8266WiFiAnalyzer を M5STACK に移植しました。     2018.04.30

ORIGINALは moononournation/ESP8266WiFiAnalyzer です。
instructables : 陳亮氏の記事:ESP8266WiFiAnalyzer
moononournation、陳亮氏 に感謝致します。

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

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

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


スケッチ:

// ESP8266 WiFi Analyzer
// Revise from ESP8266WiFi WiFiScan example.
// Require ESP8266 board support, Adafruit GFX and ILI9341 library.
// http://www.instructables.com/id/ESP8266-WiFi-Analyzer/
// moononournation/ESP8266WiFiAnalyzer
// https://github.com/moononournation/ESP8266WiFiAnalyzer
// Github:https://macsbug.wordpress.com/2018/04/30/m5stack-wifianalyzer/
#include "WiFi.h"
#include <M5Stack.h>
#include "M5StackUpdater.h"                          // SD Update
#include "Free_Fonts.h"
#define RSSI_CEILING -40                             // Graph constant
#define RSSI_FLOOR -100
#define GRAPH_BASELINE 222
#define GRAPH_HEIGHT 188
#define CHANNEL_WIDTH 20
#define NEAR_CHANNEL_RSSI_ALLOW -70
// Channel color mapping from channel 1 to 14
uint16_t channel_color[] = {RED,ORANGE,YELLOW,GREEN,CYAN,
  MAGENTA,RED,ORANGE,YELLOW,GREEN,CYAN,MAGENTA,RED,ORANGE
};

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);                      // FreeMono18pt7b
  //M5.Lcd.setFreeFont(FSS9);                        // FreeSans9pt7b
  M5.Lcd.fillScreen(BLUE);                           // init banner
  //M5.Lcd.setTextSize(1);
  M5.Lcd.setTextColor(WHITE, RED);
  M5.Lcd.setCursor(0, 0);
  M5.Lcd.print("  M5");
  M5.Lcd.setTextColor(WHITE, ORANGE);
  M5.Lcd.print("STACK ");
  M5.Lcd.setTextColor(WHITE, GREEN);
  M5.Lcd.print(" WiFi ");
  M5.Lcd.setTextColor(WHITE, BLUE);
  M5.Lcd.print(" Analyzer");
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);
}

void loop() { 
  uint8_t ap_count[] = {0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0};
  int32_t max_rssi[] = {-100,-100,-100,-100,-100,-100,-100,
                        -100,-100,-100,-100,-100,-100,-100};
  int n = WiFi.scanNetworks();
  M5.Lcd.fillRect(0, 16, 320, 224, BLACK);           // clear old graph
  M5.Lcd.setTextSize(1);
  if (n == 0) {
    M5.Lcd.setTextColor(BLACK);
    M5.Lcd.setCursor(0, 16);
    M5.Lcd.println("no networks found");
  } else {
    for (int i = 0; i < n; i++) {                    // plot found WiFi info
      int32_t channel = WiFi.channel(i);
      int32_t rssi = WiFi.RSSI(i);
      uint16_t color = channel_color[channel - 1];
      int height = constrain(map(rssi, RSSI_FLOOR, 
                   RSSI_CEILING, 1, GRAPH_HEIGHT), 
                   1, GRAPH_HEIGHT);
      ap_count[channel - 1]++;                       // channel stat
      if (rssi > max_rssi[channel - 1]) {
        max_rssi[channel - 1] = rssi;
      }
      M5.Lcd.drawLine(channel * CHANNEL_WIDTH, GRAPH_BASELINE - height, 
             (channel - 1) * CHANNEL_WIDTH, GRAPH_BASELINE + 1, color);
      M5.Lcd.drawLine(channel * CHANNEL_WIDTH, GRAPH_BASELINE - height, 
             (channel + 1) * CHANNEL_WIDTH, GRAPH_BASELINE + 1, color);
      M5.Lcd.setTextColor(color); // SSID, signal strengh and if not encrypted
      M5.Lcd.setCursor((channel - 1) * CHANNEL_WIDTH, GRAPH_BASELINE - 10 - height);
      M5.Lcd.print(WiFi.SSID(i));
      M5.Lcd.print('(');
      M5.Lcd.print(rssi);
      M5.Lcd.print(')');
      //if (WiFi.encryptionType(i) == ENC_TYPE_NONE) {
      //  M5.Lcd.print('*');
      //}
      delay(10);
    }
  }
  M5.Lcd.setTextColor(WHITE);                         // print WiFi stat
  M5.Lcd.setCursor(0, 16);
  M5.Lcd.print(n);
  M5.Lcd.print(" networks found, suggested channels: ");
  bool listed_first_channel = false;
  for (int i = 1; i <= 11; i++) { // channels 12-14 may not available
    // check previous channel signal strengh
    if ((i == 1) || (max_rssi[i - 2] < NEAR_CHANNEL_RSSI_ALLOW)) {
      // check next channel signal strengh
      if ((i == sizeof(channel_color)) || (max_rssi[i] < 
                              NEAR_CHANNEL_RSSI_ALLOW)) {
        if (ap_count[i - 1] == 0) { // check no AP exists in same channel
          if (!listed_first_channel) {
            listed_first_channel = true;
          } else {M5.Lcd.print(", ");}
          M5.Lcd.print(i);
        }
      }
    }
  }
  // draw graph base axle
  M5.Lcd.drawFastHLine(0, GRAPH_BASELINE, 320, WHITE);
  for (int i = 1; i <= 14; i++) {
    M5.Lcd.setTextColor(channel_color[i - 1]);
    M5.Lcd.setCursor((i * CHANNEL_WIDTH) - 
            ((i < 10)?3:6), GRAPH_BASELINE + 2);
    M5.Lcd.print(i);
    if (ap_count[i - 1] > 0) {
      M5.Lcd.setCursor((i * CHANNEL_WIDTH) - 
           ((ap_count[i - 1] < 10)?9:12), GRAPH_BASELINE + 11);
      M5.Lcd.print('(');
      M5.Lcd.print(ap_count[i - 1]);
      M5.Lcd.print(')');
    }
  }
  delay(5000);  // Wait a bit before scanning again
}

Written by macsbug

4月 30, 2018 @ 6:00 pm

カテゴリー: ESP32, M5STACK

コメントを残す