Build a Chatbot That Cares — Part 2

In 150 Lines of Code

Josh Zheng
IBM watsonx Assistant
5 min readDec 5, 2016

--

In part 1 of the tutorial, we finished the code required to run TJBot on a laptop. Now let’s get the code working on a Raspberry Pi. I promise this part will be much shorter. 😬

Again, this tutorial has lots of useful information as well.

Step 0 — What You Need

  1. TJBot
  2. Raspberry Pi. I’m using the latest Raspberry Pi 3.
  3. HDMI display
  4. USB keyboard
  5. USB mouse
  6. USB microphone
  7. Speaker with 3.5mm audio jack

Step 1 — Raspberry Pi Setup

To set up a RaspberryPi initially, you need the HDMI display, the USB keyboard and theUSB mouse. Then simply follow the on-screen instructions to get the pi running.

Note: If you don’t want to deal with the hassle of getting a display, keyboard, and mouse, you can also do a headless setup by buying one of these. But this setup also has its own headaches.

Step 2 — Node.js Setup

You now need to setup your pi to run Node.js.

Follow this great guide by Dave Johnson to get Node.js installed onto your pi. Also make sure to follow his instruction for setting up remote accessibility. If you have everything setup correctly, you should see your raspberry pi appear under Shared devices if you’re on a mac. I gave mine the hostname starboy (because I had the album on repeat this entire weekend, ha weekend). Then hit Connect As… on the top right to login using your username and password. If you haven’t changed the default login during setup, it should be username: “pi” and password: “raspberry”.

Note: You can also ssh into your pi using ssh pi@<your hostname>.local, so in my case it’d be ssh pi@starboy.local.

Step 3 — Install System Libraries

First, you’ll need to install ALSA for audio recording on the pi.

That should do the trick.

Note: You can adjust the volume of the speaker amixer cset numid=1 -- <number>where <number> is an integer between -10200 and +400 (centi-dB).

Then you’ll need ffmepg installed to get audio file information. This requires a bit more time (took me about 40 minutes) since you need to compile and install the library yourself. But just follow these instructions and you should be fine. I’ve pasted the commands below as well. Make sure to cd into /usr/src.

First, install H264 support

Then, install FFmpeg

Step 4— Configure Audio

Open the terminal on your pi and do

sudo raspi-config

And follow these instructions to force audio output through the 3.5mm jack. Assuming you’re using the speaker above of course.

Step 5— Modify The Code For Raspberry Pi

The code for the pi is in the same repository as part 1, in the raspberry folder.

The code to run on the pi is in the raspberrypi folder. I actually recommend using Github as the medium to transfer your old. This way, as you modify and push the code from your laptop, you can simply pull down the changes from your Raspberry Pi shell.

Another way is to simply copy the folder onto your pi (using the network folder in step 2 or scp for additional nerd cred) and run node run_raspberry.js.

Don’t forget to also copy over your config.js file and install the node modules using npm install using packcage.json.

We’ll have to make a minor modification to the code in order for it to run on the pi. We’ll have to use the ALSA library we installed earlier to play the output.wav file instead of play-sound.

First remove the play-sound import and instead import exec:

Then use the aplay library to play the output.wav audio file:

Step 6— Assemble The TJBot Chipboard Body

Here’s the link to getting started with the TJBot body kit.

Right now you need to download the file and laser cut it yourself from a piece of 18" x 18" x .080" (457mm x 457mm x 2mm) chipboard. The creators of the kit are working hard to get a process in place so you can order the kit directly. And of course the software will still work without this kit if you plan to put the pi somewhere else.

Here are more instructions on laser cutting.

Then follow this video to assembly instructions.

Here are few pictures of what it looked like while I was putting it together.

And we’re done!

That could be you in the last picture. I hope you enjoy your new found robot BFF as much as I do.

Here are some suggestions for next steps:

  1. Have the Node.js code execute on start-up. rc.local is another option.
  2. Use the Raspberry pi camera. TJBot already has a slot for it. Also look out for my upcoming tutorial where I integrate visual recognition capabilities to TJbot.
  3. Use the LED. TJBot also has a slot for that and here’s the tutorial.

Until next time robot enthusiasts.

Shoutout To

If you have any questions or suggestions for improvement, feel free to reach out at joshzheng@us.ibm.com, connect with me on LinkedIn, or follow me here on Medium.

--

--

Josh Zheng
IBM watsonx Assistant

Head of DevRel @ Great Expectations. Previously DevRe Lead at Shopify and IBM Watson. Hates writing.