Skip to content

Commit 3b61708

Browse files
author
Paweł Klockiewicz
committed
INIT
1 parent 44d7ffb commit 3b61708

File tree

1 file changed

+286
-0
lines changed

1 file changed

+286
-0
lines changed

WebHostMonitor.ino

+286
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
/*----------------------------------------------------------------
2+
Name: Web Host Monitor
3+
4+
Desc: Status monitor of hosts that can be entered using
5+
the web interface.
6+
7+
Date: 13.06.2016
8+
9+
Author: Paweł Klockiewicz @ CDV
10+
----------------------------------------------------------------*/
11+
12+
#include <SPI.h>
13+
#include <Ethernet.h>
14+
#include <LiquidCrystal.h>
15+
#include <SdFat.h>
16+
#include <NettigoKeypad.h>
17+
#include <aWOT.h>
18+
19+
// Disable warnings before converting strings to 'char *'
20+
#pragma GCC diagnostic ignored "-Wwrite-strings"
21+
22+
// MAC Address and IP of Arduino
23+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
24+
// IPAddress ip(192, 168, 2, 8);
25+
IPAddress ip(192, 168, 0, 2);
26+
// Server library initialization
27+
EthernetServer server(80);
28+
// Network Client initialization
29+
EthernetClient client;
30+
31+
// SdFat object initialization
32+
SdFat SD;
33+
34+
// Host list variables
35+
char hostsNames[4][20];
36+
int hostsCount;
37+
int maxHosts = 4;
38+
int currentHost = -1;
39+
40+
// LCD library initialization
41+
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
42+
43+
// Object for button reading
44+
NG_Keypad keypad;
45+
46+
// Object for the web interface
47+
WebApp app;
48+
49+
/**
50+
* Display Intro
51+
* Displays a welcome message
52+
*/
53+
void displayIntro()
54+
{
55+
Serial.println(F("WEB HOST MONITOR"));
56+
Serial.print(F("You have "));
57+
Serial.print(hostsCount);
58+
Serial.println(F(" hosts"));
59+
lcd.clear();
60+
lcd.print("WEB HOST MONITOR");
61+
lcd.setCursor(0,1);
62+
lcd.print("You have ");
63+
lcd.print(hostsCount);
64+
lcd.print(" hosts");
65+
}
66+
67+
/**
68+
* LCD Printer
69+
* Displays text on LCD
70+
*/
71+
void lcdPrint(char a[], char b[] = 0)
72+
{
73+
lcd.clear();
74+
lcd.setCursor(0,0);
75+
lcd.print(a);
76+
lcd.setCursor(0,1);
77+
lcd.print(b);
78+
}
79+
80+
/**
81+
* Save Hosts
82+
* Writes a list of hosts to the `hosts.txt` file
83+
*/
84+
void saveHosts(char data[])
85+
{
86+
SdFile wrfile;
87+
if(!wrfile.open("hosts.txt", O_RDWR | O_CREAT | O_TRUNC))
88+
Serial.println(F("Opening hosts.txt failed!"));
89+
else
90+
{
91+
Serial.println(F("Writing data to file..."));
92+
wrfile.println(data);
93+
wrfile.close();
94+
Serial.println(F("Writing done."));
95+
}
96+
}
97+
98+
/**
99+
* Get Hosts
100+
* Gets the list of hosts from the `hosts.txt` file
101+
*/
102+
void getHosts()
103+
{
104+
char line[20];
105+
int n, i(0);
106+
107+
SdFile rdfile("hosts.txt", O_READ);
108+
109+
if (!rdfile.isOpen())
110+
Serial.println(F("Failed to open hosts.txt"));
111+
112+
// read lines from the file
113+
while (((n = rdfile.fgets(line, sizeof(line))) > 0) && (i < maxHosts))
114+
{
115+
if (line[0] != '\n')
116+
{
117+
strtok(line, "\r\n");
118+
strcpy(hostsNames[i], line);
119+
i++;
120+
}
121+
}
122+
hostsCount = i;
123+
}
124+
125+
/**
126+
* Next Host
127+
* Starts the monitor for the next host
128+
*/
129+
void nextHost()
130+
{
131+
currentHost++;
132+
if(currentHost > (hostsCount - 1))
133+
currentHost = 0;
134+
135+
runMonitor(hostsNames[currentHost]);
136+
}
137+
138+
/**
139+
* Previous Host
140+
* Starts the monitor for the previous host
141+
*/
142+
void prevHost()
143+
{
144+
currentHost--;
145+
if(currentHost < 0)
146+
currentHost = (hostsCount - 1);
147+
148+
runMonitor(hostsNames[currentHost]);
149+
}
150+
151+
/**
152+
* Host Monitor
153+
* Check if host is available online
154+
*/
155+
void runMonitor(char* host)
156+
{
157+
if(client.connect(host, 80))
158+
{
159+
if(client.connected() || client.available())
160+
{
161+
Serial.print(host);
162+
Serial.println(F(" is UP!"));
163+
lcdPrint(host, "is UP!");
164+
}
165+
else
166+
{
167+
Serial.print(host);
168+
Serial.println(F(" is DOWN!"));
169+
lcdPrint(host, "is DOWN!");
170+
}
171+
}
172+
else
173+
{
174+
Serial.println(F("Connection failed"));
175+
lcdPrint("Connection", "failed");
176+
}
177+
client.stop();
178+
}
179+
180+
/**
181+
* Index CMD
182+
* Supports server side requests
183+
*/
184+
void indexCmd(Request &req, Response &res)
185+
{
186+
char *queryHosts = req.query("hosts");
187+
int i;
188+
189+
if(strlen(queryHosts) > 4)
190+
{
191+
saveHosts(queryHosts);
192+
getHosts();
193+
displayIntro();
194+
}
195+
196+
P(part_one) =
197+
"<html>\n"
198+
"<head>\n"
199+
"<title>Web Host Monitor</title>\n"
200+
"<meta name='viewport' content='width=device-width, initial-scale=1'>\n"
201+
"</head>\n"
202+
"<body>\n"
203+
"<h1>Web Host Monitor</h1>\n"
204+
"<h3>Your hosts (max 4):</h3>\n"
205+
"<form method='get'>\n"
206+
"<textarea name='hosts' rows='4' cols='24'>";
207+
208+
P(part_two) =
209+
"</textarea><br/>\n"
210+
"<button type='submit'>Save</button>\n"
211+
"</form>\n"
212+
"</body>\n"
213+
"</html>";
214+
215+
res.success("text/html");
216+
res.printP(part_one);
217+
218+
// hosts list
219+
for(i = 0; i < hostsCount; i++)
220+
{
221+
if((i + 1) == hostsCount)
222+
res.print(hostsNames[i]);
223+
else
224+
res.println(hostsNames[i]);
225+
}
226+
227+
res.printP(part_two);
228+
}
229+
230+
/**
231+
* Arduino Setup
232+
* Settings - one-time instruction block
233+
*/
234+
void setup()
235+
{
236+
// Initialize communication on serial port
237+
Serial.begin(9600);
238+
239+
// Initialization of the SD card reader
240+
if (!SD.begin(4))
241+
{
242+
Serial.println(F("SD Card Reader initialization failed!"));
243+
return;
244+
}
245+
246+
// Get a list of hosts
247+
getHosts();
248+
249+
// Initialization of network settings
250+
Ethernet.begin(mac, ip);
251+
Serial.println(Ethernet.localIP());
252+
253+
// Routing to handle URL requests
254+
app.get("/", &indexCmd);
255+
256+
// Operation of buttons
257+
keypad.register_handler(NG_Keypad::UP, prevHost);
258+
keypad.register_handler(NG_Keypad::DOWN, nextHost);
259+
keypad.register_handler(NG_Keypad::RIGHT, nextHost);
260+
keypad.register_handler(NG_Keypad::LEFT, prevHost);
261+
262+
// Initialization of LCD display
263+
lcd.begin(16, 2);
264+
265+
// Intro message
266+
displayIntro();
267+
}
268+
269+
/**
270+
* Arduino Loop
271+
* Infinite block of instructions
272+
*/
273+
void loop()
274+
{
275+
int rd = analogRead(0);
276+
keypad.check_handlers(rd);
277+
278+
EthernetClient client = server.available();
279+
if (client)
280+
{
281+
app.process(&client);
282+
client.stop();
283+
}
284+
285+
delay(100);
286+
}

0 commit comments

Comments
 (0)