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.




14 comments:

  1. Hi S!

    Thanks to your link, I discovered some great tutorials on DIYHacking.com

    I've been messing around trying to make the cheapest possible home automation setup, and with help from my friend Abhi, here's what we came up with, including all the equipment, connections and code:
    https://www.homesecuritylist.com/diy-home-automation-system-under-40-dollars/
    I'd be so honored if you share my tutorial with your audience.

    Here's everything we used:

    1x NodeMCU board (Price: $8)
    1x Relay (Price: $6)
    1x TMP36 Temperature sensor (Price: $ 2)
    1x HC-SR501 PIR Sensor (Price: $2)
    9-12V 1.0A DC Adapter (Price: $6)
    3.3V/5V power supply module (Price: $6)
    1x Breadboard (Price: $5)
    Jumper/electric wires (Price: $1)
    Bidirectional logic-level shifter (Price: $1)
    1x bulb/appliance that you want to control (Price: $2)

    We also did one with Raspberry Pi and Alexa which is really fun, if you're interested in sharing that one too.

    Wishing you the best,
    Ian

    Ian Mason, from under a pile of smart home gadgets.
    ianm@homesecuritylist.com | HomeSecurityList.com

    ReplyDelete
  2. One can create your own password for login. All information is for your eyes only and remains entirely confidential, have a peek at this link

    ReplyDelete
  3. Why am I getting this error? Please help me! (Raspberry Pi 3 Model B) (Raspbian Jessie Lite 2017-07-05)c(Linux/GNU 4.9.37-v7+) (Python 2.7.9) (python-crontab 2.2.3)

    Traceback (most recent call last):
    File "updateAzaanTimers.py", line 74, in
    system_cron.write()
    File "build/bdist.linux-armv7l/egg/crontab.py", line 340, in write
    IOError: Please specify user or filename to write.

    ReplyDelete
    Replies
    1. Looks like you're referring to the post regarding the Azaan prayer clock ( http://randomconsultant.blogspot.co.uk/2013/07/turn-your-raspberry-pi-into-azaanprayer.html) rather than this post.

      But your error points to crontab not being installed/setup properly. Could you maybe write a short script to check that crontab can add a single command?

      Also worth looking at some help documentation or support on crontab.

      Good luck.

      Delete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    snapho

    ReplyDelete
  9. Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post. google transcription service

    ReplyDelete
  10. Wonderful article. I’m satisfied that you simply shared this helpful information with us. Thank you so much.
    translation offices in uae

    ReplyDelete
  11. Good day! Would you mind if I share your blog with my twitter group? There's a lot of folks that I think would really enjoy your content. Please let me know. Thanks

    ReplyDelete
  12. That is really interesting, You're a very skilled blogger. I've joined your feed and sit up for in quest of extra of your fantastic post. Additionally, I've shared your web site in my social networks

    ReplyDelete