-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP8266_SERVER.ino
140 lines (130 loc) · 4.12 KB
/
ESP8266_SERVER.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//திருச்சிற்றம்பலம்
#include <ESP8266WiFi.h>
const char* ssid = "LOKESHWARAN";
const char* password ="kandupudi";
WiFiServer server(80);
String header;
String output5State = "off";
String output4State = "off";
const int output5 = 5;
const int output4 = 4;
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;
void setup()
{
Serial.begin(115200);
pinMode(output5, OUTPUT);
pinMode(output4, OUTPUT);
digitalWrite(output5, LOW);
digitalWrite(output4, LOW);
Serial.print("Connecting to ");
Serial.println(ssid);
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.begin();
}
void loop()
{
WiFiClient client = server.available();
if(client)
{
Serial.println("New Client.");
String currentLine = "";
currentTime = millis();
previousTime = currentTime;
while (client.connected() && currentTime - previousTime <- timeoutTime)
{
currentTime = millis();
if(client.available())
{
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n')
{
if (currentLine.length() == 0)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
if (header.indexOf("GET /5/on") >= 0)
{
Serial.println("GPIO 5 on");
output5State = "on";
digitalWrite(output5, HIGH);
}
else if (header.indexOf("GET /5/off") >=0)
{
Serial.println("GPIO 5 off");
output5State = "off";
digitalWrite(output5, LOW);
}
else if (header.indexOf("GET /4/on") >= 0)
{
Serial.println("GPIO 4 on");
output4State = "on";
digitalWrite(output4, HIGH);
}
else if(header.indexOf("GET /4/off") >= 0)
{
Serial.println("GPIO 4 off");
output4State = "off";
digitalWrite(output4, LOW);
}
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<link rel=\"icon\" href=\"data;,\">");
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button {background-color: #195B6A; border:none; color:white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #77878A;)</style></head>");
client.println("<body><h1>ESP8266 Web Server</h1>");
client.println("<p>GPIO 5 - State " + output5State +"</p>");
if (output5State=="off")
{
client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
}
else
{
client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("<p>GPIO 4 - State " + output4State + "</p>");
if (output4State=="off")
{
client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
}
else
{
client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
client.println();
break;
}
else {
currentLine = "";
}
}
else if (c != '\r')
{
currentLine += c;
}
}
}
header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}