Arduino Tutorials – Chapter 30 – twitter

Learn how to tweet from your Arduino.

This is chapter thirty of our huge Arduino tutorial seriesUpdated 04/12/2013

In this article you will learn how to send messages from an Ethernet-enabled Arduino to twitter. For the uninitiated who may be thinking “what is all this twitter nonsense about?”, twitter is a form of microblogging. 

You can create a message with a maximum length of 140 characters, and broadcast this on the twitter service. For people to receive your messages (or tweets) they also need to be a member of twitter and choose to subscribe to your tweets.

Generally people will use the twitter service using one of two methods: either using a web browser, or using the twitter application on a smartphone or tablet computer. For example, here is a typical web browser view:

twitter web browser

… and here is an example of a twitter application running on an Android OS smartphone:

twitter android

The neat thing about twitter on a mobile device is that if your username is mentioned in a tweet, you will be notified pretty well immediately as long as you have mobile data access. More on that later. In some areas, you can set twitter to send tweets from a certain user to your mobile phone via SMS – however if doing so be careful to confirm possible charges to your mobile phone account.

Finally, if you are worried about privacy with regards to your tweets, you can set your account to private and only allow certain people to follow your tweets.

So let’s get started.

First of all – you will need a twitter account. If you do not have one, you can sign up for one here. If you already have a twitter account, you can always open more for other uses – such as an Arduino.

For example, my twitter account is @tronixstuff, but my demonstration machine twitter account is @tronixstuff2. Then I have set my primary account to follow my machine’s twitter account.

Now log into twitter with using the account you will have for your Arduino and visit this page and get yourself a token by following the Step One link. The process will take you through authorising the “tweet library” page to login to your twitter account – this is ok. It will then present you with a long text called a “token”, for example:

twitter oauth token

Save your token somewhere safe, as you will need to insert it into your Arduino sketch. Finally, don’t give it to others as then they will be able to post onto twitter using your account. Next, follow step two from the same page – which involves download and installation of the required Arduino library.

Now for the hardware.

You will need an Arduino Uno or compatible board with an Ethernet shield that uses the W5100 Ethernet controller IC (pretty much all of them) – or consider using a Freetronics EtherTen – as it has everything all on the one board, plus some extras:

Freetronics EtherTen

Furthermore you will need to power the board via the external DC socket – the W5100 IC uses more current than the USB power can supply. A 9V 1A plug pack/wall wart will suffice. Finally it does get hot – so be careful not to touch the W5100 after extended use. In case you’re not sure – this is the W5100 IC:

Wiznet W5100If you’re looking for an Arduino-twitter solution with WiFi, check out the Arduino Yún tutorials.

From this point it would be a good idea to check your hardware is working. To do so, please run the webserver example sketch as explained in chapter sixteen (Ethernet). While you do that, we’ll have a break…

Lop Buri Thailand

Sending your first tweet

If you want your Arduino to send a simple tweet consider the following sketch. We have a simple function tweet() which simply sends a line of text (which has a maximum length of 140 characters). Don’t forget to update your IP address, MAC address and token:

You can check the status of the tweeting via the serial monitor. For example, if the tweet was successful you will see:

Arduino twitter success serial monitor

However if you try to send the same tweet more than once in a short period of time, or another error takes place – twitter will return an error message, for example:

Arduino twitter failure duplicate serial monitor

And finally if it works, the tweet will appear:

Arduino twitter success web

Previously we mentioned that you can be alerted to a tweet by your mobile device. This can be done by putting your own twitter account name in the contents of the tweet.

For example – my normal twitter account is @tronixstuff. If I put the text “@tronixstuff” in the text tweeted by my Arduino’s twitter account – the twitter app on my smartphone will let me know I have been mentioned – as shown in the following video:

You may have noticed in the video that a text message arrived as well – that service is a function of my cellular carrier (Telstra) and may not be available to others. Nevertheless this is a neat way of getting important messages from your Arduino to a smart phone or other connected device.

Sending data in a tweet

