   delay(100);
 }
 
-void handle_L0() {
-  //change_states(0);
-  handle_root();
+String genAPForm() {
+  String f = apChoice;
+  String buf = "";
+  const char *checked = " checked";
+
+  int n = WiFi.scanNetworks();
+  Serial.println("scan done");
+  if (n == 0) {
+    Serial.println("no networks found");
+    f =
+      "<html><head><title>WaterElf Aquaponics Helper: Wifi Config</title>"
+      "<style>body{background:#FFF;color: #000;font-family: sans-serif;}</style>"
+      "</head><body><p>No AP data :-(<br/>"
+      "<a href='/'>Back</a><br/><a href='/wifi'>Try again</a></p>"
+      "</body></html>";
+  } else {
+    Serial.print(n);
+    Serial.println(" networks found");
+    for (int i = 0; i < n; ++i) {
+      // print SSID and RSSI for each network found
+      Serial.print(i + 1);
+      Serial.print(": ");
+      Serial.print(WiFi.SSID(i));
+      Serial.print(" (");
+      Serial.print(WiFi.RSSI(i));
+      Serial.print(")");
+      Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
+      delay(10);
+
+      buf.concat("<input type='radio' name='ssid' value='");
+      buf.concat(WiFi.SSID(i));
+      buf.concat("'");
+      buf.concat(checked);
+      buf.concat(">");
+      buf.concat(WiFi.SSID(i));
+      buf.concat("<br/>\n");
+      checked = "";
+    }
+    f.replace("_ITEMS_", buf);
+  }
+  Serial.println("");
+
+  return f;
 }
 
-void handle_L2() {
-  //change_states(2);
-  handle_root();
+void handle_wifi() {
+  Serial.println("serving page at /wifi");
+  String toSend = genAPForm();
+  server.send(200, "text/html", toSend);
+  delay(100);
 }
 
-void handle_ALL() {
-  //change_states(0);
-  //change_states(2);
-  handle_root();
-}
-
-/*
-void change_states(int tgt) {
-  if (server.hasArg("v")) {
-    int state = server.arg("v").toInt() == 1;
-    Serial.print("LED");
-    Serial.print(tgt);
-    Serial.print("=");
-    Serial.println(state);
-    LEDstate[tgt] = state ? HIGH : LOW;
-    digitalWrite(tgt, LEDstate[tgt]);
-  }
-}
-*/
+void handle_L0() { handle_root(); } 
+void handle_L2() { handle_root(); }
+void handle_ALL() { handle_root(); }

  server.on("/L0", handle_L0);
  server.on("/L2", handle_L2);
  server.on("/ALL", handle_ALL);
  server.onNotFound(handleNotFound);
  server.begin();


  "</body></html>";

const char* apChoice = 
  "<html><head><title>WaterElf Aquaponics Helper: Wifi Config</title>"
  "<style>body{background:#FFF;color: #000;font-family: sans-serif;}</style>"
  "</head><body>"
  "<h2>Choose a wifi access point to join</h2><p><form method='POST' action='chz'> "
  "_ITEMS_<br/>Pass key: <input type='textarea' name='key'><br/><br/> "
  "<input type='submit' value='Submit'></form></p></body></html>";

const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 1, 1);
