Pages: [1] 2   Go Down
Author Topic: Twittering with Arduino+Ethernet Shield  (Read 2040 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post the code but change the twitter token.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()
{
}
Logged

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-confuse
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11164
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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);
  // ..
Logged

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The posting is okay....  smiley-grin  smiley-grin  smiley-grin
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?
Logged

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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....
Code:
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; 
   }
}

Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11164
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1] 2   Go Up
Jump to: