It's time I took my IoT projects to the next level! Recently, I got the Raspberry Pi 3, Model B, and it's a powerful little piece of kit! I got the Raspberry Pi for making more JS-based projects and not restrict it to my Macbook, and utilise it as a small, mobile computer for my projects.
This is a comprehensive guide to help you get started with the Raspberry Pi, from installing the necessary Operating System, to setting up necessary software, making it work with Johnny-Five... and maybe a little bit more... π
PreparationIf this is your first time with the Raspberry Pi, there's some things you need to prepare for in order to use it. You'll need a Micro USB B power supply, and an SD Card containing NOOBS (New Out Of Box Software). You can buy SD Cards containing NOOBS or you can download NOOBS and place it onto an SD Card of your own. You will also need an HDMI Cable, a screen with an HDMI connection, and a keyboard and mouse connected via USB. You may also use an Ethernet cable to connect your Pi your Router, though this is optional.
Once you have all of these, insert the SD Card containing NOOBS into the Pi's SD slot, and connect your monitor to the HDMI slot, with the keyboard and mouse connected to two of the four USB Ports. If you want to connect to your router to the Pi, connect it to the Ethernet Port.
Installing RaspbianPower up your Pi, and view it on your monitor. You will then be presented with Operating Systems you can install onto your Pi. It's highly suggested to install Raspbian if this is your first time using the Pi, and quite frankly this is all you'll need to get your IoT projects started.
Select Raspbian, and let it install. Now, this is going to take a while, I'd say around half an hour, as the Pi needs to install 4GB of OS onto the SD Card, and writes it, at most, 2.0MB a second.
At this point, just let it do its thing. Go for a cup of tea, heck, go for a walk! I'll see you in while!
Setting Up Your Raspberry Pi for SSH and Screen SharingOh hey, you're back! Ready for the next step?
At this point, Raspbian should be installed and running under the default user and password. Go and have a feel of Raspbian! It's got multiple language environments, including Java and Python, as well as some office software, a Terminal, and some games. It even comes with Minecraft Pi!
Okay, now you've got your way around Raspbian, let's get onto the fun stuff β setting up your Pi for remote access.
Firstly, change the password for your Pi user for security purposes. Secondly, make sure your Pi is connected to your WiFi. Lastly, give your Pi a name! The name will also be used to connect via SSH and Screen Sharing. Let's see below:
Under the 'Interfaces' tab of the preferences window, Enable SSH. This will allow you to access the Pi's CLI from another computer using your computer's CLI.
This is where the fun stuff begins β we're going to go into the Pi's Terminal!
You need to get some software to allow screen sharing. This will allow you to access your Pi's desktop on another computer. To do this, we need to install TightVNC. Simply type:
sudo apt-get install tightvncserver
And allow the software to install. Then, type tightvncserver
to get it running! You will then be asked to set a password for access to the Pi via TightVNCServer. Also note that it will say New 'X' desktop is <Your Pi Name Here>:1
. Take a note of that.
You will also want to make tightVNC to start running as soon as the Pi boots up, so simply cd
into /home/pi/.config
, and make a new folder called autostart
, and cd
into it.
mkdir autostart; cd autostart
Create a new file for tightVNC by typing:
nano tightvnc.desktop
This will open your file in the Nano editor. Simply paste this into the file, and press Ctrl-O to save the file.
[Desktop Entry]
Type=Application
Name=TightVNC
Exec=vncserver :1
StartupNotify=false
Press Ctrl-X to exit Nano, and now TightVNC will be running once your Pi boots up.
Now, hop on over to the computer you normally use, we're going to test two things.
If you set up SSH on your Pi, open up your terminal and log in to your Pi's shell by putting in your user's name on the Pi and the Pi's local address, or its IP address. Personally, I like to use the Pi's local address. Let's say for example, you kept the Pi's name as raspberrypi
, you'd simply type:
ssh pi@raspberrypi.local
As above, just use the ssh command, log in as the user pi
with your machine name's local address. Now, we can access our Pi and work on it using a secure shell. You may also want to change things in .bashrc
and add additional dotfiles to help navigate easier and make simpler functions, but that's for another day.
You also need to check if we've set up screen sharing correctly. Since I'm using a Mac, all I need to do is go to Finder>Connect to Server, then type in the Pi's VNC address
Again, using the raspberrypi
machine name as an example, the address you connect to for the VNC is vnc://raspberrypi.local:5901
. Remember the :1
from the tightvncserver
setup? That is the number we put at the end of the VNC's address.
If things went well, you should now be able to see your Pi's desktop!
Now you've got remote access set up on our Pi, you can disconnect everything from the Pi except for Power. You no longer need to work from the Pi directly!
Installing Node.js, and Yarn.Think the last step was fun? We haven't got to the best part yet!
Since we've now got the Pi accessible remotely, we'll be doing it on our main computer from here on. Access your Pi's bash by using the Pi's Terminal via screen sharing or SSH, and we'll carry on from here.
Now you need to install the latest version of Node.js, which will install the latest version of NPM as well. First, you'll need to run this command. This will update the Debian packages of Node.js to include NodeSource.
Important note: Raspbian comes with Node-RED built-in with the Operating System, however if you run these steps it will update it and not make it available in the Programming section of the OS's main menu. This shouldn't be a big deal, but it's worth knowing.
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
Now all you need to is install the latest version of Node.js, by running the following:
sudo apt-get install nodejs
Check if the version of Node is up to date by typing node
. As of this writing, it is version 8.4.0
.
Now that you've installed Node, you should also install Yarn. But you're probably thinking 'Why should I?!' Good question!
For most of my projects now, I use Yarn, and it's great! It's faster and just as reliable as NPM, and considering the Pi is a relatively low power computer compared to a common computer, this will be useful. I highly recommend you give Yarn a try!
To install Yarn, put in the following command:
curl -o- -L https://yarnpkg.com/install.sh | bash
Now you've got yarn installed and ready to go to make some awesome JavaScript projects!
Testing our Raspberry Pi with Johnny-FiveAt this point, we're going to make a new project using Johnny-Five. Johnny-Five is an IoT and Robotics framework written in JavaScript. If you're new to using JavaScript in robotics or IoT, I highly recommend you use Johnny-Five as it has a great API and supports many platforms. Out of the box, it works with Arduino using Firmata, but on other platforms, there are IO plugins that can be used to make it work on boards like the Raspberry Pi and the Tessel. This is a great framework to use for JavaScript IoT projects and works very well with other JavaScript technologies such as websockets!
Let's make a new Johnny-Five project for this! For organisational purposes, make a projects
folder in the Documents
directory, to store your projects.
Make a new directory under projects
, and call it something like j5-pi-test
. In your j5-pi-test directory, make an index.js file (you can do this by typing touch index.js)
. Initiate Yarn by doing the following:
yarn init -y
The -y
flag makes a simple Yarn package file, which is all we'll need. It will make a package.json
file and yarn.lock
file, simple as that.
We only need two packages for this β Johnny-Five and Raspi-IO. Install them by typing the following:
yarn add johnny-five raspi-io
This will install Johnny-Five for you, and an IO plugin called Raspi-IO. You can use Johnny-Five out of the box for Arduino projects, but not for Raspberry Pi. This is where Raspi-IO comes in very handy β this will give the support Johnny-Five needs to work with the IO in the Raspberry Pi.
Make a new file in the directory by running nano index.js. Attached to the post is the code for the file here. Simply copy and paste it into your file, save it and exit (ctrl-o to save, ctrl-x to exit nano), and let's do a rundown of the file.
// Test file for using the Raspberry Pi and Johnny-Five
const five = require('johnny-five');
const raspi = require('raspi-io');
// Make a new `Board()` instance and use raspi-io
const board = new five.Board({
io: new raspi(),
});
Because you're using a Raspberry Pi, you need to specify to Johnny-Five's Board class that it's being used on a Raspberry Pi board. The Raspi-IO library allows Johnny-Five to access the Pi's IO pins and work with them, so you need to specify that as the IO option in the Board class. Now we can use the pins on the Pi!
// Run Board
board.on('ready', function() {
// LED Pin variable
const led = new five.Led('P1-7');
led.on();
this.repl.inject({
on: () => {
led.on();
},
off: () => {
led.stop().off();
},
strobe: () => {
led.stop().off();
led.strobe();
},
blink: () => {
led.stop().off();
led.blink(500);
},
});
// When this script is stopped, turn the LED off
// This is just for convenience
this.on('exit', function() {
led.stop().off();
});
});
In the circuit, the LED's pin is set to to Pin 7 on the Pi's IO, and Pin 9 is the ground pin. Simple circuit. If you need to which pins are which, go to pinout.xyz and this will make it super easy to understand every pin on the Raspberry Pi!
When the circuit is running, just turn the LED on. There's also some REPL scripts you can run in the prompt, just to check if it's behaving as expected. Then, when the script is exited, just turn the LED off! Nice and easy script, just to see if the Pi's working as expected.
There's only one thing left to do, and that's run the script. Note however, since we're using the Pi's GPIO we need to use sudo in order to run it.
sudo node index.js
Congratulations! You've just got your Pi running and should have a dynamic, working LED using JavaScript and Johnny-Five!
[BONUS] Using Raspberry Pi, Arduino, and Johnny-FiveIf you've got your Pi working at this point, well done! Now, I'll leave you an extra tip and that could be a big benefit to your Johnny-Five or other JavaScript IoT projects.
One of the reasons I'm using the Raspberry Pi is to allow my projects to go untethered from my main computer β this is because Johnny-Five works on Arduino by default using Firmata, but Arduinos can only run code in C and C++. Because of this, the Arduino needs to use an interpreter for other languages, thus becoming 'tethered' to their interpreter. Of course, you can use the Pi by itself and work with Johnny-Five from there, but you can also use the two to their own benefits.
Raspberry Pi has Bluetooth and Wi-Fi working out of the box β most Arduinos don't have this, but this is mostly because Arduinos are boards prototyping with components and IO, while Raspberry Pis and similar small computers can be more than just that. They can be the main computer of the project acting as the interpreter, even the server of the project while using the Arduino to communicate and control components.
Of course, this is just one way of utilising this method! I think that there's benefits to using both for their advantages. Simply plug the Arduino to the Pi via USB, and use both the Raspberry Pi and Arduino!
ConclusionThis guide was a lot of fun to write and wasn't too challenging to do, but I think it's important for anyone who is new to Raspberry Pi, Raspbian, and the Johnny-Five project to have a (big!) guide to read to go from Installing the Pi to writing your first simple project. It's a long task but it gets you used to the Raspberry Pi and the Raspbian OS, and gives you a feel over relying on remote usage.
Now go out there and have fun with the Raspberry Pi and Johnny-Five!
If you enjoyed this project and helped you learn more about using JavaScript with your projects, feel free to pledge to my Patreon or donate to my PayPal so I can make more awesome stuff. You can also follow me on Twitter or like me on Facebook to stay up to date!
Comments