Hi everyone,
I'm making a web based sprinkler control system using an Arduino UNO R3, and ethernet shield, and an 8-relay board. It's all working fine and I have some code written up which was working great until now. I have a button that turns all the relays on, and a button that turns all relays off. I also have buttons for each individual relay to turn on or off. Out of nowhere, when I was testing the program, I clicked on the "turn all on" button and it turns all the relays on (like its supposed to do) but right after that it redirects me to a page of HTML code. It looks like this:
ton>Turn On</button><a>
Sprinkler 2 is <b><font color=red></b>OFF</font>
<a href="./?PIN3=T"><button>Turn On</button><a>
Sprinkler 3 is <b><font color=red></b>OFF</font>
<a href="./?PIN4=T"><button>Turn On</button><a>
Sprinkler 4 is <b><font color=red></b>OFF</font>
<a href="./?PIN5=T"><button>Turn On</button><a>
Sprinkler 5 is <b><font color=red></b>OFF</font>
<a href="./?PIN6=T"><button>Turn On</button><a>
Sprinkler 6 is <b><font color=red></b>OFF</font>
<a href="./?PIN7=T"><button>Turn On</button><a>
Sprinkler 7 is <b><font color=red></b>OFF</font>
<a href="./?PIN8=T"><button>Turn On</button><a>
Sprinkler 8 is <b><font color=red></b>OFF</font>
<a href="./?PIN9=T"><button>Turn On</button><a>
Sprinkler 1 is <b><font color=red></b>OFF</font>
<a href="./?
PIN2=T
"><button>Turn On</button><a>
<<a href=HTTP/1.1 200 OK
Content-Type: text/html
<title>SprinkDuino</title>
<h1 color=red style=text-align:center;>Welcome to SprinkDuino!</h1>
<a href="./?PINA=T">TURN ALL ON<a>
<a href="./?PINA=F">TURN ALL OFF<a>
Sprinkler 1 is <b><font color=green></b>ON</font>
<a href="./?PIN2=F"><button>Turn Off</button><a>
Sprinkler 2 is <b><font color=red></b>OFF</font>
<a href="./?PIN3=T"><button>Turn On</button><a>
Sprinkler 3 is <b><font color=red></b>OFF</font>
<a href="./?PIN4=T"><button>Turn On</button><a>
Sprinkler 4 is <b><font color=red></b>OFF</font>
<a href="./?PIN5=T"><button>Turn On</button><a>
Sprinkler 5 is <b><font color=red></b>OFF</font>
<a href="./?PIN6=T"><button>Turn On</button><a>
Sprinkler 6 is <b><font color=red></b>OFF</font>
<a href="./?PIN7=T"><button>Turn On</button><a>
Sprinkler 7 is <b><font color=red></b>OFF</font>
<a href="./?PIN8=T"><button>Turn On</button><a>
Sprinkler 8 is <b><font color=red></b>OFF</font>
<a href="./?PIN9=T"><button>Turn On</button><a>
FF</font>
<a href="./?PIN2=T"><button>Turn On</button><a>
Sprinkler 2 is <b><font color=red></b>OFF</font>
<a href="./?PIN3=T"><button>Turn On</button><a>
Sprinkler 3 is <b><font color=red></b>OFF</font>
<a href="./?PIN4=T"><button>Turn On</button><a>
Sprinkler 4 is <b><font color=red></b>OFF</font>
<a href="./?PIN5=T"><button>Turn On</button><a>
Sprinkler 5 is <b><font color=red></b>OFF</font>
<a href="./?PIN6=T"><button>Turn On</button><a>
Sprinkler 6 is <b><font color=red></b>OFF</font>
<a href="./?PIN7=T"><button>Turn On</button><a>
Sprinkler 7 is <b><font color=red></b>OFF</font>
<a href="./?PIN8=T"><button>Turn On</button><a>
Sprinkler 8 is <b><font color=red></b>OFF</font>
<a href="./?PIN9=T"><button>Turn On</button><a>
Sprinkler 1 is <b><font color=red></b>OFF</font>
<a href="./?
PIN2=T
"><button>Turn On</button><a>
<<a href=HTTP/1.1 200 OK
Content-Type: text/html
....
....
....
...and its pretty long. I'm guessing this is the output code from the Arduino, but I can't figure out whats going wrong. I didn't change any code between the time it was working and started doing this. Hopefully one of you has some idea of what's going on!
Here's my program code:
#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[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0x26 };
IPAddress ip(192,168,1,134);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
int pin[8] = {2,3,4,5,6,7,8,9};
String readString = String(30);
String state[8] = String(3);
String pinOn[8] = String(6);
String pinOff[8] = String(6);
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
//Sets relay pins (2-9) as output
for(int i = 2; i < 10; i++){
pinMode(i, OUTPUT);
}
for(int i = 2; i < 10; i++){
digitalWrite(i, HIGH);
state[i-2] = "OFF";
}
pinOn[0] = "PIN2=T";
pinOn[1] = "PIN3=T";
pinOn[2] = "PIN4=T";
pinOn[3] = "PIN5=T";
pinOn[4] = "PIN6=T";
pinOn[5] = "PIN7=T";
pinOn[6] = "PIN8=T";
pinOn[7] = "PIN9=T";
pinOff[0] = "PIN2=F";
pinOff[1] = "PIN3=F";
pinOff[2] = "PIN4=F";
pinOff[3] = "PIN5=F";
pinOff[4] = "PIN6=F";
pinOff[5] = "PIN7=F";
pinOff[6] = "PIN8=F";
pinOff[7] = "PIN9=F";
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (readString.length() < 30) {
readString.concat(c);
}
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
int PIN = readString.indexOf("PIN");
for(int i = 0; i < 8; i++){
if (readString.substring(PIN,PIN+6) == pinOn[i]) {
digitalWrite(pin[i], LOW);
state[i] = "ON";
}
else if (readString.substring(PIN,PIN+6) == pinOff[i]) {
digitalWrite(pin[i], HIGH);
state[i] = "OFF";
}
}
if(readString.substring(PIN,PIN+6) == "PINA=F") {
for(int i = 0; i < 8; i++){
digitalWrite(pin[i], HIGH);
state[i] = "OFF";
}
}
else if(readString.substring(PIN,PIN+6) == "PINA=T") {
for(int i = 0; i < 8; i++){
digitalWrite(pin[i], LOW);
state[i] = "ON";
}
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<title>SprinkDuino</title>");
client.println("<h1 color=red style=text-align:center;>Welcome to SprinkDuino!</h1>");
client.println("
");
client.println("<a href=\"./?PINA=T\"><button>TURN ALL ON</button><a>");
client.println("
");
client.println("<a href=\"./?PINA=F\"><button>TURN ALL OFF</button><a>");
client.println("
");
for(int i = 2; i < 10; i++){
client.print("Sprinkler ");
client.print(i-1);
client.print(" is ");
if(state[i-2] == "ON"){
client.print("<b><font color=green></b>");
client.print(state[i-2]);
client.print("</font>");
client.print("
");
}
else if(state[i-2] == "OFF"){
client.print("<b><font color=red></b>");
client.print(state[i-2]);
client.print("</font>");
client.print("
");
}
if (state[i-2] == "ON") {
client.print("<a href=\"./?");
client.print(pinOff[i-2]);
client.print("\"><button>Turn Off</button><a>");
}
else {
client.print("<a href=\"./?");
client.print(pinOn[i-2]);
client.print("\"><button>Turn On</button><a>");
}
client.print("
");
}
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
readString = "";
// close the connection:
client.stop();
}
}
Thanks in advance!
-Ryan