Using the Nuelectronics ethernet shield as a client

Document status: Updated on the 28 August 2010:  by Trystan Lea 

The nuelectronics ethernet shield makes it possible to add web connectivity to your project for a nice £12.50. Nuelectronics give a good guide on setting up the shield as a server and client here and here, but the code is somewhat hard to follow, after coming across Simon Monks simplified server library here I was inspired to have a go at making a simplified client library. 

Like Simon Monks server library the client library sits like another layer above the nuelectronics library so you will need to download and place both libraries in your Arduino/Libraries folder. Credit must go also to nuelectronics for much of the code in the library I have just rearranged it, added a few things and packaged it in the library.

Version 2 is a bit of a rebuild, It includes better verbose debugging and is more similar in the way it is used to the official ethernet library, so if you already have code using the main ethernet library it should be straightforward to port over.  I will document more of it when I get time. Let me know which one works best.

Download the NUClient library: NUClient2.0.tar.gz 

Download the nuelectronics etherShield library here.

Example Sketch

#include "etherShield.h"

#include "NUClient.h"

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

byte ip[] = {192,168,1,44};

byte gateway[] = { 192, 168, 1, 254 };

byte server[] = { 192,168,1,71 };

int port = 80;

NUClient client(mac,ip,gateway,server,80);

void setup()

{

  Serial.begin(9600);

  client.init();

  //Serial.print output of whats happening.

  client.debug();

  //Experiment with different timeouts

  //from left ot right

  //arp timeout

  //synack timeout

  //pshack timeout

  //finacktimeout

  client.setTimeouts(100,100,100,100);

}

void loop()

{

  if(client.connect())

  {

    //Send Data to server

    client.print("PUT /post.php?P=");

    client.print(100);

    client.print(" HTTP/1.0\r\n");

    client.print("Host: serverip\r\n");

    client.print("User-Agent: AVR ethernet\r\n");

    client.print("Accept: text/html\r\n");

    client.print("Keep-Alive: 300\r\n");

    client.print("Connection: keep-alive\r\n\r\n");

    client.stop();

  }

  delay(5000);

}

For information on setting up the server side of things have a look here

Pachube Example

 

#include "etherShield.h"

#include "NUClient.h"


//Set these to your feed ID and pachube api key

#define SHARE_FEED_ID              9152     // this is your Pachube feed ID that you want to share to

#define PACHUBE_API_KEY            "your api key" // fill in your API key 


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

byte ip[] = { 192, 168, 1, 44 };

byte gateway[] = { 192, 168, 1, 254 };

byte server[] = { 209,40,205,190 }; //Pachube.com

int port = 80;


NUClient client(mac,ip,gateway,server,80);


//Some variables to send

double valA = 10.23;

double valB = 22.34;


int content_length;


void setup()

{

  

  Serial.begin(9600);

  client.init();

  

  //Serial.print output of whats happening.

  client.debug();

  

  //Experiment with different timeouts

  //from left ot right

  //arp timeout

  //synack timeout

  //pshack timeout

  //finacktimeout

  client.setTimeouts(100,100,100,100);

}


void loop()

{

  

  if(client.connect())

  {

    //This seems to work set as 1?

    content_length = 1;


    // 1) Send feed id, pachube api key 

    client.print("PUT /api/");

    client.print(SHARE_FEED_ID);

    client.print(".csv HTTP/1.1\nHost: pachube.com\nX-PachubeApiKey: ");

    client.print(PACHUBE_API_KEY);

    client.print("\nUser-Agent: NUClient Arduino");

    client.print("\nContent-Type: text/csv\nContent-Length: ");

    client.print(content_length);

    client.print("\nConnection: close\n\n");

    

    // 2) Send the data - comma seperated

    client.print(valA);

    client.print(",");

    client.print(valB);

    

    client.stop();

  }

  

  delay(5000);

}

 

Version 1 files

NUClient.tar.gz

NUClientPachube.tar.gz

NUClientExample.tar.gz


Troubleshooting and connection problems

If you have problems with connecting to your server, here are some suggestions:

Turn on verbose debug mode, place client.debug(); in void setup. 

With debug mode on you may see that the problem is continuous timeouts. If it is try different timeout values.

Let me know if there is anything here I can clarify or if you have any ideas on how the library could be arranged better, suggestions are very welcome as always.

Hi Trystan, Thanks for this

Hi Trystan,

Thanks for this contribution, I succeed sending data to Pachube using your demo sketch.
http://www.pachube.com/feeds/9555

I use version 1 of the nuclient.

After that, we checked the data from the ethershield and notice a lot of ARP command in the message (every 300ms) overloading our router :)

Maybe this trouble is fixed in the NUClient release 2 ?

I have some compile problems

I have some compile problems with youre version 2.0 and the example code.
The done method/ attribute is not included in youre 2.0 version of the nuclient.

arduino compile error
In function 'void loop()':
error: 'class NUClient' has no member named 'done'

figured it out .. example

figured it out .. example code works with version 1 of the nuclient and not with version 2

 Yes thats the one, sorry its

 Yes thats the one, sorry its not very clear, I intend to rewrite the page to make things clearer once I get some more time. Did the library work for you?