Sunday, September 29, 2013

Raspberry Pi Programming & Alternatives to Python: Bash shell, PHP, C#

Rasberry Pi is a cool device, no doubt about it. But it comes with a language many of us don't like or are not willing to use: Python. So, in this post I will show you some other ways to program in Raspberry Pi.

To make things more practical, I will apply this code to some simple but real world example: The reading of the pushing of a switch button.

What do we need for the example?

Ingredients:
  • One breadboard. It's practical with it!
  • A pack of cables: 3 male-male jumpers and 3 female-female jumpers.
  • A miniature switch. This is like the "reset" switch on Arduino, by the way.
  • One 1K resistor.
  • A led. Not really necessary, but it's useful to confirm for testing/debugging that the button is really pressed and that the circuit is closed.
You can find many "Starter Kits" on e-bay that include most of this stuff.

Execution:

  1. Put the switch somewhere in the middle of the breadboard. Let's say you put the two pins of the switch on rows 13 and 15 of the breadboard. Make sure there is a lot of space below the switch, so putting it on column "e" would be a good idea. So, a good place to put the switch is on e13 and e15 on the board.
  2. Put the led with the negative side (short leg) in c15 and the positive side (long leg) in c17.
  3. Put the resistor legs in c13 and c10.
  4. Now connect each male-male cable with a female-female so that you have a long cable that is female-male. It's good to have them connected in similar colors.
  5. Connect the pin "1" from Raspberry (the pin closest to the SD card, it's also marked on the board) to the breadboard pin a17. This is the +3,3V pin on the Raspberry.
  6. Connect the third pin (from the top) next to the column of pins where you connected the +3,3V cable and plug it in pin a10 on the breadboard. This is the ground pin on the Raspberry.
  7. Connect the 4th pin from the top on the Raspberry on the same column where you connected the +3,3V and plug it on a13 on the breadboard. This is the GPIO 4 (Broadcom) on the Raspberry.
If you followed the above instructions, your setup should look like this:





Initial System Setup
First of all, we will need to make some initial setup. We need to tell the operating system that we are going to use (Broadcom-based numbering) pin number 4 to read the pressing of the switch. We do this as follows:


sudo -i
echo "4" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio4/direction


Explanation about the filesystem structure and GPIO can be found here.
A very useful Raspberry Pi pin out layout for both Broadcom and Raspberry numbering in Excel format can be found here.

1. Bash Shell
The good thing about Bash shell is that it comes "preinstalled", it's already in the Linux system, so no additional packages to be installed. Pretty useful when you have limited space on Raspberry (e.g. when with a 2Gb SD card) or when off-line.

We write the following Bash shell script (e.g. with vi editor):

#!/bin/bash
while true;
do
PIN="`cat /sys/class/gpio/gpio4/value`"

if [ "$PIN" = "1" ]; then
                echo Button pressed!
                sleep 1
fi
done

let's say we save it as "read.sh" in the current directory. We then execute:

chmod +x read.sh

to make the file executable. And finally we type:

./read.sh

Whenever we press the switch button we will now see the "Button pressed!" message. To stop program execution press CTRL+C.

2. PHP
The default Raspbian distro does not have PHP installed, so we need to install it:

sudo apt-get install php5-cli

We then create the following file:

<?php
        while(true)
        {
                $pin = file_get_contents('/sys/class/gpio/gpio4/value');
                if ($pin==1)
                {
                        echo "Button pressed!\n";
                        sleep(1);
                }
        }
?>


let's say we save it as "read.php". Then we type:

php read.php

to run it. Similarly as in Bash Shell, we press CTRL+C to stop execution.

3. C#
To run C# under Raspberry Pi we will need the Mono environment. To install it, run:

sudo apt-get install mono-complete

After installation is complete, create the following file:

using System;
using System.IO;
using System.Threading;

class Program
{
        static void Main(string[] args)
        {
                while (true)
                {
                        var pin = File.ReadAllText("/sys/class/gpio/gpio4/value");
                        if (pin.Trim()=="1")
                        {
                                Console.WriteLine("Button Pressed!");
                                Thread.Sleep(1000);
                        }
                }
        }
}

let's say we save it as "read.cs". Then we need to compile it:

mcs read.cs

and finally we need to make it executable:

chmod +x read.exe

we run it as follows:

./read.exe

As, always, with CTRL+C we stop execution.

By the way, an interesting Raspberry Pi .NET library can be found here.

Happy coding!

1 comment:

  1. It is very good to have options when programming. As they say when you are a hammer, everything looks like a nail. There are times when a shell script may be all that is needed, or that C# is a good fit, or Perl or Python fits the bill. Knowing the strengths and weaknesses of each is a great skill to have so when you have a problem you can pull the correct tool out of your toolbox.

    ReplyDelete