Hello, I'm new to Arduino and I have a problem with my code and I can't find what goes wrong.
First of all I want to make a simple web server to control 4 relays. I tried too many programs but without luck. First I tried to do it with checkboxes which I changed them to look as toggle switches, but nothing wrong so I decided to make it simplier as you will see my program below. To sum up, my problem is: Why when I try to add more code into this program, my webserver isn't responding? What I'm doing wrong? I just want one or two checkboxes (below and out of the table) to make some other things. For example I want a checkbox, when it's checked, opens all relays.
Thanks in advance!
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192,168,1, 178 };
EthernetServer server(80);
int LED = 5;
int LED1 = 6;
int LED2 = 7;
int LED3 = 8;
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
pinMode(LED, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
#define BUFSIZ 100 //Buffer size for getting data
char clientline[BUFSIZ]; //sbring that will contain command data
int index = 0; //clientline index
void loop()
{
index=0; //reset the clientline index
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if(index<BUFSIZ) //Only add data if the buffer isn't full.
{
clientline[index]=c;
index++;
}
if (c == '\n' && currentLineIsBlank)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"URL.css\" />");
client.println("<TITLE>Arduino Control</TITLE>");
client.println("<style type='text/css'> body { font:bold 0.75em/16px Arial;color:#777;}.wrapper { margin:120px auto; width:139px;}.wrapper .row { clear:both; margin:0 0 6px 0; height:16px;}.wrapper .row label { float:left; width:100px;}.wrapper .row .switch { float:left;}</style>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<hr/>");
client.println("
");
client.println("<h1><center><img border=\"0\" src=\"URLofLOGO.png\" /></center></h1>");
client.println("
");
client.println("<hr/>");
client.println("
");
client.println("
");
client.println("<table border='7' width='450' align='center' cellpadding='6'>");
client.println("<tr><td align='center'><h2> Relay1 </h2></td>");
client.println("<td><form method=get action=/?><input type=radio name=L1 value=1>On<input type=radio name=L1 value=0>Off <input type=submit value=submit></form></td></tr>");
client.println("<tr><td align='center'><h2> Relay2 </h2></td>");
client.println("<td><form method=get action=/?><input type=radio name=L2 value=1>On<input type=radio name=L2 value=0>Off <input type=submit value=submit></form></td></tr>");
client.println("
");
client.println("<tr><td align='center'><h2> Relay3</h2></td><td><form method=get action=/?><input type=radio name=L3 value=1>On <input type=radio name=L3 value=0>Off <input type=submit value=submit></form></td></tr>");
client.println("
");
client.println("<tr><td align='center'><h2> Relay4 </h2></td><td><form method=get action=/?><input type=radio name=L4 value=1>On <input type=radio name=L4 value=0>Off <input type=submit value=submit></form></td></tr></table>");
client.println("
");
break;
client.println("</BODY>");
client.println("</HTML>");
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
if(strstr(clientline,"/?L1=1")!=0) { //look for the command to turn the led on
digitalWrite(LED, 1);
} else if(strstr(clientline,"/?L1=0")!=0) { //look for command to turn led off. Note: If led is on, it will stay on until command is given to turn it off, even if multiple computers are on the site
digitalWrite(LED, 0);
}
if(strstr(clientline,"/?L2=1")!=0) { //look for the command to turn the led on
digitalWrite(LED1, 1);
} else if(strstr(clientline,"/?L2=0")!=0) { //look for command to turn led off. Note: If led is on, it will stay on until command is given to turn it off, even if multiple computers are on the site
digitalWrite(LED1, 0);
}
if(strstr(clientline,"/?L3=1")!=0) { //look for the command to turn the led on
digitalWrite(LED2, 1);
} else if(strstr(clientline,"/?L3=0")!=0) { //look for command to turn led off. Note: If led is on, it will stay on until command is given to turn it off, even if multiple computers are on the site
digitalWrite(LED2, 0);
}
if(strstr(clientline,"/?L4=1")!=0) { //look for the command to turn the led on
digitalWrite(LED3, 1);
} else if(strstr(clientline,"/?L4=0")!=0) { //look for command to turn led off. Note: If led is on, it will stay on until command is given to turn it off, even if multiple computers are on the site
digitalWrite(LED3, 0);
}
}
}
delay(1);
client.stop();
}
}