Go Down

Topic: Web control problem - code (Read 2144 times) previous topic - next topic

Mikee

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:
Code: [Select]
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("<br />");//some space between lines
 client.println("<br />");
 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<br><input type='radio' name='L' value='0'>VENT OFF<br><input type='radio' name='H' value='1'>HEAT ON<br><input type='radio' name='H' value='0'>HEAT OFF<br><input type=submit value=submit></form>");

 client.println("<br />");
 //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:
Code: [Select]
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("<br />");

 //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<br><input type='radio' name='L' value='0'>VENT OFF<br><input type='radio' name='H' value='1'>HEAT ON<br><input type='radio' name='H' value='0'>HEAT OFF<br><input type='radio' name='G' value='1'>GATE OPN<br><input type='radio' name='G' value='0'>GATE CLS<br><input type=submit value=submit></form>");

 client.println("<br />");
 //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");
  }

zoomkat

Quote
What am I doing wrong?


Looks like some of your code is missing.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Mikee

Yes I put here just the part from where the problem started - my post had too many characters, here ist whole code:
Code: [Select]
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Client.h>
#include <Server.h>
#include <Ethernet.h>

#define ONE_WIRE_BUS 3
#define TEMPERATURE_PRECISION 12

float tempC, tempF;

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// arrays to hold device addresses
DeviceAddress Teplotanasavania = { 0x28, 0x44, 0x33, 0x9D, 0x01, 0x00, 0x00, 0xAA };
DeviceAddress Teplotaodtahu = { 0x28, 0x49, 0x45, 0x9D, 0x01, 0x00, 0x00, 0x11 };

byte    mac[] =     { 0x90, 0xA2, 0xDA, 0x00, 0x8D, 0x42 };   //physical mac address
byte    ip[] =      { 192, 168, 1, 104 };                     // ip in lan
byte    gateway[] = { 192, 168, 1, 1 };                       // the IP of the router
byte    subnet[] =  { 255, 255, 255, 0 };                     //subnet mask
Server  server(80);                // server port
int     ventPin = 5;                //Ventilation pin
int     heatpin = 6;               // Heating pin
int     gatepin = 7;               // Gate pin
int     sensorPin = A0;            // analog in 0 for testing
int     sensorValue = 0;           // integer for the analog sensor
String  readString = String(30);   // string for fetching data from address
boolean VENTON = false;             // LED status flag
boolean HEATON = false;             // Heat status flag  
boolean GATEON = false;             // Gate status flag    
//
void setup()
{    
 Serial.begin(9600); //enable serial datada print
 Ethernet.begin(mac, ip, gateway, subnet); //start Ethernet
 
 pinMode(ventPin, OUTPUT); //Set pin 5 to output
 pinMode(heatpin, OUTPUT); //Set pin 6 to output
 pinMode(gatepin, OUTPUT); //Set pin 7 to output
  server.begin();          // Start up the sensors library
 sensors.begin();          // set the resolution
 sensors.setResolution(Teplotanasavania, TEMPERATURE_PRECISION);
 sensors.setResolution(Teplotaodtahu, TEMPERATURE_PRECISION);

}
 // function to get the temperature for a device
 void getTemperature(DeviceAddress deviceAddress)
{
 tempC = sensors.getTempC(deviceAddress);
 tempF = DallasTemperature::toFahrenheit(tempC);
 Serial.print("Starting server"); //for debugging
 
 sensorValue = analogRead(sensorPin);
 Serial.println("Analog in:");   //for debugging
 Serial.println("");
 Serial.println (sensorValue);
 Serial.println("");
 Serial.println("");
}
//
void loop(){

 sensors.requestTemperatures();
 Client client = server.available();  // Create a client connection
   if (client) {
     while (client.connected()) {
       if (client.available()) {

         char c = client.read();

       if (readString.length() < 30) { //read char by char HTTP request
         readString.concat(c); } //store characters to string
         
       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 Ge = readstring.indexof("G=");
         Serial.print("L= position: ");
         Serial.println(Le);
         Serial.print("H= position: ");
         Serial.println(He);
         Serial.print("G= position: ");
         Serial.println(Ge);
//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 (Ge > 1){
           
         if (readString.substring(Ge,(Ge+3)) == "G=1") { //Gate has to be opened
             digitalWrite(heatpin, LOW);                 // set the relay on
     delay(800);
             GATEON = true;
     digitalwrite(gatepin, HIGH);
             Serial.println("gate is onening");
             GATEON = true;
         }

         if (readString.substring(Ge,(Ge+3)) == "G=0") { //Gate has to be closed
             digitalWrite(heatpin, LOW);                 // set the relay on
     delay(800);
             GATEON = true;
     digitalwrite(gatepin, HIGH);
             Serial.println("gate is closing");
             GATEON = 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("<br />");

 //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<br><input type='radio' name='L' value='0'>VENT OFF<br><input type='radio' name='H' value='1'>HEAT ON<br><input type='radio' name='H' value='0'>HEAT OFF<br><input type='radio' name='G' value='1'>GATE OPN<br><input type='radio' name='G' value='0'>GATE CLS<br><input type=submit value=submit></form>");

 client.println("<br />");
 //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");
  }
//printing GATE status
 client.print("<font size='5'>Gatepin status: ");
 if (GATEON == true) {
    client.println("<font color='green' size='5'>OPENED");
    Serial.print("Gate opened");
  }
  else {
   client.println("<font color='grey' size='5'>Closed");
   Serial.println("Gate closed");
  }
getTemperature(Teplotanasavania);
 client.println("HTTP/1.1 200 OK"); // Standard HTTP response
         client.println("Content-Type: text/html\n");
         client.println("<html><head><META HTTP-EQUIV=""refresh""CONTENT=""5"">\n");
         client.println("<title>Arduino Web Server</title></head>");
         client.println("<body>\n");
         client.println("<h1>Rekuperacia 328</h1>");
         client.println("<h3><font color='blue'>Teplota nasavania FRESH AIR IN</h3>");
         client.println("Temp C:");
         client.println(tempC);
         client.println("<br/>");
         client.println("Temp F:");
         client.println(tempF);
         client.println("<br/>");
         getTemperature(Teplotaodtahu);
         client.println("<h3><font color='red'>Teplota odtahu DIRTY AIR </h3>");
         client.println("Temp C:");
         client.println(tempC);
         client.println("<br/>");
         client.println("Temp F:");
         client.println(tempF);
         client.println("<br/>");
     


 client.println("<hr />");
 client.println("</body></html>");


readString=""; //clearing string for next read

client.stop(); //stopping client

Serial.println("stopped client"); // for debugging

}}}}}  

zoomkat

I can't get your code to compile, but you may be running out of memory. You might try the F() macro like below for the static html lines like below to save memory space.

Code: [Select]

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println(F("<HTML>"));
          client.println(F("<HEAD>"));
          client.println(F("<TITLE>Arduino GET test page</TITLE>"));
          client.println(F("</HEAD>"));
          client.println(F("<BODY>"));
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up