Wouldn’t it be interesting, if a single tweet of yours can switch on/off a light? In this article, I will share with you the very basic of an IoT Twitter application where a single tweet of yours can switch on/off an LED connected to the Raspberry Pi at your home.

Step 1: Log In into your twitter account and make a twitter application from this link. Create an OAuth access token – this gives your application access to your own Twitter account. Obtain the CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY and ACCESS_SECRET

Step 2: In the Raspberry Pi, make a python file and write the code mentioned below. You can copy-paste the code if you want. Good people understands the code they write.

Note: This project will be python 2.7 project. So, the code below, wont work in python 3.

First, load the necessary packages required. Make sure to install the packages before using them.

import sys
import signal

from gpiozero import LED
from time import sleep
from twython import Twython

Next, do the following, just keeping the necessary keys/secrets in a variable:

CONSUMER_KEY = '--your key--'
CONSUMER_SECRET = '--your secret--'
ACCESS_KEY = '--your key--'
ACCESS_SECRET = '--your secret--'


Next, I will read tweets from my twitter timeline. You can read the Twython official documentation to see all you can do with Twitter API. Note that the screen_name will be different in your case, depending on whose tweet your are reading. Also, the count=1 signifies that the app is reading only the first tweet from my timeline.

    user_timeline = twitter.get_user_timeline(screen_name='rishihere', count=1)
except TwythonError as e:
   print e

for tweets in user_timeline:
   text =  tweets['text'].encode('utf-8')

After converting the tweet to text (with UTF-8 encoding) in the above code, we now convert each word of the text to an element of a list.

text = text.split(' ')
print(text) #printing to see if the list is made

The next step is interesting. Not every tweet should switch on the light, so we are looking for tweet with some specific key words, like ‘switch, ”light’, ‘on’. Once tweet with such keyword is found, the if condition becomes a success and the LED is switched on.

value =  set(['switch', 'light', 'on.']).issubset(set(text))
led = LED(17)
if value:


The signal.pause() is used so that the code does not exit after execution. Read more about it here.

Step 3: Automate: Now, you need to automate the execution of this python script, so that it runs automatically at a specific interval itself. For this, you will need to add the below line to the cron job scheduler of the Raspberry Pi. Read about cron job in Raspberry Pi here

* * * * * sudo python /home/pi/SillyTweeter/a.py

The name of my python file is a.py. You need to make the necessary changes.

After this, reboot your RaspberryPi. The OS will read tweets from your timeline every second and on finding the appropriate tweet, it will switch the LED on. The LED will come back to off state if it sees any other tweet without the right keyword.

Categories: Tutorials


Hello All! You are reading the author(s) profile of CorvineLabs Team. We may be 1,2 or ..any in number. Let that remain withing us. You enjoy our write up. If you want to join the team than do Contact Us.

Leave a Reply

Your email address will not be published. Required fields are marked *