-
Notifications
You must be signed in to change notification settings - Fork 18
/
ESP8266_WebServer.ino
46 lines (39 loc) · 1.07 KB
/
ESP8266_WebServer.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
#include <ESP8266WiFi.h>
const char *ssid = "MY_SSID";
const char *password = "MY_PASSWORD";
const int port = 80;
WiFiServer server(port);
void setup() {
Serial.begin(115200);
Serial.print("\nConnecting to network ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
Serial.print("Connected to network, local IP = ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client && client.connected()) {
Serial.println("Connection accepted, remote IP = ");
Serial.println(client.remoteIP());
// Read HTTP request
int ch = client.read();
while (ch != -1) {
Serial.print((char) ch);
ch = client.read();
}
// Send HTTP response
client.print("HTTP/1.1 200 OK\r\n");
client.print("Content-Length: 9\r\n");
client.print("Connection: close\r\n");
client.print("\r\n");
client.print("It works!");
delay(1); // Give Web client time to receive data
client.stop();
}
}