send url using a switch.?

hi,i dont know much about programing so i need your help.I have a small web server using arduino uno /ethernet and some relays for a small home automation.I can open doors,and control some lights in my house.The next step i need to do is using arduino and a button switch on a digital port to send an url to a remote device so i can control something over the internet,for example ,when i press the button and set the digital port to "high" ,an url like "http://myoffice.ip/?lightsoff" be sent to my office arduino web server. Any suggestions or code examples i can read?

hi,i dont know much about programing so i need your help.I have a small web server using arduino uno /ethernet and some relays for a small home automation.I can open doors,and control some lights in my house.

A server serves up answers in response to a client request. A client makes requests.

It appears that you have a server.

The next step i need to do is using arduino and a button switch on a digital port to send an url to a remote device so i can control something over the internet,for example ,when i press the button and set the digital port to "high" ,an url like "http://myoffice.ip/?lightsoff" be sent to my office arduino web server.

The same Arduino that is the server? Or, a different one?

A client, which is what the Arduino with switch would be, does not "send a URL". It makes a GET request of a server. In your example, the server would be identified as "myoffice.ip", and the GET request would be "GET /?lightsoff HTTP/1.1".

A server serves up answers in response to a client request. A client makes requests.

It appears that you have a server.

yes i have a server.

The same Arduino that is the server? Or, a different one?

yes the same arduino that is the server.Can the same arduino do that?be the server and the client ,to make the Get request when i press the button?

wolfgr: Can the same arduino do that?

Yes.

found this example ,i edit it and it works.

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

byte mac[] = { 0x90,0xA2,0xDA,0x00,0x55,0x8D};  //Replace with your Ethernet shield MAC
EthernetClient client;

void setup()
{
Serial.begin(9600);
Ethernet.begin(mac);
delay(1000);
Serial.println("connecting...");
}

void loop(){
String data;
data+="";
data+="data=12.7%2C0%2C50"; // Use HTML encoding for comma's
data+="&submit=Submit"; // Submitting data

if (client.connect("myoffice-ip",80)) {
Serial.println("connected");
client.println("POST /?lightsoff HTTP/1.1");
client.println("Host: myoffice-ip");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
client.println();

//Prints your post request out for debugging
Serial.println("POST /?lightsoff HTTP/1.1");
Serial.println("Host: myoffice-ip");
Serial.println("Content-Type: application/x-www-form-urlencoded");
Serial.println("Connection: close");
Serial.print("Content-Length: ");
Serial.println(data.length());
Serial.println();
Serial.print(data);
Serial.println();
}
delay(2000);

if (client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
exit(0);
}

}

now i have to put that code together with my webservers code and make it work somehow…I dont have a clue where to begin :cold_sweat:
my small webserver s code is that

#include <SPI.h>
#include <Ethernet.h>  
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 120 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(4040);                             //server port     
String readString;


