Twittering with Arduino+Ethernet Shield

Hello,

I am trying to do a simple post on my twitter account using the twittering example 'SimplePost'. I keep getting this error:

connecting ...
failed : code 0

I am using arduino 1.0.3 and have done the necessary changes to the code which are: mac address, ip address, twitter token.

The internet connection of my arduino is fine since i am able to send email to my gmail account.

Any suggestion on what i mite be overlooking...

Thanks

Post the code but change the twitter token.

Sorry, here is the code:

#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>

// Ethernet Shield Settings
byte mac[] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX };

// If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later).
byte ip[] = { xx, xx, xx, xx };

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("XXXXXXXXXXXXXXXXXX");

// Message to post
char msg[] = "Hello, World! I'm Arduino!";

void setup()
{
delay(1000);
Ethernet.begin(mac, ip);
// or you can use DHCP for autoomatic IP address configuration.
// Ethernet.begin(mac);
Serial.begin(9600);

Serial.println("connecting ...");
if (twitter.post(msg)) {
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait();

if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}

} else {
Serial.println("connection failed.");
}
}

void loop()
{
}

Is the IP address that you are using valid for your network? What Arduino board are you using? How is the Ethernet shield connected to the Internet?

Try the following and see if you get more information.

// ...
int status = twitter.wait(&Serial);
// ...

The IP address is valid. I have used it to send emails to my gmail account. I am using arduino UNO and the shield is connected through RJ45 cable.
Inclusion of '&Serial' as below doesnt affect the output anyway.
int status = twitter.wait(&Serial);

The statement ' if (twitter.post(msg))' returns true which means it is able to establish connection to twitter.
The problem lies in the posting process. Should return HTTP status code in response from Twitter, e.g. 200 - OK. Only available after posting the message is done. But in my case the status is 0 rather than 200. :~

The code you're getting is supposed to be an HTTP status code but clearly isn't; presumably the connection has closed or failed without providing an HTTP response. I suggest you pass in a Print* argument to wait() so that you can see what is being received. You should normally see something line "HTTP/1.1 200" but evidently that isn't happening in this case.

So when you changed the wait statement the serial output didn't change, right?

At this point, the only thing I can think of this that DNS isn't working for the board. Add the following "print" statements and report back on the result.

