For other interesting ARDUINO PROJECTS here
An Arduino sending tweets? Absolutely! Darren Yates shows you how to get your Arduino its own Twitter account and tweeting automatically.
Our Twitter weather station needs no soldering and uses just three parts — build it for under $30.
From
many people I’ve talked to, Twitter is one of those things you either
love or hate. Even so, its hundreds of millions of devotees make it as
key to the internet as Facebook or YouTube. If selfies from C-grade
celebrities tweeting what they’re eating or wearing isn’t doing it for
you, how about putting it to better use — with your Arduino? Yep, you
can connect your Arduino directly to Twitter and have your
microcontroller automatically send tweets that you can monitor on any
Twitter client. All of a sudden, Arduino and Twitter becomes a
combination full of possibilities.Twitter weather station
If we’re on about anything in this masterclass series, it’s about being practical: showing, not telling. So we’ll not only show you how to get your Arduino on Twitter, we’ll show you how to create your own Twitter weather station that updates temperature and humidity every 5 minutes on its own Twitter account. And just to whet your appetite, we’ve done one of those ‘here’s one we’ve prepared earlier’ jobs you can take a look at right now — just head over to Twitter and look up @IAmArduino.What you need
Don’t like soldering? Fine, because you won’t need to do any for this project. Our Twitter weather station needs only a handful of parts that just slot together. Here’s what you need:- Arduino Uno microcontroller board ($13 on eBay)
- W5100 Ethernet Shield ($10 on eBay)
- DHT11 temperature/humidity sensor ($3 on eBay)
How it works
With such a small parts list, you should be able to guess that the secret to getting this working is all in the sketch. In fact, this project’s sketch uses up 27.5KB of the 32KB of flash storage available in an Arduino Uno, so there’s not a whole heap left over. Still, it says something that you can actually squeeze a Twitter post client into just 27.5KB of storage.
The first trick we’ve
performed is getting the DHT11 to work by simply plugging it straight
into pins D4 to D7 on the Arduino Uno, with the text panel facing
outside. The DHT11 requires a supply voltage of up to 5.5V on pin 1,
data communication on pin 2 and pin 4 connected to ground (0V).
The DHT11 temperature/humidity sensor does the hard yards of taking readings for the Arduino.
The
good thing about this little sensor is that despite having its own
microcontroller on board, it only needs less than 5mA of current. Since
an Arduino’s digital outputs can supply up to 40mA, we can use the
output to supply (and sink) the required power. By installing the DHT11
so that its pin 1 slots into D7, its pin 2 (the data pin) slots into D6
and so on down to pin 4 connected to D4. And by setting D7 to digital
‘high’ and D4 ‘low’, we’ve just created a simple in-house power source
that’s more than enough to drive the sensor.One problem we need to overcome by doing it this way is that the sensor’s pin 2 data communications pin needs a pull-up resistor to work. All of the Arduino’s digital pins can act as both outputs and inputs, but they also have internal pull-up resistors, so instead of configuring D6 to be a straight input, we set it to ‘INPUT_PULLUP’, which activates the pull-up resistor and it’s good to go. You just need to make sure you install the DHT11 sensor into Arduino pins D4 to D7 with the text panel facing outside of the board.
Ethernet Shield
In actual fact, you’re installing the DHT11 sensor into the pins on the Ethernet Shield. This is one of those sandwich projects where the Ethernet Shield sits on the Arduino Uno and the DHT11 sensor slots into the same pins on the Ethernet Shield. All you need to do then is program the sketch into the Arduino, plug the Ethernet cable in one end and the other into your broadband router, add a cheap USB AC power adapter and you’re good to go.The Ethernet Shield will be the best $10 accessory you’ll buy for your Arduino. It not only gives you Ethernet and internet connectivity, it comes with a full TCP/IP stack so it’s dead easy to program, and it also comes with a microSD card slot that you can load up to 32GB of storage into.
The W5100 Ethernet Shield not only adds Ethernet, but also TCP/IP stack and microSD storage.
Twitter library
The magic that makes this all work is mostly down to Markku Rossi’s excellent Twitter library. There are a few Twitter libraries for Arduino floating around, although they all require you to send your tweets through a third-party server, which is not only limiting, but not great for security either. Rossi’s library doesn’t — it allows your Arduino to talk to Twitter’s servers directly and even handles the OAuth authentication protocol (secret handshake) that gets your tweets onto your Twitter account. You can find the library at Rossi’s web site at tinyurl.com/nzw3fba.Unfortunately, he hasn’t made it easy to get hold of or work with. First, you have to use the GitHub source code repository software to retrieve it and his suggested method of obtaining the correct Twitter access tokens and secret keys requires compiling and running Java apps. So we’ve included his library in our download file, but we’ve also got a simpler method for getting those important tokens and keys.
Signing up to Twitter
The first thing you’ll want to do is create a Twitter account for your Arduino. There’s no magic to this — get on your browser, head to twitter.com and create a new account. You may need to log out of your existing account to do it if you have one already, but otherwise, you’re just creating a new account.That’s the first part. The next bit is a little more complicated. An Arduino Uno obviously can’t use a web browser to interact with Twitter, so rather than using that front door approach, we’ll go around to the back door and use Twitter’s API (application programming interface) called REST.
It gets more complicated still. Twitter uses a series of passkeys based on OAuth, an open standard for authentication. The process is divided into two halves: a ‘Consumer Key/Consumer Secret’ pair and an app ‘Access Token/Access Token Secret’ pair. Our sketch code will need to supply Twitter with these key pairs before it’ll be allowed to publish a tweet.
While Rossi’s Twitter library handles that easily, you have to supply those key pairs. Rossi’s example is to compile and run a Java command line tool, which is fine but it needs Java and compiling. We have a simpler solution: do it on the web at Twitter’s developers web site.
Creating your Twitter App
This process is normally used by web app developers looking to get their apps to hook into the Twitter engine, although it’s equally just a way of signing up to get the ‘Token Access/Token Access Secret’ pairs and getting them connected with your app and your account. The bottom line is your Arduino board is set to become an app as far as Twitter is concerned.You can see how to get hold of the key pairs in the step-by-step guide in the last part of this article. When you get the pairs, you need to include them in the Arduino sketch. We’ve annotated it pretty clearly so you’ll know where these keys are supposed to go. Once you’ve included them in the source code, compile it, load it into your Arduino Uno board and you’re done. You won’t need to do this again unless you decide to change Twitter accounts.
For other interesting ARDUINO PROJECTS here
Tricks with Twitter
If you’ve had a look at our @IAmArduino Twitter feed, you’ll see that each entry has the temperature, humidity and time, followed by a sensor check. There’s quite a bit going on here that we’re squeezing into 140 characters and it’ll help you understand a few things.First off, Twitter doesn’t like you tweeting the same thing over and over again — das ist verboten! So what we do is use the Twitter library’s ‘gettime’ function to return the current Twitter time in seconds as an unsigned long integer. Since that’s guaranteed to always be different, we add this in to ensure each tweet is always different, even if the temperature and humidity haven’t changed. This way, Twitter is guaranteed to accept it on this score.
You also need to play nice with Twitter and not spam its servers with 100 tweets every second. You also shouldn’t abuse the system, particularly automating mentions and replies — you can read more at Twitter's guideline page.
The most important thing to remember is what’s called the Twitter rate limit — it’s the number of times you can request or send information to the Twitter servers per hour. Ultimately, you can make no more than 350 authenticated (OAuth) calls per hour, a bit under 10 per minute or call it nine to be safe. Remember, though, that’s not just sending tweets — that’s any OAuth access. The Twitter library has far more commands than we’re using here, but every command that requires OAuth access will chew up one notch on your rate limit per hour, so just be aware of it. Our sketch here tweets once every 5 minutes, although it’s easily changed.
Twitter changes
It turns out our timing for this project couldn’t have been worse. A few weeks earlier, Twitter switched from API version 1.0 to 1.1. That basically broke a lot of apps and changed the way you can tweet and view user timelines. It also broke our Twitter weather station and our ability to read tweets. In the short time since then, we’ve managed to get our sketch working again to post, but we still can’t read tweets. And if you can’t read them, you can’t use Twitter to control your Arduino, so unfortunately, we’ll have to put that side of our plan on the backburner until we figure out a solution.The main issue is that you now need authentication to read any user timeline via the API. That requires extra memory to store away the OAuth key pairs we didn’t need before, memory the Arduino doesn’t really have to spare, particularly when we need RAM to read the tweet as well. Given that we’re also down to just 4.5KB of storage remaining, at the moment it looks as though we’ve hit the Arduino Uno’s end stops for the first time in this series. We’ll see how we go.
Twitter security
One of the great things about Rosso’s library is that you’re not sending your security tokens and keys as raw text over your network. They’re encoded using SHA1 (secure hash algorithm), so they’re about as secure as you can make them — at least on an Arduino. If you peer into the ‘twitter.cpp’ file in the Twitter library, you’ll see how it all works if you look closely.Use your imagination
We’re using the Arduino here to automatically tweet temperature and humidity every 5 minutes, but if you desire you could also program your Arduino to tweet on a trigger. Here are just a few ideas we’ve come up with.- Use an HC-SR04 ultrasonic sensor and send a tweet whenever someone passes through the ultrasonic beam — you’ve got a simple tweeting entry alarm system.
- Have a light sensor such as a light-dependent resistor (LDR) inside your bar fridge so that whenever the door opens and the light comes on, it triggers your Arduino to tweet that someone’s raiding your beer.
- Use what’s called a window reed switch to trigger code that tweets if a window in your home is opened.
A window reed switch can turn your networked Arduino into a tweeting security system.
Step-by-step: How to get security tokens from Twitter
Your Arduino will be sending Tweets in just five simple steps.Step 1:
Point your browser to dev.twitter.com/apps/new and sign in with your Arduino’s Twitter account details that you just created. Haven’t done it yet? Do it now the usual way.
Step 2:
On the ‘Create an application’ page, give your app a name and a basic description. You’ll also need a web site address (create a free Blogger page at www.blogger.com if you don’t have your own). Agree to the terms of service, fill in the CAPTCHA and click the ‘Create your Twitter Application’ button. Approval is automatic.
Step 3:
When you see the new app page, click on the ‘Settings’ tab, scroll down to ‘Application Type’ and click the ‘Read and Write’ button. Do that and click the ‘Update this Twitter application’s settings’ button at the bottom of the page. Give it a few seconds and then refresh the page to check that the changes have taken effect.
Step 4:
Click the ‘Details’ tab at the top, scroll down and click the ‘Create my access token’ button at the bottom. When the page updates, click on the ‘OAuth tool’ tab and on the new page, you’ll see the ‘Consumer Key/Consumer Secret’ pair plus the ‘Access Token/Access Token Secret’ pair. Copy these and store them away in a notepad file somewhere safe.
Step 5:
Open up the Arduino sketch and copy the key/token pairs into the appropriate positions. You’ll only need to do this once, provided you don’t change the Twitter account for your Arduino. After that, your Arduino will be able to send tweets at will (well, within Twitter rate rules anyway).
Using Arduino’s Serial Monitor
You can monitor what your Arduino is doing by looking at the ‘Serial Monitor’ (the magnifying glass icon in the top-right). Plug your Arduino into your computer, load up with the sketch and launch the ‘Serial Monitor’ in the IDE. Make sure the speed (baud) rate on the bottom-right of the window is set to 115200 and you’ll be able to monitor whether the Arduino is working correctly. We’ve coded it to tell you if there’s a problem with the sensor or tweets not getting through. The ‘Serial Monitor’ is activated in the sketch by the Serial.begin(115200); line and data is written to the serial bus via the Serial.println commands.For other interesting ARDUINO PROJECTS here
Getting the sketch
You can download the sketch plus all of the libraries for the APC Twitter weather station from our web site at apcmag.com/arduino.htm (look for Project #10). Download the ZIP file, decompress it and copy the contents of the ‘libraries’ folder into your Arduino IDE’s ‘libraries’ subfolder. Remember to restart the Arduino IDE for the new library code to be added before you launch the INO sketch file. You’ll find the Arduino IDE to program your Arduino available at arduino.cc. We recommend sticking with the latest 1.0.x version.if u like the post just say thank u in comment box.
No comments:
Post a Comment
its cool