IP更改后,Esp8266 webserver重新启动

时间:2017-05-18 11:54:03

标签: webserver esp8266 access-point

我在ESP8266 arduino草图上更改了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()是不启动dhcp服务(已在网络上存在)的API的修改版本。 我已经证明这些功能有效,我只是改变了事物的顺序,以便我可以提供设备测试功能。

有没有人实现这个目标,还是我是先锋? : - )

1 个答案:

答案 0 :(得分:0)

I found that I had to use a different IP range for the access point from the WiFi.localIP() allocated to the station by the network DHCP server, which meant having to write some proxy code to communicate across networks.