Need help to connect server and client in one program.

Hi i am working on my project to read the sensor value using arduino uno board using Ethernet shield and store the sensor values in to mysql data base ,i am doing it well with arduino web-client. I want to control a relay using web-server and I don't know where am struck.... i will b thankful for your help....

#include <SPI.h>
#include <Time.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 151 };
byte gateway[] = { 192, 168, 2, 1 };
byte subnet[] = { 255, 255, 255, 0 };
byte myserver[] = { 192, 168, 2, 235  }; 

EthernetServer server(80);
EthernetClient client;
String readString;
String HTTP_req;          // stores the HTTP request
boolean LED_status = 0;   // state of LED, off by default


float cur;

void setup()
{
  pinMode(8, OUTPUT);   
  Ethernet.begin(mac, ip, gateway, subnet);
    delay(1000);
    server.begin();
    Serial.begin(9600);
     Serial.println("hi");
   requestTime();
    //setTime(00,00,00,01,01,2011);
}

void loop()
{
Serial.println("hi1");
  int sec = second();
    if(sec == 0)
    {
       
     cur = analogRead(0);

        Serial.print("\nARDUINO: Current: ");
        Serial.println(cur);
        Serial.print("ARDUINO: attempting to connect... ");

        if(client.connect(myserver, 84))
        
            Serial.println("connected...");
            Serial.println("ARDUINO: forming HTTP request message");

            client.print("GET /arduino/add.php?");
            client.print("cur=");
            client.print(cur);
            client.print("&time=");
            print_time(now());
            client.println(" HTTP/1.1");
            client.println("Host: 192.168.2.235");
            ;
            client.println();

            Serial.println("ARDUINO: HTTP message sent");
            delay(3000);

            if(client.available())
            {
                Serial.println("ARDUINO: HTTP message received");
                Serial.println("ARDUINO: printing received headers and script response...\n");

                while(client.available())
                {
                    char c = client.read();
                    Serial.print(c);
                }
            }
            else
            {
                Serial.println("ARDUINO: no response received / no response received in time");
            }

            client.stop();
        }
        else
        {
            Serial.println("connection failure1");
        }
        
   
    
    
     EthernetClient client = server.available();  // try to get client
     Serial.println("hi2");
    if (client) {  // got client?
        boolean currentLineIsBlank = true;
         Serial.println("hi3");
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;  // save the HTTP request 1 char at a time
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Arduino RELAY Control</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>LED</h1>");
                    client.println("<p>Click to switch RELAY on and off.</p>");
                    client.println("<form method=\"get\">");
                    ProcessCheckbox(client);
                    client.println("</form>");
                    client.println("</body>");
                    client.println("</html>");
                    Serial.print(HTTP_req);
                    HTTP_req = "";    // finished with request, empty string
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            }
   
     
        }
    }
}

// switch RELAY and send back HTML for Relay checkbox
void ProcessCheckbox(EthernetClient cl)
{
    if (HTTP_req.indexOf("RELAY2=2") > -1) {  // see if checkbox was clicked
        // the checkbox was clicked, toggle the Relay
        if (RELAY_status) {
            RELAY_status = 0;
        }
        else {
            RELAY_status = 1;
        }
    }
    
    if (RELAY_status) {    // switch RELAY on
        digitalWrite(8, HIGH);
        // checkbox is checked
        cl.println("<input type=\"checkbox\" name=\"RELAY2\" value=\"2\" \
        onclick=\"submit();\" checked>RELAY2");
    }
    else {              // switch RELAY off
        digitalWrite(8, LOW);
        // checkbox is unchecked
        cl.println("<input type=\"checkbox\" name=\"RELAY2\" value=\"2\" \
        onclick=\"submit();\">RELAY2");
    }
}
        
void print_time(time_t t)
{
    client.print(year(t));

    if(month(t) < 10)
    {
        client.print("0");
        client.print(month(t));
    }
    else
    {
        client.print(month(t));
    }

    if(day(t) < 10)
    {
        client.print("0");
        client.print(day(t));
    }
    else
    {
        client.print(day(t));
    }

    if(hour(t) < 10)
    {
        client.print("0");
        client.print(hour(t));
    }
    else
    {
        client.print(hour(t));
    }

    if(minute(t) < 10)
    {
        client.print("0");
        client.print(minute(t));
    }
    else
    {
        client.print(minute(t));
    }

    if(second(t) < 10)
    {
        client.print("0");
        client.print(second(t));
    }
    else
    {
        client.print(second(t));
    }
}

