Skip to content

Getting a Raspberry Pi ready for NodeBots

Bryan Hughes edited this page Apr 5, 2017 · 39 revisions

Using a Raspberry Pi for robotics can take a fair amount of work to get set up properly. This document will walk you through installing and configuring everything you need to use node-serialport or Raspi IO with Johnny-Five.

Installing the OS

There are a variety of Operating System's to choose from for the Raspberry Pi, but Raspbian is the only OS that is officially supported by Raspi IO. Other OS's have been made to work, but be forewarned that you may have to work through a lot of issues on your own first. For more information on other OSes, take a look at issue #24.

To start, download the Raspbian OS image from the official Raspberry Pi website and follow their installation instructions to copy the image to an SD card.

Connecting to the Raspberry Pi for the first time

Most of the time, we want to run the Raspberry Pi "headless," aka not connected to a monitor, mouse, and keyboard. There is some initial setup work that needs to be done, and it is usually recommended to connect to a monitor, mouse, and keyboard for the first boot to do this work. It can be done without though, and this document will describe both approaches.

Method 1, using a monitor

Note: the default username for Raspbian is "pi", and the default password is "raspberry".

Connect the following to your Raspberry Pi:

  • Connect a monitor using an HDMI cable
    • If you're monitor does not support HDMI, you may be able to buy an adapter
  • Connect a USB keyboard
  • Connect an Ethernet cable that is connected to the Internet

Once everything is connected, connect power to the Raspberry Pi to turn it on.

Method 2, using SSH

UPDATE: this method no longer works, as the SSH server is no longer enabled by default on the Raspberry Pi. New instructions for using a serial cable coming soon!

If you don't have easy access to an external monitor and keyboard, it is possible to set the Raspberry Pi by connecting using SSH the first time.

Note: the Raspberry Pi Zero does not enable SSH by default, so you will need to follow the monitor instructions above. Alternately, you can connect a USB to UART cable to the Pi and connect via TTY. This method is considered advanced and so is not detailed here.

OS X

