Arduino网络编程实战-WiFi篇-扫描网络

扫描网络

ESP-01/ESP-01 WiFi模块是由安信可科技开发,该模块核心处理器为ESP8266,在较小尺寸封装中集成了业界领先的Tensilica L06超低功耗32位微处理器(MCU),带有16位精简模式,主频支持80MHz和160MHz,支持RTOS,集成WiFi MAC/BB/RF/PA/LNA,板载天线。参考

该模块支持标准的IEEE802.11 b/g/n 协议,完整的TCP/IP协议栈。用户可以使用该模块为现有的设备添加联网功能,也可以构建独立的网络控制器。参考

ESP8266是高性能无线SOC,以最低成本提供最大实用性,为WiFi功能嵌入其他系统提供无限可能。参考

在这里插入图片描述

1、硬件准备

  • Arduino Mega2560 开发板一块
  • ESP-01/ESP-01s模块一块
  • ESP-01/ESP-01s转接板一块(请在某宝上搜索购买)
  • ESP-01/ESP-01s固件烧录器一个(请在某宝上搜索购买)
  • 杜邦线若干
  • Arduino开发板数据线一条
  • USB串口数据线一条

1)通过ESP-01/ESP-01s固件烧写器将ESP-AT的固件烧写到ESP-01/ESP-01s模块中

2)ESP-01/ESP-01s模块插入ESP-01/ESP-01s转接板

3)ESP-01/ESP-01s WiFi转接板与Arduino Mega2560硬件接线如下:

  • Arduino Mega2560 RX3<–-> ESP-01/ESP-01s TX
  • Arduino Mega2560 TX3<–-> ESP-01/ESP-01s RX
  • Arduino Mega2560 GND<–-> ESP-01/ESP-01s GND
  • Arduino Mega2560 5V<–-> ESP-01/ESP-01s VCC

2、软件准备

3、代码实现

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 定义与ESP-01模块通信串口
#define EspSerial Serial3
// 在AVR环境中使用ESP AT指令
#define ESP\_AT\_USE\_AVR true
// 开发板类型
#define BOARD\_TYPE "AVR Mega2560"
// 开发板名称
#define BOARD\_NAME BOARD\_TYPE
// 调试串口
#define DEBUG\_ESP8266\_AT\_WEBSERVER\_PORT Serial
// 扩展板类型
#define SHIELD\_TYPE "ESP8266-AT & ESP8266\_AT\_WebServer Library"

// 调试输出日志级别 0 至 4
#define \_ESP\_AT\_LOGLEVEL\_ 0

#include <ESP8266\_AT\_WebServer.h>

char ssid[] = "\*\*\*\*"; // WiFi名称
char pass[] = "\*\*\*\*"; // WiFi密码
// 无线状态
int status = WL_IDLE_STATUS;
int reqCount = 0;

// 打印MAC地址
void printMacAddress()
{

byte mac[6];
WiFi.macAddress(mac);

char buf[20];

sprintf\_P(buf, PSTR( "%02X:%02X:%02X:%02X:%02X:%02X"), mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
Serial.print(F("MAC address: "));
Serial.println(buf);
}

// 打印扫描结果
void listNetworks()
{

int numSsid = WiFi.scanNetworks();

if (numSsid == -1)
{
Serial.println(F("Couldn't get a wifi connection"));
while (true);
}


Serial.print(F("Number of available networks:"));
Serial.println(numSsid);


for (int thisNet = 0; thisNet < numSsid; thisNet++)
{
Serial.print(thisNet);
Serial.print(F(") "));
Serial.print(WiFi.SSID(thisNet));
Serial.print(F("\tSignal: "));
Serial.print(WiFi.RSSI(thisNet));
Serial.print(F(" dBm"));
Serial.print(F("\tEncryption: "));
printEncryptionType(WiFi.encryptionType(thisNet));
}
}
// 打印加密类型
void printEncryptionType(int thisType)
{
// read the encryption type and print out the name
switch (thisType)
{
case ENC_TYPE_WEP:
Serial.print(F("WEP"));
break;
case ENC_TYPE_WPA_PSK:
Serial.print(F("WPA\_PSK"));
break;
case ENC_TYPE_WPA2_PSK:
Serial.print(F("WPA2\_PSK"));
break;
case ENC_TYPE_WPA_WPA2_PSK:
Serial.print(F("WPA\_WPA2\_PSK"));
break;
case ENC_TYPE_NONE:
Serial.print(F("None"));
break;
}
Serial.println();
}
void setup()
{
Serial.begin(115200);
while (!Serial);

Serial.print(F("\nStarting ConnectWPA on "));
Serial.print(BOARD_NAME);
Serial.print(F(" with "));
Serial.println(SHIELD_TYPE);
Serial.println(ESP8266_AT_WEBSERVER_VERSION);

// 初始化ESP模块
EspSerial.begin(115200);
WiFi.init(&EspSerial);

Serial.println(F("WiFi shield init done"));

// 检查ESP-01与开发板连接状态
if (WiFi.status() == WL_NO_SHIELD)
{
Serial.println(F("WiFi shield not present"));
while (true);
}

// 连接网络
while ( status != WL_CONNECTED)
{
Serial.print(F("Connecting to WPA SSID: "));
Serial.println(ssid);

// 连接WiFi热点
status = WiFi.begin(ssid, pass);
}

Serial.print(F("You're connected to the network, IP = "));
Serial.println(WiFi.localIP());
printMacAddress();
}

void loop()
{
Serial.println("-------------------------");
Serial.println(F("Scanning available networks..."));
listNetworks();
delay(10000);
}

4、运行结果

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/122803213