Arduinos talking to eachother through ethernet and to the world through internet

Hi!

I Have two arduinos (duemiglianove and mega 1280) with wiznet ethernet shields attached.

The mega one, is connected to a relay which is activated when the level of a tank is low and deactivated when the tank is full. It is also connected to several inputs and outputs (with relays) to do various tasks. The 2009 is connected to a relay which activates a huge underwater pump.

Both are loaded with a modified scetch from that site ( http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-IO/ ) which is great! It uses ajax and offers to me a reliable way of controling each arduino, and it reports to me the state of each i/o.

I want to make them "talk" to each other.

I modified a simple scetch from this forum in which the first arduino sends a udp packet to the other and the output is going high or low. It was nice but the UDP packets worked in direct link, but through three routers and repeaters they got "lost". I prefer to keep the tcp style as i can use the http site to control anything i want.

Could you point me to the most reliable direction? It needs to be reliable cause the pump is very strong. It can fill 5 and 10 ton tanks in minutes. I cant let it overflow cause an arduino had a bad mood.

I believe that i could make both arduinos servers and clients too so that both could read and control their i/o. But will this method perform?

Is it possible to combine webserver and simple udp control (and is simple udp control without handshake and encryption reliable for me)?

You don't say how you are getting the data from the Arduinos at the moment - perhaps you are viewing it through a browser on a PC?

If so (and indeed in any case) does the PC initiate the request for information from the Arduino or does the Arduino initiate the request (and if so how does that work at both ends).

To get the devices communicating I would be tempted to have a central PC app that controls both devices. It could collect data from one device and when appropriate pass it on to the other device. The PC app could also make the data available to others.

I guess it would be possible to have a third Arduino in place of the PC but if you don't want to use a PC I would think about using a Raspberry PI.

...R

I think the answer is yes. You can have one arduino act as server and client. I don't know how the timing works out though, say a client call is blocked waiting for server response and the server side on arduino fills up with requests from ajax (I assume you poll pretty frequently) then it's going to break your code. Yea, if you could get raspberry pi to work it will be a complete computer instead of arduino.

Thank you for your answers!

I use a pc to control them, or a tablet, or a mobile. Anything with a broswer.

This is the code from the site i linked in the first post

(the code does not fit in the <9500 characters limit…)

I modified it according to my needs but basicaly this is it. On both ends. Now i need to get them to talk to each other.

I dont want to use a PC to monitor them 24/7. This is the reason i used the arduinos (i have a large collection of phidgets and with visual studio i would have made a really beautiful control panel, if a PC was preferred).
The idea of a raspberry is very good. Are you suggesting to substitute the arduinos with raspberries, or to use a raspberry to collect, share the data and control them? And to do so, how would the raspberry collect and control them? Do you suggest the raspberry for its prossesing power over arduino?

The timing would be a problem. Although i do not poll often, only to check if everything is ok, temperatures, level, etc once or twice a day, i think that a delayed client call to stop the pump would be a disaster (if the delay was >30sec), or worse if the client call wouldn t get through.

I cant post the code the site does not let me post >9500 characters. You can read it in this link. It has the arduino file and the html file.

http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-IO/web_server_IO.zip

(from http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-IO/)

bleo8onhs:
Thank you for your answers!

I use a pc to control them, or a tablet, or a mobile. Anything with a broswer.

This is the code from the site i linked in the first post

(the code does not fit in the <9500 characters limit…)

I modified it according to my needs but basicaly this is it. On both ends. Now i need to get them to talk to each other.

I dont want to use a PC to monitor them 24/7. This is the reason i used the arduinos (i have a large collection of phidgets and with visual studio i would have made a really beautiful control panel, if a PC was preferred).
The idea of a raspberry is very good. Are you suggesting to substitute the arduinos with raspberries, or to use a raspberry to collect, share the data and control them? And to do so, how would the raspberry collect and control them? Do you suggest the raspberry for its prossesing power over arduino?

