Tuesday, October 30, 2012

Raspberry Pi GPIO Input Output

Raspberry Pi GPIO with Python 3 and Fedora 17

In my last post Raspberry Pi with Fedora 17 we setup the Raspberry Pi with the Fedora 17 Remix. Now we want to use the RPi.GPIO module to access inputs wired to the pins on the RPi. The General Purpose Input/Output (GPIO) is a generic pin on a chip whose behavior (including whether it is an input or output pin) can be controlled (programmed) by the user at run time and accessed via the RPi GPIO P1 header.
 This header is connected directly to the Broadcom BCM2835 chip that is under the Samsung memory chip PoP (Package on Package) on the right. You can tell if you have 256Mb or 512Mb of memory by looking at the number just under the word Samsung on that chip. If it has "4G" it is 512 and if it has "2G" it is 256. You can connect to the GPIO pins with the Pi Cobbler Breakout Kit from Adafruit.com for $7.95.
You will need a breadboard and and experimenters kit with LED's, resistors and breadboard jumpers to get started. If you don't have the RPi yet you could get the Raspberry Pi Starter Pack from Adafruit for $105 that has everything you will need to start experimenting.
Now that we have all that we need we can setup the circuit to run the input test on the breadboard.

Boot up the RPi and log on the as the user pi. With the Graphical log on running you may not have enough memory to setup the development environment so we need to shutdown X and set the default log on to console only:

How do I change the default runlevel to 3 to boot to the console in Fedora 17?
$ su
# rm /etc/systemd/system/default.target
rm: remove symbolic link `/etc/systemd/system/default.target'? y
# ln -sf /lib/systemd/system/multi-user.target \ /etc/systemd/system/default.target

Now reboot and make sure that the RPi starts up to a text log on:
# reboot

Now you can SSH back into the RPi as the user pi and change to root then install the dev tools:

# yum groupinstall 'Development Tools'

Transaction Summary
Install  35 Packages (+35 Dependent packages)
Upgrade              (  4 Dependent packages)

Total download size: 128 M
Is this ok [y/N]:Y

This would be a good time to take a break and consume some of your favorite beverage.
After that is complete you will need to install Python 3 (Python 2.7 is installed) to get the latest RPi.GPIO to install.
$ su
# yum install python3.armv5tel python3-devel.armv5tel

Now that we have some memory freed up we can get the GPIO module installed:
You can get the exact download path from http://pypi.python.org/packages/source/R/RPi.GPIO/
and copy the link address from the green download button.

# wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.4.0a.tar.gz

Now extract the tarball:
# tar zxvf RPi.GPIO-0.4.0a.tar.gz

cd into the directory:
# cd RPi.GPIO-0.4.0a/

# python3 setup.py install

To test this install drop into python and check GPIO:

# python3
Python 3.2.3 (default, Jun  9 2012, 06:36:42)
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>> GPIO
<module 'RPi.GPIO' from '/usr/lib/python3.2/site-packages/RPi.GPIO-0.4.0a-py3.2-linux-armv6l.egg/RPi/GPIO.cpython-32mu.so'>

Now drop back to the pi user with [CTRL]+[D] and cd into pi.

# cd /home/pi

Create a file called mybutton.py
$ vi mybutton.py
press "i" to enter the -- INSERT -- mode and then type in the program:


import time
import RPi.GPIO as GPIO
GPIO.setup(11, GPIO.IN)

while True:
        mybutton = GPIO.input(11)
        if mybutton == False:
                print ("Button Pushed")

When done press [Esc] to exit the insert mode and press ":" (or [Shift]+[;]) and enter wq and press [Enter]

This will write the file to disk and quit.

With the circuit wired up we can run mybutton program:

$ sudo python3 mybutton.py
[sudo] password for pi:
Button Pushed
Button Pushed

So when you press the button you should see the LED flash and "Button Pushed" printed to the console.
To exit the running program press [CTRL+C]

The program imports the needed modules and sets the GPIO pin 11 to an input.
Then the GPIO.input reads pin 11 as a True or False state.

A note on the pinouts:
  Use GPIO.setmode(GPIO.BOARD) to use Raspberry Pi board pin numbers (The ribbon connector pins) and use GPIO.setmode(GPIO.BCM) to change to BCM GPIO numbering (GPIO numbers on the CPU pins).
  For most tutorials we will use the GPIO.BOARD numbering.

The next step is to use the GPIO as an output. Set up the circuit below by moving the 470 Ohm resistor 90 degrees and adding the GPIO 18 as input pin 12.

Next, create a new file called pushbutton.py 

# vi pushbutton.py

Press "i" to get into the insert mode and type (or copy and paste from) the program below:


# pushbutton.py

import sys, time
import RPi.GPIO as GPIO
GPIO.setwarnings(False) # Disable Warnings, Channel in use
GPIO.setup(11, GPIO.IN)
GPIO.setup(12, GPIO.OUT)

while True:
    if GPIO.input(11):
        GPIO.output(12, True)
        sys.stdout.write("\b\b /")
        sys.stdout.write("\b\b -")
        sys.stdout.write("\b\b \\")
        sys.stdout.write("\b\b |")

        GPIO.output(12, False)

Then hit the [ESC] key to exit the insert mode and press ":wq" to write and quit vi. Now run it...

# python3 pushbutton.py

This time we imported the sys module to have a little more control with output to the console.  The GPIO 18/Pin 12 is set to be an output line and when the input Pin 11 is pulled to a low state we set the output line Pin 12 low to pull down that side of the LED so that we have a voltage drop across it and it lights up.

The sys.stdout lines print a spinner to the console while waiting for a button press, then prints "on" when the button press sets the LED to on.

The GPIO.setwarning(False) disables the warning that is displayed because we didn't run GPIO.cleanup() on exit. Instead we just press [CTRL]+C to exit the running program (and not getting a chance to cleanup IO first).

If you want more detail on "Interfacing Projects for Beginners" then check out the "In Control" columns Part 1 and 2 that I used as a reference from the Raspberry Pi magazine TheMagPi.com issues 2 and 3.

Next time I will try the temperature project with the I2C  sensor tmp102 from issue 5.

Sunday, October 14, 2012

Raspberry Pi with Fedora 17

Raspberry Pi with Fedora 17      

The Raspberry Pi is a credit card sized single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools.

The Raspberry Pi has a Broadcom BCM2835 system on a chip (SoC), which includes an ARM1176JZF-S 700 MHzprocessor (The firmware includes a number of "Turbo" modes so that the user can attempt overclocking, up-to 1 GHz, without affecting the warranty), VideoCore IV GPU, and 256 megabytes of RAM. It does not include a built-in hard disk or solid-state drive, but uses an SD card for booting and long-term storage. The Foundation's goal is to offer two versions, priced at US$ 25 and US$ 35 ("B"). The Foundation started accepting orders for the higher priced model "B" on 29 February 2012.
The Foundation provides Debian and Arch Linux ARM distributions for download.

I have just received my Raspberry Pi and I also see that a Fedora 17 remix for the Pi is in the works so since I prefer Fedora I will go over the install in this post.

The post about the remix can be found here "Raspberry Pi Fedora F17 Remix – Test image 4" and the direct download is here test-release Ex:rpfr-17-xfce-RC2.zip 
Download the image to your computer and extract the .img file. I will demonstrate how to put the image onto an SDHC card from a Windows 7 system as this would be the most likely system someone new to Linux would use. We will get into Linux more when we start using the RasPi board.
Insert the SD card into the PC, I am using a SanDisk SDHC 4, 4GB card. You can get one for about $9 just about anywhere.
Download the Win32DiskImager-binary.zip from the link at RaspberryPi.org site and extract the exe and files to a directory. Open the extracted directory and right-click on Win32DiskImager.exe to "Run as Administrator"
You should see the SD card detected as [E:], if not select the correct location.
Then click the folder button to select the RasPi image...
Then click SAVE. Now with the image ready to go, press WRITE and then confirm the OVERWRITE.

Now would be a good time to get the RasPi board setup to go. After the write process is complete, click OK then EXIT the Imager and safely remove the SD card being sure to eject the card first.
Right-Click on the boot [E:] disk and click EJECT
Pull the SD card from the PC and insert it into the RasPi. Then with the keyboard/mouse plugged into the USB port, the HDMI from the monitor plugged into the HDMI port and the network plugged into the Ethernet port you can finally plug the mini-USB from the power supply into the mini-USB port to start the boot process. (The network should have DHCP enabled for this demo).
You should see the boot logo (maybe a few times).
The first screen will be the License Information, click FORWARD.
Select the US Keyboard and click FORWARD.
Select your Time Zone and click FORWARD.
Select Network Time and click FORWARD.
On the next screen you can choose how much disk space to use for the OS on the SD.
You will want to use it all, so drag the slider all the way to the end and click FORWARD.
On the next screen you will enter the Root users password (Don't forget this) and click FORWARD.
On the next screen you will create a user account for yourself, for the RasPi this user will need to be Pi for things to work when using imported packages in your code, so enter Pi and a password, then click FORWARD.
On the last screen you can go with the default System Settings and click FORWARD.
After a few reboots to re-size the disk partition, you can logon as Pi.
 After logon, open the terminal and run the command ifconfig to get the IP address.
Now with this IP you can remote SSH into the Raspi with a terminal program like Putty.

In the next post I will see what we can do with the RasPi in the real world by using the GPIO inputs and outputs.

Monday, April 30, 2012

Chrome browser client-side data storage

Web Storage: client-side data storage

The localStorage attribute is an object provided by the www.w3.org specification under Candidate Recommendation review.  This specification defines an API for persistant data storage of key-value pair data in Web clients.  Web Storage offers two different storage areas, local storage and session storage which differ in scope and lifetime. Data placed in local storage is per domain for each client and persists after the browser is closed.

Here is a simple demo html file for local storage...
<!DOCTYPE html>
<!-- LocalStorageDemo.html -->
<title>Web Storage Example</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >

<script type="text/javascript">
window.onload  = function(){

function loadvalues(){

function savethestuffLocal(){
 var object = document.getElementById("localinput");
 var thevalue = object.value;
 localStorage.setItem(1, thevalue);
function getthestuffLocal(){
 var data;
 var thediv = document.getElementById("displaylocal");
 data = localStorage.getItem(1);
 if (data){
  thediv.innerHTML = "The value is "+data;

<h1>Web Storage Example</h1>
<h2>Local Storage</h2>
<div id="displaylocal">Seems there is nothing stored in local storage yet</div>
<input type="text" name="" value="" id="localinput" />
<a href="javascript:savethestuffLocal();">Save it!</a>
<p id="local_storage_infio">This data is stored using Local Storage. Which means that even if you close the page, and re-open it, the data you saved should be loaded again.</p>

Here, we look at a simple demo (from the code above) for local storage...

Web Storage Example

Local Storage

Seems there is nothing stored in local storage yet
Save it!
This data is stored using Local Storage. Which means that even if you close the page, and re-open it, the data you saved should be loaded again.

When you type something in the input box above and click "Save It!" the data is stored on your local system in your browsers Local Storage area for the domain blog.poling.org and the local path (in my case)...

/Users/drew/Library/Application Support/Google/Chrome/Default/Local/Storage/http_blog.poling.org_0.localstorage

This file is in the SQLite format 3 binary format for Chrome and contains the key 1 and value of the data entered...

$ cat  Local\ Storage/http_blog.poling.org_0.localstorage 
?????z!11?tableItemTableItemTableCREATE TABLE ItemTable (key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB NOT NULL ON CONFLICT FAI1test1indexsqlite_autoindex_ItemTable_1ItemTable

You can also see the data from Chromes Developer tools, open VIEW --> Developer > Developer Tools
then click on Resources, under Local Storage click on blog.poling.org to see the value...

For more info you can take a look at Shwetank Dixit's post on Web Storage.

Next time I will take a look at HTML5 Filesystem.

Tuesday, February 7, 2012

Google launches a TED like event "Solve for X"

Official Google Blog: What’s your X? Amplifying technology moonshots: Last week, Google ran an experiment. They hosted a gathering, called “Solve for X,” for experienced entrepreneurs, innovators and scientists from around the world. The event focused on proposing and discussing technological solutions to some of the world's greatest problems. ...