Repeat calls with Ethernet shield

Hi

I’m using some basic code to pint twitter. I’m using a duecemilia and an arduino ethernet shield.

The code below is copied from dubdubdub.mats-vanselow.de/2009/02/08/arduino-lernt-twittern/

This works absolutely fine for me - the first time. But if I try to call the sendtwitterupdate() function again, it fails to connect. I’ve tried this with several different sketches and get exactly the same problem: first connection is fine but any subsequent ones fail. Do I have to somehow clear the ethernet shield output before requesting a connection again or something?

Thanks in advance

Mike

#include <Ethernet.h>
/*
Twitter Modul
2009 by Mats Vanselow
Mats-Vanselow.de
*/

//////////// EINSTELUNGEN ///////////////////

// Etherneteinstellungen für das Arduino Board //
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip = { 192, 168, 0, 99 };
byte gateway = { 192, 168, 0, 1 };
byte subnet = { 255, 255, 255, 0 };

// Twitter Settings //
byte server = { 128, 121, 146, 100 }; // IP von Twitter (default: 128.121.146.100)
#define TWITTERURL “/statuses/update.xml” // URL zum Update Script
#define TWITTERUSERNAMEPW “ABC=” // Base64 enkodet USERNAME:PASSWORT

Client client(server, 80);

void setup()
{
Ethernet.begin(mac, ip, gateway, subnet); // Ethernet initalisieren
Serial.begin(9600); // Serielle Verbindung für Statusmeldungen
delay(1000); // Kurz warten, um serielle Verbindung zu starten

sendTwitterUpdate(“Und jetzt kann auch das Arduino Board twittern!”); // Nachricht versenden
}

void loop()
{
fetchTwitterUpdate();
}

/// Functions ////

void sendTwitterUpdate(char* tweet) // Nachricht an Twitter überitteln
{
Serial.println(“connecting…”);

if (client.connect()) {
Serial.println(“connected”);

client.print(“POST “);
client.print(TWITTERURL);
client.println(” HTTP/1.1”);
client.println(“Host: twitter.com”);
client.print("Authorization: Basic ");
client.println(TWITTERUSERNAMEPW);
client.print(“Content-Length: “);
client.println(9+strlen(tweet));
client.println(””);
client.println(“status=”);
client.println(tweet);
Serial.println(“twitter message send”);
} else {
Serial.println(“connection failed”);
}

void fetchTwitterUpdate() // Rückmeldung von Twitter auslesen
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
Serial.println();
Serial.println(“disconnecting.”);
client.stop();
}
}

this looks like a bug in arduino 0013, check the bugs forum, there's a topic about this problem with a solution in there (sorry, not allowed to post links)

or check this direct link:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234006420/0

@wwolkers - you're a star! Thank you loads - had me foxed, but as I'm a noob, I assumed it was me. Cheers Mike

Nice to see my example code here :slight_smile:
I will add an advice to the blog post about the bug in Arduino 0013.