Single board computer projects

Raspberry Pi tips and tricks

last updated: 25/10/16 (image from 23/09/16; tested with raspi 3)

Create µSD-card (Linux)

Download the desktop based debian image with Pixel (4GiB) from https://downloads.raspberrypi.org/raspbian_latest, or the headless image without desktop (1,3GiB) from https://downloads.raspberrypi.org/raspbian_lite_latest.

Extract the image with the command unzip and burn it to an sd-card with dd. To find the device character (/dev/sdx) use dmesg after inserting the card.

    unzip 2016-09-23-raspian-jessie.zip
    sudo dd status=progress if=2016-09-23-raspbian-jessie.img of=/dev/sde bs=1M

Raspbian Jessie with Pixel

The new Raspbian with Pixel desktop is easy to use.
The filesystem of your SD-card is automatically expanded during the first boot. The system starts without login or password request.

With HDMI Monitor, USB mouse and USB keyboard you have a full desktop PC and it is simple to configure your raspi by desktop means.

Configuring raspi by desktop means

To enable your Wifi, click the icon right to the bluetooth sign. Choose your hotspot (SSID) and type the pre shared key (this settings will be automatically written to your wpa_supplicant file).

Click the start menu (a raspberry :)) and then:
Menu ⇨ Preferences ⇨ Raspberry Pi Configuration.

To set your keyboard , time zone an localisation settings click on Localisation:

Configuring localisation

To enable interfaces (1-wire, I2C, serial) click on Interfaces:

Configuring localisation

Here it is also possible to enable VNC to use your raspi remotely.

Use raspi remotely with VNC

We enable VNC in Menu ⇨ Preferences ⇨ Raspberry Pi Configuration ⇨ Interfaces From now on, VNC server will start automatically at boot. If we have no static IP address, get your raspi's private IP address by clicking the VNC icon on your menu bar.

Download VNC Viewer on https://www.realvnc.com/raspberrypi and run it on your device. Now you can take control of your raspi, as if you were sitting in front of it.

If you want to connect over the internet, you will need to configure your firewall to enable VNC, and port forward your router to your raspi's IP address.

Static IP address

It is simpler to know raspi's IP address, so we set it static. Open a terminal window (4 icon in menu bar) and type:

    gksu pcmanfm

This opens the filemanager with root privileges. Click on the file dhcpcd.conf in the /etc folder. Libreoffice is opened with root privileges.

Now you can append the following to the file /etc/dhcpcd.conf.

    # Custom static IP address for eth0.
    interface eth0
    static ip_address=192.168.1.67
    static routers=192.168.1.1
    static domain_name_servers=192.168.1.1 8.8.8.8 

    # Custom static IP address for wlan0.
    interface wlan0
    static ip_address=192.168.1.69
    static routers=192.168.1.1
    static domain_name_servers=192.168.1.1 8.8.8.8 

Save the file as txt-file and reboot.

Update and upgrade

To get the latest versions of all programs, open the terminal window (4th icon in menu bar) and type:

    sudo apt update
    sudo apt upgrade
    sudo apt dist-upgrade

It is a good idea to install the terminal file manager "midnight commander (mc) to search and edit files as root in terminal (sudo mc).

    sudo apt install mc

Log in with SSH

If you don't know the IP address of the raspi, use nmap. On your PC type:

    sudo nmap -sP 192.168.1.*

If nmap is not installed:

    sudo apt install nmap

Look for the IP address of your raspi (ex.: 192.168.1.125) and log in with ssh (putty on windows):

    sudo ssh pi@192.168.1.125

Python 3

Python3 is already installed. Use idle3 by clicking Menu ⇨ Programming ⇨ Python 3 (IDLE). CTRL+N opens a new file to type your code. Execute the code with F5.

Setting up the webserver Lighttpd with cgi

Lighttpd is an efficienct high performance webserver. It has a small memory footprint and an effective management of the cpu-load compared to other web-servers.

    sudo apt update
    sudo apt upgrade
    sudo apt install lighttpd    

Test if the webserver is running by typing the ip address of your raspi in the url field of your browser.

The html files are in in /var/www/html.

Make the cgi-bin directory /var/www/cgi-bin. After this we will edit the lighttpd config file.

    sudo mkdir /var/www/cgi-bin
    sudo nano /etc/lighttpd/lighttpd.conf

Add the following lines to the config file:

    server.modules += ( "mod_cgi" )
    alias.url += ( "/cgi-bin/" => "/var/www/cgi-bin/" )
    $HTTP["url"] =~ "/cgi-bin/" {
            cgi.assign = ( 
                  ".py" => "/usr/bin/python", 
             )
    }

We have to change the owner of the web directories to pi and reload the server:

    sudo chown -R pi.pi /var/www
    sudo service lighttpd force-reload

Now we can create our cgi python script (ex.: my1cgi.py). It has to be executable, so we change this with chmod:

    chmod 711 /var/www/cgi-bin/my1cgi.py    

If we need php:

    sudo apt-get install php5-common php5-cgi php5

Don't change the order of the packages to install or Apache will be installed. If we need a database:

    sudo apt-get install mysql-server php5-mysql

Raspbian Jessie lite

If you have an HDMI Monitor, a USB mouse and USB keyboard it is possible to login without network connection and configure your raspi. If not, use a wired network connection; get the IP adress with nmap and connect with ssh to your raspi (see "Log in with SSH"). After setting up Wifi (see "Set up Wifi") you may disconnect the wired connection.

The login is pi, the password is raspberry (with a german keyboard the password is raspberrz :)).

Raspi-config

Raspbian has a cool tool to change settings. Type

    sudo raspi-config
Expand the filesystem

Click on "1 Expand Filesystem" to use later all the space from your SD-card.

Change the keyboard layout and the timezone

When you do not use an english keyboard use sudo raspi-config and click on "4 Internationalisation Options". With "I3" you can adjust the keyboard layout. It is a good idea to use "I2" to chose the right time zone. The locale per default is 'en_GB.UTF-8 UTF-8'. If you want to change your locale you may do this under "I1".

Enable I2C, Serial and 1-Wire

Click on "Advanced Options" to get access to "A7", "A8" and "AA".

Hostname and password

In a network with more than one raspi it is good to change the hostname ("Advanced Options", "A1"). The password can be changed under "Change User Password". To update raspi-config look under "Advanced Options", last point "A0".

If all this is done, it is a good time to reboot (Bob: "reboot tut gut!):

    sudo reboot

Set up Wifi

The new raspi3 has Wifi on board, so we need to activate it to gain wireless network access.

    cd /etc/wpa_suplicant
    sudo nano wpa_supplicant.conf

Add the following lines with the ssid of your access point and your password to the file:

    network {
        ssid="weigu1"
        psk="*****"
    }

Save with CTRL+O and exit with CTRL+X.

Log in with SSH

Now it's time to find the IP address of the raspi. On your PC type:

    sudo nmap -sP 192.168.1.*

If nmap is not installed:

    sudo apt install nmap

Look for the IP address of your raspi (ex.: 192.168.1.125) and log in with ssh (putty on windows):

    sudo ssh pi@192.168.1.125

Update and upgrade

Get the latest versions of all programms:

    sudo apt update
    sudo apt upgrade
    sudo apt dist-upgrade

Midnight commander

A really cool program

can very easily be used to browse and edit your raspi files.
Install and launch it with:

    sudo apt install mc
    sudo mc

Static IP address

To get a static IP adress is very easy in Jessie. Use the editor nano to append the following to the file /etc/dhcpcd.conf.

    # Custom static IP address for eth0.
    interface eth0
    static ip_address=192.168.0.67
    static routers=192.168.1.1
    static domain_name_servers=192.168.1.1 8.8.8.8 

    # Custom static IP address for wlan0.
    interface wlan0
    static ip_address=192.168.1.69
    static routers=192.168.1.1
    static domain_name_servers=192.168.1.1 8.8.8.8 

Use sudo mc with F4 to edit (2 for nano) or

    cd /etc
    sudo nano dhcpcd.conf

Save with CTRL+O and exit with CTRL+X.

Python 3

    sudo apt install python3, rpi.gpio