This tutorial is about enabling a joking skill on the Raspberry Pi. You can quickly and easily build a basic “fun” skill, and add it to your personal assistant. This DIY personal assistant is based on the basic IoT device – a Raspberry Pi – called TJBot. What is a TJBot? It is an open source project that you can use to build a robot based on Raspberry Pi computer, and it can help you learn the basic capabilities of cloud, AI, and IoT.

Photo of a TJBot robot

If you’ve never worked with TJBot and its Raspberry Pi, don’t worry. This tutorial explains in step-by-step detail how to build your chatbot and get it telling you jokes.

Introduction

I realized that a personal assistant that can tell you a joke might be the one thing to get you on the right track on Monday mornings.

In this tutorial, I share my experience with launching a chatbot on the Raspberry Pi board of the TJBot. The Pi is equipped with the USB microphone and a small portable speaker. We’ll use these external sound interfaces with the Watson Speech to Text and Watson Text to Speech services that generate speech for users and convert sounds into text for our chatbot backend.

Hopefully this project inspires you and lets you have some fun while you learn about the IBM Watson services. So let’s get started!

Learning objectives

After you complete this tutorial, you will know how to:

  • Create a DIY personal assistant based on the open source project, TJBot
  • Use IBM Watson Assistant along with the Watson Speech to Text and Watson Text to Speech services to create a chatbot.

Prerequisites

First, you need the elements to build your own TJBot. You can build your own TJBot, or you can buy one of the TJBot kits. In addition to the Raspberry Pi 3 with an SD micro memory card, you will need just the USB microphone and a speaker that is connected by using a 3.5 mm jack (or a USB or Bluetooth speaker would also work).

Estimated time

It will take about 2 hours to complete this tutorial. Most of this time is spent downloading the Raspian OS, which is 2 GB in size.

Steps

Follow these steps to get your Raspberry Pi to tell you jokes:

  1. Set up your hardware
  2. Get Watson services from IBM Cloud
  3. Add a skill and a joke entity to the Watson Assistant service
  4. Add the joke skill to the Raspberry Pi

Step 1 – Set up your hardware

To set up the Raspberry Pi for our TJBot, I recommend that you complete these steps:

  1. Download and install the Raspbian OS using NOOBS on to the SD micro memory card.

  2. Connect all the components of the Raspberry Pi.

    1. Connect a USB keyboard, USB mouse, and display using an HDMI cable to your Raspberry Pi.
    2. Connect the USB microphone and speaker through a 3.5 mm jack.
    3. Insert the SD micro memory card.
    4. Connect the power through the micro USB port.
  3. Start your Raspberry Pi and connect it to the network. You want to connect it to wifi or ethernet and also the local LAN network to which your computer is connected.

  4. Add remote access and enable SSH. Now, you can easily ssh to your Raspberry Pi from your computer, for example:

     $ ssh pi@address-on-the-lan-to-your-pi
    

    Or, you can just use the terminal in your Raspberry Pi’s desktop.

  5. Set up the speaker and microphone in the Raspbian OS. From the terminal application using SSH or from the desktop on the keyboard and display that is connected to the Raspberry Pi, run this command:

     $ sudo raspi-config
    

    Go to Advanced options and select Sound. Select the 3.5 mm audio jack from the menu.

    To connect a USB or Bluetooth speaker, follow these Instructables steps.

Step 2 – Get Watson services from IBM Cloud

Now that we have set up our hardware, we need to set up the cloud backend services that will enable our TJBot to tell a joke.

  1. Log in to your IBM Cloud account.
  2. Go to the Catalog, and in the AI section, select the three services that we are going to use:

    • Watson Text to Speech
    • Watson Speech to Text
    • Watson Assistant
  3. To add the AI services to our cloud app, complete these steps for each of the services:

    1. Create the service.
    2. In the left nav, click Service credentials.
    3. In the service credentials section, auto-generated service credentials appear in the table. Click View credentials in the Actions column. If the credentials do not appear in the table, click the New credential button.
  4. For each of the services, copy the API key (credentials) and save those credentials to use them when you set up the services on the Raspberry Pi in your TJBot.

Step 3 – Add a skill and a joke entity to the Watson Assistant service

After you add the Watson Assistant service, you need to create the joke skill.

Create the skill and add an entity

  1. Open the Watson Assistance service, and launch the Watson Assistant tool.
  2. Click the Create a Skill button.
  3. Click Create new.
  4. On the Add Dialog Skill window, click the Use sample skill tab.
  5. Click the Entities tab, and click the Add entity button.
  6. Add the joke entity, and specify additional synonyms for joke to make the AI more accurate, and click Add value.

    The joke entity dialog

Create the dialog

  1. Click the Dialog tab, and use the menu for the first Welcome branch to add a branch after it.
  2. Name the branch tell-a-joke and add the @joke entity, and then add one or preferably more responses (that is, jokes). You might want to specify the response variations to random so that your responses occur in various orders.
  3. After training it and specifying the various responses, test out your new skill. Click the Try it button in the upper right corner.

    ai-services

  4. When you are done testing your skill, click the Skills tab, and select the menu in the upper right corner of your skill, and click View API details.

    Be sure to copy the Skill details (which will be similar to the details shown in the following code block), which you will use when binding your application on the Raspberry Pi with the cloud-based AI service:

     Skill Name:Customer Care Sample Skill
     Skill ID:SOME-SKILL-ID
     Workspace ID:THE-WORKSPACE-ID-THAT-NEEDS-TO-BE-COPIED
     Legacy v1 Workspace URL:https://gateway.watsonplatform.net/assistant/api/v1/workspaces/SOME-WORKSPACE-ID/message
     Service Credentials
     Service Credentials Name:auto-generated-apikey-SOME-CREDENTIALS
     Username:apikey
     Password:SOME-CREDENTIALS
     Service Credentials Name:auto-generated-apikey-SOME-CREDENTIALS
     Username:apikey
     Password:SOME-CREDENTIALS
    

