Google Home SMS Reader

Tech

My phone’s ringer is almost never on. Usually, it’s completely muted, not even on vibrate. I can’t check my phone at work, and I don’t like the swarm of constant notifications.

However, this causes a problem when I get back home and forget to turn the buzzer on. I’ve missed a lot texts because I simply didn’t hear them.

I received a Google Home for Christmas one year and I was hoping that it would help by reading my texts out to me. However, Google has yet to implement any function like this. So I’d have to build my own.


TABLE OF CONTENTS


SUPPLIES

  • An Android Smartphone – Unfortunately, Apple hasn’t opened up iOS messages for other services.

  • IfTTT – If This Then That is a connecting service designed to hook together internet services that normally don’t work together. This is going to listen for messages on the phone.

  • A server or always on computer – I use a Raspberry Pi Model B as a server here, but anything that is running 24/7 will do

  • Python – Python is the programming language that runs our server software. Comes Pre-Installed on the Raspberry Pi. We need Python Version 3.5+

  • HomeAssistant – This is the real magic of the project. HA will listen for a web packet from IfTTT, and make a Text-To-Speech request to my Google Home. HA is also good for automating other Internet of Things devices, and acting as a dashboard for the Google Home. I use the instructions for installing on top of an existing Python Installation. Other methods may work, but I find this the easiest.

  • A Google Home – This project will work with any hardware that is compatible with HomeAssistant. I know that Google Assistant works, but it might also work for Alexa and other similar devices.


UNDERSTANDING THE PROJECT

This project is a bit complex, and has a lot of moving parts. We need to break down the flow of what’s happening, and understand each piece.
First, I’m going to tackle the flow of information that happens.

  1. My Android phone receives a text message.

  2. IfTTT, which is listening for text messages, picks up the message and copies the content and sender name.

  3. IfTTT formats the message into a web request (json), including the sender’s name, message, and some other text that makes the end result sound more natural.

  4. IfTTT sends that web request to the Raspberry Pi.

  5. HomeAssistant, running on the Raspberry Pi, receives and decodes the web request.

  6. HomeAssistant takes the content of the web request, and formats a Text-To-Speech (TTS) request.

  7. HomeAssistant sends the TTS request to my Google Home.

  8. My Google Home sends the TTS request to the Google Assistant servers to process the text into audio.

  9. The Google Assistant servers send back audio to my Google Home, which the Google Home then plays.

Like I said, a lot of moving parts.
The way I like to think of this is like a train or bus system. There is no direct line connecting my text messages to the Google Home, so I have to make several connections and transfers to get to my destination. The end result is my Google Home playing a clean audio stream of SMS information that originated on my phone.

In some tech circles, this is called a Bodge. This isn’t a “clean” solution, but instead takes advantage of multiple interlinking parts that eventually get the job done. It’s bloated and messy, but has the end result we want. Tom Scott has an excellent YouTube Video talking about The Art of the Bodge.


GETTING STARTED

Time to get building.
The easiest thing to get started with is IfTTT. You can find the app here.


screenshot-20180325-092202-1.png

Once installed, make an account or sign in, and click on the rightmost icon in the bottom toolbar. This opens a section called “My Applets.


screenshot-20180325-111419-1.png

Once here, hit the “Plus” icon at the top, and the New Applet screen opens. Click the blue “this” to select our applet’s trigger.


screenshot-20180325-111504-1.png

We want to find and select “Android SMS”


screenshot-20180325-111622-1.png

Select “Any new SMS received”


screenshot-20180325-113223-1.png

Now we have a trigger for our IfTTT applet. Now we select the action by pressing the blue “that” button.


screenshot-20180325-113446-1.png

We want to select the “Webhooks” action


screenshot-20180325-113656-1.png

And “Make a web request”


screenshot-20180325-113827-1.png

This is where the complexity starts to ramp up.


screenshot-20180325-114128-1.png

In the URL feild, we are going to put the following:

http://xxx.xxx.xxx.xxx:8123/api/services/tts/google_say?api_apssword=xxxx

The areas with “xxx” are going to be filled in later. If you know your public IP address, you can fill that into xxx.xxx.xxx.xxx . You can find your public IP address at www.whatsmyip.org. The Method will be set to “Post” and the Content Type to “application/json”. In the body area, we are going to put:

{ "message":"Text From []. Message. []"}

When typing in the boxes, an option should appear called “Add Ingredient”. Where I have the sets of “[ ]” brackets, add the “ContactName” ingredient to the first one, and the “Text” ingredient to the second. The final result should look something like this:


screenshot-20180325-115139-1.png

