Pages: [1]   Go Down
Author Topic: server client togheter  (Read 1800 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi friends.
I'm trying to build a client-sever arduino that, from remote pc, turn on a led  and when is pressed a button on it, send a post to another arduino server, wich recieve the post and turn on a led.
Have you an idea or code to do it? I red many posts in wich are explained a lot of server-client cases, but I never found a server client wich send post to another arduino: can you help me please?

Thanks a lot, fafidio.
Logged

New River, Arizona
Offline Offline
God Member
*****
Karma: 16
Posts: 906
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Let me sure I understand.  You want one arduino to be both a client and a server, and you have another arduino that you want to be a server.  So, the PC as a client sends to the first arduino which turns on a light, then that arduino sends to the second one that also turns on a light.

If I got that correct, you can certainly do it.  You'll have to combine the server and client examples in the playground on one of the arduinos which is a little confusing, but I've done it a few times successfully.  The the second arduino is just the client example, or you can duplicate the first arduino and just not use the client code until later when you expand the system.
Logged

Trying to keep my house under control http://www.desert-home.com/

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8911
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is some combined server and client code you can experiment with. Bottom isa n example of a client POST.

Code:
//zoomkat 12-08-11, 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 \ slash to escape the " in the html
//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 }; //physical mac address
IPAddress ip(192,168,1,102); // ip in lan
IPAddress gateway(192,168,1,1); // internet access via router
IPAddress subnet(255,255,255,0); //subnet mask
//IPAddress myserver(208,104,2,86); // zoomkat web page
EthernetServer server(84); //server port
EthernetClient client;
String readString;
char serverName[] = "web.comporium.net"; // zoomkat's test web page server

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

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 12/08/11"); // keep track of what is loaded
  Serial.println("Send an g in serial monitor to test client"); // what to do to test client

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

}

