Since my scoreboard project has hit a brick wall (brick wall = my lack of knowledge/skills), I’ve been working on anything I can get my hands on that can be achieved within a day.
Whilst scanning the web for quick and pointless RPi projects, I came across a tutorial for sending/receiving emails, and at the same time found a separate tutorial for using IFTTT which is kind of like internet automation.
I pondered this for a few moments…”surely I can make something cool out of this?“…and then remembered seeing plenty of automated RPi Tweets recently saying things like “My temperature is 30 degrees #RaspberryPi“.
I was sold…it was a “go”…my Pi was going to have its own voice within 24 hours!
Note: Whilst there may be more direct ways of getting your RPi to talk to Twitter, this was clear and easy for me, and also gives you plenty of options to customise.
Step 1: What you need
You’ll need a Raspberry Pi, running Raspbian Wheezy (might work on other OS options), a Gmail email account (might work with other email providers) and a Twitter account.
You’ll also need a free account with IFTTT.
Sorted? Let’s do this…
Step 2: Install the required software
Turn on your Pi, get into Terminal, and install the software below:
1) Install the Python development headers and the pip package manager:
2) Update the pip package manager:
sudo easy_install -U distribute
3) Install Feedparser with pip
sudo pip install feedparser
That’s the software side done. Easy so far right?
Step 3: Set up IFTTT “Recipe”
Soon we will be setting up a python script to email IFTTT from your Gmail account, so that when IFTTT receives the email from your email address, it will send a tweet on your behalf using your Twitter account. You can also choose IFTTT to post a Facebook message or a bunch of other things, but we will just tweet for now and you can play later.
1) So, you’re logged in to IFTTT with your new account. Press “Create” along the top navigation bar:
2) A huge ‘ifthisthenthat’ logo will appear. Click on the word ‘this‘:
Hit the email icon:
5) As this trigger is relatively simple, there are no fields to complete in the next section, so just click “Create Trigger” to finish the trigger part:
6) Time to create our “that” i.e. time to tell IFTTT what we want it to do when we send it an email. After the step above you should see the huge ‘ifthisthenthat’ logo again. Click on the word ‘that‘:
7) Similar to before, it offers us many options as the output. We’re choosing Twitter in this example. Click on the Twitter icon:
8) The Twitter option gives you lots of options for the output, such as tweeting with an image, updating bio and even adding a user to a list. For this example, we’re keeping it simple and just sending a tweet. Click the option highlighted below:
9) Now, this bit is fun. What we do here is specify what gets tweeted based on the email sent to IFTTT. We will set the email subject and body text in the Python code which can be used in the tweet, so we need to work around that.
Not clear? Let’s take a bit of time here:
Let’s say for example that our Python code sets the email subject as “My RPI temperature is “ and the body of the text says “39 degrees“
In this screen on the IFTTT setup, the website wants you to tell it what to tweet, and if you want anything taken from the email you send to IFTTT. The default is this:
So this will tweet the email body and any attachment URL we add. This isn’t what we want, so delete those and press the blue add button:
You can now choose from a list of ‘ingredients’. Let’s choose ‘Email Subject’ and ‘Email Body’, and type in ” #RaspberryPi” afterwards so that people searching for RaspberryPi will find it:
What this will do is tweet (based on our example text above):
Ok, let’s move on.
10) Click ‘Create Action’ (image above) and you’ll be asked to give this ‘Recipe’ a name. Add a name and click ‘Create Recipe’:
That’s it – the rule has been set up and, if you remember, needs an email from your email account (the one registered with IFTTT) to trigger it. So, time to get that email sent…
Step 4: The Python Script
Remember at the start I said it wasn’t the most direct method of getting your RPi to tweet?
This is because we get the RPi to send an email to IFTTT, which in turn sends the tweet. IFTTT is kind of like the middle man. I’m sure there must be a way of getting the RPi to tweet directly to Twitter, but then again, just think of all the other possibilities IFTTT gives you with just a simple email trigger?
So what we need to do is set up a script that gets the Pi to send an email to IFTTT, including the temperature reading in the email body. Sounds harder than it really is.
1) Just to show you how we get the temperature, go into Terminal and type in:
Mine just came back with “temp=29.9’C“…like good holiday weather. I’ll come back to this code in a bit.
2) Here is our code which you need to put into a Python script. However, you need to change a few bits first:
#!/usr/bin/env python import smtplib from email.mime.text import MIMEText temp1 = "" f=os.popen("/opt/vc/bin/vcgencmd measure_temp") for i in f.readlines(): temp1 += i USERNAME = "YourA[email protected]" PASSWORD = "YourGmailPassword" MAILTO = "[email protected]" msg = MIMEText(temp1) msg['Subject'] = 'My RPi temperature is ' msg['From'] = USERNAME msg['To'] = MAILTO server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo_or_helo_if_needed() server.starttls() server.ehlo_or_helo_if_needed() server.login(USERNAME,PASSWORD) server.sendmail(USERNAME, MAILTO, msg.as_string()) server.quit()
Let’s break it down…
After the importing the modules in the initial 2/3 lines, the first part creates a new string called temp1. We then use f=os.popen (deprecated) to run that command I showed you earlier for the temperature.
The for i in f.readlines part takes the output of that temperature command and makes it ‘i‘. All we do then is say that temp1 += i…which means temp1 is “temp=29.9’C”:
temp1 = "" f=os.popen("/opt/vc/bin/vcgencmd measure_temp") for i in f.readlines(): temp1 += i
Next we set up our Gmail information. This is simply your Gmail email address, your password, and the email address to send to (i.e. to IFTTT, which should be the same as my code, but check it anyway).
NOTE: If you have Google 2-step verification, go into your Google account, create an application specific password, and enter it in place of your normal password here.
USERNAME = "[email protected]" PASSWORD = "YourGmailPassword" MAILTO = "[email protected]"
The following section is the content of the email. The first line is the email body, for which we are using the string we created earlier “(temp1)“. The second line is the subject of the email. The third and fourth lines link to the step above where we defined the email address to/from.
msg = MIMEText(temp1) msg['Subject'] = 'My RPi temperature is ' msg['From'] = USERNAME msg['To'] = MAILTO
The final block of code is the ‘stuff’ that makes it work. We have no reason to look at or change any of this:
server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo_or_helo_if_needed() server.starttls() server.ehlo_or_helo_if_needed() server.login(USERNAME,PASSWORD) server.sendmail(USERNAME, MAILTO, msg.as_string()) server.quit()
That’s the code sorted.
Step 5: How to run it?
There are a number of ways you can run this script – that part is up to you:
- In its simplest form, you could just have this as a dedicated script which sends the Tweet when you run it.
- You could add this code at the start of a script for a project, to let the world know every time you turn on your Pi (I do this, with my Internet Radio)
- You could use this code in conjunction with a GPIO button to send a tweet when you press a button.
- You could set this up to fire off a tweet at regular intervals using ‘cron’, which is like a task scheduler you can set up for your Pi (I’ll cover this with cron soon as a regular temperature check seems like a cool idea to me)
- And probably many, many more…
Step 6: Get creative!
Don’t forget that IFTTT gives you so many options, you’re not stuck with just sending a Tweet. Remember the options on screen? Facebook, Instagram, Evernote, GDive, SMS, Dropbox…there are so many possibilities for your RPi here.
I hope that helps as some of the existing guides were lacking a bit of detail for me. Follow me up on Twitter and let me know if this was useful, or perhaps comment below.