Let’s take a minute to understand what’s happening here.

  • IfTTT is taking the Contact Name and the Text parts of the message, and sending those in the body of the web request. This body section is where we format what the Google Home will actually end up saying; this body portion is carried through the rest of the project.

  • The URL section is the destination of the Web Request. The “xxx.xxx.xxx.xxx” will be filled in by the IP address of the receiving computer. The “api_password=xxxx” will prevent strangers from sending random requests to to HomeAssistant. More information about Internet Protocol (IP).

  • The Method is POST, which means we are sending information. Other options provide different interactions. More Information about Request Methods.

  • The Content Type is set to “application/json”. This will tell HomeAssistant what kind of message to expect and how it’s encoded. More Information about JSON.

The important thing to understand is, we are sending the Contact Name and Text to a location.

Hit the Check Mark at the top of the screen to complete the action.

The Applet Preview screen will come up, where you change the applet’s name, and decide if you want run notifications. Once you’re happy, hit “Finish”.

A Recommended Applet screen may come up, you can turn these on or use the arrow to leave. The IfTTT Applet is complete for now, and is ready to send messages from your phone.


SERVERS, PYTHON AND MORE

Okay, now’s a good time to take a breather. IfTTT is a powerful tool that can connect most big name internet services together in complex and useful ways. What we just did is one of the more extreme interactions we can make, most just involve hitting some buttons to get the desired result.

While IfTTT runs their own servers to provide the service, HomeAssistant is an open source project, without a central company. Therefore, we have to run the server ourselves in order to make the connection. We might need to define a “server” here. A Server is a computer that runs a piece of software 24/7. It’s job is to make sure that the software is available any time that someone wants to access it. Websites are run on servers. which is why they are always accessible whenever you navigate to them (this wasn’t always the case, until major services like Google, Facebook and other big names made this the standard). The server we are setting up is also a website, but has some more features under the hood.


I used a Raspberry Pi Model B to host my server, but it can be done on any machine that can run Python 3.5+. Most tutorials for HA will assume that you are running a Linux computer, as opposed to a Windows or Mac. Linux is an open system, that allows complete control over the system, which is why it is popular for servers, advanced computers, and computer nerds.

The Raspberry Pi runs a custom version of Linux. I’ll try to keep this tutorial general to both Linux and Windows operating systems so that most people can follow along. Mac users, you don’t want to host a server on your computer. Apple is uncooperative when it comes to their operating systems.

If you are using a Pi, here’s a guide to getting one set up.


Most Linux computers come with Python installed. If yours doesn’t you probably know what you’re doing. In any case, running the following is always a safe bet:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3

Linux is very complex, so if you’re new to it, I’d take some time to familiarize yourself with how the system works, how to navigate folders, how to run programs, and generally use the system. I’ll probably make a Linux Quick-Start guide at some point.

On Windows, we’ll be installing Python with an .exe file, like normal programs. Make sure to click the option that says “Add Python to PATH”, it will make our lives easier.


SETTING UP HOMEASSISTANT

Finally to the good stuff.

We’ll be following this tutorial from the HomeAssistant Website to get running on Linux. Sorry Windows users.

There are a lot of options for installing HomeAssistant on Linux. The tutorial link I provided is the most straightforward, but still has some complexities that I’ve ignored. We’re going to get started by installing some necessary prerequisites. This tutorial is built for Ubuntu based systems, check your documentation for methods specific instructions for your system.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3 -y
sudo apt-get install screen -y

The tutorial wants us to use HomeAssistant under a Virtual Environment or it’s own user. Feel free to, but i prefer to use screen to start it and keep it out of my way. I’ll be skipping to step 4 and adapting for that.

mkdir homeassistant
cd homeassistant
python3 -m pip install wheel
python3 -m pip install homeassistant

The tutorial also provides auto-start instructions, or you can start it manually.

hass

Once launched, HomeAssistant will take some time to do it’s own setup. It’s supposed to have a UI, but I’ve found it to be unreliable and prefer to use the web access instead. Once HomeAssistant is done with setup, we need to configure it. You should be able to access the web panel now.

Find your local IP address (Not Public) by typing ifconfig in a new terminal session. 

Linux will have IP Address under under the “wlan0” header:

inet 10.0.0.XXX

If yours isnt 10.0.0.XXX, it might be 192.168.X.XXX or some other set of front numbers, it depends on your router brand. Anything with “255” is incorrect, as well as anything with “1” at the end.

Once you’ve found your local IP address, you can connect to the web interface.

Using a computer on the same wifi or internet connection as the server, open up a web browser, and go to:

http://xxx.xxx.xxx.xxx:8123/

Where xxx.xxx.xxx.xxx is your server’s local IP Address. If your browser is running on the same computer as your server, it will be

http://localhost:8123/
or
http://127.0.0.1:8123/

HOMEASSISTANT CONFIGURATION

The last big thing to set up is the HomeAssistant config. This controls what HomeAssistant has access to, and other options.

