Created
March 15, 2018 15:23
-
-
Save dmpop/d09df67f6f2428acb618c998048571b3 to your computer and use it in GitHub Desktop.
Control LED with NodeMCU in the AP mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Sketch: ESP8266_LED_Control_02 | |
* Control an LED from a web browser | |
* Intended to be run on an ESP8266 | |
* | |
* connect to the ESP8266 AP then | |
* use web broswer to go to 192.168.4.1 | |
* | |
*/ | |
#include <ESP8266WiFi.h> | |
const char WiFiPassword[] = "12345678"; | |
const char AP_NameChar[] = "LEDControl" ; | |
WiFiServer server(80); | |
String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; | |
String html_1 = "<!DOCTYPE html><html><head><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>"; | |
String html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='LED ON' ></form><br>"; | |
String html_3 = "<form id='F2' action='LEDOFF'><input class='button' type='submit' value='LED OFF' ></form><br>"; | |
String html_4 = "</div></body></html>"; | |
String request = ""; | |
int LED_Pin = D1; | |
void setup() | |
{ | |
pinMode(LED_Pin, OUTPUT); | |
boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword); | |
server.begin(); | |
} // void setup() | |
void loop() | |
{ | |
// Check if a client has connected | |
WiFiClient client = server.available(); | |
if (!client) { return; } | |
// Read the first line of the request | |
request = client.readStringUntil('\r'); | |
if ( request.indexOf("LEDON") > 0 ) { digitalWrite(LED_Pin, HIGH); } | |
else if ( request.indexOf("LEDOFF") > 0 ) { digitalWrite(LED_Pin, LOW); } | |
client.flush(); | |
client.print( header ); | |
client.print( html_1 ); | |
client.print( html_2 ); | |
client.print( html_3 ); | |
client.print( html_4); | |
delay(5); | |
// The client will actually be disconnected when the function returns and 'client' object is detroyed | |
} // void loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment