Twitter LED (Broken/Being repaired)

Note: Before you start this article take note that the LED lighting code and word recognition should still work but twitter have made it so you can’t access tweets this way anymore. You have to sign up to be a developer and insert authentication code. I am trying to currently repair this but twitter aren’t making it easy ūüėõ

For future projects and general usefulness I have decided to try to learn a new programming language. I have decided to try python for two good reasons, because it is a good language to program projects with the Raspberry Pi and because a large chunk of my course next year is programming simulations in python.

For my first program I’m going to do a twitter feed controlled LED, I also plan to incorporate twitter into a later project so this is a good starting point.

Ingredients list: A Raspberry Pi, an LED, a 220 Ohm resistor, Internet access and a Twitter account.

First take an LED and a 220 Ohm resistor and connect the resistor to the cathode of the LED. Then connect the anode of the LED to pin 7 of the GPIO pins on the Raspberry Pi and the resistor to pin 25 using two female to female jumper wires. A GPIO pin layout diagram can be found here. Once you are sure this is done correctly, a mistake could damage the Pi, boot up your Raspberry Pi ( I am using Raspbian Wheezy). This is all the hardware setup.


Now to install the appropriate libraries type this into the command line:


Now run the installer by typing sudo: sh

Okay you know have the appropriate library the next step is to create the file. I’m using nano, to make he file type this command:


In the editor you just opened type the following code, quick tip press CTRL X to save and exit the editor. Note, I am going to put ; at the end of each line to make the program clearer do not put these into your program.

import urllib ;

import simplejson ;

import time ;

import RPi.GPIO as GPIO ;


GPIO.cleanup() ;

GPIO.setmode(GPIO.BOARD) ;

GPIO.setup(7,GPIO.OUT) ;


def latest_tweet(twitter_handle): ;

twitter_results = urllib.urlopen(‘http://’+’’+twitter_handle) ;

result_list = simplejson.loads( ;

return result_list[‘results’][0][‘text’] ;


count = 1 ;


while count > 0: ;

tweet=latest_tweet(‘@JamesLeftley’) ;


if ‘on’ in tweet: ;

print tweet,’ – LED ON’,’\n’ ;

GPIO.output(7,GPIO.HIGH) ;


if ‘off’ in tweet: ;

print tweet,’ – LED OFF’,’\n’ ;

GPIO.output(7,GPIO.LOW) ;


if ‘stop’ in tweet: ;

print tweet,’ – Stopped’,’\n’ ;

GPIO.output(7,GPIO.LOW) ;

count = 0 ;


time.sleep(2) ;

Now save and exit using CTRL X and you should be taken back to the command line. To run the program type:

sudo python

To stop the program tweet stop, to turn the LED on tweet on and to turn the LED off tweet off. The tweets can say anything as long as they include the twitter handle, in this case @JamesLeftley, and the command. All the commands are case sensitive.

To personalise the program change the twitter handle, add more outputs and change the commands.

Solving Quadratics

So to start off a new area of discovery I have decided to start to learn Python.

To start off this undertaking I downloaded Python 2.7.3 from python/download and started to play around with IDLE, python’s¬†Integrated DeveLopment Environment (IDE).

As a project to work on to learn this new language I decided to make a simple console application that try’s to calculates the value of X for a given quadratic. ¬†To do this it will use the quadratic formula


So to do this I first need the values of a, b, and c as per the formula.    Image

for example a = 1, b = -9, and c = 20.

The first step logical step was to cut the equation into 3 chunks:

  1. The¬†Discriminant (őĒ); b^2 – 4ac
    1. sqrB = pow(b, 2)
      AC = 4*a*c
      Delta = math.sqrt(sqrB – AC)
    2. the “pow(x,y)” function returns the first term to the power of the second, i.e. b^2
    3. the math.sqrt is a function of the math module which square roots the contents of the brackets, or to use the technical name, parentheses.
  2. The Numerator; -b +-¬†‚ąöőĒ
    1. NumeratorPlus = -b + Delta
      NumeratorMinus = -b – Delta
  3. The Denominator; 2a
    1. Denominator = 2*a

Then with the necessary components the values of X can then be calculated by:

XPlus = NumeratorPlus / Denominator
XMinus = NumeratorMinus / Denominator

and then be outputed on the screen using the print command:

print (“Your answer is, X = “), XPlus, (“Or X = “), XMinus

notice the comma’s after each component which are needed.

And so the final step was to allow the user to enter in values:

print (“Please¬†separate¬†the quadratic equation into aX^2 + bX + C = 0”)
a=input(“Please Enter the Value of a : “)
b=input(“Please Enter the Value of b : “)
c=input(“Please Enter the Value of c : “)

The “input” function is important as it prints the given¬†prompt¬†to the output and then reads in the data entered by the user and¬†assigns¬†it to the variable.

This is different to the “raw_input” function which is not syntax sensitive.

And so the final code looks like:

import math
from time import sleep

print (“Please seperate the quadratic equation into aX^2 + bX + C = 0”)
a=input(“Please Enter the Value of a : “)
b=input(“Please Enter the Value of b : “)
c=input(“Please Enter the Value of c : “)

sqrB = pow(b, 2)
AC = 4*a*c
Delta = math.sqrt(sqrB – AC)
NumeratorPlus = -b + Delta
NumeratorMinus = -b – Delta

Denominator = 2*a
XPlus = NumeratorPlus / Denominator
XMinus = NumeratorMinus / Denominator

print (“Your answer is, X = “), XPlus, (“Or X = “), XMinus