-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver_demo.ino
More file actions
48 lines (48 loc) · 1.43 KB
/
webserver_demo.ino
File metadata and controls
48 lines (48 loc) · 1.43 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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
/* Set these to your desired credentials. */
const char *ssid = "*****"; //Enter your WIFI ssid
const char *password = "*****"; //Enter your WIFI password
ESP8266WebServer server(80);
void handleRoot() {
server.send(200, "text/html", "<form action=\"/LED_BUILTIN_on\" method=\"get\" id=\"form1\"></form><button type=\"submit\" form=\"form1\" value=\"On\">On</button><form action=\"/LED_BUILTIN_off\" method=\"get\" id=\"form2\"></form><button type=\"submit\" form=\"form2\" value=\"Off\">Off</button>");
}
void handleSave() {
if (server.arg("pass") != "") {
Serial.println(server.arg("pass"));
}
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
delay(3000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.on ( "/", handleRoot );
server.on ("/save", handleSave);
server.begin();
Serial.println ( "HTTP server started" );
server.on("/LED_BUILTIN_on", []() {
digitalWrite(LED_BUILTIN, 1);
Serial.println("on");
handleRoot();
});
server.on("/LED_BUILTIN_off", []() {
digitalWrite(LED_BUILTIN, 0);
Serial.println("off");
handleRoot();
});
}
void loop() {
server.handleClient();
}