Two way Ethernet Comunication with Webserver

Am making a Simple Thermostat control for my home. My Goal was to Transmit the temperature readings from Arduino to Webserver and Stores it to SQL.. & i have placed ON OFF buttons in my Website, which control the relay in ARDUINO..

I have succesfully Sent the Temperature Values to Website and stored it to SQL..

But I dont know how to Turn ON/OFF the relay in arduino from webserver...

PLEASE HELP me....

Am sending the temperature values Using

#include "SPI.h"

#include "Ethernet.h"



byte mac[] = { 0x90, 0xA2, 0xDB, 0x0F, 0x3A, 0xDF };
byte ip[]={192,168,1,2};


byte subnet[] = { 255, 255, 0, 0 };

EthernetClient client ;

char server[] = "XXXXX.com";

void setup() {

// Open serial communications and wait for port to open:


Serial.begin(9600);
  Ethernet.begin(mac);


if (Ethernet.begin(mac) == 0) {

Serial.println("Failed to configure Ethernet using DHCP");

// no point in carrying on, so do nothing forevermore:

// try to congifure using IP address instead of DHCP:

Ethernet.begin(mac, ip);

}

// give the Ethernet shield a second to initialize:

delay(1000);

Serial.println("connecting...");
  
}


void loop()

{

if( client.connect(server,80))

{

float tem = getTemp();

Serial.println(tem);

client.print( "GET /add.php?");
client.print( "temp1=");
client.print(tem);

client.println( " HTTP/1.1");
Serial.println( " HTTP/1.1");
client.println("Host:XXXXX.com");
Serial.println("Host:XXXXX.com");
client.println("User-Agent: Arduino");        // ethernet related stuff
Serial.println("User-Agent: Arduino");        //
client.println("Accept: text/html");          //
Serial.println("Accept: text/html"); 
client.println("Connection: close");        //
Serial.println("Connection: close");
client.println();

client.println();

client.stop();

}

delay( 5000 );

}

float getTemp() {

float voltage, V,t ;

voltage = analogRead(A0) ;
t = voltage * (5.0 / 1023.0);
 



return t;

}

am processing this script by php and storing in sql.. its working properly, Now i have to switch on/off the relay in arduino using the button in my webpage.

Please share any resource and a solution.

You need your Arduino application to operate as a webserver and to detect incoming html requests using EthernetServer.available().

You need to define an EthernetServer object in you application (assigned to an incoming port) and .begin() it.

Once you have an incoming EthernetClient object (from EthernetServer.available()) you need to parse the html request (lots of string or String processing).

You have the choice of sending html requests (from a browser to your Arduino operating as a web server) in GET format (incoming data is appended to the URL) or POST format (incoming data is on separate lines further down in the html request). If you want the option to send many different commands to your Arduino system (web server) across the www I recommend you go down the POST route. Read up about the differences between GET and POST html requests.

An early version of my (unwieldy) Arduino web server (that processes POST html requests) is available at http://www.2wg.co.nz/PUBLIC/WEBSERVR.TXT/ . However my POST parsing (processing) is limited to only login passwords, cookies and a few other things.

Here is an example of a POST html request that contains data (a PASSWORD - but the value is fake) that can be parsed to drive an Arduino application:

POST /25125/ HTTP/1.1
Host: www.2wg.co.nz
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-nz,th;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://www.2wg.co.nz/56727/
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 18

PASSWORD=123456789 <<NOT THE REAL PASSWORD>>

And here is the result of my Arduino application's parsing of this and associated socket data:

Browser IP: 222.154.229.4
Socket #: 0
Dest Port: 49867
PASSWORD: 123456789 <<NOT THE REAL PASSWORD>>
LENGTH: 18
HOST: WWW.2WG.CO.NZ
REQUEST: /25125/
PAGE: Password Login

Your needs are quite simple and you may be able to solve your relay on/off requirement using the Arduino ethernet web server example programs that use parsing of html GET requests.

Cheers

Catweazle NZ

Am making a Simple Thermostat control for my home. My Goal was to Transmit the temperature readings from Arduino to Webserver and Stores it to SQL.. & i have placed ON OFF buttons in my Website, which control the relay in ARDUINO..

You might be able to modify the below combined client/server code to do what you want.

//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 pins 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

#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(F("server/client 1.0 test 7/03/12")); // keep track of what is loaded
  Serial.println(F("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>")); 

            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(F("Led 5 On"));
            Serial.println();
          }
          if(readString.indexOf('3') >0)//checks for 3
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println(F("Led 5 Off"));
            Serial.println();
          }
          if(readString.indexOf('4') >0)//checks for 4
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println(F("Led 6 On"));
            Serial.println();
          }
          if(readString.indexOf('5') >0)//checks for 5
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println(F("Led 6 Off"));
            Serial.println();
          }
          if(readString.indexOf('6') >0)//checks for 6
          {
            digitalWrite(7, HIGH);    // set pin 7 high
            Serial.println(F("Led 7 On"));
            Serial.println();
          }
          if(readString.indexOf('7') >0)//checks for 7
          {
            digitalWrite(7, LOW);    // set pin 7 low
            Serial.println(F("Led 7 Off"));
            Serial.println();
          }     
          if(readString.indexOf('8') >0)//checks for 8
          {
            digitalWrite(8, HIGH);    // set pin 8 high
            Serial.println(F("Led 8 On"));
            Serial.println();
          }
          if(readString.indexOf('9') >0)//checks for 9
          {
            digitalWrite(8, LOW);    // set pin 8 low
            Serial.println(F("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(F("connected"));
    client.println(F("GET /~shb/arduino.txt HTTP/1.1"));
    client.println(F("Host: web.comporium.net"));
    client.println(F("Connection: close"));
    client.println();
  } 
  else {
    Serial.println(F("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(F("disconnecting."));
  Serial.println(F("=================="));
  Serial.println();
  client.stop();

}