【问题标题】:I am unable to connect successfully to 8266 wifi module using arduino我无法使用 arduino 成功连接到 8266 wifi 模块
【发布时间】:2017-07-12 23:16:34
【问题描述】:

您好,我是 arduino 编程新手,但遇到了问题。我已经成功地使用 esp8266 模块显示了 wifi,即当我运行我的代码时,esp8266 模块创建了一个 wifi。它还要求输入密码,但之后没有成功连接的输出。我正在使用方法 wifi.softAp(username,password) 来创建 wifi 网络。我写了以下代码:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char* ssid = "Jeet";//Wifi username
const char* password = "wifibin12"; //Wifi password

ESP8266WebServer server(80);

void handleRoot() {
  server.send(200, "text/plain", "<h1>hello from esp8266!</h1>");
}

void setup(void) {
  // put your setup code here, to run once:

Serial.begin(115200);
//WiFi.mode(WIFI_AP);
Serial.print("this is my pass");
Serial.print(password);
WiFi.softAP(ssid, password);
Serial.print("Setting soft-Ap ... ");
// Wait for connection

while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }


//If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP

  server.on("/", handleRoot);      //Which routine to handle at root location

  server.begin();                  //Start server
  Serial.println("HTTP server started");

}

void loop() {
  // put your main code here, to run repeatedly:
server.handleClient();
}

当我运行代码时,我会在串行监视器上连续输出......如果有人知道我做错了什么,请帮我解决这个问题。建议也将不胜感激。

【问题讨论】:

    标签: arduino android-wifi esp8266


    【解决方案1】:

    它陷入了 while 循环。 Wifi.status() 在连接到 wifi 网络(连接到另一个接入点)时返回 WL_CONNECTED。所以如果你想让 AP 工作,你应该试试这个:

    #include <ESP8266WiFi.h>
    #include <WiFiClient.h>
    #include <ESP8266WebServer.h>
    
    const char* ssid = "Jeet";            //Wifi username
    const char* password = "wifibin12";  //Wifi password
    
    ESP8266WebServer server(80);
    
    void handleRoot() {
      server.send(200, "text/plain", "<h1>hello from esp8266!</h1>");
    }
    
    void setup(void) {
      // put your setup code here, to run once:
      Serial.begin(115200);
      Serial.print("this is my pass");
      Serial.print(password);
      WiFi.softAP(ssid, password);
      Serial.print("Setting soft-Ap ... ");
      // Wait for connection
    
    //If connection successful show IP address in serial monitor
      Serial.println("");
      Serial.print("AP name ");
      Serial.println(ssid);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());  //IP address assigned to your ESP
    
      server.on("/", handleRoot);      //Which routine to handle at root location
    
      server.begin();                  //Start server
      Serial.println("HTTP server started");
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      server.handleClient();
    }
    

    并且 WiFi.localIP() 不会返回 AP 的本地 IP。默认为 192.168.4.1。

    我建议从这里查看文档和示例:https://github.com/esp8266/Arduino/tree/master/doc/esp8266wifi

    【讨论】:

    • 谢谢@Petri。它对我有用。但是我无法在我的串行监视器上获得 serial.print 命令输出,即在我的串行监视器上没有显示上述 serial.print 输出。你能帮我解决这个问题,或者给我一个建议,为什么它没有出现。我再次感谢您的帮助。
    • 你用arduino ide的串口监视器吗?你有正确的波特率(115200)吗?还要检查接地连接。
    • 是的,我正在使用 arduino ide 的串行监视器,我已将波特率设置为 115200。你能告诉我你指的是什么接地连接吗?我正在使用 ESP 8266 01 模块。你能告诉我什么闪存大小可用于获得正确的输出。我正在向您发送工具配置屏幕截图的链接。您可以从以下链接查看屏幕截图:drive.google.com/open?id=0Bw8dCxSUylyKeUlkNjRZZVpXRHc。如果需要更改,请告诉我。非常感谢您的帮助
    • 如果你把一些串行打印放到loop()中,你能看到它们吗?并且您可以毫无问题地上传您的代码?
    • 只有在循环中我才能得到serial.print的输出。是因为什么问题吗??
    猜你喜欢
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 2022-07-31
    • 1970-01-01
    • 2020-07-08
    • 2020-10-15
    • 1970-01-01
    • 2021-09-15
    相关资源
    最近更新 更多