Archive for Fevereiro 23rd, 2011

Fun with the Arduino Ethernet Shield

Quarta-feira, Fevereiro 23rd, 2011

I’ve been using the Arduino platform as a means to develop some of the devices I’ve been needing, from the most simple (my daughter’s color-changing nightlight) to the most complex (a stand-alone, fault-tolerant, inexpensive telemetrics system). Somewhere along the way, the Ethernet shield come into the picture. To be a part of my designs, I had to tweak a few things on the shield:

* The reset is not dependable; many times the shield would simply not startup as expected, and the Arduino lost connection to the outside World. Solution: I lifted the shield’s reset pin (so that it does not enter it’s connector on the Arduino) and connected pin 8 to the shield’s reset line. Like this I can programmatically reset the shield on every boot, and every time I loose a connection to the Internet.

int rebootEthShield()
{
Serial.println("Rebooting ethernet shield...");
pinMode(8, OUTPUT);
digitalWrite(8, HIGH);
delay(50);
digitalWrite(8, LOW);
delay(50);
digitalWrite(8, HIGH);
delay(100);

Ethernet.begin(mac, ip, gateway, netmask);
server.begin();
}

You can obviously choose any other output pin.

* I test the Internet connection by connecting to a server on the outside World (i.e. my server). But since this is hardly a static World, and IPs are apparently volatile entities, I needed to have a means to change the test IP. The problem is, the Arduino does not really support object instantiation. So, you create a client socket like so:

byte cl[] = { 192, 168, 1, 1 };
Client clienteLink(cl, 80);

And that’s it. You can no longer change de IP throughout execution. :( To be able to do it, I had to add a method to the Client class: reconfigure(). On the Client.h file, add:

void reconfigure(uint8_t *, uint16_t);

On the Client.cpp file, add:

void Client::reconfigure(uint8_t *ip, uint16_t port) {
_ip = ip;
_port = port;
_sock = MAX_SOCK_NUM;
}

This is on Arduino Alpha 022 (older versions need slightly different code, but it’s very simple).

Now, all I need to do is close the Client before issuing a reconfigure(). Then I can try to connect to another server. There may be a simpler way to do this, but I couldn’t find it. If you do, please post! :)

Have fun!