LED control from web by using two arduino's.

Hi,
I have two arduino's and two ethernet shields. I have created a web server on one arduino and can control the led's connected to it. My question is that can i connect the 2nd arduino to the server created by the 1st one and control the led's of the 2nd arduino from the web page? If its possible then any suggestions on how to achieve that would be really appreciated. Thanks.

ssd_20072:
Hi,
I have two arduino's and two ethernet shields. I have created a web server on one arduino and can control the led's connected to it. My question is that can i connect the 2nd arduino to the server created by the 1st one and control the led's of the 2nd arduino from the web page? If its possible then any suggestions on how to achieve that would be really appreciated. Thanks.

The most likely way to perform the desired actions would be to set the second arduino up as a web "client" and have it send GET request to the server arduino.

zoomkat:

ssd_20072:
Hi,
I have two arduino's and two ethernet shields. I have created a web server on one arduino and can control the led's connected to it. My question is that can i connect the 2nd arduino to the server created by the 1st one and control the led's of the 2nd arduino from the web page? If its possible then any suggestions on how to achieve that would be really appreciated. Thanks.

The most likely way to perform the desired actions would be to set the second arduino up as a web "client" and have it send GET request to the server arduino.

Ok but what about the pins to which the led's are connected to the 2nd arduino? Where should i specify them to be able to be recognized by the server....in the web server code or the client?

Ok but what about the pins to which the led's are connected to the 2nd arduino? Where should i specify them to be able to be recognized by the server....in the web server code or the client?

So how did you perform the below? Seems they would be the same functions.

I have created a web server on one arduino and can control the led's connected to it.

You need to clarify whether the Arduinos are sitting next to each other or miles away from each other. If they're right next to each other I think the easiest thing would be to connect their serial ports (tx to rx and rx to tx) and communicate that way.

My question is that can i connect the 2nd arduino to the server created by the 1st one and control the led's of the 2nd arduino from the web page?

If the 2nd Arduino is a client, why does it need to ask the server to serve up a page? How is anything on that page going to be used to control the client's LEDs?

If there are just two Arduinos, the client and the server, where is independent (i.e. user) input coming from?

for the web server i'm using this code :

#include <EtherCard.h>

static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
static byte myip[] = {192,168,1,2};
byte Ethernet::buffer[700];

const int ledPin = 2;
boolean ledStatus;

char* on = "ON";
char* off = "OFF";
char* statusLabel;
char* buttonLabel;

void setup () {
 
  Serial.begin(57600);
  Serial.println("WebLed Demo");
 
  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    Serial.println( "Failed to access Ethernet controller");
 else
   Serial.println("Ethernet controller initialized");
 
  if (!ether.staticSetup(myip))
    Serial.println("Failed to set IP address");

  Serial.println();
  
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  ledStatus = false;
}
  
void loop() {
 
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  
  if(pos) {
    
    if(strstr((char *)Ethernet::buffer + pos, "GET /?status=ON") != 0) {
      Serial.println("Received ON command");
      ledStatus = true;
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?status=OFF") != 0) {
      Serial.println("Received OFF command");
      ledStatus = false;
    }
    
    if(ledStatus) {
      digitalWrite(ledPin, HIGH);
      statusLabel = on;
      buttonLabel = off;
    } else {
      digitalWrite(ledPin, LOW);
      statusLabel = off;
      buttonLabel = on;
    }
      
    BufferFiller bfill = ether.tcpOffset();
    bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
      "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
      "<html><head><title>WebLed</title></head>"
      "<body>LED Status: $S "
      "<a href=\"/?status=$S\"><input type=\"button\" value=\"$S\"></a>"
      "</body></html>"      
      ), statusLabel, buttonLabel, buttonLabel);
    ether.httpServerReply(bfill.position());
  }
}

So where do i include the pins of the 2nd arduino?

