#include <SPI.h>
#include <Ethernet.h> // call ethernet module
#include <ICMPPing.h> // call ping module
#include <LiquidCrystal.h> // call lcd moduletv
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// LiquidCrystal lcd(22,24,26,28,30,32); // tell lcd what pins are being used to control lcd
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0xED}; // set mac address for ethernet port
byte ip[] = {192, 168, 1, 187}; // set desired ip address ethernet port
// byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
// byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
IPAddress pingAddr(8, 8, 8, 8); // ip address to ping (google.com)
// IPAddress pingAddr(192,168,1,199); // ip address to ping (google.com)
const int plugRelay = 8;
boolean pingSuccess = false;
SOCKET pingSocket = 0;
const int maxNumberOfTimesToTryToPing = 10;
char buffer [256];
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
EthernetServer server(80); //server port
String readString;
void setup() {
// START THE WEBSERVER //
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// pinMode(led, OUTPUT);
// start the Ethernet connection and the server:
// Ethernet.begin(mac, ip, gateway, subnet);
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
// END THE WEBSERVER CODE //
// digitalWrite(plugRelay, LOW);
pinMode(plugRelay, OUTPUT);
Ethernet.begin(mac, ip); // start Ethernet
lcd.begin(16, 2); // start lcd
Serial.begin(9600); //
lcd.setCursor(0, 0); // set lcd to print to line 1
Serial.println("POWER UP // DISPLAY WELCOME MESSAGE"); // print to serial monitor
lcd.print(" Power-Ping "); // message to print
lcd.setCursor(0, 1); // set lcd to print to line 2
lcd.print(" Mathew Buer "); // message to print
// delay(5000); // delay for message
lcd.clear(); // clear lcd
lcd.print("Power ON"); // print
}
void checkping() {
pingSuccess = false;
for (int p = 0; p < maxNumberOfTimesToTryToPing; p++)
{
ICMPEchoReply echoReply = ping(pingAddr, 4); // start ping
if (echoReply.status == SUCCESS) // advise of success
{
Serial.println("PING SUCCESSFULL / WAIT 2 SECONDS BEFORE NEXT ATTEMPT"); // print to serial monitor
pingSuccess = true;
// Do whatever you need to with the reply data
lcd.setCursor(0, 1); // let lcd to print to line 2
lcd.print("Ping OK"); // print
digitalWrite(plugRelay, HIGH);
delay(2000);
break; // skip the rest of the iterations of the for loop
break;
}
}
if (!pingSuccess)
{
Serial.println("PING FAILED / WILL ATTEMPT AGAIN * 5 THEN RESTART"); // print to serial monitor
lcd.setCursor(0, 1);
lcd.print("Ping FAILED"); // Failed to ping the device in maxNumberOfTimesToTryToPing tries
digitalWrite(plugRelay, LOW);
lcd.setCursor(0, 0); // set lcd to print to line 1
lcd.print("Power OFF"); // print
delay(5000);
digitalWrite(plugRelay, HIGH);
lcd.setCursor(0, 0); // set lcd to print to line 1
lcd.print("Power ON"); // print
lcd.setCursor(0, 1); // set lcd to print to line 2
lcd.print("Re-Test in 5Mins"); // print
delay(300000);
}
}
void createhtml() {
// Create a client connection
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("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<link rel='stylesheet' type='text/css' href='http://www.autodoor.org.uk/powerpinger/css/button.css' />");
client.println("<TITLE>Power Pinger | Web Control</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Power Pinger Web Control</H1>");
client.println("<hr />");
client.println("
");
client.println("<a href=\"/?power-on-relay\"\">Turn On Relay</a>");
client.println("<a href=\"/?power-off-relay\"\">Turn Off Relay</a>
");
client.println("
");
client.println("
");
client.println("<p>Created by Mathew Buer!</p>");
client.println("
");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf("?power-on-relay") > 0) {
digitalWrite(plugRelay, HIGH);
}
if (readString.indexOf("?power-off-relay") > 0) {
digitalWrite(plugRelay, LOW);
}
//clearing string for next read
readString = "";
}
}
}
}
}
void loop() {
createhtml(); // creates webduino html page
// checkping();
}