Introduction: Twitter Enabled Text to Speech
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
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
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 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
Step 5: Future- Receiving Mentions From Twitter
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!