NodeMCU ESP8266用C连接到iPhone Hotspot(NodeMCU ESP8266 connecting to iPhone Hotspot with C)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
NodeMCU ESP8266用C连接到iPhone Hotspot(NodeMCU ESP8266 connecting to iPhone Hotspot with C)

我有一个关于从NodeMCU ESP8266板到Apple iPhone6个人热点的wifi连接的问题。 iOS版本是10.2.1(14027)。 我使用的NodeMCU代码使用WPA2与我的家庭WLAN一起使用没有任何问题。 如果我更改SSID和密码以连接到Apple热点, while()循环将永远运行(请参阅代码,我使用的是Arduino IDE 1.8.2):

#include <ESP8266WiFi.h> const char* ssid = "iPhone6"; const char* password = "passwd"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(1500); } } void loop() { // do something delay(10000); }

为了检查一下,我使用NodeMCU扫描wifi网络并列出了我的Apple热点(也使用了WPA2),因此主板可以“看到”它。 我可以使用iPad和戴尔笔记本电脑连接到Apple热点,这样也可以。 我找到的所有网页都在处理将NodeMCU设置为WiFi服务器而不是客户端的问题。 我的代码中是否还有其他细节? 或者我可以使用的ESP8266WiFi还有另一个WiFi库吗? 如果有人设法让这个连接工作,我将不胜感激。

I have a question regarding wifi connection from a NodeMCU ESP8266 board to an Apple iPhone6 personal hotspot. The iOS version is 10.2.1 (14027). The NodeMCU code I use works with my home WLAN using WPA2 without any problems. If I change SSID and password to connect to the Apple hotspot the while() loop runs forever (see code, I'm using Arduino IDE 1.8.2):

#include <ESP8266WiFi.h> const char* ssid = "iPhone6"; const char* password = "passwd"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(1500); } } void loop() { // do something delay(10000); }

To check things out I used the NodeMCU to scan for wifi networks and got my Apple hotspot listed (also using WPA2) so the board can "see" it. I can connect to the Apple hotspot with my iPad and my Dell Laptop so this works also. All the web pages I found are dealing with problems to set up the NodeMCU as WiFi server and not as a client. Is there another detail that I forgot in my code? Or is there another WiFi library than ESP8266WiFi I can use? If someone managed to get this connection work I would appreciate any hint.

最满意答案

问题是( 我相信 )ESP8266将处于上次启动时的任何模式( AP , STA , AP_STA ),除非您明确更改它。 因此,举例来说,你的代码对我来说很好,因为有几次,这取决于我之前在我的电路板上所做的事情。

为了使代码始终如一地工作,您必须(如@cagdas所说)明确地将其置于STA模式。 因此,对您的代码进行以下更改将会这样做。 我已经使用我的主板和iPhone 6s,1.8.2 Arduino IDE和ESP8266库1.0.0版验证了这一点。

#include <ESP8266WiFi.h> const char* ssid = "iPhone6"; const char* password = "passwd"; void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); // SETS TO STATION MODE! WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(1500); } } void loop() { Serial.print("IP is "); Serial.println(WiFi.localIP()); delay(10000); }

The problem is (I believe) that the ESP8266 will be in whatever mode (AP, STA, AP_STA) it was in the last time it was powered up, unless you explicitly change it. So, for example, your code worked fine for me, as is, a couple of times depending on what I had previously been doing with my board.

In order to get the code to work consistently, you have to (as @cagdas says) explicitly put it in STA mode. So the following change to your code will do so. I've verified this using my board and an iPhone 6s, the 1.8.2 Arduino IDE and version 1.0.0 of the ESP8266 libraries.

#include <ESP8266WiFi.h> const char* ssid = "iPhone6"; const char* password = "passwd"; void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); // SETS TO STATION MODE! WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(1500); } } void loop() { Serial.print("IP is "); Serial.println(WiFi.localIP()); delay(10000); }

更多推荐

本文发布于:2023-04-20 18:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/94e9dd49b507768341edbb9e33f95070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   NodeMCU   connecting   Hotspot   iPhone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!