Uno R4 WiFi的AP_SimpleWebServer示例连接超时问题求助
Arduino Uno R4 WiFi AP模式Web服务器连接超时问题排查
问题描述
我是Arduino新手,刚入手Uno R4 WiFi做项目练习。尝试用官方的"AP_SimpleWebServer"示例实现Web控制LED,但访问串口输出的IP链接时总是连接超时。仅修改了arduino_secrets.h中的SSID和密码,其他代码未改动。串口监视器偶尔提示Please upgrade the firmware,不确定是否影响。已参考相关论坛帖子修改过AP名称,但问题依旧。
使用的代码
/* WiFi Web Server LED Blink A simple web server that lets you blink an LED via the web. This sketch will create a new access point (with no password). It will then launch a new server and print out the IP address to the Serial Monitor. From there, you can open that address in a web browser to turn on and off the LED on pin 13. If the IP address of your board is yourAddress: http://yourAddress/H turns the LED on http://yourAddress/L turns it off created 25 Nov 2012 by Tom Igoe adapted to WiFi AP by Adafruit Find the full UNO R4 WiFi Network documentation here: https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#access-point */ #include "WiFiS3.h" #include "arduino_secrets.h" ///////please enter your sensitive data in the Secret tab/arduino_secrets.h char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key index number (needed only for WEP) int led = LED_BUILTIN; int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } Serial.println("Access Point Web Server"); pinMode(led, OUTPUT); // set the LED pin mode // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true); } String fv = WiFi.firmwareVersion(); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println("Please upgrade the firmware"); } // by default the local IP address will be 192.168.4.1 // you can override it with the following: WiFi.config(IPAddress(192,48,56,2)); // print the network name (SSID); Serial.print("Creating access point named: "); Serial.println("Arduino"); // Create open network. Change this line if you want to create an WEP network: status = WiFi.beginAP("Arduino", pass); if (status != WL_AP_LISTENING) { Serial.println("Creating access point failed"); // don't continue while (true); } // wait 10 seconds for connection: delay(10000); // start the web server on port 80 server.begin(); // you're connected now, so print out the status printWiFiStatus(); } void loop() { // compare the previous status to the current status if (status != WiFi.status()) { // it has changed update the variable status = WiFi.status(); if (status == WL_AP_CONNECTED) { // a device has connected to the AP Serial.println("Device connected to AP"); } else { // a device has disconnected from the AP, and we are back in listening mode Serial.println("Device disconnected from AP"); } } WiFiClient client = server.available(); // listen for incoming clients if (client) { // if you get a client, Serial.println("new client"); // print a message out the serial port String currentLine = ""; // make a String to hold incoming data from the client 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 // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); // the content of the HTTP response follows the header: 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>"); // The HTTP response ends with another blank line: client.println(); // break out of the while loop: break; } 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 } } } // close the connection: client.stop(); Serial.println("client disconnected"); } } void printWiFiStatus() { // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID()); // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip); // print where to go in a browser: Serial.print("To see this page in action, open a browser to http://"); Serial.println(ip); }
解决建议
- 优先升级WiFi模块固件:串口提示的固件升级非常关键,旧版本固件可能存在AP模式的兼容性问题。打开Arduino IDE,依次点击
工具->WiFi101 / WiFiNINA Firmware Updater,按照向导完成固件升级,确保版本为最新。 - 修正AP配置的关键错误:
- 代码中创建AP的名称是硬编码的
"Arduino",而非你在arduino_secrets.h中填写的SECRET_SSID,连接时要找名称为Arduino的WiFi网络,不是你的家庭WiFi。 - 注释掉
WiFi.config(IPAddress(192,48,56,2))这行代码,使用默认的AP IP(192.168.4.1),避免自定义IP和设备网络冲突。 - 代码中
WiFi.beginAP("Arduino", pass)创建的是带密码的AP,连接时输入的密码必须和SECRET_PASS一致,否则无法建立连接。
- 代码中创建AP的名称是硬编码的
- 验证设备连接状态:打开串口监视器,观察是否出现
Device connected to AP提示,若未出现,说明你的手机/电脑未成功连接到Arduino的AP,先排查WiFi连接问题。 - 调整Web访问方式:
- 连接Arduino的AP后,关闭手机的移动数据,避免网络自动切换。
- 直接在浏览器输入串口输出的IP(如
http://192.168.4.1),先确认能否打开页面,再测试LED控制链接。
- 修复代码逻辑漏洞:原代码中判断HTTP请求的位置错误,可能导致无法触发LED控制。将判断
GET /H和GET /L的代码移到换行处理的分支中,修改后的代码片段如下:
if (c == '\n') { if (currentLine.length() == 0) { // 发送HTTP响应的代码保持不变 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>"); client.println(); break; } else { // 在这里处理请求判断 if (currentLine.endsWith("GET /H")) { digitalWrite(led, HIGH); } if (currentLine.endsWith("GET /L")) { digitalWrite(led, LOW); } currentLine = ""; } }
内容的提问来源于stack exchange,提问作者Rob S
相关产品推荐
相关产品推荐

