/* Credential.ino, AutoConnect for ESP8266. https://github.com/Hieromon/AutoConnect Copyright 2018, Hieromon Ikasamo. Licensed under The MIT License. https://opensource.org/licenses/MIT An example sketch for an Arduino library for ESP8266 WLAN configuration via the Web interface. This sketch provides a conservation measures utility for saved credentials in EEPROM. By accessing the root path, you can see the list of currently saved credentials via the browser. Enter an entry number of the credential, that entry will be deleted from EEPROM. This sketch uses PageBuilder to support handling of operation pages. */ #include #include #include #include #include ESP8266WebServer Server; AutoConnect Portal(Server); String viewCredential(PageArgument&); String delCredential(PageArgument&); // Specified the offset if the user data exists. // The following two lines define the boundalyOffset value to be supplied to // AutoConnectConfig respectively. It may be necessary to adjust the value // accordingly to the actual situation. #define CREDENTIAL_OFFSET 0 //#define CREDENTIAL_OFFSET 64 /** * An HTML for the operation page. * In PageBuilder, the token {{SSID}} contained in an HTML template below is * replaced by the actual SSID due to the action of the token handler's * 'viewCredential' function. * The number of the entry to be deleted is passed to the function in the * POST method. */ static const char html[] PROGMEM = { "" "" "" "" "" "" "" "
" "
    " "{{SSID}}" "
" "

Enter deleting entry:

" "" "" "
" "

Menu

" "" "" }; // URL path as '/' PageElement elmList(html, {{ "SSID", viewCredential }}); PageBuilder rootPage("/", { elmList }); // URL path as '/del' PageElement elmDel("{{DEL}}", {{ "DEL", delCredential }}); PageBuilder delPage("/del", { elmDel }); // Retrieve the credential entries from EEPROM, Build the SSID line // with the
  • tag. String viewCredential(PageArgument& args) { AutoConnectCredential ac(CREDENTIAL_OFFSET); struct station_config entry; String content = ""; uint8_t count = ac.entries(); // Get number of entries. for (int8_t i = 0; i < count; i++) { // Loads all entries. ac.load(i, &entry); // Build a SSID line of an HTML. content += String("
  • ") + String((char *)entry.ssid) + String("
  • "); } // Returns the '
  • SSID
  • ' container. return content; } // Delete a credential entry, the entry to be deleted is passed in the // request parameter 'num'. String delCredential(PageArgument& args) { AutoConnectCredential ac(CREDENTIAL_OFFSET); if (args.hasArg("num")) { int8_t e = args.arg("num").toInt(); if (e > 0) { struct station_config entry; // If the input number is valid, delete that entry. int8_t de = ac.load(e - 1, &entry); // A base of entry num is 0. if (de > 0) { ac.del((char *)entry.ssid); // Returns the redirect response. The page is reloaded and its contents // are updated to the state after deletion. It returns 302 response // from inside this token handler. Server.sendHeader("Location", String("http://") + Server.client().localIP().toString() + String("/")); Server.send(302, "text/plain", ""); Server.client().flush(); Server.client().stop(); // Cancel automatic submission by PageBuilder. delPage.cancel(); } } } return ""; } void setup() { delay(1000); Serial.begin(115200); Serial.println(); rootPage.insert(Server); // Instead of Server.on("/", ...); delPage.insert(Server); // Instead of Server.on("/del", ...); // Set an address of the credential area. AutoConnectConfig Config; Config.boundaryOffset = CREDENTIAL_OFFSET; Portal.config(Config); // Start if (Portal.begin()) { Serial.println("WiFi connected: " + WiFi.localIP().toString()); } } void loop() { Portal.handleClient(); }