Use Google Assistant to control an ESP8266 using Blynk and IFTTT
Regular readers of this blog know we're enthusiastic users of the ESP8266. I've personally shown how to control a camera pan and tilt mechanism, make a temperature data-logger that logs straight to a Google Sheet, and even how to create a mailbox notifier that sends an email when an envelope drops in your postbox.
Click here if you want to skip my scintillating introduction and get down to the nitty-gritty.
Hard to keep up
When this blog began about four years ago, the ESP didn't exist. We were pretty quick off the mark when it emerged - and among the first to explain how to flash it. Even today, Googling "easiest way to program ESP8266" puts us right up there at the top.
At the time, Siri and Google Now were fairly new - used mostly for simple questions. The Star Ship Enterprise's Computer and 2001's, HAL were still unattainable. Remember, this was just four years ago.
Fast forward a couple of years and Amazon's Alexa showed us what it's like to have a box in the kitchen listening for commands. A year later and Alexa was already flooded with 3rd party plugins that pushed its uses and popularity into the stratosphere.
... so far into the stratosphere, that Google couldn't ignore it; last year saw the launch of Google Home, that little bulbous device that "fits into your home's decor."
Until very recently Google Home was available only in the US. Now a few other English-speaking countries have joined its exclusive club. Then Apple finally woke up too, with the imminent release of HomePod.
Meanwhile, Google Now has become Google Assistant and AI is entering our living rooms.
(Editor's note. On re-reading this post I've realised that "Google Home" is a stupid name. From here on I've changed it to GHome to make it more palatable.)
Commercial IoT Devices
As GHome and Alexa gain popularity, companies are plying for position, creating "connected devices" for them to control. So far, they're mostly lights, with a few thermostats and door locks. The industry's still in its infancy and the "iPhone" of this technology is yet to be discovered.
Only English-speakers
I alluded above to the fact that these devices have only been formally released in English-speaking countries - leaving the rest of the world wanting. And it's not just the GHome and Alexa that are difficult to acquire outside their chosen countries, but the matching peripherals too.
So what do the rest of us do?
Enthusiasts that don't live in English-speaking countries go to great lengths to import Alexas or GHomes - usually through friends or during personal excursions abroad. But when we get home we find their functionality very limited. We don't have access to many of the companies that stream music, movies and other content into your living rooms.
That's when the geek sets in
But if we're being honest, that's not why we buy them. We foreign early-adopters know these devices' limitations before we buy them. Our aim isn't to order batteries from Amazon or view Google movies, but to fiddle with our gadgets and get them to do things despite the limitations in our countries.
That's where the ESP8266 comes in. With no access to the Alexa or GHome devices sold in the US or UK, we need to make our own. (And anyway, who wants to pay extortionate prices for simple things we can all construct with a $2 ESP8266, a transistor and an old cell phone charger?)
The Nitty Gritty
I acquired my GHome a month ago, brought by a friend who'd just returned from the US. It sat in the living room telling me the weather, traffic, my calendar and other standard IA stuff. I hadn't yet worked out how to get it to talk to the plethora of ESPs I have littered around the house.
But last night I nailed it.
And here's how...
What's needed?
There are probably more efficient ways of doing this, but for reasons I describe below, I decided to use:
I'll skip over how to open Blynk or IFTTT accounts - that's too simple for this blog's readership. Even flashing the ESP with the Blynk authorization code and sketch you know how to do. (If not, read this).
Why Blynk and IFTTT?
I have 10 ESP's around the house, about half of them built into circuits or boxes that require dismantling should a re-flash be needed. (I can't use Over The Air because they were originally flashed without it.)
By using IFTTT, I don't need to touch the ESPs. I can leave them where they are, and use only IFTTT to interface between the ESP, Blynk and Google Assistant. That's a huge advantage and makes things very simple.
How to do it
Here I describe the web interface of IFTTT but it's just as easily to do on your phone.
In your IFTTT Apletts, create a New Aplett.
When presented with the IF trigger, search for Google Assistant.
Choose your phrase. (In my case I chose the Simple Phrase).
The next stage is to create the Then part. Here, search for Webhooks.
Select: Make a web request
You're now presented with a form requiring a URL and other information. Complete it as follows:
URL: http://188.166.206.43/blynk_auth_key/update/digital_pin_used_in_blynk
(Some people have trouble with this because Blynk uses different servers for different countries. I suggest you ping blynk-cloud.com
to find your server IP address and replace mine above if it's different.)
It's also important to specify "D" for "digital" prior to the pin number. So, digital pin 1 will be D1, pin 2 will be D2, etc.
Under Method, select PUT.
Under Type, select applications/json
Under Body, select 0 or 1, depending on whether you want to switch on or switch off. Make sure you use square brackets and quotations: ["0"]
.
Save your new Aplett and that's it. Say your phrase to Google Assistant, and it should command your ESP accordingly.
Naturally, you'll need to create another Aplett to command the ESP to perform the opposite logic.
Too slow
Sometimes the process can be pretty slow. I don't yet know why, but considering my command needs to be interpreted by Google, then IFTTT, then Blynk, and then sent back to my ESP, it's not really surprising. (I wonder how many continents and countries are involved?) Truth is, I'm surprised it takes only 2-3 seconds, but once or twice I've waited more than half a minute.
Improvement
As always, I'm open to suggestions for improvement. If someone has documented how to do this elsewhere, please feel free to comment. Just keep in mind that I wanted to interact with existing equipment without re-flashing and to maintain access to my equipment through the Internet - so that I can control it from outside my local network.