-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path05) Controlling_Devices_From_HTML_Webpage_using_NodeMCU.ino
69 lines (65 loc) · 1.88 KB
/
05) Controlling_Devices_From_HTML_Webpage_using_NodeMCU.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
69
#include <ESP8266WiFi.h>
WiFiClient client;
WiFiServer server(80);
#define led1 D5
#define led2 D6
void setup()
{
Serial.begin(9600);
WiFi.begin("Mooazam", "mooazam123");
while (WiFi.status() != WL_CONNECTED)
{
delay(200);
Serial.print("..");
}
Serial.println();
Serial.println("NodeMCU is connected!");
Serial.println(WiFi.localIP());
server.begin();
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop()
{
client = server.available(); //Gets a client that is connected to the server and has data available for reading.
if (client == 1)
{
String request = client.readStringUntil('\n');
Serial.println(request);
request.trim();
if (request == "GET /led1on HTTP/1.1")
{
digitalWrite(led1, HIGH);
Serial.println("LED 1 is ON");
}
if (request == "GET /led1off HTTP/1.1")
{
digitalWrite(led1, LOW);
Serial.println("LED 1 is OFF");
}
if (request == "GET /led2on HTTP/1.1")
{
digitalWrite(led2, HIGH);
Serial.println("LED 2 is ON");
}
if (request == "GET /led2off HTTP/1.1")
{
digitalWrite(led2, LOW);
Serial.println("LED 2 is OFF");
}
}
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<h1>Welcome to the Webpage!</h1>");
client.println("<h3>LED Controls</h3>");
client.println("<br>");
client.println("<a href=\"/led1on\"\"><button>LED 1 ON</button></a>");
client.println("<a href=\"/led1off\"\"><button>LED 1 OFF</button></a><br/>");
client.println("<a href=\"/led2on\"\"><button>LED 2 ON</button></a>");
client.println("<a href=\"/led2off\"\"><button>LED 2 OFF</button></a><br/>");
client.println("</html>");
}