Google+ My Python Projects: Controlling Raspberry Pi GPIO using Twitter Direct messages Google+

Thursday, January 16, 2014

Controlling Raspberry Pi GPIO using Twitter Direct messages


Hi Readers ,

This time I am going to share you about how I did use twitter direct messages to control the raspberry GPIO (actually you can control whatever you want by modifying the script).

Here is the list of things you need:
  • Raspberry Pi with Internet connectivity
  • A twitter account for your Raspberry pi
  • Access token and secret keys of that account (will be explained later)
  • wires,LED
And of course for sending direct message you must have a twitter account and you and your Pi must follow each other.I have chosen Direct message for controlling the Pi, though you can control using tweets mentioning the Pi (but DMs avoid spamming).

Step 1 : Create Twitter account for RPi , Create an Application API from https://dev.twitter.com/ .Obtain its API access credentials (do not disclose these to others)


Step 2 : Install  tweepy (link has the installation instructions) and RPi.GPIO 
              (use sudo apt-get install RPi.GPIO from your Pi)

Step 3 : Put the API credential and Username of Pi and Account from which you wish  to DM in my python script.

download it here

Step 4 : Connect LED to GPIO4 as shown in diagram.

Step 5 : Test it with instructions send through Direct Messages sent to your Pi's account.

   This program also returns confirmation as a direct message back to you.The frequency with which the Pi checks for the new DMs are limited(here it is 4 mins.) due to regulation on API requests that can be sent to the Twitter account.So your Pi will respond to your DM command at the worst delay of 4 minutes.

     As you can see, to turn on the LED DM "ON" to your Pi's account to turn off LED DM as "OFF"(without the quotes).

    This script can be modified for projects like remote home automation, since you can access twitter anywhere in the world and the only thing you need is to your Pi be online.

So thats it, and as always feedbacks and questions are appreciated.

2 comments:

  1. Hey, this is a good little project and I am looking to do something similar. The wait in between sending the DM and the light coming on. What causes this delay? and is there anyway to reduce it?

    ReplyDelete
    Replies
    1. I think there are two reasons behind this,

      1) This delay depends on the internet speed, for slower connections the Pi will take more time to send the DM, after turning the LED on/off.You my try with a faster internet connection.

      2) Second, the program itself may account for some delay(python will run a bit slower compared to C/C++, but not much). You may try C/C++ alternatives but the program will not be simple.

      And running other programs when executing this script will also have an effect.

      Apart from these I think there is not much we can do.

      Thanks for asking your question :)

      Delete