The timing would be a problem. Although i do not poll often, only to check if everything is ok, temperatures, level, etc once or twice a day, i think that a delayed client call to stop the pump would be a disaster (if the delay was >30sec), or worse if the client call wouldn t get through.

The reason a Raspberry Pi was mentioned is that it has the processing grunt and a full Linux environment to enable you to manage the stream of data

  1. UDP is not reliable and is not guaranteed delivery - you can solve this by sending info over and over until you get a response - so when the pump turns on it could run for (say) 1 minute and during that time send out a near continual stream screaming out - i am running the pump, i am running the pump - until it gets a response along the lines of - OK i hear you - tank is 1/2 full

After a minute it could turn the pump off and send this out to the remote unit

  1. The native TCP/IP support on the Arduino essentially wants you to be running a webserver - but that is just the examples as provided - you can provide any client/server (which is what you want to do) system - but it will be upto you to write it - and as you are asking the types of questions you are - i think this is out of your reach at the moment.

With your skill set that you have mentioned - i would look at getting one of the 2nd hand HTPC type units out there - put Windows on it and use your programming skills to run it and control the Arduinos.

Craig

I was suggesting raspberry pi to replace the ethernet module on arduino so arduino can do dedicated work and raspberry pi does the communication. It is a small computer you can remotely log on and see what's going on :)

For tank full task, make a switch to trip when it is too full to shut off the pump power, separate from all arduino stuff and let it shut off for at least a few minutes when triggered. Capacitors will do the timing.

craigcurtin: 1) UDP is not reliable and is not guaranteed delivery - you can solve this by sending info over and over until you get a response - so when the pump turns on it could run for (say) 1 minute and during that time send out a near continual stream screaming out - i am running the pump, i am running the pump - until it gets a response along the lines of - OK i hear you - tank is 1/2 full

After a minute it could turn the pump off and send this out to the remote unit

Craig

That is relatively easy. I already wrote it, but it needs some tweaking. In this case i have problems getting the packets through my network. And the screaming is easy, but there is no guaranty that the packets will arrive on time. There is a randomness that i cant have.

craigcurtin: 2) The native TCP/IP support on the Arduino essentially wants you to be running a webserver - but that is just the examples as provided - you can provide any client/server (which is what you want to do) system - but it will be upto you to write it - and as you are asking the types of questions you are - i think this is out of your reach at the moment.

With your skill set that you have mentioned - i would look at getting one of the 2nd hand HTPC type units out there - put Windows on it and use your programming skills to run it and control the Arduinos.

Craig

You will be amazed with my skill set, but you will hear that a lot. Anyway thats why i asked here...

So that anyone who does have the skill set at the moment (cause i may not have the tools now, but tomorrow?), to share an example of server/client code that talk to each other.

I just need the code for the following example:

Lets say that we have two arduinos with ethernet shields. The first one lights a led for one minute then one minute off, and states the state of the led on its html page (optional but very helpful). The second one lights its led when the led in the first one is off, and off when the first one is on, (it doesnt know for how long, it just waits for the state to change).

With that example, i will resolve everything. I will try to make it happen but my arduinos are already attached and i dont have any to experiment.. Thats why i asked in the first place. The parts for an htpc are already here and there but i dont want to use a pc. If i get to that, i will use an raspberry..

liudr: I was suggesting raspberry pi to replace the ethernet module on arduino so arduino can do dedicated work and raspberry pi does the communication. It is a small computer you can remotely log on and see what's going on :)

For tank full task, make a switch to trip when it is too full to shut off the pump power, separate from all arduino stuff and let it shut off for at least a few minutes when triggered. Capacitors will do the timing.

I have already a separate relay for the tank level, but i need the arduino to shut off the pump which is on a remote site!

thank you in advance!

bleo8onhs:

craigcurtin: 1) UDP is not reliable and is not guaranteed delivery - you can solve this by sending info over and over until you get a response - so when the pump turns on it could run for (say) 1 minute and during that time send out a near continual stream screaming out - i am running the pump, i am running the pump - until it gets a response along the lines of - OK i hear you - tank is 1/2 full

After a minute it could turn the pump off and send this out to the remote unit

Craig

Your easiest out of the box solution is to use nearbus.net - i am using it to pump data out of my system and upto google drive where it gets put into a spreadsheet - this act as a logger and graphic interface for me - the sync time is 10 sec (minimum cycle)

I have been running this for about a month now and it has not missed a beat

Craig

That is relatively easy. I already wrote it, but it needs some tweaking. In this case i have problems getting the packets through my network. And the screaming is easy, but there is no guaranty that the packets will arrive on time. There is a randomness that i cant have.

craigcurtin: 2) The native TCP/IP support on the Arduino essentially wants you to be running a webserver - but that is just the examples as provided - you can provide any client/server (which is what you want to do) system - but it will be upto you to write it - and as you are asking the types of questions you are - i think this is out of your reach at the moment.

With your skill set that you have mentioned - i would look at getting one of the 2nd hand HTPC type units out there - put Windows on it and use your programming skills to run it and control the Arduinos.

Craig

You will be amazed with my skill set, but you will hear that a lot. Anyway thats why i asked here...

So that anyone who does have the skill set at the moment (cause i may not have the tools now, but tomorrow?), to share an example of server/client code that talk to each other.

I just need the code for the following example:

Lets say that we have two arduinos with ethernet shields. The first one lights a led for one minute then one minute off, and states the state of the led on its html page (optional but very helpful). The second one lights its led when the led in the first one is off, and off when the first one is on, (it doesnt know for how long, it just waits for the state to change).

With that example, i will resolve everything. I will try to make it happen but my arduinos are already attached and i dont have any to experiment.. Thats why i asked in the first place. The parts for an htpc are already here and there but i dont want to use a pc. If i get to that, i will use an raspberry..

liudr: I was suggesting raspberry pi to replace the ethernet module on arduino so arduino can do dedicated work and raspberry pi does the communication. It is a small computer you can remotely log on and see what's going on :)

For tank full task, make a switch to trip when it is too full to shut off the pump power, separate from all arduino stuff and let it shut off for at least a few minutes when triggered. Capacitors will do the timing.

I have already a separate relay for the tank level, but i need the arduino to shut off the pump which is on a remote site!

thank you in advance!

Actually thinking about this - what is your failsafe for all this ?

You have a massive pump pushing huge amount of water - what happens if the router in the middle dies and is down so there is no comms between the units or out to the internet - what turns the pump off ?

Craig

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 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
//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>")); 

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

}

zoomkat:
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 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
//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(""));
            client.println(F(""));
            client.println(F(“Arduino GET test page”));
            client.println(F(""));
            client.println(F(""));

client.println(F(“

Zoomkat’s simple Arduino 1.0 button

”));

