WiFi 控制 LED(接入点)
该项目允许您通过网络界面控制 LED 灯。Arduino 板充当 WiFi 接入点,创建自己的本地网络,您可以使用网络浏览器连接到该网络。连接后,您可以通过网络浏览器导航到设备的 IP 地址,在那里您将找到用于打开和关闭 LED(连接到板的引脚 13)的选项。该项目通过串口监视器提供 LED 状态的实时反馈,使调试和理解操作流程更加容易。
1. 上传代码
打开路径 elite-explorer-kit-main\r4_new_feature\01-wifi_ap 下的 01-wifi_ap.ino 文件,或将此代码复制到 Arduino IDE 中。
备注
通过随 Arduino UNO R4 Core 一起提供的内置 WiFiS3 库启用 Wi-Fi 支持。安装该核心会自动安装 WiFiS3 库。
您仍然需要创建或修改 arduino_secrets.h,将 SECRET_SSID 和 SECRET_PASS 替换为您的 WiFi 接入点的名称和密码。该文件应包含:
//arduino_secrets.h header file
#define SECRET_SSID "yournetwork"
#define SECRET_PASS "yourpassword"
2. 代码说明
导入所需库
导入用于 WiFi 功能的
WiFiS3库和用于密码等敏感数据的arduino_secrets.h。#include "WiFiS3.h" #include "arduino_secrets.h"
配置和变量初始化
定义 WiFi SSID、密码和密钥索引,以及 LED 引脚和 WiFi 状态。
char ssid[] = SECRET_SSID; char pass[] = SECRET_PASS; int keyIndex = 0; int led = LED_BUILTIN; int status = WL_IDLE_STATUS; WiFiServer server(80);
setup()函数初始化串行通信并配置 WiFi 模块。
void setup() { // ... setup code ... // Create access point status = WiFi.beginAP(ssid, pass); // ... error handling ... // start the web server on port 80 server.begin(); }
我们还检查 uno R4 wifi 的固件版本是否是最新的。如果不是最新版本,将显示升级提示。您可以参考 更新UNO R4 WiFi板上的无线电模块固件 进行固件升级。
... String fv = WiFi.firmwareVersion(); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println("Please upgrade the firmware"); } ...
您可能需要修改以下代码,以便能够更改 Arduino 的默认 IP。
WiFi.config(IPAddress(192,48,56,2));
主
loop()函数Arduino 代码中的
loop()函数执行几个关键操作,具体包括:检查是否有设备连接或断开接入点。
监听发出 HTTP 请求的传入客户端。
读取客户端数据并基于该数据执行操作——比如打开或关闭 LED。
这里,让我们分解
loop()函数以使这些步骤更易于理解。检查 WiFi 状态
代码首先检查 WiFi 状态是否已更改。如果有设备连接或断开,串口监视器将相应地显示信息。
if (status != WiFi.status()) { status = WiFi.status(); if (status == WL_AP_CONNECTED) { Serial.println("Device connected to AP"); } else { Serial.println("Device disconnected from AP"); } }
监听传入客户端
WiFiClient client = server.available();等待传入客户端。WiFiClient client = server.available();
处理客户端请求
监听传入客户端,并向其提供 HTML 网页。当用户在被提供的网页上点击 “Click here to turn the LED on” 或 “Click here to turn the LED off” 链接时,会向 Arduino 服务器发送 HTTP GET 请求。具体来说,将访问 URL “http://yourAddress/H” 以打开 LED,”http://yourAddress/L” 以关闭 LED。
WiFiClient client = server.available(); if (client) { // ... client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); client.print("<p style=\"font-size:7vw;\">Click <a href=\"/H\">here</a> turn the LED on<br></p>"); client.print("<p style=\"font-size:7vw;\">Click <a href=\"/L\">here</a> turn the LED off<br></p>"); // ... }
Arduino 代码监听这些传入的 GET 请求。当它在传入文本行(HTTP 头)的末尾检测到
GET /H时,它将连接到引脚 13 的 LED 设置为 HIGH,从而打开它。类似地,如果检测到GET /L,它将 LED 设置为 LOW,从而关闭它。while (client.connected()) { // loop while the client's connected delayMicroseconds(10); // This is required for the Arduino Nano RP2040 Connect - otherwise it will loop so fast that SPI will never be served. if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out to the serial monitor if (c == '\n') { // if the byte is a newline character ... } else { // if you got a newline, then clear currentLine: currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } // Check to see if the client request was "GET /H" or "GET /L": if (currentLine.endsWith("GET /H")) { digitalWrite(led, HIGH); // GET /H turns the LED on } if (currentLine.endsWith("GET /L")) { digitalWrite(led, LOW); // GET /L turns the LED off } }
参考