void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // 
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  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.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<TITLE>HOME AUTO SERVER</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>HOME AUTOMATION PANEL</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>PRESS TO OPEN AND CLOSE</H2>");
           client.println("
");
           client.println("
");
           client.println("<a href=\"/?button4on\"\"> INDOOR </a>");
           client.println("
");
           client.println("
");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\"> OUTDOOR </a>");
           client.println("
");
           client.println("
");
           client.println("
"); 
           client.println("<a href=\"/?button2on\"\"> LIGHTS  ON </a>");
           client.println("<a href=\"/?button2off\"\"> LIGHTS  OFF </a>
");
           client.println("
");     
           client.println("
");
           client.println("
"); 
           client.println("<a href=\"/?button3on\"\"> MAIN SWITCH ON </a>");
           client.println("<a href=\"/?button3off\"\"> MAIN SWITCH OFF </a>
");
           client.println("
");     
           client.println("
");
           client.println("
"); 
           client.println("
");   
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
          
           
           if (readString.indexOf("?button1on") >0){
               digitalWrite(6, HIGH);
               delay(3000);
               digitalWrite(6, LOW);
           }
          
           if (readString.indexOf("?button2on") >0){
               digitalWrite(7, HIGH);
           }
           if (readString.indexOf("?button2off") >0){
               digitalWrite(7, LOW);
           }
           if (readString.indexOf("?button3on") >0){
               digitalWrite(8, HIGH);
           }
           if (readString.indexOf("?button3off") >0){
               digitalWrite(8, LOW);
           }
           if (readString.indexOf("?button4on") >0){
               digitalWrite(9, HIGH);
               delay(3000);
               digitalWrite(9, LOW);
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
}
}

Any help from the experianced people here is welcome

Create a function containing the client logic from your example client sketch. (Leave out the exit() call at the end of the function.)

Put the function (and any global data it requires) in your web server sketch.

Make any changes you need to that so that it carries out the HTTP request that you want.

Call that function at the point you want that request to be performed within your web server sketch.

PeterH: Create a function containing the client logic from your example client sketch. (Leave out the exit() call at the end of the function.)

Put the function (and any global data it requires) in your web server sketch.

Make any changes you need to that so that it carries out the HTTP request that you want.

Call that function at the point you want that request to be performed within your web server sketch.

trying to do it that way (the exit() is my addition so i dont overflow my server with requests when testing),but i cant make it work ,i see at the serial monitor window that it is connecting and sending tha request but nothing happens.When i test the example by itself ,it is working nicely.

If you post your code, perhaps somebody will be able to spot the cause of your problem.

The next step i need to do is using arduino and a button switch on a digital port to send an url to a remote device so i can control something over the internet,for example ,when i press the button and set the digital port to “high” ,an url like “http://myoffice.ip/?lightsoff” be sent to my office arduino web server.

Looks like you need an event based client application that sends a client request when the button is pressed. To prevent flooding your server, there needs to be a toggle or delay function in the code such that the button press only sends one request per button press or until the button is released. Below is some event based client test code that demonstrates the event concept.

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

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

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

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

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    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(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

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

}

thanx for your answers,still stragling to make it work ,this is the scetch i am trying

#include <SPI.h>
#include <Ethernet.h>  
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 120 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(4040);                             //server port
EthernetClient client;

String readString;


void setup() {
{
Serial.begin(9600);

delay(1000);
Serial.println("connecting...");
}
  // LEDs
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  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.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>HOME AUTO SERVER</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>HOME AUTOMATION PANEL</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>PRESS TO OPEN AND CLOSE</H2>");
           client.println("
");
           client.println("
");
           client.println("<a href=\"/?button4on\"\"> INDOOR </a>");
           client.println("
");
           client.println("
");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\"> OUTDOOR </a>");
           client.println("
");
           client.println("
");
           client.println("
"); 
           client.println("<a href=\"/?button2on\"\"> LIGHTS  ON </a>");
           client.println("<a href=\"/?button2off\"\"> LIGHTS  OFF </a>
");
           client.println("
");     
           client.println("
");
           client.println("
"); 
           client.println("<a href=\"/?button3on\"\"> MAIN SWITCH ON </a>");
           client.println("<a href=\"/?button3off\"\"> MAIN SWITCH OFF </a>
");
           client.println("
");     
           client.println("
");
           client.println("
"); 
           client.println("
");   
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
         
           if (readString.indexOf("?button1on") >0){
               digitalWrite(6, HIGH);
               delay(3000);
               digitalWrite(6, LOW);
           }
          
           if (readString.indexOf("?button2on") >0){
               sendGET();
           }
           if (readString.indexOf("?button2off") >0){
               digitalWrite(7, LOW);
           }
           if (readString.indexOf("?button3on") >0){
               digitalWrite(8, HIGH);
           }
           if (readString.indexOf("?button3off") >0){
               digitalWrite(8, LOW);
           }
           if (readString.indexOf("?button4on") >0){
               digitalWrite(9, HIGH);
               delay(3000);
               digitalWrite(9, LOW);
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
}
}
void sendGET(){


    if (client.connect("myoffice-ip",80)) {
    Serial.println("connected");
    client.println("GET /?lightsoff HTTP/1.1"); //download text
    client.println("Host: myoffice-ip");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }


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

}

the void sendGET is called allright when i press the “button2on” ,i see at serial monitor that gets connected with server,waits and then disconnect,but nothing happens,no data gets posted.Where am i wrong?

Not sure just what you are trying to do, but below is some combined client/server 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 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.println(readString); //print to serial monitor for debuging 

            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println("HTTP/1.1 204 Zoomkat");
            client.println();
            client.println();  
          }
          else {   
            client.println("HTTP/1.1 200 OK"); //send new page on browser request
            client.println("Content-Type: text/html");
            client.println();

            client.println("<HTML>");
            client.println("<HEAD>");
            client.println("<TITLE>Arduino GET test page</TITLE>");
            client.println("</HEAD>");
            client.println("<BODY>");

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

            client.println("<a href='/?on1' target='inlineframe'>ON</a>"); 
            client.println("<a href='/?off' target='inlineframe'>OFF</a>"); 

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

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

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

          ///////////////////// control arduino pin
          if(readString.indexOf("on1") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //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.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  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();

}

as you see i have allready an ethernet client and a server,what i want (if it is doable) is with that scetch to send a http request to a second server.

I haven't tried it, but you might be able to setup two client instances like below, or something similar.

char serverName1[] = "web.server1.net";
char serverName2[] = "web.server2.net";

EthernetClient client1;
EthernetClient client2;

if (client1.connect(serverName1, 80)) { 

if (client2.connect(serverName2, 80)) {