-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled_via_wifi.ino
More file actions
92 lines (78 loc) · 2.8 KB
/
led_via_wifi.ino
File metadata and controls
92 lines (78 loc) · 2.8 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <ESP8266WiFi.h>
// Replace with your network credentials
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";
// Create a WiFiServer object
WiFiServer server(80);
void setup() {
// Start the Serial Monitor
Serial.begin(115200);
// Initialize the LED pin
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // Turn off LED (ESP-12E/ESP-07 built-in LED is active LOW)
// Connect to Wi-Fi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print the IP address
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Start the server
server.begin();
Serial.println("Server started");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("New Client.");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
// If the current line is blank, you got two newline characters in a row.
// That's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// The actual HTML content
client.println("<html><body><h1>ESP8266 LED Control</h1>");
client.println("<p><a href=\"/LED=ON\"><button>Turn ON</button></a></p>");
client.println("<p><a href=\"/LED=OFF\"><button>Turn OFF</button></a></p>");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
break;
} else { // If you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // If you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check if the client request was to turn the LED on or off
if (currentLine.endsWith("GET /LED=ON")) {
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (LOW level is the voltage level for the active LOW LED)
}
if (currentLine.endsWith("GET /LED=OFF")) {
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off (HIGH level is the voltage level for the active LOW LED)
}
}
}
// Close the connection
client.stop();
Serial.println("Client Disconnected.");
}