Average Ticker

Make Your Raspberry Pi Tweet Its Temperature

Make Your Raspberry Pi Tweet Its TemperatureIf This Then That…clever!

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!

Twitter screenshot

Very rewarding when your Pi finally tweets for itself

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:

    sudo apt-get install python-pip python2.7-dev

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:

IFTTT create recipe

Select ‘Create’

2) A huge ‘ifthisthenthat’ logo will appear. Click on the word ‘this‘:

IFTTT select this

Select ‘This’

3) The site will then show you an impressive wall of options to be your ‘trigger’. As you know, we will be using the email option so that when IFTTT receives an email, it will do something as a result (send a tweet in this example). 

Hit the email icon:

IFTTT select trigger

IFTTT – Lots of options

4) Once you have selected ’email’ in the step above, you will be shown 2 options. We will be using “Send IFTTT any email” which just means that the email just needs to come from a specific email address – it doesn’t have to be tagged with anything:
IFTTT choose trigger

Select the ‘any’ email option

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:

IFTTT complete trigger

Create a trigger

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‘:

IFTTT select that

Click “that”..yes…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:

IFTTT output channels

More options…

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:

IFTTT choose action

Select to send a tweet

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:

IFTTT complete action fields

Enter the tweet content

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:

IFTTT select ingredient

‘Ingredient’ selection

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:

IFTTT create action

Enter the tweet content

What this will do is tweet (based on our example text above):

“My RPi temperature is 39 degrees #RaspberryPi”
(which is SUBJECT + BODY + #RaspberryPi)

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’:

IFTTT create recipe

Complete by creating the ‘recipe’ – anyone hungry yet?

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:

     /opt/vc/bin/vcgencmd measure_temp

Mine just came back with “temp=29.9’C“…like good holiday weather. I’ll come back to this code in a bit.

Raspberry Pi temperature terminal command

Checking the temperature using a simple command

2) Here is our code which you need to put into a Python script. However, you need to change a few bits first:

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”:

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.

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.

The final block of code is the ‘stuff’ that makes it work. We have no reason to look at or change any of this:

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.

Average Man

4 Comments on "Make Your Raspberry Pi Tweet Its Temperature"

  1. Leo Placid | 29/02/2016 at 13:39 | Reply

    Hi: This is a very useful post. However I ran into problem with the f=os.popen
    You have to add import os at the top of the Python Script to fix this problem.
    Also the trigger@ifttt.com has been retired. Use trigger@recipe.ifttt.com to trigger your Email Channel Recipes.
    One more question:

    Is it possible to have multiple triggers such as Twitter and SMS at the same time?


  2. import smtplib

    IndentationError : unexpected indent

    How to fix this error??

Leave a comment

Your email address will not be published.


This website uses cookies. Please visit our privacy policy page for more information. Privacy policy

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.