So what if you have  a sensor or other device whose data you want to know about via twitter? You can send data generated from an Arduino sketch over twitter without too much effort.

In the following example we’ll send the value from analogue pin zero (A0) in the contents of a tweet. And by adding your twitter @username you will be notified by your other twitter-capable devices:

You may have noticed a sneaky sprintf function in void loop(). This is used to insert the integer analogZero into the character array tweetText that we send with the tweet() function. And the results of the example:

Arduino twitter success web data

So you can use the previous sketch as a framework to create your own Arduino-powered data twittering machine. Send temperature alerts, tank water levels, messages from an alarm system, or just random tweets to your loved one.

Conclusion

So there you have it, another useful way to send information from your Arduino to the outside world. Stay tuned for upcoming Arduino tutorials by subscribing to the blog, RSS feed (top-right), twitter or joining our Google Group. Big thanks to @neocat for their work with the twitter  Arduino libraries.

And if you enjoyed the tutorial, or want to introduce someone else to the interesting world of Arduino – check out my book (now in a third printing!) “Arduino Workshop” from No Starch Press.

tronixstuff

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

The following two tabs change content below.

John Boxall

Founder, owner and managing editor of tronixstuff.com.

13 Responses to “Arduino Tutorials – Chapter 30 – twitter”

  1. Jack says:

    why only be updated on the first try?
    then failed connection

  2. dan says:

    Hi this is super interesting, can the arduino read tweets for remote control of the arduino? now that would be cool.
    Thanks, great blog well done

  3. Ali says:

    Thank you for your effort.

    Yesterday, I was able to send tweets however today I can not and it gives me the following message in the serial:

    connecting …
    failed : code 0

    I did not change anything in the code except the tweet in order to avoid error 403

    I am using Arduino mega 2560 with Arduino Ethernet Shield

    • John Boxall says:

      Hard to say without being there. Just double-check everything or if all else fails just start again with the examples.

      • Felipe says:

        John same here.

        connecting …
        failed : code 0

        My guess, Twitter changed something in their code.
        I think this library is not able to send tweets anymore.
        Somebody need review the library code.

      • John Boxall says:

        It worked on 4/12/2013 after the twitter changes. I’ll try it again in the next few days.

      • Felipe says:

        John no longer need. I managed to send a tweet.
        I was having problems with permissions internet.
        I packed everything and is now working again.
        Thank you very much.

      • John Boxall says:

        Ah good news, thanks for letting me know.

  4. ali meq says:

    I want to tweet corresponding to a situation which means I need to tweet many times. I did like this:
    void loop()
    {
    checkState();
    }
    void tweet(){
    Serial.println(“Connecting to Twitter”);
    if(twitter.post(“PV is greater than 5″)){
    Serial.print(dataString);
    int status = twitter.wait();
    if (status==200){
    Serial.println(“Successful”);
    } else {
    Serial.println(“Tweet failed.”);
    Serial.println(status);
    }
    } else {
    Serial.println(“Connection to Twitter failed”);
    }
    Serial.println(“30 Seconds timeout started”);
    delay(3000);
    }
    void checkState(){
    // read PV voltage and append to the string:
    char analogPin = 0;
    char PV = analogRead(analogPin);
    dataString= PV;
    if (PV>=1023){
    Serial.println(PV);
    tweet();
    delay(2000);
    }
    }
    But it doesn’t work. Do you have any suggestion?

  5. ali meq says:

    Hi again,
    I did the same as you mentioned but it just tweets once. what should I do?

  6. Rajarshi says:

    I did as u told.My response comes connecting …
    failed : code 0
    What does this mean?Can u pls help me out

Trackbacks/Pingbacks


Leave a Reply

Receive notifications of new posts by email.

The Arduino Book

Arduino Workshop

Für unsere deutschen Freunde

Dla naszych polskich przyjaciół ...

Australian Electronics!

Buy and support Silicon Chip - Australia's only Electronics Magazine.

Use of our content…

%d bloggers like this: