Python on Raspberry Pi with Buildroot


I’m in need of python on my custom embedded Linux system for Raspberry Pi. This post shows how I enable it. For some reason python build was failing with errors such as

build error: unknown type name ‘wchar_t’

A clean build, as follows, resolved it

make clean
make

To add python to your Buildroot config, invoke

make menuconfig

Enable WCHAR support, under Toolchain

WCHAR support

Enable python or python3, under Target packages, Interpreter languages and scripting

Python

Driving an LED with a Raspberry Pi


I recently began tinkering with a Raspberry Pi Model B rev2. This post shows how you can drive an LED using the GPIO pins on a Pi running Raspbian. If you are unfamiliar with Pi take a look at their quick start guide.

GPIO Pin Numbering

There are two ways to specify the GPIO you want to control. One is based on the numbering of the pin on the P1 connector, the other is based on the Broadcom SOC (BCM2835) GPIO numbering. For instance, pin 12 on P1 connector corresponds to pin GPIO18 on BCM2835.

This can be derived from the screenshots of the schematic below, and is also documented elsewhere on the internet for easier consumption.

P1

BMC2835

RPi.GPIO Python module

The RPi.GPIO module is available on Raspbian. Let’s assume you connect the LED as shown below. I have come to realize that the current driven by GPIO is low enough, that you can drive the LED shown without need for a resistor in series.

Driving a LED using Pi (Fritzing)

This is how you can blink an LED using P1 connector pin numbering, on the command line.

sudo python
import RPi.GPIO as gpio
gpio.setmode(gpio.BOARD)
gpio.setup(12, gpio.OUT)
gpio.output(12, gpio.HIGH)
gpio.output(12, gpio.LOW)

This is how you do the same thing using BCM2835 GPIO numbering.

sudo python
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(18, gpio.OUT)
gpio.output(18, gpio.HIGH)
gpio.output(18, gpio.LOW)

The project Wiki has many more examples.

Using GPIO filesystem

You can achieve the same result from the shell using the GPIO filesystem under /sys/class/gpio. The pin number needs to be the GPIO number specified for BCM2835. You cannot do PWM with this mechanism, except at very low frequencies.

sudo echo "18" > /sys/class/gpio/export
sudo echo "out" > /sys/class/gpio/gpio18/direction
sudo echo "1" > /sys/class/gpio/gpio18/value
sudo echo "0" > /sys/class/gpio/gpio18/value
sudo echo "18" > /sys/class/gpio/unexport

The first two lines initialize and set the direction for GPIO18. The possible values for direction are “in” and “out”. This approach can be used from any programming language, using its text file input/output implementation.

If you have installed mono (sudo apt-get install mono-complete), you can use csharp shell to achieve the same results.

sudo csharp
using System.IO;
if(!File.Exists("/sys/class/gpio/gpio18/value"))
{
File.WriteAllText("/sys/class/gpio/export", "18");
}
File.WriteAllText("/sys/class/gpio/gpio18/direction", "out");
File.WriteAllText("/sys/class/gpio/gpio18/value", "1");
File.WriteAllText("/sys/class/gpio/gpio18/value", "0");
File.WriteAllText("/sys/class/gpio/unexport", "18");

The example above was inspired by the RaspberryPi.NET implementation.

PyCharm 3.0 Community Edition


PyCharm, now in its third edition, is an excellent Python IDE from the folks at JetBrains, the creators of IntelliJ. It has a Professional edition, and a fairly competent Community Edition (CE) available for free. The latter edition is an open source project hosted at GitHub. Professional edition has support for various frameworks such as Django.

I find editing, refactoring, executing, and debugging Python code very effective. The integrated Git support is also quite good, although I couldn’t get it to work with my remote repository for some reason. I’ll be using PyCharm CE for Python programming going forward.

Install python and pip on Mac OS X


Mac OS X ships with python. You can see its version using

python --version

You can see where it is installed using

which python

You may want the latest version of python, or install python packages using pip. The way I found to get the latest versions of python on Mac is using macports. To install macports download the packaged version from their site and install. Further instructions can be found on their install page.

Once macports is installed, you can install latest version of python 2 thus

sudo port install python27

To learn all python versions available

port list python*

To find out all python packages available

port list py*

To install pip

sudo port install py27-pip

Now that pip is installed you can install python packages. To see installed packages

pip-2.7 list

To search for available packages e.g. all packages with Django in the text

pip-2.7 search Django 

To install django

sudo pip-2.7 install Django==1.4.8

To see more details about a package such as where it is installed

pip-2.7 show Django

To use pip packages with the version of python that ships with OS X, export their path to PYTHONPATH and call python

export PYTHONPATH=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

Learning Python by Mark Lutz; O’Reilly Media


Learning Python