Connect power to the Raspberry Pi to turn it on (don't connect Ethernet just yet). Watch the ACT LED (aka activity light). Once this stops flashing, that means the Raspberry Pi has finished booting up.

Go into System Preferences->Sharing and configure your computer to share Internet from WiFi to your Ethernet adapter, and then click the check box next to Internet Sharing on the left to turn it on.

Now connect the Ethernet cable between your Raspberry Pi and Laptop. If you have a new Macbook, Macbook Air, or newer Macbook Pro, you will need to get a USB/USB C/Thunderbolt to Ethernet adapter (Apple sells these).

The Rapsberry Pi should now have an IP address obtained from your computer. To find out what address it obtained, run

cat /var/db/dhcpd_leases

This will print out all IP addresses your computer has given out. Note that deleting this file will cause all devices to be issued new IP addresses. The output should look like this:

{
	name=raspberrypi
	ip_address=192.168.2.2
	hw_address=1,b8:27:eb:ca:58:ee
	identifier=1,b8:27:eb:ca:58:ee
	lease=0x56f61490
}

The IP address should always be 192.168.2.2, unless you have used Internet Sharing for other devices. Now you can connect to the Raspberry Pi by running:

ssh pi@<IP address from above>

Once you have logged in, run

sudo raspi-config

This will bring up the blue configuration screen discussed in Method 1. Select the option to expand the disk and reboot.

Note: Internet Sharing usually seems to stop working after you disconnect the Ethernet adapter, and the Raspberry Pi isn't very good at detecting when Internet Sharing has been enabled. If you are unable to connect to your Raspberry Pi, do the following steps in this order:

  • Uncheck the checkbox next to Internet Sharing to turn it off
  • Check the checkbox next to Internet Sharing to turn it back on
  • Unplug the Ethernet cable from the Raspberry Pi
  • Plug the Ethernet cable back in, and the Raspberry Pi should now obtain an IP address

Linux and Windows

The steps for Linux and Windows should be similar to the OS X instructions, but replacing the OS X specific steps with equivalent steps for your OS. If you figure out how to do it, let us know by filing an issue so we can add it to the wiki!

Configuring the Raspberry Pi for remote access

Once the system has booted up, you should be presented with a configuration screen. If it asks you to log in, do so first. If you don't see the configuration screen after logging in, run sudo raspi-config. From this screen, you will need to do three things:

  1. Select the option to change the user password, and change it to something you can remember. This is necessary to prevent the Pi from complaining to us every time we log in (and is generally a good idea anyways).
  2. Select "Interfacing Options," then select "SSH," and enable it. This will allow us to connect to the Raspberry Pi over the network in the future, without the need for a keyboard, mouse, and monitor.
  3. Select "Advanced Options," and then "Expand Filesystem." When the disk image is copied to the SD card, it doesn't use all of the space on the SD card and the extra space is not accessible to Raspbian. This option changes that so all space on the SD card can be used by Raspbian.

Once these steps have been completed, reboot the Raspberry Pi for these changes to take effect.

Once you have logged in again using one of the two methods from above, run the command ifconfig. You should see something that looks like this:

eth0      Link encap:Ethernet  HWaddr b8:27:eb:d5:5d:ac
          inet addr:192.168.2.3  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::49ec:5b88:b16f:95f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:233 errors:0 dropped:0 overruns:0 frame:0
          TX packets:152 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:35422 (34.5 KiB)  TX bytes:25588 (24.9 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:200 errors:0 dropped:0 overruns:0 frame:0
          TX packets:200 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:16656 (16.2 KiB)  TX bytes:16656 (16.2 KiB)

Note: this is from one of my Raspberry Pi 2s.

This shows the two networking interfaces that Raspberry Pi's come with by default (lo is short for loopback, and is a local-only networking "virtual" interface). If you are running a Raspberry Pi 3, then you will see a third interface called "wlan0", which is the WiFi interface.

Take a look at the interface called "eth0", this is the Ethernet port built in to the Raspberry Pi. Note both it's MAC address, signified by "HWaddr", and the IP address, represented by "inet address". If you are running IPv6, then take a look at "inet6 address" instead. If you don't know what IPv6 is, then don't worry, you're probably not using it.

Once you have this address noted, you can disconnect using whichever connection method you chose, and connect to the Raspberry Pi using SSH from here on out by running:

ssh pi@<IP address from above>

If you have control over your local network, it is advisable to reserve the IP address in your DHPC server (aka your router). This way, the IP address is guaranteed not to change. To reserve the IP address, consult your router's documentation and use the MAC address from above.

Installing other dependencies

There are a few things that need to be installed before Raspi IO will work. If you are running the full version of Raspbian, these should all be installed. If you are running the lite version, however, you will need to install these first:

sudo apt-get install git wiringpi

If you are unsure if these are installed or not, go ahead and run the above command. It doesn't hurt anything if they're already installed!

Installing Node.js

Now that we are connected to the device, it's time to install Node.js! We are going to use the NodeSource Ubuntu packages to install Node.js. You will need to choose which release of Node.js to install. For details on Node.js releases and version numbering, check out the Node.js LTS documentation. Generally speaking, you can choose between a Long Term Support (LTS) release, or a non-LTS release. LTS versions are more stable and recommended for production use, and is recommended for NodeBots.

As of this writing, the current LTS version is version 6.x, so we will use that in this section. The first step is to tell Raspbian about NodeSource' packages:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

Note the 6 in the URL. If you wish to use a different version, see the NodeSource installation instructions.

Next, install Node.js with:

sudo apt-get install -y nodejs

Once Node.js is installed, run node -v and npm -v to verify that they were installed properly.

It is possible to install Node.js using nvm as well, but this method is not recommended for NodeBots because Raspi IO requires installing as non-root, but running as root, and the way nvm symlinks things means you have to do some manual work to get everything linked correctly. As such, nvm is not supported by Raspi IO.

Installing node-serialport

If you are using node-serialport independent of Johnny-Five and Raspi IO, the final step is to install it with npm:

npm install serialport

After this step, you're done and can't stop reading here!

Installing Johnny-Five and Raspi IO for the first time

Now it's time to install Johnny-Five and Raspi IO for the first time! In a project folder somewhere (e.g. ~/documents/hello_world), run

npm install johnny-five raspi-io

This will download the modules, compile all native modules (there are several), and modify a few of Raspbian's configuration files. If there are any issues, please file an issue and let us know.

IMPORTANT: Once you are done installing Raspi IO for the first time, make sure you reboot before you run any code! This reboot is not necessary for subsequent installs of Raspi IO

Blinking an LED

Whew, now that all that's done, it's time to run some code! Paste the following code into a file called index.js in the project folder.

const raspi = require('raspi-io');
const five = require('johnny-five');
const board = new five.Board({
  io: new raspi()
});

board.on('ready', () => {
  // Create an Led on pin 7 on P1 (GPIO4)
  // and strobe it on/off
  const led = new five.Led('P1-7');
  led.strobe(500);
});

Now, run sudo node index.js and you should see the ACT LED flashing twice a second! Note that sudo is always required when running Raspi IO code.

Clone this wiki locally