// DIY buttons
            client.println(F(“Pin5”));
            client.println(F(“ON”));
            client.println(F("OFF

"));

client.println(F(“Pin6”));
            client.println(F(“ON”));
            client.println(F("OFF

"));

client.println(F(“Pin7”));
            client.println(F(“ON”));
            client.println(F("OFF

"));

client.println(F(“Pin8”));
            client.println(F(“ON”));
            client.println(F("OFF

"));

client.println(F(“Pins”));
            client.println(F(" ALL ON"));
            client.println(F(" ALL OFF"));

client.println(F(""));

client.println(F(""));
            client.println(F(""));
          }

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

}

Very nice !!!

Craig

zoomkat:
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 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
//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(""));
           client.println(F(""));
           client.println(F(“Arduino GET test page”));
           client.println(F(""));
           client.println(F(""));

client.println(F(“

Zoomkat’s simple Arduino 1.0 button

”));

// DIY buttons
           client.println(F(“Pin5”));
           client.println(F(“ON”));
           client.println(F("OFF

"));

client.println(F(“Pin6”));
           client.println(F(“ON”));
           client.println(F("OFF

"));

client.println(F(“Pin7”));
           client.println(F(“ON”));
           client.println(F("OFF

"));

client.println(F(“Pin8”));
           client.println(F(“ON”));
           client.println(F("OFF

"));

client.println(F(“Pins”));
           client.println(F(" ALL ON"));
           client.println(F(" ALL OFF"));

client.println(F(""));

client.println(F(""));
           client.println(F(""));
         }

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 actually found this last night when i was googling about it. If you google arduino server client same time eventualy you will find yourself in this http://forum.arduino.cc/index.php/topic,134966.0.html
Havent read it yet …

Thank you! (You are the driver of the s2000 in the video?)

Craig my failsafes are, water alarms(sirens) in case of overflow, time relays which kill the pump after 20 minutes, flow sensors all over the network, amp sensors all over the network, and thermal relays to kill the pump if they indicate a spike in amperage. All these have and a gsm notification. I actually had a very long and expensive cable to turn the pump on or off, but road works destroyed it.

Anyway, i will look at the code and post the results. And i will try the nearbus too!

Im in love.. http://forum.arduino.cc/index.php?topic=146866.0

Thank you! (You are the driver of the s2000 in the video?)

No, the guy in the video actually knows how to drive! :slight_smile:

zoomkat:
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 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
//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(""));
            client.println(F(""));
            client.println(F(“Arduino GET test page”));
            client.println(F(""));
            client.println(F(""));

client.println(F(“

Zoomkat’s simple Arduino 1.0 button

”));

// DIY buttons
            client.println(F(“Pin5”));
            client.println(F(“ON”));
            client.println(F("OFF

"));

client.println(F(“Pin6”));
            client.println(F(“ON”));
            client.println(F("OFF

"));

client.println(F(“Pin7”));
            client.println(F(“ON”));
            client.println(F("OFF

"));

client.println(F(“Pin8”));
            client.println(F(“ON”));
            client.println(F("OFF

"));

client.println(F(“Pins”));
            client.println(F(" ALL ON"));
            client.println(F(" ALL OFF"));

client.println(F(""));

client.println(F(""));
            client.println(F(""));
          }

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

}

One question mr zoomcat (or enyone else), because i cant find info about the get method. I understand the usage in your example and in all the other with search commands to google etc.

If i want a client to connect to a local site (or an arduino webserver) and print in serial the entire html (so that i can debug/find what to ask), i enter

if (client.connect(192.168.2.3,6785)>0) {

client.println(“GET //what to put here// HTTP/1.0”);
client.println();

what should i enter after the get?

Is it possible for a arduino client to connect to a (different) arduino webserver html page? I ask cause every time i tried i get connection failed.

i have an arduino with a webserver, with a index.htm file in sd. The server is at 192.168.2.3:6785

i try to connect with another arduino with a client code like that

#include <SPI.h> /
#include <Ethernet.h>
byte mac[] = { 0x02, 0xF3, 0xA1, 0x5B, 0xB0, 0x68 };
IPAddress serverName(192,168,2,3);
EthernetClient client;
void setup()
{
Serial.begin(9600);
if(Ethernet.begin(mac) == 0) { 
Serial.println("Failed to configure Ethernet using DHCP");
while(true) 
;
}
delay(1000); 
Serial.println("connecting...");
}
void loop()
{
if (client.connect(serverName,6785)>0) {
Serial.println(" connection established");
//client.println("GET  HTTP/1.0");
//client.println();
}
else {
Serial.println(" connection failed");
}

client.stop();
client.flush();
}

I keep geting connection failed.

bleo8onhs:
One question mr zoomcat (or enyone else), because i cant find info about the get method. I understand the usage in your example and in all the other with search commands to google etc.

If i want a client to connect to a local site (or an arduino webserver) and print in serial the entire html (so that i can debug/find what to ask), i enter

if (client.connect(192.168.2.3,6785)>0) {

client.println(“GET //what to put here// HTTP/1.0”);
client.println();

what should i enter after the get?

Is it possible for a arduino client to connect to a (different) arduino webserver html page? I ask cause every time i tried i get connection failed.

i have an arduino with a webserver, with a index.htm file in sd. The server is at 192.168.2.3:6785

i try to connect with another arduino with a client code like that

#include <SPI.h> /

#include <Ethernet.h>
byte mac = { 0x02, 0xF3, 0xA1, 0x5B, 0xB0, 0x68 };
IPAddress serverName(192,168,2,3);
EthernetClient client;
void setup()
{
Serial.begin(9600);
if(Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
while(true)
;
}
delay(1000);
Serial.println(“connecting…”);
}
void loop()
{
if (client.connect(serverName,6785)>0) {
Serial.println(" connection established");
//client.println(“GET  HTTP/1.0”);
//client.println();
}
else {
Serial.println(" connection failed");
}

client.stop();
client.flush();
}




I keep geting connection failed.

Have you tried this from another PC to hit the webpage ?

Are you sure that you are putting the server on the correct port that you think you are using ?

Post up your server code also

What is the IP address that your client gets from the DHCP Server ? Do some serial printing so we can see those details also

Craig

Have you tried the below code with the space and without the >0 in it?

if (client.connect(serverName, 6785)>0)

I did and i will post what craig asked later today, but i think my problem is my ethernet shield. I tried the exact same code that i use on my server with an index.htm at the sd and when i call the page i get one character repeating all the time. If i use a webserver example it works, if i try the sd examples they work, my server code does not. Tried on the same shield over an uno and mega, and with an 2gb and 8gb sd. Same results.

i am loading this sketch to the arduino uno r3 with wiznet ethernet shield and sd attached with index.htm

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

#define REQ_BUF_SZ   60


byte mac[] = { 0x8A, 0xF1, 0x24, 0xAA, 0x7F, 0x59 };
IPAddress ip(192, 168, 2, 126); 
EthernetServer server(9999);  
File webFile;              
char HTTP_req[REQ_BUF_SZ] = {0}; 
char req_index = 0;            
boolean LED_state[4] = {0}; 

void setup()
{
 
    pinMode(10, OUTPUT);
    digitalWrite(10, HIGH);
    
    Serial.begin(9600);       
  
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;  
    }
    Serial.println("SUCCESS - SD card initialized.");
  
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return; 
    }
    Serial.println("SUCCESS - Found index.htm file.");
   
    pinMode(2, INPUT);
    pinMode(3, INPUT);
    pinMode(5, INPUT);
  
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    
    Ethernet.begin(mac, ip); 
    server.begin();        
}

void loop()
{
    EthernetClient client = server.available();  

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   
                char c = client.read(); 
                
                if (req_index < (REQ_BUF_SZ - 1)) {
                    HTTP_req[req_index] = c;         
                    req_index++;
                }
                
                if (c == '\n' && currentLineIsBlank) {
                    
                    client.println("HTTP/1.1 200 OK");
                    
                    if (StrContains(HTTP_req, "ajax_inputs")) {
                       
                        client.println("Content-Type: text/xml");
                        client.println("Connection: keep-alive");
                        client.println();
                        SetLEDs();
                        
                        XML_response(client);
                    }
                    else {  // web page request
                       
                        client.println("Content-Type: text/html");
                        client.println("Connection: keep-alive");
                        client.println();
                        
                        webFile = SD.open("index.htm");      
                        if (webFile) {
                            while(webFile.available()) {
                                client.write(webFile.read()); // send web page to client
                            }
                            webFile.close();
                        }
                    }
                   
                    Serial.print(HTTP_req);
                    
                    req_index = 0;
                    StrClear(HTTP_req, REQ_BUF_SZ);
                    break;
                }
                
                if (c == '\n') {
                   
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                  
                    currentLineIsBlank = false;
                }
            } 
        } 
        delay(1);      
        client.stop();
    } 
}


void SetLEDs(void)
{
   
    if (StrContains(HTTP_req, "LED1=1")) {
        LED_state[0] = 1;  // save LED state
        digitalWrite(6, HIGH);
    }
    else if (StrContains(HTTP_req, "LED1=0")) {
        LED_state[0] = 0;  // save LED state
        digitalWrite(6, LOW);
    }
   
    if (StrContains(HTTP_req, "LED2=1")) {
        LED_state[1] = 1;  // save LED state
        digitalWrite(7, HIGH);
    }
    else if (StrContains(HTTP_req, "LED2=0")) {
        LED_state[1] = 0;  // save LED state
        digitalWrite(7, LOW);
    }
 
    if (StrContains(HTTP_req, "LED3=1")) {
        LED_state[2] = 1;  // save LED state
        digitalWrite(8, HIGH);
    }
    else if (StrContains(HTTP_req, "LED3=0")) {
        LED_state[2] = 0;  // save LED state
        digitalWrite(8, LOW);
    }
  
    if (StrContains(HTTP_req, "LED4=1")) {
        LED_state[3] = 1;  // save LED state
        digitalWrite(9, HIGH);
    }
    else if (StrContains(HTTP_req, "LED4=0")) {
        LED_state[3] = 0;  // save LED state
        digitalWrite(9, LOW);
    }
}


void XML_response(EthernetClient cl)
{
    int analog_val;            
    int count;               
    int sw_arr[] = {2, 3, 5};  
    
    cl.print("<?xml version = \"1.0\" ?>");
    cl.print("<inputs>");
    // read analog inputs
    for (count = 2; count <= 5; count++) { // A2 to A5
        analog_val = analogRead(count);
        cl.print("<analog>");
        cl.print(analog_val);
        cl.println("</analog>");
    }
    
    for (count = 0; count < 3; count++) {
        cl.print("<switch>");
        if (digitalRead(sw_arr[count])) {
            cl.print("ON");
        }
        else {
            cl.print("OFF");
        }
        cl.println("</switch>");
    }
    
    cl.print("<LED>");
    if (LED_state[0]) {
        cl.print("checked");
    }
    else {
        cl.print("unchecked");
    }
    cl.println("</LED>");
   
    cl.print("<LED>");
    if (LED_state[1]) {
        cl.print("checked");
    }
    else {
        cl.print("unchecked");
    }
     cl.println("</LED>");
    
    cl.print("<LED>");
    if (LED_state[2]) {
        cl.print("on");
    }
    else {
        cl.print("off");
    }
    cl.println("</LED>");
   
    cl.print("<LED>");
    if (LED_state[3]) {
        cl.print("on");
    }
    else {
        cl.print("off");
    }
    cl.println("</LED>");
    
    cl.print("</inputs>");
}


void StrClear(char *str, char length)
{
    for (int i = 0; i < length; i++) {
        str[i] = 0;
    }
}


char StrContains(char *str, char *sfind)
{
    char found = 0;
    char index = 0;
    char len;

    len = strlen(str);
    
    if (strlen(sfind) > len) {
        return 0;
    }
    while (index < len) {
        if (str[index] == sfind[found]) {
            found++;
            if (strlen(sfind) == found) {
                return 1;
            }
        }
        else {
            found = 0;
        }
        index++;
    }

    return 0;
}

and what i get from the serial whenever i m trying to connect

Initializing SD card…
SUCCESS - SD card initialized.
SUCCESS - Found index.htm file.
GET / HTTP/1.1
Host: 192.168.2.126:9999
User-Agent: MozilGET / HTTP/1.1 (second atempt)
Host: 192.168.2.126:9999
User-Agent: MozilGET / HTTP/1.1 (third)
Host: 192.168.2.126:9999
User-Agent: MozilGET / HTTP/1.1 (…)
Host: 192.168.2.126:9999
User-Agent: Mozil

faulty ethernet shield?

  1. on your suggestion, without the >0 i got connection established, with the same arduino uno and ethernet shield as the above. Is it possible to have a faulty sd socket?

now can you tell me what to write in the get line to read the code from the page at my serial to find what should i seek for each i/o?