/* This example uses the AdafruitHTTPServer class to create a simple webserver */ #include #include #define WLAN_SSID "yourSSID" #define WLAN_PASS "yourPassword" #define PORT 80 // The TCP port to use #define MAX_CLIENTS 3 int ledPin = PA15; int visit_count = 0; void info_html_generator (const char* url, const char* query, httppage_request_t* http_request); void file_not_found_generator (const char* url, const char* query, httppage_request_t* http_request); const char hello_html[] = "

Hello World!

"; HTTPPage pages[] = { HTTPPageRedirect("/", "/hello.html"), // redirect root to hello page HTTPPage("/hello.html", HTTP_MIME_TEXT_HTML, hello_html), HTTPPage("/info.html" , HTTP_MIME_TEXT_HTML, info_html_generator), HTTPPage("/404.html" , HTTP_MIME_TEXT_HTML, file_not_found_generator), }; uint8_t pagecount = sizeof(pages)/sizeof(HTTPPage); // Declare HTTPServer with max number of pages AdafruitHTTPServer httpserver(pagecount); /**************************************************************************/ /*! * @brief Example of generating dynamic HTML content on demand * * Link is separated to url and query * * @param url url of this page * @param query query string after '?' e.g "var=value" * * @param http_request This request's information */ /**************************************************************************/ void info_html_generator (const char* url, const char* query, httppage_request_t* http_request) { (void) url; (void) query; (void) http_request; httpserver.print("Bootloader : "); httpserver.print( Feather.bootloaderVersion() ); httpserver.print("
"); httpserver.print("WICED SDK : "); httpserver.print( Feather.sdkVersion() ); httpserver.print("
"); httpserver.print("FeatherLib : "); httpserver.print( Feather.firmwareVersion() ); httpserver.print("
"); httpserver.print("Arduino API : "); httpserver.print( Feather.arduinoVersion() ); httpserver.print("
"); httpserver.print("
"); visit_count++; httpserver.print("visit count : "); httpserver.print(visit_count); } /**************************************************************************/ /*! * @brief HTTP 404 generator. The HTTP Server will automatically redirect * to "/404.html" when it can't find the requested url in the * list of registered pages * * The url and query string are already separated when this function * is called. * * @param url url of this page * @param query query string after '?' e.g "var=value" * @param http_request Details about this HTTP request */ /**************************************************************************/ void file_not_found_generator (const char* url, const char* query, httppage_request_t* http_request) { (void) url; (void) query; (void) http_request; httpserver.print(""); httpserver.print("

Error 404 File Not Found!

"); httpserver.print("
"); httpserver.print("Available pages are:"); httpserver.print("
"); httpserver.print(""); httpserver.print(""); } /**************************************************************************/ /*! @brief The setup function runs once when the board comes out of reset */ /**************************************************************************/ void setup() { Serial.begin(115200); // Wait for the USB serial to connect. Needed for native USB port only. while (!Serial) delay(1); Serial.println("Simple HTTP Server Example\r\n"); // Print all software versions Feather.printVersions(); // Try to connect to an AP while ( !connectAP() ) { delay(500); // delay between each attempt } // Connected: Print network info Feather.printNetwork(); // Tell the HTTP client to auto print error codes and halt on errors httpserver.err_actions(true, true); // Configure HTTP Server Pages Serial.println("Adding Pages to HTTP Server"); httpserver.addPages(pages, pagecount); Serial.print("Starting HTTP Server ... "); httpserver.begin(PORT, MAX_CLIENTS); Serial.println(" running"); } /**************************************************************************/ /*! @brief The loop function runs over and over again */ /**************************************************************************/ void loop() { togglePin(ledPin); delay(1000); } /**************************************************************************/ /*! @brief Connect to the defined access point (AP) */ /**************************************************************************/ bool connectAP(void) { // Attempt to connect to an AP Serial.print("Please wait while connecting to: '" WLAN_SSID "' ... "); if ( Feather.connect(WLAN_SSID, WLAN_PASS) ) { Serial.println("Connected!"); } else { Serial.printf("Failed! %s (%d)", Feather.errstr(), Feather.errno()); Serial.println(); } Serial.println(); return Feather.connected(); } /**************************************************************************/ /*! @brief TCP/HTTP disconnect callback */ /**************************************************************************/ void disconnect_callback(void) { Serial.println(); Serial.println("---------------------"); Serial.println("DISCONNECTED CALLBACK"); Serial.println("---------------------"); Serial.println(); httpserver.stop(); }