WiFiScan.ino 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/core/gray
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray
  8. *
  9. * Describe: Wifi scan. wifi扫描
  10. * Date: 2021/7/28
  11. *******************************************************************************
  12. */
  13. #include <M5Stack.h>
  14. #include "WiFi.h"
  15. void setup() {
  16. M5.begin(); // Init M5Stack. 初始化M5Stack
  17. M5.Power.begin(); // Init power 初始化电源模块
  18. WiFi.mode(WIFI_STA); // Set WiFi to station mode and disconnect from an AP
  19. // if it was previously connected.
  20. // 将WiFi设置为站模式,如果之前连接过AP,则断开连接
  21. WiFi.disconnect(); // Turn off all wifi connections. 关闭所有wifi连接
  22. delay(100); // 100 ms delay. 延迟100ms
  23. M5.Lcd.print("WIFI SCAN"); // Screen print string. 屏幕打印字符串
  24. }
  25. void loop() {
  26. M5.Lcd.setCursor(0, 0); // Set the cursor at (0,0). 将光标设置在(0,0)处
  27. M5.Lcd.println("Please press Btn.A to (re)scan");
  28. M5.update(); // Check the status of the key. 检测按键的状态
  29. if (M5.BtnA.isPressed()) { // If button A is pressed. 如果按键A按下
  30. M5.Lcd.clear(); // Clear the screen. 清空屏幕
  31. M5.Lcd.println("scan start");
  32. int n = WiFi.scanNetworks(); // return the number of networks found.
  33. // 返回发现的网络数
  34. if (n == 0) { // If no network is found. 如果没有找到网络
  35. M5.Lcd.println("no networks found");
  36. } else { // If have network is found. 找到网络
  37. M5.Lcd.printf("networks found:%d\n\n", n);
  38. for (int i = 0; i < n;
  39. ++i) { // Print SSID and RSSI for each network found.
  40. // 打印每个找到的网络的SSID和信号强度
  41. M5.Lcd.printf("%d:", i + 1);
  42. M5.Lcd.print(WiFi.SSID(i));
  43. M5.Lcd.printf("(%d)", WiFi.RSSI(i));
  44. M5.Lcd.println(
  45. (WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
  46. delay(10);
  47. }
  48. }
  49. delay(1000);
  50. }
  51. }