The Raspberry Pi is a series of credit card-sized single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and developing countries.

The following document shows Rasberry Pi configuration and related technologies to use it as an IoT device.

1 Overview

One powerful feature of the Raspberry Pi is the row of GPIO (general purpose input/output) pins along the edge of the board. These pins are a physical interface between the Pi and the outside world. At the simplest level, you can think of them as switches that you can turn on or off (input) or that the Pi can turn on or off (output). Seventeen of the 26 pins are GPIO pins; the others are power or ground pins.

1.1 How the GPIO pins work

You can program the pins to interact in amazing ways with the real world. Inputs don't have to come from a physical switch; it could be input from a sensor or a signal from another computer or device, for example. The output can also do anything, from turning on an LED to sending a signal or data to another device.

2 Making a raspbian system image

Download a raspbian system image

If file is in zip format, you should extract the .img file it using tar
Copy
$ tar xvf 2016-09-23-raspbian-jessie.zip
  1. Install a writable SD card on mac and on a terminal run:
    Copy
    $ diskutil list
  2. Determine de sd disk partition, for example diskX and unmount it
    Copy
    $ diskutil unmountDisk /dev/diskX
  3. Now copy the image to the disk partition, in this example: diskX.
    Copy
    $ sudo dd bs=1m if=2016-05-27-raspbian-jessie.img of=/dev/rdiskX
  4. Once copied, it's ready to be used. Eject the sd card, put into rasberry pi and connect to power. You should see boot process on a HDMI monitor.

warning

Ensure SD card has write permissions. Also you may need user permission to write on /dev

2.1 Backup a card

As before, you can read card contents of an unmounted raw device by using dd

Copy
sudo  dd if=/dev/rdisk2 of=pimaster.img

3 Booting in headless configuration

Once boot you can login using default credentials

Copy
Login: pi
Password: raspberry

4 Booting in UI configuration

If PI image has UI it will automatically login to desktop.

4.1 Boot with HDMI

Raspberry Pi OS can now be configured. First, click on top right bar and configure your wifi.

4.2 Boot without HDMI

If we don't have an HDMI monitor or a keyboard / mouse, we will use a Linux machine to do the setup.

If you don't have a Linux machine available, you could even use another Rasberry Pi with a card reader to set this up.

4.2.1 Step 1 - mount SD on local computer

After you have copied the Raspbian image onto the SD card you will need to mount it to your system. The easiest way to do this is just unplug your card reader and plug it back in the host computer.

4.2.2 Step 2 - Configure your WiFi

Next we're going to configure the network interface. Edit the interfaces file /etc/network/interfaces.

Find this block in the file:

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
                    

Then change it to this:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
                

If you want to have a static IP instead of using DHCP (easier to find once the Pi has come up on your network) then change it to this instead:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
    address 192.168.1.20 # IP for the Zero
    netmask 255.255.255.0
    gateway 192.168.1.1 # Your router IP
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf                
                

If you're using static networking you will want to setup your DNS servers as well. Edit /etc/resolv.conf and add the following DNS servers:

                
# Google's public DNS servers
nameserver 8.8.8.8
nameserver 8.8.4.4
                

Edit the file /etc/wpa_supplicant/wpa_supplicant.conf and add your WIFI network parameters.

network={
  ssid="my network name"
  psk="my network password"
  proto=RSN
  key_mgmt=WPA-PSK
  pairwise=CCMP
  auth_alg=OPEN
}
                

Finally remove the SD card from your computer (you may wish to unmount it first) and place into your Pi.

4.2.3 Step 3 - Boot the Pi

Now it's time to boot the Raspberry Pi. Wait about 1 minute to boot and connect to WiFi network.

If you configured your Pi to use DHCP you will need to find it's IP address. There are a few ways you can do this:

  • Most routers will tell you somewhere in their web interfaces what IP allocations they have assigned to devices.
  • You could use nmap to scan the local network for devices running with port 22.
    If you don't have namp installed on MAC, use brew to install nmap.
    Copy
    $ sudo nmap -p22 -sV 192.168.1.0/24

