Now I’m going to add to the usefulness of my personal Twitter stream by having a Raspberry Pi automatically tweet its current CPU temperature every hour, and a webcam picture!

Getting Started

This project uses Python; a simple programming language ideal for DIY projects. We’ll begin by installing Twython on the Pi – a Python module for interfacing with Twitter; setting up a Twitter “application” to get an API key; then go onto make the Pi tweet stuff on our behalf. It’s going to be so much fun!

Registering a Twitter app

In order to use the Twitter API – that is, the REST interface that we’ll use to post new Tweets and generally interact with Twitter outisde of the twitter website – we’ll need to register a new app. Do that from this link – you needn’t specify a callback URL, and just make up a website if you want.

You’ll see something resembling this once you’re done – these keys are unique to you.

By default, the app is set to read-only, so we won’t be able to publish tweets without changing that to Read and Write. Go to the Settings tab and change the Application type.

Once saved, head back to the Details tab and click the button at the bottom to create an OAuth access token – this gives your application access to your own Twitter account. Refresh, and leave the page open for later – we’ll need to copy paste some of those keys in a minute.

Create Your Python Project

Begin by making a new directory to house your Tweet project, then create a new file.

mkdir SillyTweeter
cd SillyTweeter
sudo nano SillyTweeter.py

You can call it whatever you like, obviously.

In the text editor that appears, copy and paste the following, replacing the consumer key with the relevant key from the Twitter application page we left open earlier. Each key is surrounded by single quotes, so be sure not to miss those. Note that ACCESS_KEY is referred to as Access token on the Twitter app page.

I won’t explain this code too much because it doesn’t really matter – it runs a command that grabs the temperature, then splits up the output to extract the number, and tweets that with a custom message. You can find the complete example code here.

In short, you’ve initialised the webcam at a specific resolution (you may need to adjust this is it’s a really old cam), snapped a picture, and saved it as a jpg. We’re just going to overwrite the same webcam.jpg each time the app is run.

Change that to * * * * * if you want it to run every minute, and be prepared to lose followers faster than a Twitter account that loses followers quickly.

That’s for today. I’m happy to have contributed more silliness to the vast wealth of useless bytes on the Internet, and I hope you do too! Show your appreciation for this tutorial by tweeting it, and then let us know what your own Twitter bot is going to tweet about in the comments.

I can execute the Twitter python script manually, but when run from crontab it doesn't send the Tweet - even when combined with other working crontab python scrips, which tells me that it's something on Twitter's end?

thanks but it didn't work for me
i did every step and when i want to send a tweet it says:
Traceback (most recent call last) :
File "Twitter.py", line 2, in (module)
from twython import Twython
ImportError: No module named twython

hi, I've found that if a tweet is made then another tweet cannot be made if it contains the exact same text. An error is returned. This is especially noticeable with the temp as often this is the same. What have i done wrong? Thanks for the article though

is it possible to make bot to retweet some other people tweets every 10 minutes (maybe random time every 10 - 15 minutes) and doing this by searching phrase I am interested in... For example every 10-15 min retweeting something about #raspberrypi... ?

Wonderfull tutorial! However it worked 3 times and now I have this error message that I abbreviate " Twitter API returned a 403 (forbidden), User is over daily status update limit."
I read a bit the API documentation and it seems that Twitter changed the permission rules and that one need now the read, write and direct message but I am not sure.
anyway I change to read, write and direct message but error message is still here and it does not tweet anymore. Any coding modifications?
best
Francois, Bordeaux, France

Many thanks! this worked perfectly and is a great motivator for students. For the CPU temp - we got an error 'incompatible VCHIQ library' but needed a reboot and was fixed
Going to try webcam tomorrow. But great instructions. thanks!

That can happen when then site goes offline and our dead link tracker catches it. You still see the strikethrough? I'm not sure which link you're asking about precisely, so can't offer an alternative...

I have my first Raspberry PI here and i have some questions about this script (for tweeting the temperature). Can you also give other temperatures such as motherboard temps? Or amount of RAM (and how much free), the amount of disk space (and how much free), CPU load etc?

Thanks for sharing this. I think posting stuff this is really useful. I am just an amateur t this, so I appreciate any instructions that you can provide. The Pi is a fun device to tinker with. The projects take a while, even the simple ones. Lots of time spent checking and double checking the commands...worth it. Keep posting your ideas!

Network attached home security and surveillance. Raspberry pi connected to a PIR array via
usb2serial to an arduino. If motion is detected, a signal is sent down the serial cable and the PI sends a text to my phone as well as emailing me 8 images from 4 cameras I have. Total cost - $73.

Let's MakeUseOf technology to do something interesting. Using the twitter api can be done from any computer and OS's are multitasking so you can have background processes. No need for a Pi when you can run the same job in the background from any Linux box.

Let me give you an example that shows why this is cool. I teach high school science. My environmental science class is building RPi units that connect to our (normally manually read) building power sub-meters and monitor both building instantaneous loads and total power consumption and then tweet the data every 10 minutes. We are also working up a web app that will receive the tweets and record and aggregate them for display and analysis on the school web site. Of course, anyone who wants to follow a building twitter account (think science classes,) can also monitor power consumption that way.

It's cool because the submeter vendor wants about $10000 for hardware and software that does the same thing (with no Twitter). We are going to be able to monitor 6 buildings for around $500 and some sweat equity. My kids are EXCITED! It's TWITTER! The rest of our school gets to use these tools to (hopefully) reduce energy consumption.

The Raspberry Pi makes this doable along with all the help from the RPi community, freely given. Just "any Linux box" falls short on cost, form factor and support. I rest my case.

"Network attached home security and surveillance. Raspberry pi connected to a PIR array via
usb2serial to an arduino. If motion is detected, a signal is sent down the serial cable and the PI sends a text to my phone as well as emailing me 8 images from 4 cameras I have. Total cost – $73."

because this sounds awesome but I can't seem to find the article you've seen. Can you point me at it please?

The Pi was designed to give children/people a way to learn computing and programming without risking the cost of a full blown computer. The fact that it's small makes it a nice media center, but mostly it's for creating little projects and enjoying the process of creating something.

Unfortunately the Internet is full of trolls who will try to make everything that may be interesting to children lame, while they sit back and play with their PS4 and learn absolutely nothing.

Not necessarily. For example it could be used to report the weather. Or it could be used to report data when triggered by an external trigger (i.e., too cold in the house). While this could be spamish, the big difference is that tweets are opt-in.