Project

General

Profile

Raspberry Pi 3 headless installation and preparation (On Mac, Raspbian OS)

What is needed:

  1. Minimum - Raspberry Pi 3 motherboard, ethernet cable, power supply with USB and of course a router to connect RPi over ethernet cable

  2. Suggested - a case for RPi, GPIO to Breadboard Interface Board, Ribbon Cable, Large Breadboard

  3. Optimum set - CanaKit Raspberry Pi 3 Ultimate Starter Kit - 32 GB Edition

OS Setup on MicroSD card

Most of RPi kits come with MicroSD card preloaded with NOOBS which allows you to select an OS to be installed at the first startup time. However, this requires a connected monitor and a keyboard. Not very useful for a headless installation. So, regardless whether the MicroSD was card provided as a RPi kit or separately purchased, it needs to be loaded with an OS. There are many OSes available for Raspberry Pi right now and all of them can be installed in a similar way.

  1. As this is going to be headless installation, we do not need any desktop features, so download Raspbian Jessie Lite OS image: https://www.raspberrypi.org/downloads/raspbian/

  2. Unzip the image

  3. Setting up an OS image on MicroSD card is described on the Raspberry Pi website here is more or less a copy for Mac to do it all from a command line:

    1. Open a terminal, then run: diskutil list
    2. Identify the disk (not partition) of your SD card e.g. disk4, not disk4s1.
    3. Unmount your SD card by using the disk identifier, to prepare for copying data to it: diskutil unmountDisk /dev/disk where disk is your BSD name e.g. diskutil unmountDisk /dev/disk4
    4. Copy the data to your SD card: sudo dd bs=1m if=image.img of=/dev/rdisk where disk is your BSD name e.g. sudo dd bs=1m if=2016-05-27-raspbian-jessie.img of=/dev/rdisk4
    5. This may result in a dd: invalid number '1m' error if you have GNU coreutils installed. In that case, you need to use a block size of 1M in the bs= section, as follows: sudo dd bs=1M if=image.img of=/dev/rdisk
    6. This will take a few minutes, depending on the image file size. You can check the progress by sending a SIGINFO signal (press Ctrl+T).
    7. If this command still fails, try using disk instead of rdisk, for example: sudo dd bs=1m if=2016-05-27-raspbian-jessie.img of=/dev/disk4 or sudo dd bs=1M if=2016-05-27-raspbian-jessie.img of=/dev/disk4
    8. After the dd command finishes, eject the card: sudo diskutil eject /dev/rdisk4
    9. Example session:
downloads $ diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *751.3 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage Macintosh HD            750.4 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1 (internal, virtual):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                  Apple_HFS Macintosh HD           +745.9 GB   disk1
                                 Logical Volume on disk0s2
                                 D2454C45-B558-4375-9D9B-642145C506DB
                                 Unencrypted
/dev/disk3 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *31.9 GB    disk3
   1:             Windows_FAT_32 NO NAME                 31.9 GB    disk3s1
downloads $ diskutil unmountDisk /dev/disk3
Unmount of all volumes on disk3 was successful
downloads $ sudo dd bs=1m if=2016-05-27-raspbian-jessie-lite.img of=/dev/rdisk3
Password:
1323+0 records in
1323+0 records out
1387266048 bytes transferred in 20.651608 secs (67174723 bytes/sec)
downloads $ sudo diskutil eject /dev/rdisk3
Password:
Disk /dev/rdisk3 ejected
downloads $

Raspbian Stretch or later ( taken from this source)

Before you put the MiniSD card into RPi you need to activate ssh and WiFi. (WiFi is optional if you plan to use ethernet instead.)

Once you've burned/etched the Raspbian image onto the microSD card, connect the card to your computer and you'll see the card being mounted as "boot". This is where you have to make some simple modifications in order to activate ssh and WiFi.

SSH

Latest Raspbian does not start ssh by default, so you cannot ssh to it right away. One more step is necessary.

Inside this "boot" directory, you need to create a new file: ssh.

$ cd /Volumes/boot/
$ touch ssh

Eject the card and put it into RaspberryPi.

WiFi

You can setup WiFi connectivity here as well.

Create another file name wpa_supplicant.conf. This time you need to write a few lines of text for this file. For this file, you need to use the FULL VERSION of wpa_supplicant.conf. Meaning you must have the 3 lines of data namely country, ctrl_interface and update_config.

Example:

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

network={
    ssid="your_real_wifi_ssid"
    scan_ssid=1
    psk="your_real_password"
    key_mgmt=WPA-PSK
}

The above instructions for setting up WiFi, worked right away when I followed them the first time. Then, few months later, tried it again and it did not work. After spending a few hours on the Internet, trying different solutions, and some even worked but none would let me having working WiFi right away, I reinstalled Raspbian on the MiniSD card again, from scratch and followed above steps again. The second time WiFi worked right away without any additional playing...
No idea why this happened, but maybe worth remembering.

The first startup and OS settings finalization

  1. Put the MicroSD card into the RPi motherboard, connect ethernet cable and USB power cable, the RPi should startup. It may take a while for RPi to be ready. Red diode lighting up, means the power supply is OK. Briefly blinking green diode next to red power light means RPi activity. There should be also green light next to ethernet socket.

  2. Most recent Raspian broadcasts RPi hostname out of the box. So, you do not need to figure out its IP address. You can access the RPi by its hostname. By default it is: raspberrypi.

  3. Check if the RPi is up and running (remember about adding .local to the hostname):

downloads $ ping raspberrypi.local
PING raspberrypi.local (192.168.1.25): 56 data bytes
64 bytes from 192.168.1.25: icmp_seq=0 ttl=64 time=0.680 ms
64 bytes from 192.168.1.25: icmp_seq=1 ttl=64 time=0.437 ms
64 bytes from 192.168.1.25: icmp_seq=2 ttl=64 time=0.454 ms
64 bytes from 192.168.1.25: icmp_seq=3 ttl=64 time=0.453 ms
^C
--- raspberrypi.local ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.437/0.506/0.680/0.101 ms
  1. Now you can connect via ssh (default password is raspberry):
downloads $ ssh pi@raspberrypi.local
The authenticity of host 'raspberrypi.local (fe80::bf79:9fce:6863:94e1%en3)' can't be established.
ECDSA key fingerprint is SHA256:/l7s5SVUIQ8u7Oia0gDhCqDZePv/O554qZQyu199BPc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'raspberrypi.local,fe80::bf79:9fce:6863:94e1%en3' (ECDSA) to the list of known hosts.
pi@raspberrypi.local's password:

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
pi@raspberrypi:~ $
  1. Change password from default to something else. Maybe it does not matter while the RPi sits on your desk and you play with it but once it is something useful and goes away from your desk you do not want to make it easy for anybody to login and mess it up, command: passwd

  2. Now, make sure the system is up to date with commands: sudo apt-get update and sudo apt-get upgrade:

sudo apt-get update && sudo apt-get -y upgrade

or you can just go ahead and update and install all of what we normally need on RPi:

sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get -y install oracle-java8-jdk mercurial gcc \
  python-pygame python-dev pigpio python-pigpio python-rpi.gpio raspi-gpio wiringpi libudev-dev python-pip \
  avahi-utils git maven maven2 gradle emacs-nox
  1. Setup a hostname if you want a different one from the default. This is especially important if you want to run more than 1 on your network. sudo raspi-config and then go to Advanced Options in the menu. After the hostname is changed, the raspi-config prompts to restart the device on exit. Once restarted RPi should be available under a new hostname.

Getting rid of the ethernet cable ( taken from this source)

  1. WiFi Network Scan: sudo iwlist wlan0 scan

This will tell you all the available networks. We did this just to test that it is working because we probably already know the SSID of the network we want.

  1. Using VI for Linux Open WPA Supplicant File, execute the following over SSH: sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

The file probably won’t be empty. Go to the bottom of the file. When you’re at the bottom, add the following new lines:

network={
    ssid="your_network_ssid_here"
    psk="your_wifi_password"
}
  1. When you’ve done this, power down the Raspberry Pi by executing the following over SSH: sudo halt

After a minute when you’re sure it shut down, remove the ethernet cable and power the system back up. You should be good to go. Discover the Raspberry Pi on your network again using the previous step, but this time it will be through WiFi.

Congratulations, you set your Raspberry Pi to work without ever connecting it to a monitor. Now you can SSH to it and use command line Linux without issues.

Java setup

  1. The first step is JDK installation: sudo apt-get -y install oracle-java8-jdk

  2. Installing Device I/O library

    1. Install Mercurial to download source code form OpenJDK repository: sudo apt-get -y install mercurial
    2. Clone the Device I/O library source code to local machine: hg clone http://hg.openjdk.java.net/dio/dev dio
    3. Change working directory to dio: cd dio
    4. Set environment variables:
    5. export PI_TOOLS=/usr
    6. export JAVA_HOME=/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt
    7. Build binaries from source codes: make
    8. Install binaries in JRE: sudo cp -r build/deviceio/lib/* $JAVA_HOME/jre/lib*
    9. Install the JAR library on your development machine as well. Run this from your development machine to get the library: scp pi@raspberrypi.local:~/dio/build/deviceio/lib/ext/dio.jar .
    10. Do not forget to add dio.jar in Libraries category as Compile-time library.
  3. Installing pi4j library: curl -s get.pi4j.com | sudo bash

Netbeans setup for remote code execution.

Netbeans has the ability to compile the code locally and execute it on a remote machine. Here is how to set NB up for remote platform.

  1. Add a new Java Platform in Tools | Java Platforms

  2. In the Java Platform Manager*, click the *Add Platform button and on a new window select Remote Java Standard Edition and click Next

  3. On the new window, fill: Platform name - the RPi hostname is a good idea, Host - this is the host at which the RPi can be reached - raspberrypi.local*, *Username - pi*, *Password - password for the username account, Remote JRE Path - /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/

  4. Once you enter all values, click Finish*. Netbeans will attempt to connect to the remote machine testing if all fields are filled correctly. If successful, it will just add a new position to the *Remote Java JRE list.

  5. In your project, select properties and update Run category to use your remote platform for RPi.

  6. Do not forget to add dio.jar in Libraries category as Compile-time library.