The web interface may have already picked up your google home. You can experiment with the interface, using the media controls and sending TTS requests to the Google Home.

If HA hasnt found your Google Home yet, don’t worry. We’ll make sure that gets set up in the config.


The HomeAssistant configuration file controls which items appear on the web interface. We want to ensure that Google Services and TTS both work. We also need to modify some settings, so we’ll hit the important lines one at a time.

This is a copy of my configuration.yaml file, with personal information removed.

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home   
  # Location required to calculate the time the sun rises and sets 
  latitude: XX  
  longitude: XX 
  # Impacts weather/sunrise data (altitude above sea level in meters) 
  elevation: 0  
  # metric for Metric, imperial for Imperial  
  unit_system: imperial 
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones 
  time_zone: America/New_York 
  # Customization file 
  customize: !include customize.yaml
  # Show links to resources in log and frontend
  introduction:
  # Enables the frontend
  frontend:
  # Enables configuration UI
  config:
  http:
    # Secrets are defined in the file secrets.yaml
    api_password: XXXX
    # Uncomment this if you are using SSL/TLS, running in Docker container, etc.
    # base_url: example.duckdns.org:8123
  # Checks for available updates Note: This component will send some information about your system to the developers to assist with development of Home Assistant. For more information, please see: https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
  updater:
  # Optional, allows Home Assistant developers to focus on popular components.
  include_used_components: true
  # Discover some devices automatically
  discovery:
  # Allows you to issue voice commands from the frontend in enabled browsers
  conversation:
  # Enables support for tracking state changes over time
  history:
  # View all events in a logbook
  logbook:
  # Enables a map showing the location of tracked devices
  map:
  # Track the sun
  sun:
  # Weather prediction
  sensor: platform: yr
  platform: uptime
  # Text to speech
  tts:
  platform: google
  # Cloud
  cloud:
  group: !include groups.yaml
  automation: !include automations.yaml
  script: !include scripts.yaml

There are a lot of options available here that you may want to personalize. However, these are most important to us.

api_password: XXXX

This controls access to HomeAssistant Server. Set this to something you can remember, and change it in the IfTTT applet as well.

discovery:

This line allows HomeAssistant to search for supported devices on the network.

tts:- platform: google

This line tells HomeAssistant exactly which TTS platform we are using.

If any of those lines are missing from the config, it’s important that you add them.

The configuration.yaml file should be in the homeassistant folder we created earlier. Modify it with ‘nano’ or your favorite editor.

nano configuration.yaml

Hopefully your configuration file is correct from the start, and you don’t have to modify it too much.


PORT FORWARDING

Okay, we’re almost done I swear. Make sure you’ve changed the api_password in both the config file and in the IfTTT applet.

The last step is to make the site open to the internet, so that IfTTT can send our message.

Port Forwarding opens a small hole in your internet connection, to allow specific traffic into your network. Each internet router has a different method, so you’ll need to find the instructions for yours. We want to open port 8123 for incoming and outgoing traffic on both UDP and TCP connections.

This website explains Port Forwarding really well, and has a lot of useful resources.


CONCLUSION

That’s It! We’re finally done.

Assuming everything got set up correctly, whenever you receive a text from someone, your Google Home will read it to you. Get someone to text you, or make an IfTTT Applet to text yourself.

That was a lot of work for such a simple request. All we wanted was for one device to send information to another device that it’s normally already connected to.

If there’s one thing this project displays is how complex the internet of things really is. Most systems these days are built on top of older parts, which are built on even older parts.

Hopefully you were able to pick something up while doing this project. Here’s some possible take home points:

  • Bodging is a perfectly fine way of making a project work

  • You can use IfTTT to connect internet services together

  • Servers are computers that remain on to provide a service

  • Python is a programming language that many services use

  • Python has sub-modules, that are installed using Pip

  • HomeAssistant connects Internet of Things devices on your home network

  • IP Addresses are like street addresses, and are how you communicate between computers.

  • There is a difference between your local and public IP Address

  • The Appdata folder in Windows contains saved program information

  • Port forwarding allows outside computers to access parts of your home network

Once this project is set up, as long as HomeAssistant stays running, it should work forever. IfTTT wont run while the phone is power saver mode, but otherwise runs in the background. It takes only a few seconds between receiving a text, and my google home reading it to me. This project has potential with other Google Home interactions, and uses in situations where someone is not able to read their texts, or wants other information from their phone.

This was a lot of work to get figured out, and to set up correctly. You will run into issues, and it will not work properly the first time, but that’s okay, it’s part of the building process. There’s a lot of room to branch off from this project; take a look at some of the links i provided.

Overall, I had a lot of fun doing this project. I love the satisfaction of a finishing a project and having it work just the way I want. I’m still running the programs almost 3 months later, and I appreciate having my texts read to me.