void requestTime()
{
    Serial.println("please enter year (e.g. 2011): ");
    while(Serial.available() < 4){ /*loop until 4 bytes received */ }
    int y = (Serial.read() - '0');
    y = (10 * y) + (Serial.read() - '0');
    y = (10 * y) + (Serial.read() - '0');
    y = (10 * y) + (Serial.read() - '0');

    Serial.println("please enter month (00-31): ");
    while(Serial.available() < 2){ /*loop until 2 bytes received */ }
    int mon = (Serial.read() - '0');
    mon = (10 * mon) + (Serial.read() - '0');

    Serial.println("please enter day (00-31): ");
    while(Serial.available() < 2){ /*loop until 2 bytes received */ }
    int d = (Serial.read() - '0');
    d = (10 * d) + (Serial.read() - '0');

    Serial.println("please enter hours (00-23): ");
    while(Serial.available() < 2) { /*loop until 2 bytes received */ }
    int h = (Serial.read() - '0');
    h = (10 * h) + (Serial.read() - '0');

    Serial.println("please enter minutes (00-59: ");

    while(Serial.available() < 2) { /*loop until 2 bytes received */ }
    int m = (Serial.read() - '0');
    m = (10 * m) + (Serial.read() - '0');

    Serial.println("please enter seconds (00-59): ");
    while(Serial.available() < 2) { /*loop until 2 bytes received */ }
    int s = (Serial.read() - '0');
    s = (10 * s) + (Serial.read() - '0');

    setTime(h,m,s,d,mon,y);

    if(timeStatus() == timeSet)
    {
        Serial.println("thankyou. Time is set");
    }
}

Combined client and server test code.

