• Home
  • blog
  • Teaching Your Raspberry Pi to Speak with IBM Watson

Teaching Your Raspberry Pi to Speak with IBM Watson

Raspberry Pi and IBM Bluemix Adventures Part Two
Artwork by SitePoint/PatCat, Logo rights: IBM and Raspberry Pi Foundation

It turns out there’s a lot you can do with a Raspberry Pi and IBM Bluemix. In the first article in this series, we explored how to connect up a Raspberry Pi to IBM Bluemix, IBM Watson and Node-RED. If you haven’t yet got your Raspberry Pi set up with Bluemix and Node-RED, go through part one first and then come back! I personally used a Raspberry Pi 3 for this, however I’ll assume a Raspberry Pi 2 can work just as well.

In this article, we are going to explore how to bring in text to speech to our Node-RED flow from the previous example.

Connecting Text to Speech in IBM Bluemix

In order to have access to IBM Watson’s text to speech services, we need to add the “Text to Speech” service in Bluemix. Let’s head over to the Bluemix Services page and find the “Text to Speech” service (be careful not to choose “Speech to Text”… that’s different!).

The Text to Speech service in IBM Bluemix

That should take us to the Text to Speech service Bluemix page.

On that page, we’ll see various options for adding this service to our IBM Bluemix arsenal. We make sure our space that we are using for our Raspberry Pi is selected (I called mine “Dev” in the last article) and leave the app unbound. We can give the service a name (I called mine “The Voice”) and give the credentials a name (I left it as is). The only plan I had available was “Standard”, so I left that one as is too. Once we’re happy with our settings, we click “Create”.

Creating our text to speech instance

Once the service is created in our space, we’ll be taken to the page for that service. We click the “Service Credentials” menu item on the left to access the username and password we will need to give Node-RED in order to have access to our new IBM Watson Text to Speech service. Copy down the username and password from this page:

Copying our credentials

Adding New IBM Watson Services to Node-RED

In order to access the IBM Watson Text to Speech service in Node-RED, we will need to install some new nodes. To do so, we SSH into our Pi (or open the terminal from our Pi directly) and type in:

[code language=”bash”]
cd ~/.node-red

This brings us to the Node-RED app folder. From within here, we install a new collection of Node-RED nodes called node-red-node-watson. This includes access to a whole range of IBM Watson services, including the Text to Speech that we need. To install it, we run the following command on our Pi from the Node-RED folder:

[code language=”bash”]
sudo npm install node-red-node-watson

Installing the ALSA Dev Library

The IBM Watson Text to Speech functionality will convert our Node-RED app’s text into spoken audio files, but we also need a way to get the Pi to play these files — otherwise those words will never be heard! In order for this to work on your Pi, you may need to run the following command to install the dev ALSA library:

[code language=”bash”]
sudo apt-get install libasound2-dev

Restarting Node-RED

In order for the new Node-RED node changes to come into effect, we need to restart Node-RED. To do so, we run the following two commands:

[code language=”bash”]


Our New Node-RED Flow

We now have all the parts in place to be able to perform text to speech and have it playback — it is now time to put these into Node-RED and get it to run.

When we open up Node-RED after installing the node-red-node-watson package and restarting, we should see a bunch of new nodes under “IBM_Watson” on the left-hand side:

Our new IBM Watson section in Node-RED

Let’s scroll down, find the “text to speech” node and drag it into our Node-RED sheet:

The text to speech node

Once that’s in our sheet, we double click it to open up its settings:

Double click the node

This is the section where we add in our credentials that we copied earlier from IBM Bluemix (the username and password). We paste those into here and choose a language and a voice for our Pi’s voice. We leave the file format as “WAV”, then we click “OK”:

The settings for our text to speech node

Our text to speech is ready to work its magic. We now need a way to tell it what to say. Lets get it to tell us what the temperature of our Pi is, just as we previously were sending to IBM Bluemix’s system in the last article.

To begin, we’ll drag in a new function node into our sheet:

Drag a new function node into the sheet

Double click that new node and enter in the following code to its function:

[code language=”js”]
msg.payload = “My current CPU temperature is ” +
msg.payload.replace(“temp=”,””).replace(“‘Cn”,””) +
” degrees celsius”;
return msg;

This function is working to format our message just like the one we used in the last article, however rather than formatting it into a JSON string, we are formatting it into a human readable sentence. The sentence will say, “My current CPU temperature is X degrees celsius”. We store this sentence within the msg.payload variable, which is what the IBM Watson Text to Speech node expects. We can also give the function node a label, I called mine “Temperature Text”:

Our function to turn it into a readable sentence

Continue reading %Teaching Your Raspberry Pi to Speak with IBM Watson%