Monday, June 12, 2017

Raspberry PI Zero W - Speech Synthesizer using MQTT protocol

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.
  1. Raspberry PI Zero W currently $10.00
  2. 16 or larger Gigabyte SD card with Raspberian installed and configured $7 to $10
  3. 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.


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”  
So:
ssh pi@rpi-speak.local will let me ssh into my system with out knowing the IP address.


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”


*** 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.
Install instructions:


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).


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:
python say.py

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:



Tuesday, May 23, 2017

Generic Raspberry PI 2.2" TFT Screen w/Buttons and IR

Generic 2.2” TFT Screen w/buttons and IR INFORMATION
Or What I learned about the Chinese TFT Screens for Raspberry PI 2 and 3


1st Here is the screen I bought from eBay:
Raspberry-Pi-2-3-B-2-2-034-TFT-Screen-LCD-Display-Expansion-Board-Buttons-IR-Sensor


2.2"TFT Screen LCD Display HAT 320x240 for Raspberry Pi 3/2 w/Buttons IR Sensor

Features:

This auction is for a 2.2 inch screen with buttons and IR function for Raspberry Pi 3 and 2 Model B.


Details:

Screen sizing 2.2 inch and supporting a 320x240 resolution with high PPI, being small but able to provide a fine image.

Size: 65mm×56.5mm, standard as a Raspberry Pi HAT board.

Multi-button design: 6 buttons, meeting different demands for buttons from various users

With an Infrared receiver.

Quick-responding technical support for free.


Introduction:

The 2.2 inch screen is a standard Raspberry Pi HAT (hardware attached on top). It supports a 320x240 resolution with high PPI.

Though small, it can display any images clearly. With the 6 buttons on the board, you can define their functions by yourself. And an

Infrared receiver is equipped so you can control the screen remotely.




  • Like Most cheap things, there isn’t documentation provided, and you have to search
  • Also like most cheap things there is a lot of just plain miss leading information.

The key to this display appears to be the fact that it has IR on it. Once I searched “w/IR” I was able to find the same display being sold by Sunfounder (for a liTTYe bit more then you can find on eBay).


Sunfounder provides a Raspberry PI Image and a/or a Python script for setting up the LCD.
(Plus information on how to get the buttons working/IR working)

Their image is based on Raspbian Jessie, and the current OS is Raspbian Pixie
So If you already have a working install (which I did) use the python script to add the needed files and information to your install.

1st thing I learned even the instructions are not quite right. You need to run the python script from the CLI (command line), and as ROOT (sudo).

I answered all the question with the default entries or “y” for yes.

The system rebooted, and the screen (after a liTTYe while) did start to display, and even displayed the “X” server (Thou, my screen is a liTTYe small to use the GUI effectively).

What was a disappoint was it shut the HDMI off and I didn’t get a GUI on it.
But by hitting “CTRL-ALT-F2”, “CTRL-ALT-F4”, and “CTRL-ALT-F6” I was able to get to the command line.


*It was at this point I shutdown the system, unplugged the display, and hoped that it would switch back to HDMI - it did not *
I was still able to get to TTY2,TTY4 and TTY6 - so I could fix the issue from the command line.

Once again I started to google the problem - and once again I can across many people who had the same problem, and a dozen different answers.

Finally I came across this post on Stackexchange

This answer is what worked:
Although this question is very old, I would like to post my answer that I found recently. I am running Raspberry PI 2 Model B with 2.8 PiTFT capactive display. Once I figured out how to get my pitft display to work with Raspberry PI, I could not get Raspberry PI to switch over to HDMI output no matter what I did. Then, I came across this information, which worked for me.
There's two ways to do it. In older Pi installs, use the fb0 framebuffer when you want to display stuff on the HDMI/TV display, for example: FRAMEBUFFER=/dev/fb0 startx will use the HDMI/TV framebuffer for X windows instead of the PiTFT
On Jessie Pi installs, run sudo nano /usr/share/X11/xorg.conf.d/99-fbdev.conf to edit the configuration file and make sure it contains: Copy Code
Section "Device"
Identifier "display"
Driver "fbdev"
Option "fbdev" "/dev/fb0"
EndSection
change the Option "fbdev" "/dev/fb0" line to Option "fbdev" "/dev/fb1" if you want the xdisplay on the PiTFT or fb1 to fb0 for HDMI output.
Update: If don't see the code above in the file, then simply copy and paste what is shown in this answer at the bottom of that file. If you can't find 99-fbdev.conf file in that folder, then there is probably another file with different name like 10-evdev.conf in the same folder which you will have to modify to make it work as described above.
answered Jul 25 '16 at 14:40



The file in /usr/share/X11/xorg.conf.d/ did not exist, and I had to create it.
I didn’t make any other changes or anything to any of the other config files.

Rebooted the system, and sure enough I had X running on the HDMI again.

The TFT screen did boot, but came to a blank screen with a flashing cursor.

** What I learned **
1 It seems TTY1 is on the TFT screen, but linked to X running on the HDMI (in other words what you do in that session seems to have direct effect on X)
2 TTY2, TTY4, TTY6 appear to work on the HDMI
3 “CTRL-ALT-F7” is the X screen on HDMI
4. TTY3, TTY5 appear to work on the TFT screen
5. If you try to start X from TTY1,TTY3,TTY5 the system locks up and displays a bunch of error messages.  Which is fine, X on that small screen was not great anyway.