I connected my lights and fans to a eight channel relay board which is connected to my arduino and ethernet shield.
The html page loads fine and all the relays work, but after a few hours the html page stops loading until I press reset.
Here's the code, would really appreciate some help.
Check the attachment for the source code
/*Induino R3 User Guide - Program 18.2 - A Simple Webserver that Controls using Links
Modified original Webserver Example
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
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
String req; // a variable to store the GET request
boolean state = 0; // a variable to store the state of the LED
boolean state1 = 0;
boolean state2 = 0;
boolean state3 = 0;
boolean state4 = 0; // a variable to store the state of the LED
boolean state5 = 0;
boolean state6 = 0;
boolean state7 = 0;
String state_val[]={"OFF","ON"}; // A Variable to store the State of the LED as String
String state_val1[]={"1OFF","1ON"};
String state_val2[]={"2OFF","2ON"};
String state_val3[]={"3OFF","3ON"};
String state_val4[]={"4OFF","4ON"}; // A Variable to store the State of the LED as String
String state_val6[]={"6OFF","6ON"};
String state_val7[]={"7OFF","7ON"};
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode(5,OUTPUT); // RGB LED RED is on Pin 6
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(2,OUTPUT); // RGB LED RED is on Pin 6
pinMode(3,OUTPUT);
pinMode(9,OUTPUT);
pinMode(14,OUTPUT);
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) { // Check if there is a client available
Serial.println("new client");
while (client.connected()) { // check if the client is connected
if (client.available()) { // Check if there is a request from the client
Serial.print("$$$$");
req=""; // reset the request variable
while(client.available()) // Read data from the client 1 byte at a time as long as there is data
{
char c=client.read(); // Read 1 byte of data from the client
if(c==13) // Check if the data read is a Carriage Return a.ka. Enter, This means end of first line - the line starting with GET and ending with HTTP/1.1
break; // Since we have read the first line, we need not read the remaining data for storage, so exit the while loop
req += c; // append the read data to the existing value
delayMicroseconds(1000);
}
// if the while loop above exited from the break statement, then there is data in the client buffer, which needs to be read to be removed
// we need to read this data to empty the buffer
while(client.available()) // While loop to read the data to empty buffer
{
client.read();
delayMicroseconds(1000);
}
Serial.println(req); // print the value of the request to the Serial Monitor for debugging
Serial.println("XXX");
}
if(find_string(req,"GET /?ON")) // Check if the received Request containts the String ON
{
state = 1; // Set the State Variable
digitalWrite(5,state); // Set the state to the RED LED
}
if(find_string(req,"GET /?OFF")) // Check if the received Request containts the String ON
{
state = 0; // Set the State Variable
digitalWrite(5,state);
}
if(find_string(req,"GET /?1ON")) // Check if the received Request containts the String ON
{
state1 = 1; // Set the State Variable
digitalWrite(6,state1); // Set the state to the RED LED
}
if(find_string(req,"GET /?1OFF")) // Check if the received Request containts the String ON
{
state1 = 0; // Set the State Variable
digitalWrite(6,state1);
}
if(find_string(req,"GET /?2ON")) // Check if the received Request containts the String ON
{
state2 = 1; // Set the State Variable
digitalWrite(7,state2); // Set the state to the RED LED
}
if(find_string(req,"GET /?2OFF")) // Check if the received Request containts the String ON
{
state2 = 0; // Set the State Variable
digitalWrite(7,state2);
}
if(find_string(req,"GET /?3ON")) // Check if the received Request containts the String ON
{
state3 = 1; // Set the State Variable
digitalWrite(8,state3); // Set the state to the RED LED
}
if(find_string(req,"GET /?3OFF")) // Check if the received Request containts the String ON
{
state3 = 0; // Set the State Variable
digitalWrite(8,state3);
}
if(find_string(req,"GET /?4ON")) // Check if the received Request containts the String ON
{
state4 = 1; // Set the State Variable
digitalWrite(2,state4); // Set the state to the RED LED
}
if(find_string(req,"GET /?4OFF")) // Check if the received Request containts the String ON
{
state4 = 0; // Set the State Variable
digitalWrite(2,state4);
}
if(find_string(req,"GET /?5ON")) // Check if the received Request containts the String ON
{
state5 = 1; // Set the State Variable
digitalWrite(3,state5); // Set the state to the RED LED
}
if(find_string(req,"GET /?5OFF")) // Check if the received Request containts the String ON
{
state5 = 0; // Set the State Variable
digitalWrite(3,state5);
}
if(find_string(req,"GET /?6ON")) // Check if the received Request containts the String ON
{
state6 = 1; // Set the State Variable
digitalWrite(9,state6); // Set the state to the RED LED
}
if(find_string(req,"GET /?6OFF")) // Check if the received Request containts the String ON
{
state6 = 0; // Set the State Variable
digitalWrite(9,state6);
}
if(find_string(req,"GET /?7ON")) // Check if the received Request containts the String ON
{
state7 = 1; // Set the State Variable
digitalWrite(14,state7); // Set the state to the RED LED
}
if(find_string(req,"GET /?7OFF")) // Check if the received Request containts the String ON
{
state7 = 0; // Set the State Variable
digitalWrite(14,state7);
}
Serial.println("here 1"); // for debugging
client.println("HTTP/1.1 200 OK"); // start responding with the DATA to the client & Send out the response header
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the responsedelay(1);
client.println();
client.println("<!DOCTYPE HTML>"); // Start of the HTML Page
client.println("<html>");
client.println("<br />");
delayMicroseconds(500);
if(state) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val[1]+" \">");
}
client.println("Click Here to Switch "+state_val[!state]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state1) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val1[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val1[1]+" \">");
}
client.println("Click Here to Switch "+state_val1[!state1]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state2) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val2[0]+" \">");
}
else
}
client.println("<a href= \"/?"+state_val2[1]+" \">");
}
client.println("Click Here to Switch "+state_val2[!state2]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state3) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val3[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val3[1]+" \">");
}
client.println("Click Here to Switch "+state_val3[!state3]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state4) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val4[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val4[1]+" \">");
}
client.println("Click Here to Switch "+state_val4[!state4]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state5) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val5[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val5[1]+" \">");
}
client.println("Click Here to Switch "+state_val5[!state5]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state6) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val6[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val6[1]+" \">");
}
client.println("Click Here to Switch "+state_val6[!state6]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state7) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val7[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val7[1]+" \">");
}
client.println("Click Here to Switch "+state_val7[!state7]+" -RED of RGB LED");
client.println("</a>");
client.println("</html>");
break;
}
client.stop();
Serial.println("client disonnected");
}
}
// A Function to locate a given search string in a given base string
boolean find_string(String base, String search)
{
int len = search.length(); // find the length of the base string
for(int m = 0; m<((base.length()-len)+1);m++)// Iterate from the beginning of the base string till the end minus length of the substring
{
if(base.substring(m,(m+len))==search) // Check if the extracted Substring Matches the Search String
{
return true; // if it matches exit the function with a true value
}
}
return false; // if the above loop did not find any matches, control would come here and return a false value
}