Learn how to tweet from your Arduino in chapter thirty of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – A tutorial on the Arduino universe.
[Updated 26/7/2013]
In this article we will learn how to send messages from our 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 three methods: using a web browser on a personal computer or internet device, on a mobile phone, or using a specific application such as TweetDeck on one of the aforementioned devices. For example, here is a typical web browser view:
Image may be NSFW.
Clik here to view.
And here is an example of a twitter application running on an Android OS smartphone:
Image may be NSFW.
Clik here to view.
So as you can see, it is easy enough to read peoples’ tweets. Therein lies the reason for this article – we can harness twitter as an output device for our Arduino systems. We can broadcast various messages, so systems can be created to monitor specific parameters and report on their status at regular intervals, upon an event occurring, and so on.
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. Once you have logged into twitter with your machine account, visit this page and get yourself a token by following the Step One link. Save your token somewhere safe, you’ll need to insert it into your Arduino sketch.
Next, you will need some hardware. Apart from your usual Arduino board, you will need an Ethernet shield. However to save space and money I’ll be using the Freetronics EtherTen:
Image may be NSFW.
Clik here to view.
If you are unfamiliar with using Arduino and Ethernet, please review chapter sixteen before continuing forward with this article. From a software perspective, we will need another library for our Arduino IDE. Download and install the twitter library from here. Now, at this point – please run the Webserver example described in chapter sixteen and ensure it is working before moving forward from this point. While you do that, we’ll have a break…
Image may be NSFW.
Clik here to view.
Now it is time to send our first tweet. The following sketch is a modification of the demonstration version, in which we have isolated the tweet-sending into a separate function called (strangely enough) tweet();. It is not complex at all:
So after uploading the above sketch, running a network cable from your access point to the Ethernet shield, and powering up the Arduino board – your tweet should appear as such:
Image may be NSFW.
Clik here to view.
Excellent – it works. And I hope yours did as well. If it did not, open the serial monitor box to get some feedback from the sketch. From experimentation the most amount of errors are caused by incorrect IP and trying to send multiple tweets too quickly. If you get excited and try to run the sketch again by hitting reset, twitter will reply back with an error – it does not allow duplicate tweets to be sent (over a short period of time). Twitter will reply to your tweet with a code which describes the result of your tweet. This code is stored in an integer variable using the function:
For example, 200 means the tweet was sent successfully, and 403 means you have attempted a duplicate tweet. However you can omit the code-checking if you are not fussed about your tweet’s status.
Although it was fun tweeting Hello world, let’s create an example that reacts to various events and tweets about them. To simulate some events I have connected four buttons to digital inputs (using the button board from chapter twelve). Pressing a button sends of the matching message. However you can use any form of digital output or decision-making in your sketch. For now, here is the example sketch:
And here is a screen shot of the results after pressing buttons one, four, two then three:
Image may be NSFW.
Clik here to view.
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.
Image may be NSFW.
Clik here to view.
Have fun and keep checking into tronixstuff.com. Why not follow things on twitter, Google+, subscribe for email updates or RSS using the links on the right-hand column, or join our 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 post Moving Forward with Arduino – Chapter 30 – twitter appeared first on tronixstuff.