Tuesday, November 27, 2012

Raspberry PI, GPIO and blinking LEDs

My Raspberry PI is the revision B model with 512 MB of RAM. Currently, I am running Raspbian Wheezy 2012-09-18 version, which I keep up-to-date using

$ sudo apt-get update

$ sudo apt-get upgrade

This process, which I execute on about a two week basis,  usually takes at least half an hour to complete, so go have a coffee, rake the lawn, do the dishes, then come back.

Now for our blinking LEDS:

There are several ways to accomplish the feat of blinking a LED on the PI. The reason I chose the approach below is because it doesn’t need to run as ‘root’, which is a good thing, especially when you are running it in conjuction with a we server, which is what I will be doing.

Two software packages actually are needed to get things to work (that is, if you are programming in Python and not C). First, you need the C library and then you need the Python library that talks to the C library, something that is called a ‘wrapper’ in software parlance. The Python library conveniently insulates us from all that nasty low level C language, you know, the stuff adored by real geeks.

As to how to do all this, I read this webpage, which is a pretty good start:


The thing to do there is NOT to overlook the phrase ‘First follow the instructions found here to install WiringPi’, which is exactly what I did, and then caused a whole heap of unnecesssary trouble. You see, that caused me to skip the C portion. So install the C portion of WiringPi first, using Gordon’s excellent instructions.

Next, follow Sebastian’s set of instructions for the Python wrapper.

If everything installs properly, you should be able to run this application in Python:

import wiringpi
from time import sleep
import os
os.system("gpio export 18 out")
os.system("gpio export 24 out")
while True:
    print wiringpi.digitalRead(18)
    print "18 on"
    print wiringpi.digitalRead(24)
    print "24 off"
    print wiringpi.digitalRead(18)
    print "18 off"
    print wiringpi.digitalRead(24)
    print "24 on"
With the proper setup, this will cause 2 LEDs to blink. What is the proper setup you ask? It is this:

With the Raspberry Pi’s micro USB power connector situated on the top left and the General Purpose Input Output (GPIO) pins on the right hand side of the board, go to pin 6 (physical pin 6). To get to pin 6, start at the upper right hand side of the board and count down 6. That pin is pin 18 in the above setup. Trust me. If you want to know more, go here. For pin 24, the pin to go to is pin 9. Now get yourself some wire, 2 LEDs and 2 220 or 270 Ohm resistors, doesn’t really matter too much. Put an LED in series with a resistor, hook one end up to pin 6 and the other to pin 3 (ground). Repeat for the next LED/resistor pair: pin 9 and the other end to pin 3. Now run the above program. It helps to have a bread board and some connecting wires: an old hard drive ribbon cable is great.

My contribution to the overall progress of technology in this case is embedding the lines os.system(“gpio export 18 out”) (the same for 24). These 2 lines make pins 18 and 24 available to the current user, e.g. pi. In theory, if you script has a proper ending it should call the line os.system(“gpio unexport 18”) (the same for 24), in order to restore the system to the way it was, so another user can use those pins.

Sending an SMS from Raspberry Pi using Python, Gmail

Sending an SMS using Python on the Raspberry Pi is as easy as, well you know, pie.

   1: #!/usr/bin/env python
   3: import smtplib
   4: import sys,datetime
   6: def sms(user, password):
   7:     """Login and attempt to send message"""
   8:     try:
   9:         mailserver.login(user, password)
  10:     except smtplib.SMTPException:
  11:         print 'Problem logging in'
  12:         sys.exit(1)
  13:     body = 'Test'
  14:     to = '5195554444@txt.bellmobility.ca'
  15:     print 'Sending...\n'
  16:     try:
  17:         mailserver.sendmail(user, to, body)
  18:     except smtplib.SMTPException:
  19:         print 'Couldnt send'
  20:     else:
  21:         print 'Message sent at ' + cCurrentTime = str(datetime.datetime.now())[:19]
  22:     mailserver.quit()
  24: if __name__=="__main__":
  25:     mailserver = smtplib.SMTP('smtp.gmail.com')
  26:     print mailserver.ehlo()
  27:     print mailserver.starttls()
  28:     print mailserver.ehlo()
  29:     user = 'yourgmailusername@gmail.com'
  30:     passw = '1a2b3c4d5f'
  31:     sms(user, passw)      

Python 2.7 will already have the smtplib as well as sys installed, so no extras are required.

Simply replace the phone number,carrier,gmail userid as well as gmail password with your own values and it should run.

You can remove the ‘print’ statements from before the mailserver.ehlo, starttls and ehlo methods. They are there just to give you some feedback just in case sending doesn’t work.

My example lists txt.bellmobility.ca as carrier. Yours likely will be different. You can get a list of carriers here.

I am not sure if gmail processes these SMS messages for any carrier outside North America.

Naturally, your recipients won’t be able to respond to your message, so it really should be used to send one way messages only. Such as when you are 5,000 km away on vacation and your homebrew alarm system sends you a message that someone just broke into your front door and absconded with your brand new 60” LED flat panel TV. It is always better to be informed.