So, I have a web server running perfectly fine on the Arduino Uno. What it is supposed to do is turning the PC of and on based on the button clicked on the web server and the status of the PC. I am running a server for friends to have a 24/7 accessible game server. But as it is not used 24/7 i thought, it would be nice to remotely be able to turn the server on and off. And this works all fine, my friends can do this from the internet. But after some time 30+(?) minutes the Arduino web server becomes unreachable and the browser displays a timeout. It is also not responding to pinging it. I have no idea how to solve this issue as i am entirely new to this kind of programming. Any help would be appreciated.
Hardware: Ethernedshield rev 3 with w5100 chip
the code:
#include <Ethernet.h>
#include <SPI.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = {0x28, 0xC3, 0x2E, 0x94, 0x2C, 0xAC};
IPAddress ip(192, 168, 2, 22);
EthernetServer server(48809); // create a server at port 80
String HTTP_req; // stores the HTTP request
boolean power_Status = 0; // state of LED, off by default
int power_pin = 7;
void setup(){
Serial.begin(9600); // Open serial communications and wait for port to open:
Ethernet.begin(mac, ip); // start the Ethernet connection:
if (Ethernet.hardwareStatus() == EthernetNoHardware) { // Check for Ethernet hardware present
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
server.begin(); // start server and print local IP address is test:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
pinMode(power_pin, OUTPUT);
digitalWrite(power_pin, HIGH);
}
void loop(){
EthernetClient client = server.available(); // try to get client
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
Serial.print("power status is: ");
Serial.println(power_Status);
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Power switch website</title>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Server</h1>");
client.println("<p>Click to switch the server on and off.</p>");
client.println("<form method=\"get\">");
checkServer(client);
ServerStatus(client);
ProcessCheckbox(client);
client.println("</form>");
client.println("</body>");
client.println("</html>");
HTTP_req = ""; // finished with request, empty string
break;
}
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
}
}
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
}
}
void checkServer(EthernetClient cl){
Serial.println(HTTP_req);
Serial.println(HTTP_req.indexOf("?"));
if (HTTP_req.indexOf("?") > -1) { // see if checkbox was clicked
// the checkbox was clicked, toggle the LED
if (power_Status) {
power_Status = 0;
}
else {
power_Status = 1;
}
}
}
void ServerStatus(EthernetClient cl){
if(power_Status){
cl.println("<p>Server is powered :)</p>");
}else{
cl.println("<p>Server is dead :(</p>");
}
}
// switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl){
if(HTTP_req.indexOf("?") > -1){
if (power_Status) { // switch LED on
digitalWrite(power_pin, LOW);
delay(250);
digitalWrite(power_pin, HIGH);
// checkbox is checked
cl.println("<input type=\"button\" name=\"LED2\" value=\"Power off\" onclick=\"submit();\">");
cl.println("<p> server is booting, give it some time</p>");
}
else { // switch LED off
digitalWrite(power_pin, LOW);
delay(250);
digitalWrite(power_pin, HIGH);
// checkbox is unchecked
cl.println("<input type=\"button\" name=\"LED2\" value=\"Power on\" onclick=\"submit();\">");
cl.println("<p>Server is killed</p>");
}
}
if(HTTP_req.indexOf("?") == -1){
if (power_Status) { // switch LED on
cl.println("<input type=\"button\" name=\"LED2\" value=\"Power off\" onclick=\"submit();\">");
}
else {
cl.println("<input type=\"button\" name=\"LED2\" value=\"Power on\" onclick=\"submit();\">");
}
}
}