The Raspberry Pi is a pint sized computer designed for the experimenter and maker. But to put this small computer to work we need to be able to program it.

Since the OS on the Raspberry Pi is Linux we can take advantage of all the programming languages available on Linux. But also being a small computer, means it does not easily support the graphical and programming applications available on a Linux Laptop. Besides, one of the best way to work on a Raspberry Pi is over SSH.

So we are going to configure a Raspberry Pi for remote access over SSH, then explore how to program it for some temperature measurements using both Python and Perl.

Setup for Remote Access

Since I have already covered this in my talk Raspberry Pi: Introduction, I am not going to repeat the steps here. The information you need is Setting up the Raspberry Pi from Scratch, Setting up the Raspberry Pi for remote access, and Setting up a SAMBA Server on Raspberry Pi.

In short we are setting up the Raspberry Pi for remote access. This does not mean that we must use remote access, it only means that we can use remote access. Unlike the Arduino, where the programming is done on a host computer and uploaded to the Arduino, all the programming for the Raspberry Pi can be done on the board.

This does not mean that it can not be done on a host computer and uploaded, but it does not need to be programmed on another computer.

Being able to access and program a Raspberry Pi remotely means that you can revise and update the programs from a different location. For example, suppose I setup a weather station for my house. I would place the Raspberry Pi in a box near the sensors, which would be in the yard or above my roof. If at some point I would want to revise the code on the Raspberry Pi, I would not need to get the board down from it’s remote location to work on it. I could just use SSH to access the board remotely. Even though this is possible in windows, it is much easier in Linux to work with headless computers. Headless computers have no display, keyboard, or mouse normally.

I discovered a couple of issues with the Samba server setup. I ignored the grep command to copy the smb.conf. I just took the [data] section and pasted it to the end of /etc/samba/smb.conf file. The other issue I found is that when you mount the samba share you need to use the pi creditionals. This means that when mount it from Linux, on /mnt/weather, I used the command

mount -t cifs  // /mnt/weather --verbose -o username=pi,password=raspberry

I then found that only root on the Linux box could copy files into the samba share, but the local user, john, could read them. On the Raspberry Pi, the Pi user could read and write the share point.


For this discussion of programming we are going to discuss the use of the Inter-integrated Circuit (I2C) Protocol.

The Inter-integrated Circuit (I2C) Protocol is a protocol intended to allow multiple “slave” digital integrated circuits (“chips”) to communicate with one or more “master” chips. Like the Serial Peripheral Interface (SPI), it is only intended for short distance communications within a single device. Like Asynchronous Serial Interfaces (such as RS-232 or UARTs), it only requires two signal wires to exchange information. I2C

I chose this as a starting point since it is a simple interface for talking to some of the sensors.

In order to setup the I2C interface lets work from Raspberry Pi SPI and I2C Tutorial.

HTU21DF in C

OK now that I have a C program that works with the device we are going to have a look at the source code from htu21df temperature and humidity sensor support for Raspberry Pi which I am displaying in HTU21DF in C.

HTU21DF in Python

As an example of how to access the htu21df module in python I have copied the examples from dalexgray/RaspberryPI_HTU21DF in python. It uses the The pigpio library, to interface with the htu21df.

Lets look at the code it uses, both for test and for operation in htu21df in python.

MPL3115A2 in perl

I have a version of the code to run the MPL3115A2 sensor using perl. The code comes from Raspberry Pi and Repeated Start I2C, and uses a perl module HiPi modules for Perl.

Lets have a look at the code and the HiPi modules from the sites above.

MPL3115A2 in Python

As an example of accessing mpl3115A2 in python I have copied code from embest/rpi_sensor_board. I downloaded the library snesor.so and run the program mpl3115a2.py.

Lets look at the code is used. MPL3115AW IN Python

Finding and Installing Modules

One issue is how do you find and install modules for specific hardware. Before you purchase a new sensor or controller to use with your Raspberry Pi, you should check if there exists a programming module for that device. You can program sensors by hand, but it is much more complex than using an already defined module.

So lets look at how to find and install modules in perl and python.

Perl Modules

If you are looking for perl modules, a good place to look is HiPi Perl Modules. Or you can check out the Perl archive for HiPi on Cpan. Additionally you can directly search the Cpan archive by doing a search for Raspberry Pi.

When it comes time to install this set of modules have a look at HiPi install on Raspberry Pi.

Python Modules

Searching for Python modules can be done in PyPI - the Python Package Index. To be more specific you can search the archive for Raspberry Pi Search.

Another interesting site is Adafruit’s Raspberry-Pi Python Code Library.

Once you have your modules selected you can use Installing Python Packages for info on how to install the modules on the Pi.

Google is your friend

One of the simplest and often the best way to find modules and examples is to do a Google Search. The number and quality of the code you can find online is constantly growing.

Debugging your code

Writing code is not like building a house. You draw up a plan and build it all at once, since the architect has done all the planning.

Writing code is more like exploring a new lego set. You read the available information, then build sample programs to test ideas. Once you have built and tested all the functions, you can combine them into a final program.

Perl Debugging and Documenting

Since perl has been used for a number of years, there are a number of debugging tools and methods available. Here are a list of debugging tools and ideas. Some I have discovered myself, some are from the web.

  • Script Checker is a method I use ofter to make sure I have not made any typo’s in my code. I use the command perl -cTw <filename>. This tells perl to parse the script to look for errors.

  • PerlDebug is a debugger built into perl. For a full explanation have a look at perldebug. Additionally there is a Tuturial at PerlDebTut. The web site perldoc.perl.org is a great site for help with perl.

  • Log4Perl is a way to capture debug information when running a script. For information on using this have a look at Logging with Log4perl the easy way, as well as 8 Useful Log::Log4perl Recipes. I use this as a way to capture a log of what is happening with the script.

  • POD stands for Plain Old Documentation. This is a method of writing documentation inside a perl script. It allows you to document your code in a format that can be extracted using the command perldoc <filename>. This allows you to document what you were doing when you wrote the program for later reference. See PerlPod.

Python Debugging and Documenting

Written by John F. Moore

Last Revised: Wed Oct 18 11:01:24 EDT 2017

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
HTML5 Powered with CSS3 / Styling, and Semantics