Introduction: Twitter Enabled Text to Speech

About: I post updates on twitter and instagram: @amandaghassaei

Let the world know what you're eating for breakfast with an 80's style vocal synthesizer! This project uses an Arduino to send your Twitter stream to a voice generator chip called the SpeakJet. The setup that I use here can also be extended to other applications where you'd want to translate text into speech, some ideas:

-a robot which announces things or responds to its environment by speaking
-some kind of assitive technology for the deaf or handicapped
-at the very least, it's a fun alternative to using the serial monitor to print data from the Arduino

In this setup the Arduino requests and receives information from Twitter via an Ethernet shield, which is directly plugged into a router (the USB connection shown in the picture is being used solely for power, not data transmission). An intermediary chip called the TTS256 translates serial text data from the Arduino into a series of allophones to send to the SpeakJet. The output from the SpeakJet is sent to an amplifier and then to a small speaker.

A demo of the SpeakJet's voice can be found here.

Parts List:

(x1) SpeakJet IC Sparkfun COM-09578
(x1) Text to Speech chip for SpeakJet - TTS256 Sparkfun COM-09811
(x1) Arduino Uno Sparkfun DEV-11021
(x1) Arduino Ethernet Shield Sparkfun DEV-09026
(x1) 8ohm Speaker Sparkfun COM-09151

(x1) LM386 Amplifier Digikey LM386N-1/NOPB-ND
(x1) 0.047uF ceramic capacitor Digikey P4307-ND
(x1) 220uF electrolytic capacitor Digikey P15322CT-ND
(x1) 10ohm resistor Digikey CF14JT10R0CT-ND
(x1) 10kOhm potentiometer, linear taper Digikey 987-1308-ND
(x1) LED Digikey 365-1175-ND
(x1) 220ohm resistor Digikey CF14JT220RCT-ND
(x2) 10kOhm resistor Digikey CF14JT10K0CT-ND

Other Materials:

Jumper wire Amazon
Solder
USB cable Amazon
Ethernet cable Amazon
breadboard (this one comes with jumper wires) Amazon

Step 1: Wire Up the SpeakJet With Arduino

I wired up the SpeakJet similarly to how it is wired in Sparkfun's Arduino shield for the SpeakJet called the "VoiceBox Shield," here is their schematic.  The main difference between the way I set up the SpeakJet and the way it is set up on Sparkfun's shield is the amplifier;  I didn't include an amplifier in my circuit yet as I was just trying to get the chip working with the minimum amount of parts, I'll show how to set up an amplifier in a later step.  Additionally, I was confused by the Jumper labeled "jumper-32-3" in the sparkfun schematic, in my circuit I just hooked digital pin 2 of the Arduino directly to pin 10 of the SpeakJet.  The circuit I set up is shown above, this schematic shown in fig 2 was modified from the original eagle files provided by Sparkfun, I've also included some photos of the breadboard setup for reference.

When you wire up the circuit and upload the code below (written by the Sparkfun folks), the SpeakJet will say "ready" then output the message "All your base are belong to us" in Robot-Speak voice followed by some R2D2-esque beeps, and light up the green LED attached to pin 16.  (Of course, you will need to hook up a speaker between pin 18 of the SpeakJet, labeled "V_OUT" in the schematic above, and GND to hear this message).  I used a small, 8Ohm speaker and only heard the message very faintly (this is why it's good to eventually amplify the output from the SpeakJet before sending it to speakers).  Press the Arduino's reset button (fig 4) to hear the message as many times as you like.
If you want to add you own messages into this code you can use the SpeakJet Dictionary as a reference.  I'll walk you through an example here.  I want to get the SpeakJet to say the phrase "Amanda is rad."  The first thing I do is construct each of the words based on the examples in the SpeakJet dictionary.  For "Amanda" we have:

man= \MM \SLOW \AY \SLOW \NE
duck= \DO \SLOW \UX \KE


Combine these to get

Amanda= \SLOW \UX \MM \SLOW \AY \SLOW \NE \DO \SLOW \UX

"is" is listed in the dictionary:

is = \SLOW \IH \ZZ

for "rad" combine the following:

rabbit=\SLOW \RR \AY \BE \RELAX \IH \TT
red = \RR \SLOW \EH \ED


to get:

rad= \SLOW \RR \AY \ED

Pages 15 and 16 of the SpeakJet User Manual gives all the numerical codes for each of these sounds.  Here is my example transcribed to numerical form:

Amanda = \SLOW \UX \MM \SLOW \AY \SLOW \NE \DO \SLOW \UX
Amanda =      8      134  140      8     132      8     141  175      8     134

is = \SLOW \IH \ZZ
is =      8     129 167

rad = \SLOW \RR \AY \ED
rad =      8     148  132 176


We can set the volume, speed, and other parameters of the speech by calling the numbers 20-23, I copied the following values from the Sparkfun code above:

20, 96, 21, 114, 22, 88, 23, 5,

this sets Volume (20) to 96, Speed (21) to 114, Pitch (22) to 88, and Bend (23) to 5.  You can play around with these numbers to learn more about how they work.  If you read the table on page 15 of the SpeakJet Manual you can find out about other control commands, as well as how to incorporate pauses into your speech. 

Putting this all together, I have the following series of numbers:

{20, 96, 21, 114, 22, 88, 23, 5, 8, 134, 140, 8, 132, 8, 141, 175, 8, 134, 8, 129, 167, 8, 148, 132, 176}

And here is the final code, it is mostly copied straight from the Sparkfun code above (I made the last "a" and "d" in "rad" slow by putting an extra 8 in front of the numbers 132 and 176 to get these parts to sound a bit more clear):

Step 2: Using the TTS256 Text to Speech With the SpeakJet

The TTS256 is an IC which used predefined speech rules to break up text into various sound components (called allophones) and then translates these into the numbers we looked up in the last step.  The combination of TTS256 and SpeakJet allow you to turn text strings stored in the Arduino into speech.  This is obviously much more convenient than what we had to do in the last step to make a phrase, but due to the complexities of pronunciation, this chip will not always work perfectly.  Sometimes it is best to spell words in a way that the TTS256 will handle better (hint hint it does not know how to spell).  For example, if you wanted to use the word "pterodactyl," it's a good idea to spell it "terodactil."

I loosely followed the directions found on the Sparkfun website to get the TTS256 up and running with the SpeakJet.  First connect 5V to pin 28 and ground to pin 14 of the TTS256.  Next unplug the connection between digital pin 2 of the Arduino and pin 10 of the SpeakJet.  Connect digital pin 2 to pin 18 of the TTS256 instead.  Connect pin 24 of the TTS256 to pin 10 of the SpeakJet.  Connect Pin 20 of the TTS256 to pin 15 of the SpeakJet.  A schematic is shown in fig 2, and figs 1, 3, 4, and 5 show the circuit on a breadboard.  Now that you have the hardware setup, upload the following code.  You should hear the SpeakJet output the word "ready" when it is done resetting, then it will output whatever message you encapsulate in the line:

speakjet.println("your message here")
You may have noticed in this code that the pins E0-E7 (the yellow wires) are no longer being used, they are set to low and held low while the TTS256 sends data to the SpeakJet.  I left these connections bc I didn't want to confuse you before, but if the code above is working, then you can go ahead and disconnect these pins from the Arduino and permanently attach them to ground.  This will free up many of the Arduino's pins for other things.  You can also attach the SpeakJet's reset pin directly to the Arduino's reset (near the power pins), it was previously attached to digital pin 3.  I've included a schematic (fig 6) and some breadboard pics (figs 7 and 8).  Now there should only be three connections between the Arduinos data pins and the SpeakJet/TTS256:

Digital Pin 2- this pin is responsible for sending messages to the TTS256
Digital Pin 4 - this is connecting to the SpeakJet's SPK pn- this pin lets us know that the SpeakJet is currently speaking (HIGH) and currently not speaking (LOW)
Digital Pin 13 - this is connected to the SpeakJet's RDY pin- this pin lets us know that the SpeakJet is ready for more data (HIGH) or not ready (LOW)

note- any of the Arduino's digital pin connections may be reassigned, there is nothing special about the setup I've provided as an example here.  To change the pin setup change the numbers in the following lines:

#define txPin 2
#define RDY  13
#define SPK  4


Here is the simplified code to go with the simplified hardware setup.  To get this code to start from the beginning again, press the Arduino's reset button.
Finally, here's a piece of code that sends messages to the SpeakJet from stored strings:

Step 3: Receiving Tweets From Twitter

The Arduino Ethernet Shield lets you request data from the internet and parse it with the Arduino.  If you've never used the Arduino Ethernet Shield before or want to learn more about how it works, I recommend reading Randofo's Ethernet Shield Tutorial.  The following piece of code reads incoming messages from a twitter feed, checks to see if the tweet is new (different from the last tweet saved in the Arduino's memory), and if it is new, sends the tweet to the TTS256/SpeakJet.
The hardware setup has changed slightly from the last step to make room for the Ethernet shield connections.  A full schematic is given in fig 2.  The new pin connections are as follows:

Digital Pin 2 connected to pin 18 0f the TTS256
Digital Pin 3 connected to pin 16 of the SpeakJet
Digital Pin 5 connected to pin 17 of the SpeakJet


These pin assignments can be changed by editing the following lines of the firmware:

#define txPin 2
#define RDY  5
#define SPK  3


there are two lines that you will want to change to customize this Twitter setup.  The first is the ip address line:

IPAddress ip(192,168,22,112); //<<< ENTER YOUR IP ADDRESS HERE!!!

you will need to find out your IP address and put these numbers in the parentheses in place of what I've written.  The second line to change is the Twitter Feed address line:

String twitterName = "GET /1/statuses/user_timeline.xml?screen_name=instructables&count=1 HTTP/1.1";

As it is written here, this firmware will always look at the instructables feed, to change it to another feed follow this format:

"GET /1/statuses/user_timeline.xml?screen_name=[YOUR TWITTER NAME HERE}&count=1 HTTP/1.1"

When you upload the firmware you will hear the SpeakJet say "Ready" and then "SpeakJet is ready" when it is done initializing.  If this happens then you know your hardware connections to the SpeakJet are good.  Then you will hear the SpeakJet say "connecting to server" if it says "could not connect" then your ethernet shield is having trouble connecting to the internet.  Make sure you have the right IP address in the firmware.  If everything is ok, you will hear "connected, making HTTP request" and "ready for tweets" when the initialization of the ethernet connection is done.  Then the SpeakJet will read off the most recent tweet from your twitter feed of choice.  The Arduino will keep connecting to Twitter every 10 seconds to check if the Twitter feed has been updated and it will read any new tweets out loud.

Step 4: SpeakJet Amplifier LM386

I mentioned earlier that you will want to amplify the output from the SpeakJet before sending it to speakers if you want to output to be louder.  Here's a simple circuit I set up using an LM386, a common amplifier you can even find at Radioshack.  My schematic is shown in fig 2, I copied this from the lm386 datasheet.  I didn't have the exact capacitor values handy, so I substituted a 220uF for the 250uF and a 0.047uF for the 0.05uF.... this is fine.  The amplifier is powered with 0V and 5V, if you wanted it to be louder, you could try powering it with 9V.  The images above show the final circuit on a breadboard.

Step 5: Future- Receiving Mentions From Twitter

I would like to extend my Arduino code so that I can search the mentions of a particular account (when anyone tweets something @instructables, for example) and have the SpeakJet announce each of these mentions as they come in.  Unfortunately Twitter recently changed their API to require authentication for search requests, so the code will have to be changed a bit from step 3 to get this to work.  I've searched for a while but still haven't figured out a great solution that doesn't require running a dedicated computer with the Arduino to manage the authentication.  I'm sure that it is possible to do this with only an Arduino, maybe someone with more experience working with the Twitter API has some insight they could leave in the comments?

I do have a few things figured out.  I want to make a GET search request.  I think the format of the request will look something like this for a search of @instructables:

"GET /search.xml?q=\"%40instructables\"&rpp=1&result_type=recent"

where:
q= refers to my search terms
the %40 replaces the @ in the search
rpp mean results per page, I'm only interested in the most recent result, so I'll leave that at 1
type=recent sorts the search by time

On solution to the authentication problem might be to connect to another site that can request the search from Twitter.  If anyone has suggestions for how to set this up I'd love to hear them!