IP 更改后 Esp8266 网络服务器重启

Esp8266 webserver restart after IP changed

在我的 ESP8266 arduino sketch 上更改了 IP 地址后,我试图访问网络服务器,但它没有响应(但有 ping)。
草图以 WIFI_AP 模式开始(用于设备测试目的),但如果在异步 WiFi 扫描后发现网络,回调代码将断开 WiFi,然后在 WIFI_AP_STA 模式下创建连接(作为辅助网络接入点)。

ESP8266WebServer webServer(80);


void myWifiScanCompleted( int networksFound ) {
  IPAddress ip(192,168,1,2);  // fixed IP address of this access point
  IPAddress gateway(192,168,1,254);
  IPAddress subnet(255,255,255,0);
  int count;
  int quality;

  networkConnection = NET_CONNECT_NONE;   // initialise as not found
  for(count = 0; count < networksFound; count++) {
    quality = getRSSIasQuality( WiFi.RSSI(count) );
    if(strcmp(WiFi.SSID(count).c_str(), ap_ssid ) == 0) {
      debug_print("found network %s (%d\%)", ap_ssid, quality);
      networkConnection = NET_CONNECT_FOUND;
      break;
    } else {
      debug_print("ignoring network %s (%d\%)", WiFi.SSID(count).c_str(), quality);
    }
  }

  if(networkConnection == NET_CONNECT_FOUND) {  // found network = try to connect
    // reset any existing connection
    webServer.stop();
    WiFi.disconnect();
    delay(100);

    WiFi.mode(WIFI_AP_STA);  // need both modes to join the network to get IP address, and then create access point
    WiFi.begin(ap_ssid, ap_password);
    count = 0;
    while (WiFi.status() != WL_CONNECTED)  {
      toggle_led(); // Blink the LED
      if(++count > (15 * 10)) {
        debug_print("error connecting to network %s", ap_ssid);
        networkConnection = NET_CONNECT_NONE;
        break;    // failed to connect
      }
      delay(100);
    }

    if(networkConnection != NET_CONNECT_NONE) {
      networkConnection = NET_CONNECT_CONNECTED;  // we've successfully connected to the network, now set up the access point
      ip = WiFi.localIP();
      gateway = (uint32_t)0;
      kev_softAPConfig(ip, gateway, subnet, false);    // no DHCP server
      if(! kev_softAP(ap_ssid, ap_password, MY_WIFI_CHANNEL, MY_WIFI_HIDDEN, MY_WIFI_MAX_CONNECTIONS)) {
        debug_print("create %s softAP failed - performing network reset", ap_ssid);
        WiFi.disconnect();
        networkConnection = NET_CONNECT_NONE;
      } else {
        // access point started = restart server
        webServer.begin();    // start web server on the new IP address
        debug_print("create %s softAP OK", ap_ssid);
      }
    }

  }
}

函数 kev_softAPConfig() 和 kev_softAP() 是 API 的修改版本,它们不启动 dhcp 服务(网络上已经存在)。 我已经证明这些功能有效,我只是改变了事物的顺序以便我可以提供设备测试功能。

有没有人做到过,或者我是先驱? :-)

我发现我必须使用与网络 DHCP 服务器分配给站点的 WiFi.localIP() 不同的接入点 IP 范围,这意味着必须编写一些代理代码来进行通信网络。