Friday 31 May 2013

The code!!!

Ok, this is probably the moment you've all been waiting for!  This is a tidied up version of the code for the pi-jarvis program.

Please bear in mind that while some customisation is possible (i.e. you can change what it says) there are some severe limitations.  Notably, the weather.  It has been tailored to work with data taken from the Met Office's 5 day UK forecast, and I'm afraid that if you are from another region it won't work.
That said, the basic concepts are in place, so there is nothing to stop you editing it to get and parse data from another source.

Before you start compiling the code though, there are two more things to do.  You need to create a shell script to download the xml file containing the weather information and another shell script to run the program.

You should put this in the same directory as the speech.sh script and the program itself.  I've got all mine in /home/pi (terrible practice I know, but I haven't got round to changing it yet).

The first script you need is here.  There are two things you might want to change.

First, the section of the URL that says "xml/310011?". The number, 310011, is the weather station I am getting the data from.  You will almost certainly want to change this, unless you happen to live in Leicester.

The full list of weather stations can be found here, scroll down to the section that says XML and search through it for your nearest station.

The other thing you might want to change is the location you are saving the xml document.  To do this, just alter the final section (--output-document=/home/pi/weather.xml). Note that the file name must be 'weather.xml' else the program will not be able to open it.

The second script is even simpler and I'll just write it here rather than post to git for ease.

#! /bin/bash
/home/pi/pijarvis


Change the directory /home/pi to the directory you will be using and save it as "runspeech.sh", again, in the same directory.

Ok, so now for the code itself.

It can be found here.  There might be a few of my test 'printf' statements floating around, if you spot any, feel free to delete them, they aren't important.

Copy it all into a file called pijarvis.c and save it to the same directory as the two shell scripts from earlier.

Now, customisation.  The variable 'args[]' stores the text that will be converted into speech.  You can change this into anything you like.  Just remember that args[0] has to remain as "speech.sh" else it won't work.

You will probably want to change args[1] from "Good morning Tom" to something else that you feel is appropriate.

For those not familiar with programming, in basic terms, the number in the [] following 'args' is the order in which the text will be spoken.

Whatever you do with that array, remember that the final element, in my case, 'args[12]' has to be set to '\0' to terminate the array.

To compile the program, run the command "gcc pijarvis.c -o pijarvis"
Hopefully, it will compile without any errors.  If, for some reason, you get an error, leave a comment and I'll see if I can spot the problem.


Test it to make sure it works with the command "./pijarvis".  If all has gone well, you should hear speech from your speakers.

Finally, to make it run at a certain time, you need to setup some cronjobs.

Run the command "crontab -e" and add the following to the end of the file:

05 6 * * * /home/pi/weather.sh
20 06 * * * /home/pi/runspeech.sh

What this does is sets the two scripts to run at 6:05 and 6:20 respectively.  The first script will download the weather data and the second will run the program.

Obviously, you probably don't want the program running at 6:20am every morning!  The first number is the minutes and the second the hour.  Change them to whatever you want, then press 'ctrl+x' and then 'y' to save and exit.

Just check the cronjobs have been saved by running the command "crontab -l", with will output a copy of the file you've just edited to the terminal.

Chances are there are simpler ways or methods that don;t involve so many shell scripts, but that is how I have it set up at the minute on my Pi and I know it works.

Good luck!

Oh, one more thing. At the minute, it won't run at the weekend.  This is because I wanted it to do something slightly different.  In the next few days, I will add this.

Thursday 30 May 2013

Preliminary setup

In this post, I'll outline the preliminary steps you need to get text-to-speech working.  This can be done entirely from the terminal or through SSH, but if you are more comfortable using the GUI, there are places you can use that instead.

It's probably best to start with a fresh image of Raspbian, but if you want to add this to an existing image, it will almost certainly still work.

First, you need some speakers.  It shouldn't matter if they are going through the audio jack or USB.

Next, follow steps 1-4 from the RPI Text to Speech guide on the Wiki.
You may already have alsa-utils installed, but it's best to run the command anyway to check.

Then, instead of installing mplayer, we want to install mpg123.
Run 'sudo apt-get install mpg123', hit 'y' and wait for it to install.

Following that, create a file called 'speech.sh' with nano (nano speech.sh) and copy the script from this site into the file.

For those not familiar with scripts, copy everything from '#!/bin/bash' to 'done', then (if you're using nano) press 'ctrl+x' then 'y' when prompted to save the file.

Finally, give the script execute permission by running 'sudo chmod +x speech.sh'.

You can test this is working by running the script with './speech.sh' followed by a string of text you want spoken.

For instance, if you want to have it say 'Hello world', you'd run './speech.sh hello world'.  If everything has worked, you should hear 'hello world' from your speakers.

Coming soon, my horribly crude C program :-)

Background/Basics

This blog is as much for me to keep track of what/how I've done things with text to speech on the Raspberry Pi as it is for people to follow my progress.

So far, the project is split into three main files.
#1 - my program, written in C.
#2 - a shell script that sends the text I want turned into speech to Google Translate (This script here, by Dan Fountain)
#3 - a simple script which downloads the five day forecast for Leicester from the Met Office site.

What the main program does:
-Says "Good morning Tom, the time is 6:20am" at 6:20am.
-Reads in the weather data for Leicester from the Met Office API, finds the current day's weather and announces the type of weather (sunny, light showers, overcast, heavy snow, etc.), maximum temperature and the wind direction.

What I'm aiming to have it do soon:
-Announce the wind speed and chance of rain.  I might also throw in the visibility.
-Announce the top three stories from the BBC (this looks like it'll be significantly more complex than the weather data, based on the size of the xml document produced).

Long term goals:
-Possibly add in speech recognition, maybe using Voice Command by Steven Hickson or something much like it (using Google Voice to convert the speech to txt).
-Set up a webcam and Motion to detect if people enter the room and say different things, either based on the time of day or facial recognition.

Please bear in mind that what I have so far is largely tailored for my specific needs, and while it should be customisable to an extent in its current state, a lot of work would need to be done to make it a) more user friendly and b) work for everyone. 
It is my hope that, even if what I have doesn't work for you, I will give you the basics that you need to get your own version working.