-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebHostMonitor.ino
286 lines (245 loc) · 5.47 KB
/
WebHostMonitor.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*----------------------------------------------------------------
Name: Web Host Monitor
Desc: Status monitor of hosts that can be entered using
the web interface.
Date: 13.06.2016
Author: Paweł Klockiewicz @ CDV
----------------------------------------------------------------*/
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include <SdFat.h>
#include <NettigoKeypad.h>
#include <aWOT.h>
// Disable warnings before converting strings to 'char *'
#pragma GCC diagnostic ignored "-Wwrite-strings"
// MAC Address and IP of Arduino
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// IPAddress ip(192, 168, 2, 8);
IPAddress ip(192, 168, 0, 2);
// Server library initialization
EthernetServer server(80);
// Network Client initialization
EthernetClient client;
// SdFat object initialization
SdFat SD;
// Host list variables
char hostsNames[4][20];
int hostsCount;
int maxHosts = 4;
int currentHost = -1;
// LCD library initialization
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// Object for button reading
NG_Keypad keypad;
// Object for the web interface
WebApp app;
/**
* Display Intro
* Displays a welcome message
*/
void displayIntro()
{
Serial.println(F("WEB HOST MONITOR"));
Serial.print(F("You have "));
Serial.print(hostsCount);
Serial.println(F(" hosts"));
lcd.clear();
lcd.print("WEB HOST MONITOR");
lcd.setCursor(0,1);
lcd.print("You have ");
lcd.print(hostsCount);
lcd.print(" hosts");
}
/**
* LCD Printer
* Displays text on LCD
*/
void lcdPrint(char a[], char b[] = 0)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(a);
lcd.setCursor(0,1);
lcd.print(b);
}
/**
* Save Hosts
* Writes a list of hosts to the `hosts.txt` file
*/
void saveHosts(char data[])
{
SdFile wrfile;
if(!wrfile.open("hosts.txt", O_RDWR | O_CREAT | O_TRUNC))
Serial.println(F("Opening hosts.txt failed!"));
else
{
Serial.println(F("Writing data to file..."));
wrfile.println(data);
wrfile.close();
Serial.println(F("Writing done."));
}
}
/**
* Get Hosts
* Gets the list of hosts from the `hosts.txt` file
*/
void getHosts()
{
char line[20];
int n, i(0);
SdFile rdfile("hosts.txt", O_READ);
if (!rdfile.isOpen())
Serial.println(F("Failed to open hosts.txt"));
// read lines from the file
while (((n = rdfile.fgets(line, sizeof(line))) > 0) && (i < maxHosts))
{
if (line[0] != '\n')
{
strtok(line, "\r\n");
strcpy(hostsNames[i], line);
i++;
}
}
hostsCount = i;
}
/**
* Next Host
* Starts the monitor for the next host
*/
void nextHost()
{
currentHost++;
if(currentHost > (hostsCount - 1))
currentHost = 0;
runMonitor(hostsNames[currentHost]);
}
/**
* Previous Host
* Starts the monitor for the previous host
*/
void prevHost()
{
currentHost--;
if(currentHost < 0)
currentHost = (hostsCount - 1);
runMonitor(hostsNames[currentHost]);
}
/**
* Host Monitor
* Check if host is available online
*/
void runMonitor(char* host)
{
if(client.connect(host, 80))
{
if(client.connected() || client.available())
{
Serial.print(host);
Serial.println(F(" is UP!"));
lcdPrint(host, "is UP!");
}
else
{
Serial.print(host);
Serial.println(F(" is DOWN!"));
lcdPrint(host, "is DOWN!");
}
}
else
{
Serial.println(F("Connection failed"));
lcdPrint("Connection", "failed");
}
client.stop();
}
/**
* Index CMD
* Supports server side requests
*/
void indexCmd(Request &req, Response &res)
{
char *queryHosts = req.query("hosts");
int i;
if(strlen(queryHosts) > 4)
{
saveHosts(queryHosts);
getHosts();
displayIntro();
}
P(part_one) =
"<html>\n"
"<head>\n"
"<title>Web Host Monitor</title>\n"
"<meta name='viewport' content='width=device-width, initial-scale=1'>\n"
"</head>\n"
"<body>\n"
"<h1>Web Host Monitor</h1>\n"
"<h3>Your hosts (max 4):</h3>\n"
"<form method='get'>\n"
"<textarea name='hosts' rows='4' cols='24'>";
P(part_two) =
"</textarea><br/>\n"
"<button type='submit'>Save</button>\n"
"</form>\n"
"</body>\n"
"</html>";
res.success("text/html");
res.printP(part_one);
// hosts list
for(i = 0; i < hostsCount; i++)
{
if((i + 1) == hostsCount)
res.print(hostsNames[i]);
else
res.println(hostsNames[i]);
}
res.printP(part_two);
}
/**
* Arduino Setup
* Settings - one-time instruction block
*/
void setup()
{
// Initialize communication on serial port
Serial.begin(9600);
// Initialization of the SD card reader
if (!SD.begin(4))
{
Serial.println(F("SD Card Reader initialization failed!"));
return;
}
// Get a list of hosts
getHosts();
// Initialization of network settings
Ethernet.begin(mac, ip);
Serial.println(Ethernet.localIP());
// Routing to handle URL requests
app.get("/", &indexCmd);
// Operation of buttons
keypad.register_handler(NG_Keypad::UP, prevHost);
keypad.register_handler(NG_Keypad::DOWN, nextHost);
keypad.register_handler(NG_Keypad::RIGHT, nextHost);
keypad.register_handler(NG_Keypad::LEFT, prevHost);
// Initialization of LCD display
lcd.begin(16, 2);
// Intro message
displayIntro();
}
/**
* Arduino Loop
* Infinite block of instructions
*/
void loop()
{
int rd = analogRead(0);
keypad.check_handlers(rd);
EthernetClient client = server.available();
if (client)
{
app.process(&client);
client.stop();
}
delay(100);
}