4.2.4 Step 4 - Extras

If you're going to be using your Pi completely headlessly there are various things you can do to save energy and speed up the device.

Boot up into multi-user mode (disable GUI on boot)

Copy
$ sudo systemctl set-default multi-user.target

To disable HDMI edit /etc/rc.local and add the following line at the bottom above exit 0 line:

Copy
$ /usr/bin/tvservice -o

You may want to run

Copy
$ sudo raspi-config

to change other common Raspberry Pi settings as well.

4.3 Running raspi-config

After the first boot completes, make sure to run the configuration utility and expand the filesystem, and configure the keyboard, timezone and locale.

  1. You can either use the desktop by going to Menu -> Preferences -> Raspberry Pi Configuration or by command line by typing.
    Copy
    $ sudo raspi-config
  2. Set the keyboard, timezone and wifi country
  3. You can also update the firmware
    Copy
    $ sudo rpi-udpate
  4. Check for updates available for the operating system.
    Copy
    $ sudo apt-get update
  5. Download and install the updates
    Copy
    $ sudo apt-get -y upgrade
  6. And finally, clean up all items not need
    Copy
    $ sudo apt-get -y autoremove

5 Clone the SD card

Cloning is the process of making an exact copy. The steps shown, below, will take you through the process of creating a disk image of your existing Raspberry Pi SD card, regardless of the exact operating system you have on it or how it has been set up.

The benefit of having a disk image of your SD card is that you have a backup of your data from which you can easily write a new SD card.

Copy
$ sudo bs=4m dd if=/dev/diskX of=raspberrypi.img bs=128m

6 Networking

6.1 Static IP

To configure a static IP edit /etc/dhcpcd.conf and configure static parameters for the required interface.

6.2 Wifi

With Raspbian Jessie release simply edit /etc/wpa_supplicant/wpa_supplicant.conf to add networks (will be added automaticaly if you connect any net from desktop wireless manager)

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB

network={
    ssid="MOVISTAR_3F90"
    psk="A67C5DD6F3B0C59386BC"
    key_mgmt=WPA-PSK
    priority=1
    id_str="home1"
}

network={
    ssid="MOVISTAR_67BC"
    psk="A67C5DD6F3B0C59386BC"
    key_mgmt=WPA-PSK
    priority=2
    id_str="home2"
}

network={
    ssid="TP-LINK_Guest_2.4GHz"
    key_mgmt=NONE
    priority=3
    id_str="home_ml"
}
  • priority: when multiple networks are available simultaneously, the one with the highest priority value is selected.
  • id_str: for each network, you can give this parameter a specific value (a string). If none is provided, "default" is used as text string. This string is used in /etc/network/interfaces as a virtual interface identifier. This allows creating specific configuration blocks for each network.

DNS will automatically set in /etc/resolv.conf when a network is selected.

6.2.1 Using SSH to connect to Raspberry Pi

At this point you should try to access the sysem from other computer using ssh. The default user is pi and the default password is raspberry

Check and keep in mind your Raspberry’s IP address using

Copy
$ hostname -I    
192.168.1.53
Copy
$ ssh -l pi 192.168.1.53
Ensure ssh is enabled. Enter sudo raspi-config in the terminal, then navigate to ssh, hit Enter and select Enable or disable ssh server.

6.3 Enable SSH

To enable SSH daemon:

Copy
$ sudo systemctl enable ssh
$ sudo systemctl start ssh

6.4 Remote Desktop

Enable VNC by accessing Interfaces menu from raspi-config

Copy
sudo raspi-config

6.4.1 Installing TightVNC as a server

