-
Notifications
You must be signed in to change notification settings - Fork 63
Getting a Raspberry Pi ready for NodeBots
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.
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.
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.
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. Once the system has booted up, you should be presented with a blue configuration screen (you may need to login first). Select the option to expand the disk. 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. Reboot the Raspberry Pi for the changes to take effect.
Once you have logged in again, 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 the monitor and keyboard 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.
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.
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
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!
Now that we are connected to the device, it's time to install Node.js! Go to https://nodejs.org/en/download/ to copy the download link for the ARM version of Node.js. If you are on a Raspberry Pi 2 or Raspberry Pi 3, copy the ARMv7 link, otherwise copy the ARMv6 link.
Note: if you prefer to run the latest non-LTS version of node, download it from https://nodejs.org/en/download/current/
From the command line on the Rasbperry Pi, run the following commands:
wget https://nodejs.org/dist/vX.X.X/node-vX.X.X-linux-armvXl.tar.xz # Copied link
tar -xf node-vX.X.X-linux-armvXl.tar.xz # Name of the file that was downloaded
sudo mv node-vX.X.X-linux-armvXl /usr/local/node
cd /usr/local/bin
ln -s /usr/local/node/bin/node node
ln -s /usr/local/node/bin/npm npm
You can use nvm to install Node.js as well, if you prefer, but note that you'll need to manually modify your PATH so that the root user can also see the Node executables.
Once Node.js is installed, run node -v
and npm -v
to verify that they were installed properly.
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!
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 isn't necessary for subsequent installs of Raspi IO
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.
var raspi = require('raspi-io');
var five = require('johnny-five');
var board = new five.Board({
io: new raspi()
});
board.on('ready', function() {
// Create an Led on pin 7 on P1 (GPIO4)
// and strobe it on/off
var 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.