ESP8266中WiFi管理器的连接不一致 [英] Inconsistent connectivity via WiFi manager in ESP8266

查看:154
本文介绍了ESP8266中WiFi管理器的连接不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一个特殊的问题.以下代码段连接到新的WiFi网络.程序中没有硬编码的ssid或密码.我正在使用 AsyncWifiManager AsyncWebServer 模块.当我连接到在自动连接门户中提供ssid和密码的家用WiFi路由器时,NodeMCU已连接,服务器工作正常.但是,每当我更改WiFi,连接到手机热点时,尽管我在串行监视器中获取了本地IP地址,服务器仍未运行.

I am facing a peculiar problem. The below code snippet connects to new WiFi network. No hard-coded ssid or password in the program. I am using AsyncWifiManager and AsyncWebServer modules. When I am connecting to my home WiFi router providing ssid and password in autoconnect portal, NodeMCU is getting connected and the server is working fine. But whenever I am changing the WiFi, connecting to my mobile phone hotspot, then server is not running, though I am getting local IP address in Serial Monitor.

#include <ESP8266WiFi.h>          
#include <ESPAsyncTCP.h>          

#include <ESPAsyncWebServer.h>     
#include <ESPAsyncWiFiManager.h> 

#include <FS.h>
#include <Wire.h>

   AsyncWiFiManager wifiManager(&server,&dns);

  // To clean previous settings. Use one time, then comment
  // wifiManager.resetSettings();



  // set custom static ip for portal
  IPAddress staticIP(192,168,0,20);  //ESP static ip
  IPAddress gateway(192,168,0,1); //IP Address of your WiFi Router (Gateway)
  IPAddress subnet(255,255,255,0); //Subnet mask
  wifiManager.setSTAStaticIPConfig(staticIP, gateway, subnet);


// Open WiFi Setup portal
  wifiManager.autoConnect();

  Serial.println("Connecting to WiFi..");


  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());


  WiFi.begin();


 while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
}

我正在通过Arduino IDE编程NodeMCU板.

I am programming the NodeMCU board via Arduino IDE.

推荐答案

由于代码使用IP/子网/网关的修复参数,因此您必须相应地设置不同的热点,或者在连接时可以选择以下选项ESP8266服务器到不同的热点:

As your code uses fix parameters for IP/subnet/gateway you have to setup your different hotspots accordingly or you have the following options to choose from as you connect your ESP8266 server to different hotspots:

  • 始终处于同一网络/子网络中(所有热点都是本地网络的一部分),您可以为与所连接的热点无关的所有设备使用固定IP地址
  • 如果您不想使用以上内容(=为所有设备使用固定IP):将DHCP服务器设置为始终根据NodeMCU的MAC地址为它们提供相同的IP地址.
  • 您使用mDNS并给ESP8266一个固定名称,然后通过 http://nyPreferredESP.local 进行调用(尽管IP有所不同),并在Android手机上使用像这个APP
  • 如果要使用更改的网关(设备不在同一网络/子网中,则可以通过Internet进行访问):这将需要更强大的功能.将动态DNS服务与域名一起使用.如果您的网关地址更改,动态DNS将几乎实时更新您的DNS记录.此处的示例
  • always being in the same network/subnetwork (all hotspots are part of a local network) you can use fix IP-addresses for all devices irrelevant of the hotspot you connect to
  • If you do not want to use above (=fix IP for all devices): Set the DHCP server to always give the same IP addresses to the NodeMCUs based on their MAC addresses.
  • You use mDNS and give the ESP8266 a fixed name and call via http://nyPreferredESP.local (allthough the IP varies) and use on your Android phone something like this APP
  • If you want to work with changing gateways (Devices not on the same net/subnet, access over the internet): This would require something a little more robust. Use a Dynamic DNS service along with a domain name. The Dynamic DNS will update your DNS records in almost real time if your gateway address changes. Example here

解决方案的复杂性是由于始终在同一网络/子网,固定网关或其他所有因素(设备的MAC地址和名称是固定的)中导致的,其余因素可能是可变的.阅读有关设置的一些基础知识本地网络

The complexity of the solution results from the factor always in the same network/subnet and a fixed gateway or everything (except MAC-address and name of the device are fix) and the rest may be variable. Read some basics about setting up a local network here

这篇关于ESP8266中WiFi管理器的连接不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