void loop(){
  // check for serial input
  if (Serial.available() > 0)
  {
    byte inChar;
    inChar = Serial.read();
    if(inChar == 'g')
    {
      sendGET(); // call 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
            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=\"/?on\" target=\"inlineframe\">ON</a>");
            client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");

            //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
            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("on") >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/receive GET request data.
{
  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();

}


post
Code:
client.println("POST hxxp://yourdomain.com/SMSSite/XMLInterface/Postxml.aspx HTTP/1.0");
// add a header indicating you have content-data to send.  If you set this too low, the server won't see it.  If it is too high, the server may wait, or report an error.
client.print("content-length: ");
client.println(strlen(big_string));
// tell the server you are done with the header.
client.println();
// send the posted data.
client.print(big_string);
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Full Member
***
Karma: 0
Posts: 112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@draythomp
Thanks for answer.
I must be more clear:
I have two arduino on my private network, one must have a webserver where, with my pc I call html page and turn on and off a led on it.
On this arduino (number 1), must be on a button: when pressed, it will send a post to the other arduino (number 2, different address), wich is always waiting post of the first arduino. When post will be arrived, it will turn on a led on arduino 2.
I hope to be clear.

@zoomkat
Very good code; I've tried  to use it but it doesn't turn  led on arduino 2.
First part of the code has token by your old post wich four led-button and data read on your web page by client.
Code:
............
//////////////////////////
void sendGET() //client function to send and receive GET data from external server.
{
        buttonStateOld=buttonState;
        buttonState = digitalRead(buttonPin);
 
    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState != buttonStateOld)  {
                  if (buttonState == HIGH){
                           client.connect(serverName, 80);//connette il client
                           Serial.println("connesso"); //stampa su seriale;
                           client.println("POST /led=1");//invia il comando "accendi
led";
                       
                           client.println();
                             
                           client.stop();}
                    }
           {if (buttonState != buttonStateOld)
           {if (buttonState == LOW){
            client.connect(serverName, 80);
            Serial.println("connesso"); //stampa su seriale;
            client.println("POST /led=0");//invia il comando "spegni led";
           
            client.println();
            client.stop();
           
   
        }
       }
      }
          delay (50);//end delay before loop starts again

{
  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();

}
}
Thanks fafidio
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8911
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tested the code I posted, and using the web page served by the arduino, I can make pin 5 on the arduino high and low. Also sending a g to the arduino from the serial monitor resulted in a return from my web server. If you can not get the code (modified for yor router, etc.) to work, then there is not much else I can do for your project. Sorry.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Full Member
***
Karma: 0
Posts: 112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have modified your code for my ip address and it work very well:
but I don't know how I can modify it to send a post ,from client, to  another server.
With your code I can read all what in server is, but I don't know to use your post metod in previews message:
could you explain where I could put "post" instead of get request? Thanks, fafidio
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8911
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure why you want to use POST instead of GET, but for a GET request you probably could modify the same code for arduino # 1 to make get request to arduino #2 to operate the LEDs.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8911
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is some quick test code for arduino #1 you should be able to change to connect to arduino #2. This is still setup to connect to my web server for testing. note this code ip address end is 103 instead of 102 to keep the ip address different. The final idea is to send an o from the serial monitor to have arduino #1 send an "on" GET request to arduino #2 to turn its LED on. Send  f to do the same to turn the LED off.  As is, a g, o, or f sent from the serial monitor will all download the same text from my web server for testing.

Code:
//zoomkat 3-05-13, 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 \ slash to escape the " in the html
//address will look like http://192.168.1.103: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 }; //physical mac address
IPAddress ip(192,168,1,103); // arduino #1 ip address
IPAddress gateway(192,168,1,1); // internet access via router
IPAddress subnet(255,255,255,0); //subnet mask
IPAddress myserver(208,104,2,86); // zoomkat web page

//IPAddress myserver(192,168,1,102); // arduino #2 ip address

EthernetServer server(84); //server port
EthernetClient client;
String readString;
//char serverName[] = "web.comporium.net"; // zoomkat's test web page server
byte inChar;

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

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 12/08/11"); // keep track of what is loaded
  Serial.println("Send an g, o, or f, 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' || inChar == 'o' || inChar == 'f')
    {
      sendGET(); // call 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
            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=\"/?on\" target=\"inlineframe\">ON</a>");
            client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");

            //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
            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("on") >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/receive GET request data.
{
  //if (client.connect(myserver, 84)) { //arduino #2 on port 84
  if (client.connect(myserver, 80)) {
    Serial.println("connected");
    if(inChar == 'g') client.println("GET /~shb/arduino.txt HTTP/1.0");
    if(inChar == 'o') client.println("GET /~shb/arduino.txt HTTP/1.0");
    if(inChar == 'f') client.println("GET /~shb/arduino.txt HTTP/1.0");

    //GET request to arduino #2
    //if(inChar == 'o') client.println("GET /?on HTTP/1.0"); //LED on
    //if(inChar == 'f') client.println("GET /?off HTTP/1.0"); //LED off

    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();

}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Full Member
***
Karma: 0
Posts: 112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Zoomkat, you are a great person! Thanks a lot, I have modified your code to press button on arduino 1 and when pressed it turns on a led on arduino2. It makes a GET only when it is pressed. So, if I open webserver on arduino 1, I can turn on a led on it. If I open webserver on arduino 2 I can check led status. Perfect, it's what I wanted. Now I will work  to send sms with google calendar when button on arduino 1 is pressed.....then I will post code: someone could need it!
Is "calendar" the only methode to send sms without gms shield?
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8911
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Down the road I hope to use this simple code setup to have pots/joystick connected to arduino #1 to control servos connected to arduino #2.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Zoomkat
I was trying your code, though I changed it to use the WiFi shield, and I cannot seem to get it working. It never goes into the "listening for request" stage and sometimes the sendGet method is activated when I post to the Arduino.

Code:
//zoomkat 12-08-11, 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 4 high/low
//use the \ slash to escape the " in the html
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <WiFi.h>

IPAddress myserver(192,168,0,103); // zoomkat web page
WiFiServer server(80);
int status = WL_IDLE_STATUS;

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

void setup(){
   Serial.begin(9600);
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while(true);
  }
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    status = WiFi.begin("name", "pass");
    delay(10000);
  }
  server.begin();
 
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void loop(){
  sendGET(); // call sendGET function
  delay(100);
  WiFiClient responseClient = server.available();
  if (responseClient) {
    while (responseClient.connected()) {
      if (responseClient.available()) {
        char c = responseClient.read();
          Serial.print(c);
        if (c == '\n') {
            responseClient.println("HTTP/1.1 200 OK"); //send new page
            responseClient.println("Content-Type: text/html");
            responseClient.println();
            responseClient.println("Just some content");
           
          delay(1);
          //stopping client
          responseClient.stop();
          Serial.println("Finished");
        }
      }
    }
  }
}

//////////////////////////
void sendGET() //client function to send/receie GET request data.
{
  WiFiClient flaskClient;
  if (flaskClient.connect(myserver, 9090)) {
    Serial.println("connected");
    flaskClient.println("GET /arduino/status/1/ HTTP/1.1");
    flaskClient.println();
  }
  else {
    Serial.println("connection failed");
    Serial.println();
  }

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

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

}

Did anyone have the same problem? Or maybe see what I am doing wrong?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5367
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Wifi shield may have a bit of trouble dealing with multiple sockets. I am not sure it has the capability. I tried to assist another user with the FTP client code, and that person had the same problem with the Wifi shield. It would not open a second socket/connection while the first was still open.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I get similar problems between 2 Arduinos with Redfly Wifi Shields (Watterott), which I want to communicate directly, without computer.
I described that on a recent post: Wlan-Wifi communication beetween 2 Arduinos+Redfly http://arduino.cc/forum/index.php?PHPSESSID=ba3edac5d42177cdfd859939764d07fc&topic=152130.0
(thank you Zoomkat for your nice cooperation)
Logged


Pages: [1]   Go Up
Jump to: