Last active
May 15, 2019 03:55
-
-
Save vaibhavpandeyvpz/ee822764c45a225381c48b8eddd66743 to your computer and use it in GitHub Desktop.
Remote Controlling LED Using NodeMCU (ESP8266) Board: https://blog.vaibhavpandey.com/2018/10/remote-controlling-led-using-nodemcu.html
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
#include <ESP8266WiFi.h> | |
int PIN_N = 15; // pin on which LED is connected | |
int PIN_MODE = LOW; | |
int SERIAL_BAUD = 115200; | |
const char* WIFI_SSID = "ESP8266"; // your WiFi SSID | |
const char* WIFI_PASSWORD = "nodemcutest"; // your WiFi password | |
WiFiServer server(80); | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(PIN_N, OUTPUT); | |
delay(1000); | |
connect(); | |
server.begin(); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
WiFiClient client = server.available(); | |
if (client) { | |
bool blank = true; | |
String headers; | |
while (client.connected()) { | |
if (client.available()) { | |
char c = client.read(); | |
headers += c; | |
if ((c == '\n') && blank) { | |
Serial.println(headers); | |
if (headers.indexOf("GET /state/OFF") == 0) { | |
PIN_MODE = LOW; | |
} else if (headers.indexOf("GET /state/ON") == 0) { | |
PIN_MODE = HIGH; | |
} | |
digitalWrite(PIN_N, PIN_MODE); | |
respond(client); | |
break; | |
} | |
if (c == '\n') { | |
blank = true; | |
} else if (c != '\r') { | |
blank = false; | |
} | |
} | |
} | |
delay(1); | |
client.stop(); | |
} | |
} | |
void connect() { | |
Serial.begin(SERIAL_BAUD); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.print(WIFI_SSID); | |
Serial.print('.'); | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print('.'); | |
} | |
Serial.println("Connected!"); | |
Serial.print("You may now open http://"); | |
Serial.print(WiFi.localIP()); | |
Serial.println(" in your browser."); | |
Serial.println(); | |
} | |
void respond(WiFiClient client) { | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-type: text/html"); | |
client.println("Connection: close"); | |
client.println(); | |
client.print("<!doctype html>" | |
"<html lang=\"en\">" | |
"<head>" | |
"<meta charset=\"utf-8\">" | |
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">" | |
"<title>ESP8266 - LED (Remote Control)</title>" | |
"<style>" | |
"body { font-family: 'Roboto', -apple-system, 'Segoe UI', sans-serif; font-size: 12pt; margin: 0, padding: 8px 24px; text-align: center }" | |
"</style>" | |
"</head>" | |
"<body>" | |
"<main>"); | |
client.print("<p>Current <strong>LED</strong> state is:</p>"); | |
client.print(PIN_MODE == HIGH ? "<h1 style=\"color: green\">ON</h1>" : "<h1 style=\"color: red\">OFF</h1>"); | |
client.print("<a href=\"/state/"); | |
client.print(PIN_MODE != HIGH ? "ON" : "OFF"); | |
client.print("\">TURN "); | |
client.print(PIN_MODE != HIGH ? "ON" : "OFF"); | |
client.print("</a>" | |
"</main>" | |
"</body>" | |
"</html>"); | |
client.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment