Arduino与Protesu仿真-WiFi网络扫描 WiFi网络扫描 文章目录
本文将介绍如何Arduino在Proteus的仿真环境中进行WiFi网络扫描。
1、软件准备 1)Arduino IDE或 VSCode + PlatformIO
2)Proteus电路仿真软件
3)Arduino的Proteus仿真模型库,ArduinoLibraryforProteusV2.0.zip
4)串口调试工具软件
在前面的文章中,我们对如何搭建Arduino在Proteus中如何进行网络仿真,WiFi网络连接,请参考:
2、硬件准备 1)WiFi模块:ESP01-S模块或其他带标准AT固件的WiFi模块,本文使用ESP01-S模块。
2)串口(TTL)转USB模块。该模块用于连接ESP01-S的串口引脚并通过USB接口连接到电脑。
3、仿真电路原理图
4、仿真代码实现 示例代码使用到如下开源库:
该库与Arduino的WiFi 库和Ethernet 类似,因此使用起来非常方便。下面将实现如何进行WiFi热点扫描。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 #include <Arduino.h> #define DEBUG\_ESP8266\_AT\_WEBSERVER\_PORT Serial // 日志输出 #define \_ESP\_AT\_LOGLEVEL\_ 1 #define ESP\_AT\_USE\_AVR 1 #define BOARD\_TYPE "AVR Mega2560" #define EspSerial Serial1 #define BOARD\_NAME BOARD\_TYPE #define MULTIPLY\_FACTOR 1 #include "ESP8266\_AT\_WebServer.h" // 打印ESP模块MAC地址 void printMacAddress(); // 打印WiFi网络名称 void listNetworks(); // 打印网络加密类型 void printEncryptionType(int thisType); void setup() { // 初始化串口,用于调试 Serial.begin(115200); // 初始化连接ESP模块的串口 Serial1.begin(115200); // 初始化ESP模块 WiFi.init(&Serial1); // 检查WiFi模块是否已经连接 if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WiFi shield not present"); while (true); } // 打开模块MAC地址 printMacAddress(); } void loop() { // 扫描网络 Serial.println(); Serial.println("Scanning available networks..."); listNetworks(); delay(10000); } void printMacAddress() { byte mac[6]; // 查询ESP模块的MAC WiFi.macAddress(mac); // 向串口打印MAC char buf[20]; sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]); Serial.print("MAC address: "); Serial.println(buf); } void listNetworks() { // 扫描附近WiFi热点 int numSsid = WiFi.scanNetworks(); if (numSsid == -1) { Serial.println("Couldn't get a wifi connection"); while (true); } // 打印网络列表 Serial.print("Number of available networks:"); Serial.println(numSsid); for (int thisNet = 0; thisNet < numSsid; thisNet++) { Serial.print(thisNet); Serial.print(") "); Serial.print(WiFi.SSID(thisNet)); Serial.print("\tSignal: "); Serial.print(WiFi.RSSI(thisNet)); Serial.print(" dBm"); Serial.print("\tEncryption: "); printEncryptionType(WiFi.encryptionType(thisNet)); } } void printEncryptionType(int thisType) { // 打印网络加密类型名称 switch (thisType) { case ENC_TYPE_WEP: Serial.print("WEP"); break; case ENC_TYPE_WPA_PSK: Serial.print("WPA\_PSK"); break; case ENC_TYPE_WPA2_PSK: Serial.print("WPA2\_PSK"); break; case ENC_TYPE_WPA_WPA2_PSK: Serial.print("WPA\_WPA2\_PSK"); break; case ENC_TYPE_NONE: Serial.print("None"); break; } Serial.println(); }
代码如何工作?
1)导入依赖库头文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <Arduino.h> #define DEBUG_ESP8266_AT_WEBSERVER_PORT Serial // 日志输出 #define _ESP_AT_LOGLEVEL_ 1 #define ESP_AT_USE_AVR 1 #define BOARD_TYPE "AVR Mega2560" #define EspSerial Serial1 #define BOARD_NAME BOARD_TYPE #define MULTIPLY_FACTOR 1 #include "ESP8266_AT_WebServer.h"
2)声明相关功能函数原型
1 2 3 4 5 6 7 // 打印ESP模块MAC地址 void printMacAddress(); // 打印WiFi网络名称 void listNetworks(); // 打印网络加密类型 void printEncryptionType(int thisType);
3)在setup函数中
初始化调试串口
初始化WiFi模块连接串口
初始化WiFi模块
检查WiFi模块是否已经连接
1 2 3 4 5 if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WiFi shield not present"); while (true); }
打印WiFi模块的MAC信息
4)在loop函数中,扫描网络热点,打印网络热点名称和加密类型
5)功能函数实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 void printMacAddress() { byte mac[6]; // 查询ESP模块的MAC WiFi.macAddress(mac); // 向串口打印MAC char buf[20]; sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]); Serial.print("MAC address: "); Serial.println(buf); }
WiFi.macAddress(mac);函数用于查询WiFi模块的MAC信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 void listNetworks() { // 扫描附近WiFi热点 int numSsid = WiFi.scanNetworks(); if (numSsid == -1) { Serial.println("Couldn't get a wifi connection"); while (true); } // 打印网络列表 Serial.print("Number of available networks:"); Serial.println(numSsid); for (int thisNet = 0; thisNet < numSsid; thisNet++) { Serial.print(thisNet); Serial.print(") "); Serial.print(WiFi.SSID(thisNet)); Serial.print("\tSignal: "); Serial.print(WiFi.RSSI(thisNet)); Serial.print(" dBm"); Serial.print("\tEncryption: "); printEncryptionType(WiFi.encryptionType(thisNet)); } }
函数 WiFi.scanNetworks()用于扫描当前附近的WiFi热点,并返回扫描到的数量。
函数WiFi.SSID(thisNet)用于查询指定索引的WiFi热点名称。
函数WiFi.RSSI(thisNet)用于查询指定索引的WiFi热点的RSSI信息,即信号强度
函数WiFi.encryptionType(thisNet)用于查询指定WiFi热点的加密类型,并返回加密类型的索引。
5、仿真结果
从仿真结果可以知道,Arduino在Proteus中已经成功通过ESP模块扫描了附近的WiFi热点。
文章来源: https://iotsmart.blog.csdn.net/article/details/133149706
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!