void setup()
{
  delay(1000);
  Ethernet.begin(mac, ip);
  // or you can use DHCP for autoomatic IP address configuration.
  // Ethernet.begin(mac);
  Serial.begin(9600);
  Ethernet.localIP.printTo(Serial);
  Ethernet.subnetMask.printTo(Serial);
  Ethernet.gatewayIP.printTo(Serial);
  Ethernet.dnsServerIP.printTo(Serial);
  // ..

What Arduino are you using? If your using the Uno there is not enough sram. I had to use my Atmega2560 board and the example worked fine.
Don

I tried with Atmega2560 board and still no luck.
The result is:

117.17.80.199
255.255.255.0
117.17.80.1
117.17.80.1
connecting ...
failed : code 0

I don’t know much about networks, but is it okay to have the same IPs for gateway and dns as shown in the result. I used this statement: Ethernet.begin(mac, ip);
When I included dns i.e Ethernet.begin(mac, ip, dns); the dns IP changes:

117.17.80.199
255.255.255.0
117.17.80.1
6.0.0.0
connecting ...
failed : code 0

In either case the IP of dns server is different compared to my desktop computer.
Is there way to declare the dns IP?

I compared my working code with yours, the only difference I see I'm using DHCP to get a ip address it takes about 5 secs to get a ip from my router. Posting to Twitter is really fast. If you get a error 403
you send a duplicate tweet. I copied this right from my working sketch. If you want to follow me on twitter @AlertsChicago I'm posting weather and emergency broadcast alerts here in Chicago.
Don

void setup()
{
  delay(1000);
  
  if (!Ethernet.begin(mac))  // Try to connect using DHCP
    Ethernet.begin(mac, ip); // Try to connect using a Static IP address.
  delay(100);
  
  Serial.begin(9600);
  delay(100);
  
  Serial.print(F("Connected to IP address: "));  // Print out the IP address.
  for (int i = 0; i < 4; i++)
    {
      Serial.print(Ethernet.localIP()[i], DEC);
      Serial.print("."); 
    }
  Serial.println();
  Serial.println();
}

I copied this right from my working sketch.

if you want to follow me on twitter @AlertsChicago

I am using my universities network so due to restrictions, I cannot use DHCP to automatically assign the IP hence iam doing it manually.
Ok i declared the DNS server IP:

IPAddress myDns(168, 126, 63, 1);
Ethernet.begin(mac, ip, myDns);

It gives the following result:

117.17.80.199
255.255.255.0
117.17.80.1
168.126.63.1
connecting ...
HTTP/1.0 403 Forbidden
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Vary: Accept-Encoding
Date: Thu, 24 Jan 2013 01:19:45 GMT
Server: Google Frontend

Error 403 - Status is a duplicate.
failed : code 403

what might be wrong now?

did anything post to twitter? if you use those setting in your computer can you get on the internet
get to twitter or google, yahoo? to see if they really would maybe ping, traceroute
403 means you are trying to send a duplicate tweet...
Don

The posting is okay.... :smiley: :smiley: :smiley:
Thought nothing was being posted by looking at errors i was getting.
Its working now...Thanks mate.
But m worried about the errors i am getting...if i ignore it, will it b fine?

I bet the after the Arduino is completed programing it resets runs your setup code posting the message.
Then you fire up the serial monitor and the board resets again, you double tweeted.

If you remove the posting of the tweet from setup() function and do something like this in loop() you can control it yourself using the serial monitor put a "t" in the line and press send....

void loop()
{
   byte inChar;
   
   inChar = Serial.read();
   delay(100);
   if(inChar == 't')
   {
        Serial.println("connecting ...");
       if (twitter.post(msg)) 
        {
           // Specify &Serial to output received response to Serial.
          // If no output is required, you can just omit the argument, e.g.
          // int status = twitter.wait();
          int status = twitter.wait();

          if (status == 200) {
          Serial.println("OK.");
        }
      else
        {
           Serial.print("failed : code ");
           Serial.println(status);
        }
    
     inChar = 0;  
   }
}

Abelavit:
HTTP/1.0 403 Forbidden
what might be wrong now?

That means you've succeeded in accessing the network but the server didn't like your tweet. It means you've solved the problem. Now if you want to sends tweets for real, you need to make them unique.

Oh great...so it was posting twice.
Its working perfectly now.
Had to do the posting part in void loop.
Thanks for your help guys.

Peter
I think that after the Ardunio programs it resets and runs his code. Then he is watching it via serial monitor to debug and that is why he is double posting or tweeting that will give you a 403 since he is sending the same message. He will need to change his tweet some too since he can't keep sending the same thing.
Maybe read millis

unsigned long time;
 
time = millis();
  //prints time since program started
 client.println(time);

Oh great...so it was posting twice.
Its working perfectly now.
Did the posting bit in void loop
Thanks guys for your help.

Abelavit:
I am using my universities network so due to restrictions, I cannot use DHCP to automatically assign the IP hence iam doing it manually.
Ok i declared the DNS server IP:

IPAddress myDns(168, 126, 63, 1);
Ethernet.begin(mac, ip, myDns);

It gives the following result:

117.17.80.199
255.255.255.0
117.17.80.1
168.126.63.1
connecting ...
HTTP/1.0 403 Forbidden
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Vary: Accept-Encoding
Date: Thu, 24 Jan 2013 01:19:45 GMT
Server: Google Frontend

Error 403 - Status is a duplicate.
failed : code 403

what might be wrong now?

There maybe a problem in the future if some other computer starts using that assigned IP assigned. It would be nice if the university would give you another IP address for your second computer.

And what is the code now?