Python Exemplary - RPi Tutorial
deutsch     english    

DEVELOPMENT TOOLS

 

 

Introduction

 

The main programming language for the Raspberry Pi is Python. Countless programs and library modules are available for all kind of activities using the GPIO interface. But the number and the quality of development environments for Python is still poor. Because Python is an interpreted language, the development cycle is simple: Any text editor can be used to write the source program (often call a Python script). Then the Python interpreter is started that displays an old-style (black) terminal window and the script is loaded and executed using old-fashioned command lines. In modern times where people are accustomed to sophisticated graphical user interfaces, this way of working is outdated.

There are several attempts to overcome this deficiency, but modern Python IDEs that are simple to use, but hides the ugly Python shell are still rare. We recommend PyCharm (by JetBrains) for the professional programmer and TigerJython (by Tobias Kohn) and Geany for occasional users, kids and students.

Whatever you decide to use, do not underestimate the importance of a simple and stable development environment for beginners. Frustrations with the IDE and the lack of appropriate error messages may spoil your teaching efforts and break down interest and motivation for programming.

In this section we present current procedures for programming Python on the Raspberry Pi. But finally we recommend "remote development" with an IDE that runs locally on your notebook/desktop. This shields you from the details of the Linux operating system. The development cycle then resembles to the procedure commonly used with other microprocessor systems like the Arduino, where programs are written/compiled on a "host" and downloaded to the "target" for execution. But in contrast to the Arduino, it is not the machine program, but the source script that is downloaded to the target, where the Python interpreter executes it. Furthermore to facilitate debugging, all output to stdout and stderr (print and error messages) are automatically reported back to the development system and shown in a terminal window without changing any line of code.

 

 

Addons to the NOOBS Distribution

 

We provide some useful addons to the NOOBs installation as GitHub distribution. Moreover you can download our 8GB SD-card image from here and you are ready to go. Consult the RaspiBrick installation site for more information. The main features of our RaspiBrick firmware are:

  • Autostartup displaying IP address (playing/showing last three numbers as Morse code on GPIO pin 22)
  • Autostart autonomous program /home/pi/scripts/autostart.py
  • Setup utility RaspiBrickConfig (Java program) in FAT partition to select the hotspot SSID/password at next boot
  • Preinstalled VNC server (IP port: 5901, password: 123456)
  • Preinstalled Java IDE BlueJ and RaspiTransfer tool for the development if Java programs
  • Preinstalled Bluetooth RFComm server written in Python for file transfer without TCP
  • Additional commands

     rkill Kills all Python processes
     rmon (Re)starts the RaspiBrick firmware
     pyrun <prog.py> Starts a Python program with raspilib modules in Python path  (as sudo)
     jrun <prog.jar> <main-class> Starts a Java program with RaspiLib.jar in Java classpath (as sudo)

  • Support of the Pi2Go robot. For more information about robotics, consult the Pi2Go site.
  • Automatic detection of Pi2Go mode. The autostart process assumes that GPIO pin 18 (battery monitor pin) is LOW to start the firmware in Pi2Go mode. For standalone operation, GPIO pin 18 should be left open (or HIGH) at boot time
  • Smooth integration into TigerJython's remote execution commands
  • Full source distribution on GitHub

 

 

Typical Development Scenarios


 

1 Screen/Keyboard/Mouse Attached to the Raspberry Pi

  To access the Raspi Linux system, a screen, keyboard and mouse can be plugged directly into the RPi ports. You may work in the Linux command shell or start the Raspian desktop. This is the most direct working environment given that you owe a HDMI or DVI monitor and you are familiar with Linux. It corresponds to the common way to use a Linux computer.

