webserveresp8266access-point

Esp8266 webserver restart after IP changed


I'm trying to access a webserver after I've changed IP addresses on my ESP8266 arduino sketch, but it's not responding (but a ping is).
The sketch starts in WIFI_AP mode (for device testing purposes), but if a network is discovered after an asynchronous WiFi scan, the call back code disconnects the WiFi and then creates a connection in WIFI_AP_STA mode (acting as a secondary network access point).

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);
      }
    }

  }
}

The functions kev_softAPConfig() and kev_softAP() are modified versions of the API that do NOT start the dhcp service (which already exists on the network). I've proved these functions work, I've just changed the order of things so that I can provide device testing features.

Has anyone achieved this, or am I a pioneer? :-)


Solution

  • 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.