Hi Guys,
I made a code for controlling two relays (Vent, Heater) based on codes i found here on Forum.
I´d like to add more relays (Gate opener-works with push button, once pushed gate opens, second time pushed gate closes), but when I added code I can´t even open control page in web browser. What am I doing wrong?
Original (working) code:
void loop(){
//indexOf("L=")
if (c == '\n') { //if HTTP request has ended
Serial.println("");
Serial.println(readString); // print for debugging
Serial.println("");
int Le = readString.indexOf("L="); //here is a key component of where
int He = readString.indexOf("H="); //the status is being read by the arduino
Serial.print("L= position: ");
Serial.println(Le);
Serial.print("H= position: ");
Serial.println(He);
//lets check if Vent should be lighted
if (Le > 1){
if (readString.substring(Le,(Le+3)) == "L=1") { //vent has to be turned ON
digitalWrite(ventPin, LOW); // set the LED on
Serial.println("ventpin paa");
VENTON = true;
}
if (readString.substring(Le,(Le+3))== "L=0") {
//Vent has to be turned OFF
digitalWrite(ventPin, HIGH); // set the Vent OFF
Serial.println("ventpin av");
VENTON = false;
}}
if (He > 1){
if (readString.substring(He,(He+3)) == "H=1") { //heat has to be turned ON
digitalWrite(heatpin, LOW); // set the heat on
Serial.println("heatpin paa");
HEATON = true;
}
if (readString.substring(He,(He+3)) == "H=0") {
//heat has to be turned OFF
digitalWrite(heatpin, HIGH); // set the heat OFF
Serial.println("heatpin av");
HEATON = false;
}}
client.println("HTTP/1.1 200 OK"); //output HTML data starting with standart header
client.println("Content-Type: text/html");
client.println();
client.print ("<body style=background-color:white>"); //set background to white
// HTML Code
client.println("<font color=’red’><h1>Arduino 328 control</font></h1>");//send first heading
client.println("<hr />");
client.println("<font color=’blue’ size=’5?>Analog input: "); //output some sample data to browser
sensorValue = analogRead(sensorPin);
client.print(sensorValue);//Analog input.
Serial.print(sensorValue);
client.println("
");//some space between lines
client.println("
");
client.println("<hr />");
//controlling led via checkbox
client.println("<h1>Schwitch control</h1>");
// address will look like http://192.168.xxx.xxx/?L=1 when submited
// this line will give the radiobuttons that give the input
client.println("<form method=get name=VENT> <input type='radio' name='L' value='1'>VENT ON
<input type='radio' name='L' value='0'>VENT OFF
<input type='radio' name='H' value='1'>HEAT ON
<input type='radio' name='H' value='0'>HEAT OFF
<input type=submit value=submit></form>");
client.println("
");
//printing VENT status
client.print("<font size=’5?>VENT status: ");
if (VENTON == true) {
client.println("<font color=’green’ size=’5?>ON");
Serial.print("Vent on");
}
else {
client.println("<font color=’grey’ size=’5?>OFF");
Serial.println("Vent off");
}
New code:
void loop(){
Serial.print(c); //output chars to serial port for debugging
//indexOf("L=")
if (c == '\n') { //if HTTP request has ended
Serial.println("");
Serial.println(readString); // print for debugging
Serial.println("");
int Le = readString.indexOf("L="); //here is a key component of where
int He = readString.indexOf("H="); //the status is being read by the arduino
int Do = readString.indexOf("D=");
Serial.print("L= position: ");
Serial.println(Le);
Serial.print("H= position: ");
Serial.println(He);
Serial.print("D= position: ");
Serial.println(Do);
//////////////////////////////////////////////////////////////////////////////////////String for VENT
//if Vent should be turned ON
if (Le > 1){
if (readString.substring(Le,(Le+3)) == "L=1") { //vent has to be turned ON
digitalWrite(ventPin, LOW); // set the Relay on
Serial.println("ventpin on");
VENTON = true;
}
if (readString.substring(Le,(Le+3))== "L=0") { //Vent has to be turned OFF
digitalWrite(ventPin, HIGH); // set the Relay OFF
Serial.println("ventpin off");
VENTON = false;
}}
///////////////////////////////////////////////////////////////////////////////////////String for HEAT
if (He > 1){
if (readString.substring(He,(He+3)) == "H=1") { //heat has to be turned ON
digitalWrite(heatpin, LOW); // set the relay on
Serial.println("heatpin onn");
HEATON = true;
}
if (readString.substring(He,(He+3)) == "H=0") { //heat has to be turned OFF
digitalWrite(heatpin, HIGH); // set the relay OFF
Serial.println("heatpin off");
HEATON = false;
}}
//////////////////////////////////////////////////////////////////////////////////////////String for GATE
if (Do > 1){
if (readString.substring(Do,(Do+3)) == "G=1") { //Gate has to be opened
digitalWrite(heatpin, LOW); // set the relay on
delay(800);
DoON = true;
digitalWrite(dopin, HIGH);
Serial.println("gate is onening");
DoON = true;
}
if (readString.substring(Do,(Do+3)) == "D=0") { //Gate has to be closed
digitalWrite(dopin, LOW); // set the relay on
delay(800);
DoON = true;
digitalWrite(dopin, HIGH);
Serial.println("gate is closing");
DoON = false;
}}
////////////////////////////////////////////////////////////////////////////////////////////////
client.println("HTTP/1.1 200 OK"); //output HTML data starting with standart header
client.println("Content-Type: text/html");
client.println();
client.print ("<body style=background-color:white>"); //set background to white
client.println("<font color=’red’><h1>Arduino control</font></h1>");//send first heading
client.println("<hr />");
client.println("<font color=’blue’ size=’5'>Analog input: "); //output some sample data to browser
sensorValue = analogRead(sensorPin);
client.print(sensorValue);//Analog input.
Serial.print(sensorValue);
client.println("
");
//controlling led via checkbox
client.println("<h1>Schwitch control</h1>");
// address will look like http://192.168.xxx.xxx/?L=1 when submited
// this line will give the radiobuttons that give the input
client.println("<form method=get name=VENT> <input type='radio' name='L' value='1'>VENT ON
<input type='radio' name='L' value='0'>VENT OFF
<input type='radio' name='H' value='1'>HEAT ON
<input type='radio' name='H' value='0'>HEAT OFF
<input type='radio' name='G' value='1'>GATE OPN
<input type='radio' name='G' value='0'>GATE CLS
<input type=submit value=submit></form>");
client.println("
");
//printing VENT status
client.print("<font size=’5'>VENT status: ");
if (VENTON == true) {
client.println("<font color=’green’ size=’5'>ON");
Serial.print("Vent on");
}
else {
client.println("<font color=’grey’ size=’5'>OFF");
Serial.println("Vent off");
}
//printing HEAT status
client.print("<font size=’5'>Heatpin status: ");
if (HEATON == true) {
client.println("<font color=’green’ size=’5'>ON");
Serial.print("Heat on");
}
else {
client.println("<font color=’grey’ size=’5'>OFF");
Serial.println("Heat off");
}