-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebServer.cpp
More file actions
82 lines (69 loc) · 2.51 KB
/
webServer.cpp
File metadata and controls
82 lines (69 loc) · 2.51 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
#include <ESP8266WiFi.h>
const char* ssid = "SSID";
const char* password = "PASS";
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}
// prepare a web page to be send to a client (web browser)
String createHtmlPage(){
String htmlPage =
String("HTTP/1.1 200 OK\r\n") +
"Content-Type: text/html\r\n" +
"Connection: close\r\n" + // the connection will be closed after completion of the response
"Refresh: 1\r\n" + // refresh the page automatically every 1 sec
"\r\n" +
"<!DOCTYPE HTML>" +
"<html>"+"<body>"
"<header>"
"<nav>"
"<ul style = 'list-style:none; position: relative; display: inline-flex;'>"
"<li><a style='margin: 2px; padding: 5px 10px; text-decoration: none; color: rgb(101, 192, 101); cursor: pointer;' href='www.google.com'>Home</a></li>"
"<li><a style='margin: 2px; padding: 5px 10px; text-decoration: none; color: rgb(101, 192, 101); cursor: pointer;' href='#Dinamarca'>Projects</a></li>"
"<li><a style='margin: 2px; padding: 5px 10px; text-decoration: none; color: rgb(101, 192, 101); cursor: pointer;' href='proyectos.html'>IoT</a></li>"
"</ul>"
"</header>" +
"<center>"
"<h1 style ='color:green'>Analog input: " + String(analogRead(A0)) +"C"
"</h1>"
"</center></html>" +
"\r\n";
return htmlPage;
}
void loop()
{
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client)
{
Serial.println("\n[Client connected]");
while (client.connected()) {
// read line by line what the client (web browser) is requesting
if (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
// wait for end of client's request, that is marked with an empty line
if (line.length() == 1 && line[0] == '\n') {
client.println(createHtmlPage());
break;
}
}
}
delay(1); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("[Client disonnected]");
}
}