-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPinger_Finger.ino
280 lines (247 loc) · 8.21 KB
/
Pinger_Finger.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
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
modified 02 Sept 2015
by Arturo Guadalupi
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Thread.h>
int FirstRun = 0;
int PreBootCounter = 0;
int RebootEnabled = 1;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // mac address for ethernet shield
// the dns server ip
IPAddress dnServer(8, 8, 8, 8);
// the router's gateway address:
IPAddress gateway(10, 1, 1, 254);
// the subnet:
IPAddress subnet(255, 255, 255, 0);
//the IP address is dependent on your network
IPAddress ip(10, 1, 1, 100);
//The Ip To Ping
//byte ServerAddr[] = {10, 1, 1, 13}; // ip address to ping
char ServerAddr[] = "www.arduino.cc";
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
EthernetClient client;
String readString;
const long delayMS = 30L * 1000L; // delay between successive pings (60 * 1000 = 60 seconds) the L is for the long datatype
long delaySEC = delayMS / 1000; // delay between successive pings (60 * 1000 = 60 seconds) the L is for the long datatype
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
//const unsigned long postingInterval = 30L * 1000L; // delay between updates, in milliseconds
int HowManyFailes = 50;
#define ledPing 2
#define ledOk 3
#define ledFail 4
#define relayPin 5
#define on LOW
#define off HIGH
void ClientStart()
{
digitalWrite(ledOk, off);
digitalWrite(ledFail, off);
digitalWrite(ledPing, on);
}
void ClientEnd()
{
digitalWrite(ledPing, off);
}
void ClientSuccess()
{
digitalWrite(ledFail, off);
digitalWrite(ledOk, on);
Serial.println(F("Client Connected OK..."));
RebootEnabled = 1;
PreBootCounter = 0;
}
void ClientFail()
{
digitalWrite(ledFail, on);
digitalWrite(ledOk, off);
Serial.println(F("Client Connection Failed..."));
PreBootCounter++;
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(ServerAddr, 80)) {
Serial.println(F("connected..."));
// send the HTTP GET request:
client.println("GET /index.html HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
ClientSuccess();
} else {
// if you couldn't make a connection:
Serial.println(F("connection failed"));
ClientFail();
}
}
//My Thread
Thread ClientThread = Thread();
// callback for myThread
void ClientThreadCallback(){
Serial.println(F("ClientThread callback"));
ClientStart();
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.write(c);
}
Serial.print(F("First Run: "));
Serial.println(FirstRun);
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > delayMS) {
httpRequest();
}
delay(250);
ClientEnd();
Serial.print("PreBootCounter: ");
Serial.println(PreBootCounter);
Serial.print("RebootEnabled: ");
Serial.println(RebootEnabled);
if(PreBootCounter >= HowManyFailes){
if(RebootEnabled == 1){
Serial.println("Rebooting NAS...");
digitalWrite(relayPin, on);
delay(1000);
digitalWrite(relayPin, off);
RebootEnabled = 0;
}else{
Serial.println("Nas Still Down... No Reboot Needed... Waiting...");
}
}
}
void Blink(int DigitalPin){
digitalWrite(DigitalPin, on);
delay(1000);
digitalWrite(DigitalPin, off);
}
void setup()
{
// Configure PingThread
ClientThread.onRun(ClientThreadCallback);
ClientThread.setInterval(delayMS);
pinMode(ledPing, OUTPUT);
pinMode(ledOk, OUTPUT);
pinMode(ledFail, OUTPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, off);
// start serial port:
Serial.begin(9600);
Serial.println("Starting ethernet connection");
// start Ethernet
Ethernet.begin(mac, ip, dnServer, gateway, subnet);
//Ethernet.begin(mac, ip);
server.begin();
Serial.print(F("Pinger is at "));
Serial.println(Ethernet.localIP());
// blink all LEDs on
Serial.println(F("Testing RED LED."));
Blink(ledFail);
Serial.println(F("Testing BLUE LED."));
Blink(ledPing);
Serial.println(F("Testing GREEN LED."));
Blink(ledOk);
digitalWrite(ledOk, on);
}
void loop()
{
if (FirstRun == 0){
Serial.println(F("First Run Connection Start..."));
httpRequest();
FirstRun = 1;
Serial.println(F("First Run Connection Finished..."));
Serial.println(lastConnectionTime);
}
noInterrupts(); // Call to disable interrupts
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println(F("Content-Type: text/html"));
client.println();
client.println(F("<HTML>"));
client.println(F("<HEAD>"));
client.println(F("<meta name='apple-mobile-web-app-capable' content='yes' />"));
client.println(F("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />"));
client.println(F("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />"));
client.println(F("<TITLE>The Pinger Finger - Tal Ziv</TITLE>"));
client.println(F("</HEAD>"));
client.println(F("<H1>The Pinger Finger v1</H1>"));
client.println(F("<hr />"));
client.println(F("<H2>Status And details</H2>"));
client.println(F("<br />"));
client.print(F("My IP: "));
client.println(Ethernet.localIP());
client.println(F("<br />Ping Destination: 192.168.1.251 <br />"));
client.print(F("Web GET DealyInSec: "));
client.println(delaySEC);
client.print(F("<br />How Many Failes takes to do a Reboot: "));
client.println(HowManyFailes);
client.print(F("<br />PreBootCounter: "));
client.println(PreBootCounter);
client.print(F("<br />RebootEnabled: "));
client.println(RebootEnabled);
client.println(F("<br /><br />"));
if(PreBootCounter >= HowManyFailes && RebootEnabled == 0){
client.println(F("NAS was Rebooted<br />NAS Still Down...<br />No Need To Reboot Again...<br />Waiting...<br /><br />"));
}
client.println(F("<a href=\"/?button1on\"\">Hold RESET Button</a>"));
client.println(F("<a href=\"/?button1off\"\">Release RESET Button</a><br /><br /><br />"));
client.println(F("<p>Created by Tal Ziv.</p><br />"));
client.println(F("</BODY></HTML>"));
delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf("?button1on") >0){
digitalWrite(relayPin, on);
}
if (readString.indexOf("?button1off") >0){
digitalWrite(relayPin, off);
}
//clearing string for next read
readString="";
}
}
}
}
interrupts(); // Call to enable interrupts
// checks if thread should run
if(ClientThread.shouldRun()){
ClientThread.run();
}
}