Log in as user: pi with password: raspberry (if you use a wrong keyboard layout, the y may be at the z position, but you don't see it because the password is not visible).

In terminal mode you write a Python script using any of the well-known console based editors:

  • nano (preinstalled)
  • vi (preinstalled)
  • vim (install with sudo apt-get install vim)
  • emacs (install with sudo apt-get install emacs)

To execute the script, type python <progname> or start the Python shell by typing python and run it with the command execfile("<progname>"). This is a very basic and old-fashioned way of programming, but it still works fine.

To start the Linux desktop, type startx. Now you can use one of the GUI based editors, like

  • IDLE (from Menu | Programming)
  • Leafpad (from Menu | Accessories | Text Editor)
  • TigerJython (preinstalled with RaspiBrick firmware, for more information how to use it, see below)

Python programming with IDLE is explained in many online tutorials. It is considered to be somewhat outdated.

2 Remote Terminal (SSH)

A. Access via Ethernet Hub/Switch/Router

The router delivers an IP address by its DHCP server, normally in the range 192.168.0.nnn or 192.168.1.nnn. There are many options to find out the last three numbers:

  • If you are the only one using the router, nnn starts often at 100
  • You may enter the router manager (often at 192.168.0.1 or 192.168.1.1) and check the "Connected clients"
  • Install an IP scanner, e.g. Angry IP Scanner, and search for the hostname (default: raspberrypi)
  • If the Raspberry Pi starts with the BrickGate firmware, the IP address is shown on a attached 7-segment display and the last 3 digits played on a buzzer or LED in Morse code at GPIO pin #18

There are some inconveniences with this scenario:

  • Each station needs a router
  • Modern notebooks do not have a built-in Ethernet port, an USB adapter is needed

 

B. Direct Connection via Ethernet

With a trick you can configure a notebook running Windows or MacOS so that it activates a DHCP server on its Ethernet port and thus assigns an IP address to a Raspberry Pi connected to it (if your notebook has no Ethernet port, use an Ethernet-USB adapter). For a Raspberry Pi embedded in a system, this is the recommended configuration.

The notebook is used as it would share its WLAN access to the Internet with another device connected to the Ethernet port (ICS: Internet Connection Sharing). It is not really necessary that the notebook is connected to the Internet via WLAN. The setup requires the following steps:

Windows 7/8/10:
Under Network Connections | Change Adapter setting, right click on the WLAN adapter and select Properties. Click on the tab "Sharing" and activate the option "Allow other network users...
" (Better you reboot now.)

ide2dide2e  

This will activate a DHCP server on the Ethernet port that provides IP addresses in the segment 192.168.137.nnn (determined by a Windows registry entry). To find out the last three digits, an IP scanner may be used (for example Angry Scanner, see above). Once the connection is established, the PC accesses the Raspberry Pi in the usual way, e.g. with PuTTY, WinSCP or VNC.

To prevent the IP address from changing, you can set a fixed IP address for the Ethernet port on the Raspberry Pi by editing the file /etc/dhcpcd.conf. For nnn = 12 insert the following two lines at the end of the file:

interface eth0
static ip_address=192.168.137.12

With our RaspiBrick firmware, this entry can be added with the set_static_ip command, for example

set_static_ip 192.168.137.12

Typing

set_static_ip remove

removes the entry.

Note that the Ethernet port of the notebook is no longer available for an ordinary router connection until you remove the share. (To do so, the USB Ethernet adapter must be plugged in.)

Remarks: You may also use a second Ethernet port instead of a WLAN. Remember that you must perform the share setup at the adapter that is connected to the Internet, not the adapter that is connected to the Raspberry Pi.

MacOS:
You find analogeous settings under System Settings | Sharing where you activate Internet in the left window. Select in the right window under Share connection: WLAN and With computers over: Ethernet. After booting, the Mac enables a DHCP server on the Ethernet port in the 169.254.124.nnn segment.

puTTY is not available on the Mac, but the Mac has a built-in SSH terminal. Just type in the the terminal window (for the IP address 169.254.124.10):

ssh pi@169.254.124.10

and you are logged-in with the password raspberry.


Note: The DHCP segments may vary with the version of the operating system. To find out the current address, you can access the Raspberry Pi while is is connected to the PC/Mac via a directly connected screen or WLAN, and type in a terminal ifconfig. Under the adapter name eth0 the address is visible. (For newer versions of MacOS the segment is probably 192.168.2.nnn.)

 

C. Access via WLAN Router/Hotspot/Access Point

 

Multiple Raspberry Pi's can be accessed with the same router. Modern devices have a Mac-to-IP binding option, so the Raspberry Pi gets always the same fixed IP address. A desktop PC may be connected to the router with an Ethernet cable. You can also run a virtual hotspot using a smartphone app.

There are some problems to overcome:

  • Institute routers cannot be configured by non-admins, so an extra individual router is needed. In a institute this may be prohibited and you may get difficulties to connect it to the Internet
  • How to setup the WLAN authentication without using a attached monitor/keyboard?
  • What IP address did you get?
  • WLAN dongle required (WLAN included in Raspberry Pi 3)

Once the IP connection between the Raspberry Pi and the host system (PC) is established, you may start a remote terminal program that supports SSH (port 22). The most famous free remote terminal program is PuTTY. Log-in as user: pi with password: raspberry and you are at the same point as in scenario 1, except that you cannot start the GUI desktop.

Instead of using SSH commands to transfer and manage files, the comfortable and free GUI based file explorer WinSCP is recommended. Unfortunately is only available under Windows.

 

3 Virtual Desktop

Prerequisites: A TCP link is established and the IP address of the Raspberry Pi is known (see above).

With the RaspiBrick firmware, a VNC server is pre-installed and automatically started at boot time. You can access the Raspbian desktop by any VNC client (viewer) running on the development system. There are several free products available for all major operating systems, among them TightVNC and VNC@Viewer. (VNC port number: 5901, password: 123456).

Remote desktop operation is widely used for all kind of server management. It provides the "feeling" of a locally attached terminal like in scenario 1 without the necessity to purchase a separate monitor / mouse / keyboard.

 

4 Remote IDE - PyCharm

Prerequisites: A TCP link is established and the IP address of the Raspberry Pi is known (see above).

There are some crucial advantages to perform program development on the host system:

  • Program sources and documentation are kept and managed locally on a PC or a cloud in a more secure environment than on the target system
  • Development with PC/Mac/Linux in a common environment without the need to deal with the Raspberry Pi OS
  • Same scenario as for standard microcontroller development with a edit/compile/link/download/execute cycle

There are not many development systems that fully integrate local editing, source download and execution with the target Python interpreter. One of the best is PyCharm, but as usual with professional tools, the project setup is by far not trivial. Since PyCharm is our favorite IDE for the professional development of Python programs, we enjoy its extension for remote execution on the Raspberry Pi (only available in the professional version, sales with academic discount). The execution window provides the look and feel like working locally on the Raspberry Pi. All error messages and print output are sent back to PyCharm and displayed in the bottom area. Finally programs can be started and stopped remotely without an extra terminal window.

The setup a project for the Raspberry Pi is somewhat tricky and is explained in more details here.

 

 

5 Remote IDE - TigerJython

TigerJython is a programming learning platform that comprises a special designed IDE with an full-featured text editor, a debugger, a Python console and many education oriented libraries for turtle graphics, sound, robotics, game development, database applications, etc. The distribution consists of a single JAR file that runs on every platform with a preinstalled JRE. Developed as part of a research project at the Swiss Federal Institute of Technology (ETH) in Zurich, it contains a unique Python parser that performs extended syntax error checking and reporting.

A special interface for Robotics with Lego Mindstorm EV3 and Pi2Go (a robot platform using the Raspberry Pi) is smoothly integrated. Python programs are written in TigerJython, downloaded to the robot and executed autonomously with the target Python interpreter (autonomous mode). Programs using the same library calls may also be executed on the host system (remote mode). They communicate with a TCP socket server that runs on the robot (BrickGate) and interprets the transferred commands to control the robot. A rich and carefully designed object-oriented robotics library for Lego EV3 and the Pi2Go is available.

To develop Python programs on the Raspberry Pi using the GPIO, a remote execution interface is available that comprises the basic options for downloading and executing Python scripts on the Raspberry Pi. To enable this extra option, select Raspberry Pi in the Preferences dialog.

 

A new menu entry Tools is now shown that comprises the follwing options:

  • Remote Terminal: Opens a simple PuTTY-like SSH terminal
  • Download to Target: Downloads the current program to /home/pi/script/MyApp.py and executes it, if the Preferences option Run after download is activated
  • Execute on Target: Executes /home/pi/script/MyApp.py
  • Download Module to Target: Downloads the current program to /home/pi/scripts without changing the name
  • Terminate Python on Target: Kills all processes that use the Python interpreter
  • Shutdown Target: Smooth shutdown of Raspberry Pi
  • Restart Target: Smooth shutdown/restart of Raspberry Pi

Most syntax errors are detected prior to downloading/executing the program. Errors (output to stderr) and Python print (output to stdout) are displayed in the TigerJython's Output window.

 

To download and remotely start a program, a click at an icon will do.

If at this moment another Python program is still executing, it will be automatically killed before the new program is launched. This is convenient because several programs accessing the Raspberry Pi GPIO interfere in a unpredictable manner.

At boot time, the shell script /home/pi/raspibrick/autostart.sh is executed that you may adapt to your needs. As distributed it searches for the Python program /home/pi/scripts/autostart.py. If present, the program is automatically run by the Python interpreter. So if you want to run a Python program at boot time automatically, name it autostart.py and download it using the Download to target option.

Because TigerJython communicates with the Raspberry Pi by using some SSH copy and remote execution commands and results are reported back in TigerJython's Output window, some additional shell scripts are necessary that are part of our RaspiBrick firmware. So here for more information.

 

6 Remote IDE - Geany

Geany is a flexible program editor that is widely user-configurable. It is the recommended editor for the Raspberry Pi when working in the GUI desktop (with a directly connected terminal or via VNC). We provide a Java-based RaspiRemoteManager (RRM) tool that integrates smoothly into the Geany Build Commands. With RRM Geany may run at a remote development system under Mac/Linux/Windows and Python scripts are automatically downloaded and executed to the Raspberry Pi via SSH in a one click action. The output to stdout and stderr (Python print commands and error messages) are captured and shown in a separate console-like terminal. In our opinion, this combination of Geany and RRM provides the most convenient way to program the Raspberry Pi: Just hit F9 (or press Build icon) and the current content of the editor is saved locally and transferred to the Raspberry Pi. Any other running Python program is then halted and the new script executed by Rasberry Pi's Python interpreter, reporting any error or ouput messeges back to the development system. Have a look how the Build menu is populated:

ide11

 

By closing the window, the program (actually the Python process) is stopped even if it hangs in a while-true loop.

Other menu options are:

Download Downloads the current program without executing it (for additional program modules)
Ask IP Address Asks the IP address of the Raspberry Pi (stored in user home)
Shutdown Performs a smooth shutdown of the Raspberry Pi
Restart Smooth shutdown and restart of the Raspberry Pi
Kill Python Kills all Python2.7 processes
Remote Terminal Opens a simple Putty-like remote terminal (Linux shell)

Installation:

  • First download and install Python2.7 and Geany on your computer. (Consult the Geany homepage for more information. A nice tutorial can be found here.)
  • Download RaspiRemoteManager.jar from here and copy it in any directory (in the following example under Windows in c:\rrm). The distribution also contains the source, so you can learn how the SSH connection is performed and adapt it to your needs.
  • Open Geany's Build | Set Build Commands and enter the command options link shown below (use copy&paste to simplify the editing). The shortcut (underlined character) is selected be prefixing the letter with _).

 

 

Remarks:

  • In the Ask IP Address field, make sure that after -setip you insert a space (because the IP is appended thereafter)

  • By default the Execute commands section contains only two lines. To extend it two three lines you must edit geany.conf and modify the entry
    number_exec_menu_items=0
    to
    number_exec_menu_items=3

  • By inspecting the code of the RaspiRemoteManager tool, you see that the copy and copyrunx commands assume that the folder /home/pi/scripts exists (they copy the current file via SSH into this folder). For the execution, the following Linux shell script startApp is called that you should copy (with execute rights) in /usr/bin.
#! /bin/sh
# Starting Python scripts from remote SSH command
case "$(pidof python2.7 | wc -w)" in

0)  sudo python -Qnew $1 2>&1
    ;;
*)  echo "Other program running. Killing..."
    sudo pkill python
    sudo python -Qnew $1 2>&1
    ;;
    esac

Highlight program code (Ctrl+C copy, Ctrl+V paste)

The script is called with a parameter that specifies the fully qualified path to the Python program. It first checks, if any Python2.7 process is running and eventually kills all of them to ensure that the next program has exclusive access to the GPIO (in case the program did not terminate, for instance because you use a while-True loop). It then starts the Python interpreter with sudo rights, sets the -Qnew flag to enable floating point division and redirects stderr to stdout, so that error messages are reported back to the development system. (If you use our RaspiBrick firmware image, startApp is already preconfigured.)