-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHTTP_Server_Blinkie.ino
202 lines (174 loc) · 5.79 KB
/
HTTP_Server_Blinkie.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* This sketchbook was built for the ESP32 to server as a HTTP Webserver. The purpose of this was built to host
* a web server that the Palo Alto Networks NGFW can make HTTP Post calls to and provide a visual notification
* when an alert triggers from NGFW.
*
* WARNING:
* __________
* This is an HTTP Webserver over tcp/80. None of the data will be encrypted. If you would like to
* encrypt your ES32 then please import and configure a TLS-enabled web server. A great example can be found here:
* https://github.com/fhessel/esp32_https_server
* __________
*
*/
#include <WiFi.h>
#include <WebServer.h>
/* Put your SSID & Password */
const char* ssid = "Your SSID Here"; // Enter SSID here
const char* password = "Your Pre-Shared Key Here"; //Enter Password here
// declare an object of WebServer library
WebServer server(80);
//Variable to store the HTTP request
String header;
// Auxiliary variables to store the current output state
int ledState = LOW;
// Assign variables
const byte ledPins[] = {4,5,12}; //update this to the GPIO pins you will be using to power your LEDs
const byte numberOfLights = sizeof(ledPins) / sizeof(ledPins[0]);
int timer = 500; // the higher the number, the slower the timing.
int repeat = 0;
int i;
void setup() {
Serial.begin(115200);
delay(100);
yield();
//initialize ledPins array as outputs
for (int i = 0; i< numberOfLights; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(BUILTIN_LED, OUTPUT);
}
//we start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to "); Serial.println(ssid);
//connect WiFi to local network
WiFi.begin(ssid, password);
//check WiFi status
while (WiFi.status() != WL_CONNECTED) {
delay(timer);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
yield();
server.on("/", handle_OnConnect);
server.on("/LED/on", handle_on);
server.on("/LED/off", handle_off);
server.on("/LED/wave", handle_wave);
server.on("/alert", handle_alert);
server.onNotFound(handle_NotFound);
server.begin();
yield();
Serial.println("HTTP server started");
Serial.println("Navigate to the IP in your browser to see it working");
}
void loop() {
server.handleClient();
}
void handle_OnConnect() {
ledState = LOW;
server.send(200, "text/html", SendHTML(ledState));
yield();
}
void handle_on() {
Serial.println("LED Status: ON");
for (int i = 0; i < numberOfLights; i++) {
ledState = HIGH;
digitalWrite(ledPins[i], HIGH);
digitalWrite(BUILTIN_LED, HIGH);
yield();
}
server.send(200, "text/html", SendHTML(ledState));
}
void handle_off() {
Serial.println("LED Status: OFF");
for (int i = 0; i < numberOfLights; i++) {
ledState = LOW;
digitalWrite(ledPins[i], LOW);
digitalWrite(BUILTIN_LED, LOW);
yield();
}
server.send(200, "text/html", SendHTML(ledState));
}
void handle_wave() {
Serial.println("Doin' the W-A-V-E!");
server.send(200, "text/html", SendHTML(ledState));
int repeat =0;
while (repeat < 3) {
for (int i = 0; i <= numberOfLights; i++) {
digitalWrite(ledPins[i], HIGH);
digitalWrite(BUILTIN_LED, HIGH);
delay(200);
digitalWrite(ledPins[i], LOW);
digitalWrite(BUILTIN_LED, LOW);
yield();
}
delay(300);
for (int i = 0; i >= numberOfLights; i--) {
digitalWrite(ledPins[i], HIGH);
digitalWrite(BUILTIN_LED, HIGH);
delay(200);
digitalWrite(ledPins[i], LOW);
digitalWrite(BUILTIN_LED, LOW);
yield();
}
delay(timer);
repeat++;
}
yield();
}
void handle_alert() {
Serial.println("RED ALERT!");
server.send(200, "text/html", SendHTML(ledState));
int repeat = 0;
while (repeat <=3) {
for (int i = 0; i< numberOfLights; i++) {
//turn the LEDs on:
digitalWrite(ledPins[i], HIGH);
digitalWrite(BUILTIN_LED, HIGH);
yield();
}
delay(200);
for (int i = 0; i< numberOfLights; i++) {
//turn the LEDs off:
digitalWrite(ledPins[i], LOW);
digitalWrite(BUILTIN_LED, LOW);
yield();
}
delay(300);
repeat++;
}
yield();
}
void handle_NotFound(){
server.send(404, "text/plain", "Oopsie, you took a wrong turn somewhere...");
}
/// HTML Web Page
String SendHTML(uint8_t ledstatus){
String ptr = "<!DOCTYPE html> <html>";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
ptr +="<link rel=\"icon\" href=\"data:,\">";
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}";
ptr +=".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;";
ptr +="text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}";
ptr +=".button2 {background-color: #555555;}";
ptr +=".button3 {background-color: #CC0000;}";
ptr +=".button4 {background-color: #FF8000;}";
ptr +="</style></head>";
// Web Page Heading
ptr +="<body><h1>DuckDuckSOC Web Server</h1>";
ptr +="<img src=https://i.imgur.com/qtpFo58.png title=Quack! Quack! align=top height=300 width=300>";
// If the ledState is off, it displays the ON button
if (ledstatus) {
ptr +="<p>LED Status: ON</p><a href=\"/LED/off\"><button class=\"button button2\">OFF</button></a>";
} else {
ptr +="<p>LED Status: OFF</p><a href=\"/LED/on\"><button class=\"button\">ON</button></a>";
}
ptr +="<p><a href=\"/LED/wave\"><button class=\"button button4\">WAVE</button></p></a>";
ptr +="<p><a href=\"/alert\"><button class=\"button button3\">ALERT</button></p></a>";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}