栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

arduino WiFiManager 配置静态链接IP采坑

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

arduino WiFiManager 配置静态链接IP采坑

arduino WiFiManager 配置静态链接IP采坑!!

这个是官网的示例代码

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();

  //WiFiManager
  //Local intialization. once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  //清除wifi保存的数据  如果上线后注释
  //wifiManager.resetSettings();


  //start-block2
  IPAddress _ip = IPAddress(10, 0, 1, 78);
  IPAddress _gw = IPAddress(10, 0, 1, 1);
  IPAddress _sn = IPAddress(255, 255, 255, 0);
  //end-block2
  
  wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);


  if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
    Serial.println("failed to connect, we should reset as see if it connects");
    delay(3000);
    ESP.restart();
    delay(5000);
  }

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");


  Serial.println("local ip");
  Serial.println(WiFi.localIP());
}

我的是esp-01S

坑爹的地方在这里 我配置上去后 就是访问不到 10.0.1.78 我换了几个版本测试了一下午,
我还以为是我DNS没有配置 然后我又配置了 wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn,_dns); 这个方法在新版本才有老版本也没用
结果发现还是没有用 ,我就在想

我就先让他自动分配wifi 然后查看 ip 网关 掩码和Dns

然后吧他自动配置的网段,在静态配置一下

结果发现!!只有 在这个网段才有用!!吐血

IPAddress local_IP(192,168,137,107); // 设置ESP8266-NodeMCU联网后的IP
IPAddress gateway(192, 168, 137, 1);    // 设置网关IP(通常网关IP是WiFI路由IP)
IPAddress subnet(255, 255, 255, 0);   // 设置子网掩码
IPAddress dns(192,168,137,1);           // 设置局域网DNS的IP(通常局域网DNS的IP是WiFI路由IP)
WiFiManager wifiManager;
IPAddress local_IP(192,168,137,107); // 设置ESP8266-NodeMCU联网后的IP
IPAddress gateway(192, 168, 137, 1);    // 设置网关IP(通常网关IP是WiFI路由IP)
IPAddress subnet(255, 255, 255, 0);   // 设置子网掩码
IPAddress dns(192,168,137,1);           // 设置局域网DNS的IP(通常局域网DNS的IP是WiFI路由IP)
void WiFiManagerInit()
{
  wifiManager.resetSettings();
  USE_SERIAL.println("WiFiManagerInit myssid: " + String(myssid) + "  mypassword :  " + String(mypassword));
  
  //比较新的版本才有
  wifiManager.setSTAStaticIPConfig(local_IP, gateway, subnet,dns);

  
  if (!wifiManager.autoConnect(myssid, mypassword)) {
    Serial.println("failed to connect, we should reset as see if it connects");
    delay(3000);
    ESP.reset();
    delay(5000);
  }
  
  USE_SERIAL.print("ESP8266 Connected to ");
  USE_SERIAL.println(WiFi.SSID());              // WiFi名称
  USE_SERIAL.print("IP address:t");
  USE_SERIAL.println(WiFi.localIP());           // IP
  USE_SERIAL.print("IP subnetMask:t");
  USE_SERIAL.println(WiFi.subnetMask());           // subnetMask
  USE_SERIAL.print("IP gatewayIP:t");
  USE_SERIAL.println(WiFi.gatewayIP());           // dnsIP
  USE_SERIAL.print("IP dnsIP:t");
  USE_SERIAL.println(WiFi.dnsIP());
   
  
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/468703.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号