Learning Python, now in its fifth edition, is a humongous tome that covers Python 2 and 3. In covering both versions of Python, it does a tremendous service to the beginner. As a new Python programmer who’s well-versed with other programming languages, I found the book easy to get up to speed with, and its verbosity and thoroughness an asset rather than a liability.

Part 2 of the book has excellent coverage of basic types and data structures. Having read Parts 1 and 2, Parts 3, 4, and 5 seem repetitive sometimes, and are probably most useful as a reference.

Chapter 3 makes a quick reference to pdb and Winpdb, but the coverage of debuggers is otherwise quite sparse. Debugging multithreaded apps without a good debugger is a pain. Not a huge omission as the book does not cover multithreading.

Chapter 9 covers the json module briefly, but the book covers neither socket nor httplib/http.client modules. Network and web programming are required knowledge even for beginners these days. Programming Python, by the same author, does cover these topics in great detail.

Chapter 25 covers advanced topics regarding modules, such as using dual mode code for implementing tests, but that is about the only coverage of unit testing you’ll find in the book. It refers you to the official documentation for modules such as unittest and doctest.

I found Test Your Knowledge Quiz and Answers at the end of each chapter useful. After having read syntax highlighted code in books such as Python Cookbook, I found the code samples in the book rather dull.

I thank O’Reilly Media for providing the book for review.

Python Cookbook by David Beazley, Brian K. Jones; O’Reilly Media


Python Cookbook

Python Cookbook is an extensive tome of recipes for the Python 3 programmer. It is a perfect companion book for those migrating Python 2 code to Python 3. If you are stuck with Python 2, you may still find the second edition of the book for sale, but the recipes may be dated as they cover Python 2.4. It is not a beginners book. If you are looking for a beginners book, I recommend Learning Python by Mark Lutz.

A quick chapter summary follows.

Chapter 1 has recipes involving manipulation of built-in structures such as dictionaries and sequences. Usage of heapq module for implementing priority queues is demonstrated. Chapter 2 covers string and text manipulation, with extensive use of regular expressions. Chapter 3 has recipes for working with numbers, dates, and times. Usage of numpy module for matrix and linear algebra calculations is demonstrated.

Chapter 4 provides recipes for implementing iterators and generators. Chapter 5 covers File and I/O, including recipes for reading and writing compressed files, memory mapping binary files, and communicating with serial ports. Chapter 6 moves on to more advanced recipes for encoding and processing, such as reading and writing CSV, JSON, XML, Hex digits, and Base64.

Chapter 7 provides recipes for functions and closures. Chapter 8 provides recipes for classes and objects, such as creating managed attributes, lazily computed properties, and extending classes with mixins. It also covers common patterns such as state, and visitor. Chapter 9 digs deeper into metaprogramming. Chapter 10 has recipes for modules and packages, such as for splitting a module into multiple files using packages, and loading modules from another machine using import hooks.

Chapter 11 provides recipes for network and web programming. I didn’t know you could use ip_network objects to generate IP addresses and check for membership. It also covers event-driven I/O but does not introduce any new framework. Chapter 12 has recipes for concurrency. It discusses implementing concurrency using generators (coroutines), but doesn’t cover frameworks such as gevent, it does mention PEP 3156 that covers those.

Chapter 13 has recipes for writing utility scripts for system administration. Chapter 14 has recipes for unit testing, debugging, exception handling, and profiling. Chapter 15 wraps it up with recipes for extending Python using C.

I’ve added this book to my list of references to look into, before heading to Google. Source code listings use syntax highlighting, a nice touch that makes the code easier, and less boring, to read.

I thank O’Reilly media for providing the book for review.

Handling TCP Keepalive


TCP Keepalives are useful for scenarios where one end of the connection disappears without closing the connection. This can happen when a NAT or firewall resets, or forcibly closes the connection. The following code in Python enables sending a Keepalive message when there is no data activity over a socket for 60 seconds. If it does not get a response, it sends Keepalive messages 4 times at intervals of 15 seconds. After that, the connection is closed. The code has been tested with Python 2.7.2 and Ubuntu 12.04.

To test, create a TCP listener/server using netcat on a different PC (or nc on a Mac)

netcat -l 192.168.0.120 8001

Now, disable the network connection, enable a firewall, or power off the router, to see Keepalive in action. Wireshark highlights Keepalive messages when TCP sequence number analysis is enabled.

You’ll see the following messages when the connection times out

Traceback (most recent call last):
  File "socket_test.py", line 39, in do_work
    req = sock.recv(10)
error: [Errno 110] Connection timed out
Other Socket err, exit and try creating socket again

On Mac and Windows you can enable Keepalive, but cannot set TCP_KEEPIDLE and other parameters. You’ll get the following error message (Python 2.7.2 with macports) if you try to do so

Traceback (most recent call last):
  File "socket_test.py", line 65, in <module>
    do_work()
  File "socket_test.py", line 19, in do_work
    sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 60)
AttributeError: 'module' object has no attribute 'TCP_KEEPIDLE'