Every evening, I like to know what the weather is going to be like for the next day. Should I pack a salad or have a hot meal? Do I need my coat or not? Sometimes I forget to check my phone/tablet/laptop/TV etc to find out what its going to be like the next day, so I made these wizzy little scripts to get the Raspberry Pi to tell me my local weather forecast at 10pm every night.

The system uses the Google Text to Speech service and an RSS feed from the BBC Weather site and gives a three day forecast for your local weather. Even though its a UK site, the BBC Weather site does have RSS feeds for all over the world.

The steps we need to take are as follows:

  1. Setup mplayer
  2. Write a bash script to access the Google Text to Speech service.
  3. Write a PHP script to pull down the RSS feed and call the Text to Speech service.
  4. Setup a crontab so that the scripts run at the same time every night/day.

The tutorial assumes that your Raspberry Pi is connected to the Internet to access both web services.

Setup mplayer

mplayer is a command line music player for the Raspberry Pi which may be already setup correctly on your box. You can test this by putting a test.wav file on your Raspberry Pi and running:

mplayer test.wav

If it works, then you're ready to go onto the next part (Setting up your bash script), although you might want to add the line to your conf file mentioned below as it will get rid of any error messages that mplayer shows.

If it isn't setup, then the following are the steps that you'll need to take.

Firstly, upgrade your distribution to the newest version:

sudo apt-get update
sudo apt-get upgrade

If you do not already have sound on your Raspberry Pi, then you will need the alsa sound utilities:

sudo apt-get install alsa-utils

and edit the file /etc/modules using:

sudo nano /etc/modules

to have line:


If this line is already there then don't change the file.

Next install mplayer by typing:

sudo apt-get install mplayer

To sort out the mplayer error message, edit file /etc/mplayer/mplayer.conf using:

sudo nano /etc/mplayer/mplayer.conf

to add line


This last bit is worth doing even if mplayer was already working, as mentioned earlier.

Now mplayer is setup, you're ready to write the script that calls the Google Text to Speech service

Write a bash script to access the Google Text to Speech service

Create a text file, called speech.sh as follows:

sudo nano speech.sh

Now copy/paste the following into the text file:

local IFS=+;/usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols "http://translate.google.com/translate_tts?tl=en&q=$*";  
say $*

Press control X to Save and then Y to confirm.

Now change the permissions on the file as follows:

chmod u+x speech.sh

You should now test your file by typing:

./speech.sh Woohoo! I'm ready to move onto the next part of the tutorial.

You are now in fact ready to move onto the next part :)

Write a PHP script to pull down the RSS feed and call the Text to Speech service.

First, we'll need to make sure that PHP is installed. Simply type the following at the terminal to check:

php -i

If you get what looks like a configuration file listing on the screen, followed by the PHP warranty then you can skip the next bit.

If PHP isn't installed, then install it by typing:

sudo apt-get install php5-cli

You are now ready to edit your PHP file, as follows:

sudo nano getWeather.php

And copy/paste the following into the file:

// first say welcome etc.
$title = "Hi, its me, Wanda, with the weather for the next three days.";
$speech="/home/pi/Projects/Weather/speech.sh"." ".$title;
// load RSS file from BBC weather website
$rss = simplexml_load_file('http://open.live.bbc.co.uk/weather/feeds/en/hu17/3dayforecast.rss');
// loop through the three day forecast, saying the weather for each day
foreach($rss->channel->item as $item) {
$title = (string)$item->title[0];
$title = str_replace('°C ', ' degrees.', $title);
$title= preg_replace("/\([^)]+\)/","",$title);
$speech="/home/pi/Projects/Weather/speech.sh"." ".$title;
// now say goodbye etc.
$title = "Thats the end of the 3 day forecast. Enjoy the weather, people!";
$speech="/home/pi/Projects/Weather/speech.sh"." ".$title;

NB. You'll have to replace the RSS feed above with the weather forecast that you require. You can simply search for the weather for your town/city on the BBC weather site and then click on the RSS feed to get the link for it.

Again, press control X to Save and then Y to confirm.

As before, change the permissions on the file as follows:

chmod u+x getWeather.php

You can now run your weather report from the command line. Make sure you have speakers (or headphones) plugged into the Raspberry Pi headphone socket.

sudo php getWeather.php

You should have a talking weather girl called Wanda, giving you the weather for your part of the world. How cool is that?

Getting the weather read to you every night

Our final step is to automate the weather report so that it is read every night. I run mine at 21:55 and I've set this up by using the crontab service, which is used to automate things in Unix/Linux/Raspberry Pi land. You edit the crontab as follows:

sudo crontab -e

And add the following line at the end of the file:

55 21 * * * sudo /home/pi/Projects/Weather/php getWeather.php

Now you're all set up and ready to run every night. You've got the basics here to pull in any data from the Internet and get good old Wanda to say it for you. Let me know if you come up with any cool ideas!

Date Category Technology  Tags raspberry pi
Gary Hall
Article by Gary Hall
A teacher based in Beverley, England. Enjoys walking, travelling, reading and writing interesting content to help others. Feel free to comment below.


comments powered by Disqus