Step 4 – Add the joke skill to the Raspberry Pi

Now that we’ve defined our joke skill, we need to add it to the Raspberry Pi. We’ll use the TJBot open source project to quickly set up the sound and chatbot capabilities on the Raspberry Pi.

Adding TJBot and node to Raspian OS

  1. From a terminal, issue the following command to install some TJBot bootstrap code:

     $ curl -sL https://raw.githubusercontent.com/ibmtjbot/tjbot/master/bootstrap/bootstrap.sh | sudo sh -
    

    As the final step, you will see this screen:

    ai-services

  2. To run our joke skill, you need to install the Node.js 9 for Raspian (Stretch) library:

     $ curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
     $ sudo apt-get install -y nodejs
    
  3. Optionally you can remove now outdated software packages:

     $ sudo apt-get -y autoremove
    
  4. Reboot your Raspberry Pi:

     sudo reboot
    
  5. After the Raspberry Pi restarts, clone the project from the GitHub repository:

     $ cd ~/Desktop
     $ git clone https://github.com/ibmtjbot/tjbot
    
  6. While there are other projects, we need just the one with the chatbot. Change the directory to the conversation sample:

     $ cd tjbot/recipes/conversation
    

Configuring the Watson services

Remember those API keys that you copied up in Step 2? Here’s where you specify them, in the configuration file for the services. Also, we’ll change the wakeup word for our chatbot.

  1. Copy the configuration-default.js file to the configuration.js file.

  2. Edit the configuration.js file (using vim or nano; you can install vim with this command: $ sudo apt-get install vim) and update the API keys for Watson Speech to Text, Watson Text to Speech, and Watson Assistant services.

  3. Still in the configuration.js file, change the wakeup word, or name, for TJBot. Be sure to choose a name that can be easily transcribed by the Watson Speech to Text service.

     // set up TJBot's configuration
     exports.tjConfig = {
     log: {   level: 'verbose'    },
     robot: {   name: 'tee jay bot'  }
     };
    

Building the application and running the chatbot

Finally, you’re ready to run the chatbot.

  1. Compile the application so that you can run the chatbot:

     $ npm install
    
  2. Run the chatbot:

     $ sudo node conversation.js
    

    The node application listens to the sound and generates text with Watson Speech to Text service.

  3. Let’s test out our chatbot. Issue the request (run the joke skill). Say the following phrase:

     wakeup word, tell a joke
    

    The chatbot processes your speech, converting it to text, and then sends back this response, which the chatbot converts the text into speech, and our TJBot vocalizes it to us: “The wake up word is 'robot'“. (This assumes that you followed my example of using “robot” as the wakeup word.) To have your TJBot tell you a joke, say the following phrase:

    robot, tell a joke
    

    You can see the recording of what’s happening in the JavaScript program in the Debug tab in Node-RED:

     pi@tjbot1:~/Desktop/tjbot/recipes/conversation $ sudo node conversation.js
     verbose: TJBot initializing microphone
     verbose: TJBot initializing LED
     verbose: TJBot initializing servo motor on PIN 7
     verbose: TJBot initializing Camera
     verbose: TJBot initializing assistant service
     verbose: TJBot initializing speech_to_text service
     verbose: TJBot initializing text_to_speech service
     verbose: TJBot initializing visual_recognition service
     info: Hello from TJBot! My name is robot.
     verbose: TJBot library version v1.5.1
     You can ask me to introduce myself or tell you a joke.
     Try saying, "robot, please introduce yourself" or "robot, what can you do?"
     You can also say, "robot, tell me a joke!"
     verbose: TJBot initializing microphone
     info: TJBot heard: robot tell me a joke
     verbose: TJBot response from conversation workspace id some-number-id
     What robots do during vacations? \n Recharge batteries. :
     verbose: TJBot speaking with voice en-US_MichaelVoice
     info: TJBot heard: yeah
     verbose: TJBot speaking: What robots do during vacations? \n Recharge batteries.
     ========= { filename: '/tmp/tjbot11921-2643-axpqqt.6udfc',
     gain: 100,
     debug: true,
     player: 'aplay',
     device: 'plughw:0,0' }
    

You should hear the speech. If it is too low volume, you can turn up the volume. Use the Raspberry Pi terminal command to adjust the volume:

amixer sset PCM,0 90%

This command sets the volume on the 3.5 mm jack to desired level.

Have fun and add more jokes!

Troubleshooting

If you do not hear the sound, you might want to run the following command:

$ sudo update-initramfs -u

Then, reboot Raspberry Pi, and validate that snd modules are there:

$ lsmod

What to do next

Now you are ready to add the camera (raspicam 5 MB or 8 MB) to your TJBot, and add a Watson Visual Recognition skill, so it can recognize objects.

You can also set up your TJBot to use Node-RED and take advantage of the Node-RED nodes you can use to control your TJBot.

And, please follow me on the Twitter (@blumareks) and share your jokes with me, so that I can add them to my TJBot and make my Monday mornings better.