Quick5pnt0:
You need to clarify whether the Arduinos are sitting next to each other or miles away from each other. If they're right next to each other I think the easiest thing would be to connect their serial ports (tx to rx and rx to tx) and communicate that way.

Well actually they will be sitting next to each other. But i am trying to make a project for 'The Internet of Things' in my college. So to demonstrate devices connected to the internet, I'm giving this example. If you have any suggestions or other examples on how to demonstrate IOT using arduino that would be really helpful.

PaulS:

My question is that can i connect the 2nd arduino to the server created by the 1st one and control the led's of the 2nd arduino from the web page?

If the 2nd Arduino is a client, why does it need to ask the server to serve up a page? How is anything on that page going to be used to control the client's LEDs?

If there are just two Arduinos, the client and the server, where is independent (i.e. user) input coming from?

I'm sorry I didn't quite understand your questions. Could you please explain? Thanks

So where do i include the pins of the 2nd arduino?

You don't. The server can know nothing about the client - not even that it is an Arduino.

The server serves up the page to any client that connects, including other Arduinos, web browsers, net bots, etc.

The Arduino as client can get information from the Arduino as server, but that information now causes the server to do something, not the client. The client for the server you have would be a web browser, where a human could click, or not, the button.

How would the Arduino as client click the button?

PaulS:

So where do i include the pins of the 2nd arduino?

You don't. The server can know nothing about the client - not even that it is an Arduino.

The server serves up the page to any client that connects, including other Arduinos, web browsers, net bots, etc.

The Arduino as client can get information from the Arduino as server, but that information now causes the server to do something, not the client. The client for the server you have would be a web browser, where a human could click, or not, the button.

How would the Arduino as client click the button?

No i didn't say that the client would click the button, I said that i would turn the led ON or OFF from the browser. I know that it is possible to control the LED connected to the arduino as server. But will it be possible to control the LED connected to the arduino as client from the same web page by me clicking on the button?

A web browser is a client. What you appear to want to do is have one client control the action of another client, and that can't happen.

What you could do is have the Arduino as client make different GET requests, and get different responses, from the GET requests made by the browser.

Then, the Arduino as client could do something based on the responses it gets from the server.

The Arduino as client would always lag behind the browser as client's GET requests to change the pin state.

The Arduino as server can not push data to the Arduino as client. Clients can only pull data.

PaulS:
A web browser is a client. What you appear to want to do is have one client control the action of another client, and that can't happen.

What you could do is have the Arduino as client make different GET requests, and get different responses, from the GET requests made by the browser.

Then, the Arduino as client could do something based on the responses it gets from the server.

The Arduino as client would always lag behind the browser as client's GET requests to change the pin state.

The Arduino as server can not push data to the Arduino as client. Clients can only pull data.

Ahhh i see. Now i got it. So the client would change its LED status based upon the different responses from the server. Thanks a lot for clearing things up mate.
Also have you got any ideas on how to demonstrate the Internet Of Things using arduino?

Simple server test code for turning an arduino pin high/low. Easy to expand to several pins.

//zoomkat 4-1-12
//simple button GET for servo and pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html, or use ' instead of " 
//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>

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

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

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server servo/pin 5 test 1.0"); // so I can keep track of what is loaded
}

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("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on\">ON</a>"); 
          client.println("<a href=\"/?off\">OFF</a>"); 

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            myservo.write(40);
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            myservo.write(140);
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

simple client test code that sends a GET request to a server based on an event, in this case the event is receiving an e via the serial port.

//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//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 
//the arduino lan IP address { 192, 168, 1, 102 } may need 
//to be modified modified to work with your router.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical 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
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.begin(9600); 
  Serial.println("Better client test 4/04/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(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    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

}

im confused why have you got two arduinos if you need a server and a client could you not just set the server up on one arduino and use a pc or mobile webbrowser to access the web server. this was you could put a button on the webpage that would turn an led on and off on the server then if for exampe you gave me the web address i could turn the led on from here (assuming you get it available on the internet but even without this you could do it over the network from differnt rooms)