The selfie-taking twitter bot - #EverydayEngineers

A smiling Raspberry Pi display

Categories: Technology

23 February 2016


James Pearson is a sixth form student who was inspired to start a hacking project when the original Raspberry Pi computer was released. James has used his Pi to invent a twitter bot – a robot which can monitor its own temperature, send tweets and take selfies.

Why did you decide to make a twitter bot?

I had originally planned to build a webserver to host a website for a Javascript game that I was making for a computing project at college. However, I didn't want to end up breaking my Pi by leaving it on for too long, so I wanted a way to periodically measure the temperature so I would know if it was overheating. I decided to use Twitter to notify myself and I started this project.

I later found the Twitter bot more interesting than what I was originally doing so I worked on that instead, getting it to tweet random messages, then getting the Pi HAT add-on, and also a camera which gave me more things to tweet about the surroundings and let me do some fun and silly things with it! It’s not really especially useful any more, but it’s been quite fun to make!

How does it work?

Cron is a system tool that is used to set off tasks at specified times. I used it to start a Python script every hour which will randomly select one of the six things to tweet. It can tweet the pressure, humidity, room temperature, CPU temperature, selfies and random messages from a list. At the end of each of the scripts I used a Python library called Twython to send the message to Twitter.

Taking selfies is the most recent thing that I’ve added, and the most difficult. The code selects three random colours to generate a hex colour code, and this is used to pick the colour of the LEDs on the display. It then picks a random pattern out of a few different faces; this activates the different LEDs on the display. The camera (which is held above the Pi with a Lego stand, some blue tack and a twisty tie) then takes a picture of the display, and then the display will turn off.

How easy was it to make - did you come across any challenges?

Once you’ve got all the information you need and you have a vague understanding of it all, it’s actually not too hard to do. I’ve come across various problems along the way, although more often than not it’s when I do something really silly like missing out a character somewhere in the code!

Taking selfies was probably the most complicated thing and it, therefore, had the most challenges. One of these issues I faced was that I couldn’t upload pictures to start with. I kept on tweaking the code, and it eventually I realised that I had unplugged the USB Wifi dongle, weirdly without an internet connection it becomes difficult to upload photos!

How did you learn how to use your Raspberry Pi?

Trial and error, with quite a lot of error! The internet has been a wonderful teacher for me, as the information is usually up to date and free. None of the tutorials have told me exactly what I wanted to do for any one thing, so I used lots of different sources to help me through the individual steps. I hadn’t used Python much before but it wasn’t really too hard to pick up.

How much did all the parts cost?

I used a Raspberry Pi B+, although really any Raspberry Pi would do, even the Pi Zero (£4.20) could manage to run the basic twitter bot. The Pi B+ was really great for me, and at the moment you can get it for about £16. I used a USB WiFi dongle (about £3) to connect to the internet, but an Ethernet cable would also be fine. You’ll also need a micro USB cable and plug socket (make sure it’s a 2 Amp one if you’re using it with the Sense HAT) which will probably be about £10. You’ll need a micro SD card – you can get an 8GB one for about £5. I also used the Sense HAT add-on board, this gave me extra sensors and the LED display for my faces (just over £20), and the Pi Camera add on (£15.99), although these aren’t really necessary for the basic part of the project.

What tips would you give to someone who is interested in making something with a Raspberry Pi?

I think an important thing to remember when you’re starting is that it probably won’t work the first time around, and you will make mistakes, but you’ve just got to keep going and try it again. The internet will almost always have an answer to whatever problem you might have!

Click here to check out James' Twitter feed, and remember to tweet us your creative hacks using #EverydayEngineers!

Related Articles