Raspberry PI Zero W - Speech Synthesizer using MQTT protocol
Jun 12, 2017
The goal of this project is to make a low cost speech synthesizer using text to speech (TTS) for the Arduino or other Microcontroller to use. *And one day voice Recognition*
For the project you need just three things and some time.
- Raspberry PI Zero W currently $10.00
- 16 or larger Gigabyte SD card with Raspberian installed and configured $7 to $10
- Fe-PI Audio sound card (I2S sound) for the Raspberry PI $14
Total Cost: about $34.
You will spent quite a bit of time setting this up and that maybe worth more than money.
I used the Zero W because in the end I needed it connected to the internet, you could use the Zero but you will most likely want to get it on the net.
A Raspberry PI (model A or B) 1st generation, B+, 2, and 3 should work as well.
I’m not going into how to setup the PI other than the software that needs to be installed.
- For a headless install follow this tutorial:
One of the first things you’ll also want to do is setup the Fe-PI. See my notes here:
- The Fe-PI is currently supported in the kernel and setup isn’t as bad as it seems.
After you have your SD card setup and the PI booting. And are able to connect to your network. You will most likely want to rename your Raspberry PI from the default name.
Again, I’m not going to go into this as there are hundreds of tutorials on the subject.
I renamed mine to rpi-speak
Now to connect to the PI with SSH or Web, or MQTT I can use “rpi-speak.local”
ssh firstname.lastname@example.org will let me ssh into my system with out knowing the IP address.
- This works most of the time, and sometimes you do need the IP address, I’m not entirely sure why that is. Find the IP address tutorial (headless) https://www.raspberrypi.org/documentation/remote-access/ip-address.md
Next you’ll want to make sure you are up to date.
sudo apt-get update
sudo apt-get upgrade
- This might take a while, you’ll want to reboot when it done.
You’ll want to install flite - (festival-lite) speech synthesizer. http://www.festvox.org/flite/
sudo apt-get install flite
Once installed give it a test and make sure you hear the audio.
flite -t “Hello World”
- It should be noted my audio levels were/are pretty low, I forget how I fix/changed this
- It has to do with the ALSA mixer. Mine however keeps defaulting to a low volume.
- sudo apt-get install alsa-utils this has alsamixer and amixer
*** OPTIONAL STEP ***
Install Apache2 and PHP, Why? At first I thought I would beable to just send a web form and have the PI speak. It seems that while I was able to execute the commands, everything was being piped to the webpage. And it just didn’t work.
I didn’t uninstall because there are some useful things that can still be done with a web site running on the PI. But at this point, it’s a optional step, and not needed.
- I did install some CGI and PHP scripts that I found on the net. For more Information see below. Again not required.
- http://www.yolinux.com/TUTORIALS/BashShellCgi.html *I installed most of these*
- All of the above is good information, but not needed to complete this project.
It was at this point that I still needed away for the Arduino to “talk” to the PI. There are many ways to do this, and here are just a few (I didn’t use).
- Arduino YUN Bridge - The YUN is a linux based controller with a “leonardo” on board as well, for the two processors to communicate, a Bridge is created. https://www.arduino.cc/en/Reference/YunBridgeLibrary
- The YUN Bridge was ported to the PI by Paul Stoffregen
- The Bridge seems like a good idea, but doing some reading may be a bit to setup.
- I settled on a MQTT protocol mainly because of easy of setup.
What is MQTT?
Almost every tutorial on the subject starts off asking this question. Some answers go into great detail, and others are very simple. I’ll stick with the simple.
MQTT is a machine to machine simple protocol, that uses a publish/subscribe. MQTT uses a BROKER which is really just a server that handles all the messages.
MQTT Brokers can either be public or private, and there are good uses for both.
A Public Broker generally is reachable from any where, and by any one on the internet. Most do provide some type of security. The draw back however is that anyone/everyone can subscribe to your information. It is unwise to publish sensitive data to a public Broker. Most of the FREE public brokers will not guarantee 100% update.
Here are some public Brokers: (There are many more)
http://www.mqtt-dashboard.com/ (a HiveMQ Dashboard)
I used a Private MQTT Broker, and it’s running on the Raspberry PI.
Mosquitto is a open source, free broker that is easy to install and easy to use.
Here are some tutorials I used to install this, but it comes down to a simple apt-get command on the PI.
sudo apt-get install mosquitto mosquitto-clients python-mosquitto
It’s really that simple, the mosquitto server starts as soon as it’s installed, the mosquitto-clients has the command line commands to publish and subscribe. And python-mosquitto is the library to use MQTT with python.
If you are using linux, you can also install the mosquitto-clients package, and test your install out.
MQTT uses “Topics” - this is how messages are delivered. For my project I use two “topics” - Say and Said (I will probably end up changing at least one of these).
Topics are created by a subscribe or publish to the MQTT Broker.
Here is where it gets interesting thou.
On the Raspberry PI I have a python script running that subscribes to “Say”, and publishes to “Said”. Using the desktop MQTT client I can run one CLI to publish to “Say” and one CLI to subscribe to “Said”. The effect, the two computers can talk to each other.
So with the MQTT (mosquitto broker) running on the PI,
ssh into your pi, and issue the command:
mosquitto_sub -h localhost -t “Say” -v
And issue this command on your desktop.
mosquitto_pub -h rpi-speak.local -t “Say” -m ‘-voice slt -t “Hello I am Sally”’
You should see:
-voice slt -t “Hello I am Sally”
In your SSH window.
So the -h is the host flag, it is the server running the MQTT broker, and can be public or private (such as in this case)
-t is the topic we want to subscribe or publish to “Say”.
It’s really that simple to send a message to a machine running a MQTT subscription.
Once you verify that is working, you’ll need to install a couple of more things, and a python script to get it all working.
Installing paho-mqtt for python.
Type: pip install paho-mqtt on the Raspberry Pi.
I published my python script to github here:
Most of what I learned, I learned from here:
And while he is turning GPIO on and off, it contained enough information to make my “Speech” script.
You will want to copy and paste the script to your Raspberry PI. I just put it in my home directory. “/home/pi/say.py”
You can test the script at this point by typing:
And on your desktop, publish a message to the “Say” topic.
mosquitto_pub -h rpi-speak.local -t “Say” -m ‘-t “Hello World!”’
You should hear, the PI say “Hello World”.
We are almost done with the Raspberry PI, we will want to set the script to start when the PI is booted or rebooted.
The easiest way is to setup a cron job. You probably don’t have any cron jobs, yet - so the PI will create a new cron file.
Type: crontab -e to edit the file, and add @reboot python /home/pi/say.py & to the end of the file, save and exit nano, and reboot. If all goes well when the system is ready - it will say “Ready”
- Don’t forget to the & at the end of the reboot command.
That is everything you need for the Raspberry PI, at the start of this project I wanted a cheap
speech synthesizer that an Arduino, or other microcontroller could use. So we need to add a MQTT library to the Arduino IDE. There are a number of MQTT libraries, but this tutorial
https://www.baldengineer.com/mqtt-tutorial.html recommends the PubSubClient library.
The easiest way to install is with the “library manager” on 1.6.x and above IDE.
The github for the library is here: https://github.com/knolleary/pubsubclient and the full documentation for it is here: http://pubsubclient.knolleary.net/
This is a pretty easy to use library, I modified the mqtt_esp8266 example to publish to the “Say” topic, and subscribe to the “Said” topic.
The only problem I had was the library was unable to connect with the host name “rpi-speak”
Or “rpi-speak.local” and needed an IP address.
This was the only hickup - But being able to SSH into the PI, or using the Webserver and CGI script for the environment (env.sh) it was pretty easy to get the IP address.
My modified sketch can be found here: