-
Notifications
You must be signed in to change notification settings - Fork 0
/
LED_Web_Control.ino
68 lines (56 loc) · 1.54 KB
/
LED_Web_Control.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*ESP8266 LED control over webpage (local IP)!
https://www.clevertronics.blogspot.com
Used Hardware : ESP8266 (NodeMCU)
Used IDE : Arduino IDE 1.8.12
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid = "Your_SSID"; //Update the following areas.
const char* password = "Your_Password";
ESP8266WebServer server(80); //instantiate server at port 80 (http port)
String page = "";
int LEDPin = 13;
void setup(void)
{
//The HTML of the web page
page = "<h1>Simple NodeMCU Web Server</h1><p><a href=\"LEDOn\"><button>ON</button></a> <a href=\"LEDOff\"><button>OFF</button></a></p>";
pinMode(LEDPin, OUTPUT);
digitalWrite(LEDPin, LOW);
delay(1000);
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", []()
{
server.send(200, "text/html", page);
});
server.on("/LEDOn", []()
{
server.send(200, "text/html", page);
digitalWrite(LEDPin, HIGH);
delay(1000);
});
server.on("/LEDOff", []()
{
server.send(200, "text/html", page);
digitalWrite(LEDPin, LOW);
delay(1000);
});
server.begin();
Serial.println("Web server started!");
}
void loop(void)
{
server.handleClient();
}