TightVNC is a free remote control software package

  1. Install TightVNC server on Raspberry Pi:
    Copy
    $ sudo apt-get install tightvncserver
  2. Start VNC server:
    Copy
    $ vncserver
    On the first run you’ll be asked to enter password which will be used to access remote desktop.
  3. From a mac, you can use safari to connect using
    Copy
    vnc://192.168.xx.yy
    and when a popup window is open, add port 5901 to the ip address like 192.168.xx.yy:5901

Finally, to startup vnc automatically at Rasberry pi boot

  1. Change to config directory
    Copy
    $ cd /home/pi/.config
  2. Issue the command below to create a new directory inside .config called 'autostart'.
    Copy
    $ mkdir autostart
  3. All that remains is to edit a new configuration file. So type the following command to open the vi editor on the new file:
    Copy
    $ vi tightvnc.desktop
  4. Edit the contents of the file with the following text and save it.
    Copy
    [Desktop Entry]
    Type=Application
    Name=TightVNC
    Exec=vncserver :1
    StartupNotify=false

The next time you reboot the VNC server will restart automatically

6.4.2 Installing x11vnc server

TightVNC is simple but has the inconvenience that generates a session different from the already initiated on the HDMI console.

x11vnc provides access to the same LXDE desktop session as live on RPi, unlike tightvncserver which creates a new virtual desktop session for each connection.

  1. Install x11vnc
    Copy
    $ sudo apt-get install x11vnc
  2. Setup the password to login
    Copy
    $ x11vnc -storepasswd
    Enter VNC password: 
    Verify password:    
    Write password to /home/pi/.vnc/passwd?  [y]/n y
    Password written to: /home/pi/.vnc/passwd
  3. Configure the file to start the service at boot
    Copy
    $ cd .config
    $ mkdir autostart (already done if TightVNC is installed)
    $ vi x11vnc.desktop
  4. [Desktop Entry]
    Name=X11VNC Server
    Comment=Share this desktop by VNC
    Exec=x11vnc -gui tray=setpass -rfbport PROMPT -bg -o %%HOME/.x11vnc.log.%%VNCDISPLAY
    Icon=computer
    Terminal=false
    Type=Application
    StartupNotify=false
    #StartupWMClass=x11vnc_port_prompt
    Categories=Network;RemoteAccess;
                            
    After reboot, x11vnc will ask you to select the VNC port in raspberry pi console.
  5. Reboot or run directly
    Copy
    $ x11vnc -usepw
If you are unable to start x11vnc automatically try to start x11vnc at boot (not from the user profile) it would be better to add the following line to the /etc/rc.local (before exit 0)

/usr/bin/x11vnc -xkb -auth /var/run/lightdm/root/:0 -noxrecord -noxfixes -noxdamage -rfbauth /etc/x11vnc.pass -forever -bg -rfbport 5900 -o /var/log/x11vnc.log  > /dev/null 2>&1

6.4.3 Examine TCP services running

It could be useful to know witch services are running. As we have started tightvncserverm we can see it's listening on 5901. Also we can see ssh on port 22.

Copy
$ sudo netstat -apn | grep -w tcp | grep LISTEN
tcp        0      0 0.0.0.0:5900            0.0.0.0:*               LISTEN      1751/x11vnc     
tcp        0      0 0.0.0.0:5901            0.0.0.0:*               LISTEN      852/Xtightvnc   
tcp        0      0 0.0.0.0:6001            0.0.0.0:*               LISTEN      852/Xtightvnc   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      574/sshd

7 Install a Java IDE

  • BlueJ - an Integrated Development Environment (IDE) written in Java that already incldues Pi4J (normally already include in distribution)
    Copy
    $ sudo apt-get install bluej
  • Geany - a simple lightweight IDE (normally already include in distribution)
    Copy
    $ sudo apt-get install geany
  • Eclipse JDT (Java development tools)
    Copy
    $ sudo apt-get install eclipse-jdt

From the menu, you can run the selected IDE

Any way, running eclipse on Raspberry Pi is very slow. You can use Eclipse on your desktop computer using maven to include Pi4j libraries. Then, transfer the compiled version to Raspberry Pi.