//zoomkat 7-03-12, combined client and server
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor and send an g to test client and
//see what the arduino client/server receives
//web page buttons make pin 5 high/low
//use the ' in html instead of " to prevent having to escape the "
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //assign arduino mac address
byte ip[] = {192, 168, 1, 102 }; // ip in lan assigned to arduino
byte gateway[] = {192, 168, 1, 1 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port arduino server will use
EthernetClient client;
char serverName[] = "web.comporium.net"; // (DNS) zoomkat's test web page server
//byte serverName[] = { 208, 104, 2, 86 }; // (IP) zoomkat web page server IP address

String readString; //used by server to capture GET request 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  pinMode(6, OUTPUT); //pin selected to control
  pinMode(7, OUTPUT); //pin selected to control
  pinMode(8, OUTPUT); //pin selected to control

  //pinMode(5, OUTPUT); //pin 5 selected to control
  Ethernet.begin(mac,ip,gateway,gateway,subnet); 
  server.begin();
  Serial.begin(9600); 
  Serial.println("server/client 1.0 test 7/03/12"); // keep track of what is loaded
  Serial.println("Send an g in serial monitor to test client"); // what to do to test client
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) 
  {
    byte inChar;
    inChar = Serial.read();
    if(inChar == 'g')
    {
      sendGET(); // call client sendGET function
    }
  }  

  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.print(readString); //print to serial monitor for debuging 

            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println(F("HTTP/1.1 204 Zoomkat"));
            client.println();
            client.println();  
          }
          else {   
            client.println(F("HTTP/1.1 200 OK")); //send new page on browser request
            client.println(F("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>"));

            client.println(F("<H1>Zoomkat's simple Arduino 1.0 button</H1>"));

            // DIY buttons
            client.println(F("Pin5"));
            client.println(F("<a href=/?on2 target=inlineframe>ON</a>")); 
            client.println(F("<a href=/?off3 target=inlineframe>OFF</a>

")); 

            client.println(F("Pin6"));
            client.println(F("<a href=/?on4 target=inlineframe>ON</a>")); 
            client.println(F("<a href=/?off5 target=inlineframe>OFF</a>

")); 

            client.println(F("Pin7"));
            client.println(F("<a href=/?on6 target=inlineframe>ON</a>")); 
            client.println(F("<a href=/?off7 target=inlineframe>OFF</a>

")); 

            client.println(F("Pin8"));
            client.println(F("<a href=/?on8 target=inlineframe>ON</a>")); 
            client.println(F("<a href=/?off9 target=inlineframe>OFF</a>

")); 

            client.println(F("Pins"));
            client.println(F("&nbsp;<a href=/?off2468 target=inlineframe>ALL ON</a>")); 
            client.println(F("&nbsp;<a href=/?off3579 target=inlineframe>ALL OFF</a>

")); 

            
            
                      // mousedown buttons
          client.println(F("<input type=button value=ON onmousedown=location.href='/?on4;' target=inlineframe>")); 
          client.println(F("<input type=button value=OFF onmousedown=location.href='/?off5;' target=inlineframe>"));        
          client.println(F("&nbsp;<input type=button value='ALL OFF' onmousedown=location.href='/?off3579;' target=inlineframe>

"));        
                   
          // mousedown radio buttons
          client.println(F("<input type=radio onmousedown=location.href='/?on6;' target=inlineframe>ON</>")); 
          client.println(F("<input type=radio onmousedown=location.href='/?off7; target=inlineframe'>OFF</>")); 
          client.println(F("&nbsp;<input type=radio onmousedown=location.href='/?off3579;' target=inlineframe>ALL OFF</>

"));    
   
          
          // custom buttons
          client.print(F("<input type=submit value=ON target=inlineframe style=width:100px;height:45px onClick=location.href='/?on8;'>"));
          client.print(F("<input type=submit value=OFF target=inlineframe style=width:100px;height:45px onClick=location.href='/?off9;' target=inlineframe>"));
          client.print(F("&nbsp;<input type=submit value='ALL OFF' target=inlineframe style=width:100px;height:45px onClick=location.href='/?off3579;' target=inlineframe>"));

            
            client.println(F("<IFRAME name=inlineframe style='display:none'>"));          
            client.println(F("</IFRAME>"));

            client.println(F("</BODY>"));
            client.println(F("</HTML>"));
          }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf('2') >0)//checks for 2
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led 5 On");
            Serial.println();
          }
          if(readString.indexOf('3') >0)//checks for 3
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led 5 Off");
            Serial.println();
          }
          if(readString.indexOf('4') >0)//checks for 4
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led 6 On");
            Serial.println();
          }
          if(readString.indexOf('5') >0)//checks for 5
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led 6 Off");
            Serial.println();
          }
          if(readString.indexOf('6') >0)//checks for 6
          {
            digitalWrite(7, HIGH);    // set pin 7 high
            Serial.println("Led 7 On");
            Serial.println();
          }
          if(readString.indexOf('7') >0)//checks for 7
          {
            digitalWrite(7, LOW);    // set pin 7 low
            Serial.println("Led 7 Off");
            Serial.println();
          }     
          if(readString.indexOf('8') >0)//checks for 8
          {
            digitalWrite(8, HIGH);    // set pin 8 high
            Serial.println("Led 8 On");
            Serial.println();
          }
          if(readString.indexOf('9') >0)//checks for 9
          {
            digitalWrite(8, LOW);    // set pin 8 low
            Serial.println("Led 8 Off");
            Serial.println();
          }         

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

        }
      }
    }
  }
} 

//////////////////////////
void sendGET() //client function to send and receive GET data from external server.
{
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0");
    client.println();
  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();
    Serial.print(c);
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}

I think if you were to provide more details as to what you are doing and how you are doing things you would possibly receive more help.
It seems you are asking people to read and understand your program and to offer you help based on that.

Firstly, could I suggest you tidy up your program, there are many blank lines that makes reading difficult.
It appears to be cobbled together from different routines you have grabbed.

Give it more documentation, at each routine and specific function, so it follows what you are doing.
To me, It appears you have set up a local server running SQL, is that correct?
You haven't scaled or done anything with your analog reading, it will be just raw and unfiltered, is that what you intend?
Why do you have a delay(3000) after you do a http GET?

You certainly have a lot of serial text being sent, and at a slow speed of 9600, I would suggest to rationalise this.

So, again, simply saying you want to control a relay using a web server is not providing much insight.
I am not intending to be critical, simply to let you know how I read your post.


Paul

PS, thanks zoomkat, thought you pass through with some code. :slight_smile: