Showing posts with label text-to-speech. Show all posts
Showing posts with label text-to-speech. Show all posts

Tuesday, 17 January 2017

Get your Raspberry PI to Read Out your Text Messages using IFTTT

Continuing the theme of talking Raspberry Pi's from my recent posts (Get your RPi to Speak the News and Turn your RPi into a Amazon Echo like device), using the brilliant service IFTTT (If This Then That), I've created a way the my Raspberry Pi reads out a notification whenever I receive a SMS on my (android) mobile phone.

If you don't know what IFTTT is, wikipedia calls it :

IFTTT is a free web-based service that allows users to create chains of simple conditional statements, called "applets", which are triggered based on changes to other web services such as Gmail, Facebook, Instagram, and Pinterest. IFTTT is an abbreviation of "If This Then That".

The below shows the sequence of events of how information regarding a SMS received on your phone ends up on the Raspberry Pi.


In IFTTT, you create an "applet",  which fires a trigger when an SMS arrives on your phone, it then sends this info to the central IFTTT service which then performs an action you select. This action could be to turn on light using something like WeMo or Phillips Hue, or adding a row in a Google Sheet, or even sending a Gmail. But in this case the action is to send a webrequest to my raspberry pi.  The web request is handled by some PHP code using JSON , which reads the info send and then executes

Please note I am using the Android SMS trigger on IFTTTT. There doesn't seem to be an equivalent for IPhones

So what do you need to set this up:

1. Setup your Pi as a Webserver

The below 3 steps are covered on the site: http://www.penguintutor.com/linux/light-webserver
  • Raspberry Pi running a web server such as lighttpd
  • Have a fixed IP or use a dynamic dns service such as noip.com to you have a URL which connects to your Raspberry Pi . You may also need to setup your home router to forward incoming http requests to your Raspberry Pi
  • Install PHP to right server side code to handle IFTTT actions and/or to run other web applications you may want to write. 
2. Setup Script to execute Text to Speech 
3. Setup PHP page to handle request 

  • Create a file readSMS.php in your /var/www  directory (this is the default for lighttpd) with the following content :




if ($_SERVER['REQUEST_METHOD'] == 'POST')
{

   $data = file_get_contents("php://input");

 //Removes all 3 types of line breaks
 $data = str_replace("\r", " ", $data);
 $data = str_replace("\n", " ", $data);  

 $result = json_decode($data, true);

 $myfile = fopen("readSMStest.txt", "w") or die("Unable to open file!");
 fwrite($myfile, $data);


 $strText = "SMS received from " . $result['contact'];
 fwrite($myfile,"\n" .$strText);
 fclose($myfile);

 $strCommand = "sudo /home/pi/Documents/speech.sh" . " " .  $strText ; 

 exec($strCommand, $command_output);

 foreach($command_output as $line) :

  echo $line  ;

 endforeach;

}

?>

  • This code reads the input from an HTTP Post (we will setup IFTTT later to send an http post request) , decodes the JSON sent by IFTTT, and extracts the contact name of who sent the SMS to your phone. It then executes the speech.sh script which you should have set up from the previous step. 
  • You may need to change the $strCommand string to the location of where you saved the speech.sh file
  • The script also for debugging purposed writes a text file readSMStest.txt to the same folder
  • The above can be downloaded from:

4. Create Applet in IFTTT

  • Sign up to IFTTT and download the app to your phone 
  • In the IFTTT site click on My Applets then on the New Applet button
  • You will see the below Applet Maker, click on the "+ this" to setup the trigger (which is any SMS received)


  • Start typing "android" and then click on "Android SMS"
  • Now click on "Any new SMS received"

  • Now click on "+that" to setup the Action
  • Start typing "maker" and select the "Maker" action service. 
  • Click on "Make a web request"
  • Fill in the web request as below:
    • Change the URL to the address of your webserver. 
    • The JSON Body Text is below which is easier to copy and paste. This sends the info about your SMS message to your php page using JSON
    • {"contact":"{{ContactName}}" , "message":"{{Text}}" , "Occured":"{{OccurredAt}}", "FromNumber":"{{FromNumber}}"}
      

  • Hit Save and you're done 
  • You can rename the Applet to whatever you want, and can toggle whether you want logging

5.Test it out

Now all you need to do is test it out. Get somebody to send you a text or send one to yourself and hopefully you should hear your Raspberry PI (remember to turn on the speakers and volume is up) say "SMS received from" and then the name from your address book

6.Customise it

Now you can tailor this to do whatever you want. Maybe read out the full message, or use one for the other triggers. Or maybe get your php page to perform some other action like turning on lights


Hope the above is helpul. Any problems , please leave something in the comments.




Thursday, 15 December 2016

Get your Raspberry Pi to Speak the News


Talking devices are the in thing at the moment with the Amazon Echo and the newer Google Home, so I thought I would try to get my raspberry pi to read out the news.

Here's what you need to do:

1, Install mplayer (audio player) on your raspberry pi
2. Setup a script to invoke Google Translates text to speech engine
3. Create a Python Script to pull the latest headlines from the BBC rss feed and then read them out
4. Add the script as a cron job to run every hour

Below is the detail behind each step

1. Install mplayer on your raspberry pi

(The below instructions are based on http://elinux.org/RPi_Text_to_Speech_(Speech_Synthesis))

a. First you will need to update Raspian (this can take up to an hour if not run in some time)

sudo apt-get update
sudo apt-get upgrade

b. Add sound utilities

 sudo apt-get install alsa-utils

c. Edit the follow file with the line

sudo nano /etc/modules

to have line

 snd_bcm2835 

d. Install mplayer

sudo apt-get install mplayer

2. Setup a script to invoke Google Translates text to speech engine

a. Create a file call speech.sh with the following content:




#!/bin/bash
say() { local IFS=+;/usr/bin/mplayer -volume 100 -ao alsa -really-quiet -noconsolecontrols "http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=$*&tl=En-us"; }
say $*


b. Add execute permissions to the script

chmod u+x speech.sh

c. Test it out









./speech.sh Hello I can talk

Remember you will need speakers plugged into your raspberrypi to hear anything!

3. Create a Python Script to pull the latest headlines from the BBC rss feed and then read them out

a, Install feedparser which is a python libararyto read RSS feeds. Follow the instructions from the below link:

https://pypi.python.org/pypi/feedparser#downloads

b. Create a file getNews.py with the following content


##!/usr/bin/env python

import subprocess
import feedparser
import time 


d = feedparser.parse('http://feeds.bbci.co.uk/news/rss.xml?edition=uk')

introMessage = "BBC News Headlines at " + time.strftime("%H:%M") 

subprocess.call(["/home/pi/Documents/speech.sh", introMessage])

maxItems = 3
itemCounter = 0 

for post in d.entries:
        print post.title
 subprocess.call(["/home/pi/Documents/speech.sh", post.title])
 itemCounter = itemCounter + 1
 if itemCounter == maxItems:
  break

Remember to change the location to where you saved speech.sh
Also change maxItems if you want the script to read out more than 3 headlines. 
You could change the link to another news website of your choice as long as they provide a rss feed.

c. Test the file








python getNews.py

You should hear the top 3 headlines read out

4. Add the script as a cron job to run every hour (or how every frequently you want)

a. Open crontab with:

crontab -e

b. Add the line








@hourly python /home/pi/Documents/getNews.py > /dev/null 2>&1


Check out the following site on how to set up cron jobs if not familiar:
http://www.howtogeek.com/101288/how-to-schedule-tasks-on-linux-an-introduction-to-crontab-files/


And there you go, you now have a news reading speaking rasperberry pi. You could adapt this to read out any other type of site which uses rss feeds.

The following files can be downloaded from the links below:

getNews.py
speech.sh