/* * NodeMCU/ESP8266 act as AP (Access Point) and simplest Web Server * to control GPIO D1 * Connect to AP "NodeAPtest", * Open browser, visit 192.168.4.1 */ #include #include #include const String HtmlHtml = "" ""; const String HtmlHtmlClose = ""; const String HtmlTitle = "

ESP8266 AP WebServer control


\n"; const String HtmlCenter = "
"; const String HtmlLedStateLow = "LED is now ON
\n"; const String HtmlLedStateHigh = "LED is now OFF
\n"; const String HtmlButtons = "
" "
"; const String HtmlCloseCenter = "
"; const char *ssid = "NodeAPtest"; const char *password = "password"; const int output1 = D1; #define output1_OFF digitalWrite(output1, LOW) #define output1_ON digitalWrite(output1, HIGH) int stateLED = LOW; ESP8266WebServer server(80); void handleRoot() { response(); } void handleLedOn() { stateLED = LOW; output1_ON; response(); } void handleLedOff() { stateLED = HIGH; output1_OFF; response(); } void response(){ String htmlRes = HtmlHtml + HtmlCenter; htmlRes += HtmlTitle; if(stateLED == LOW){ htmlRes += HtmlLedStateLow; }else{ htmlRes += HtmlLedStateHigh; } htmlRes += HtmlButtons; htmlRes += HtmlCloseCenter; htmlRes += HtmlHtmlClose; server.send(200, "text/html", htmlRes); } void setup() { delay(1000); Serial.begin(115200); Serial.println(); WiFi.mode(WIFI_AP); //WiFi.softAP(ssid, password); //or WiFi.softAP(ssid); IPAddress apip = WiFi.softAPIP(); Serial.print("visit: \n"); Serial.println(apip); server.on("/", handleRoot); server.on("/LEDOn", handleLedOn); server.on("/LEDOff", handleLedOff); server.begin(); Serial.println("HTTP server beginned"); pinMode(output1, OUTPUT); output1_OFF; } void loop() { server.handleClient(); }