Introduction: Voice Controlled Arduino
Once you have your project up and running with a Web Interface, its only a little more work to link it into a Voice Assistant like Alexa or Google Home.
We will extend the example given in the Arduino Power Plug Web Control Instructable, to give us access to our web interface from the public internet, as well as be able to link into a Voice Assistant (or many other abilities once we are configured)
Admin Access to your Router
Google Assistant Device and Account Details
ESP32 Board or another WiFi Capable Arduino device
Step 1: Making Your Arduino Public
First we will make our Arduino accessible from anywhere on the internet, using Port Forwarding on your local router.
To do this you will need to set your Arduino to a Static IP Address (or add a Reservation for its MAC on your Router).
Then we will need to decide on a port (e.g. 9090) to serve our Arduino Webserver on (80 is in use by your routers' web interface externally normally, you can use this so all requests hit your Arduino without any other changes).
Once that's decided we can add a Service which defines the port ranges, and then add it as a rule to allocate it to the IP address from the previous step.
Now that's configured you can access it via the External IP of your router (available on your Router or at https://www.whatismyip.com/) with a Port e.g. http://188.8.131.52:9090, however as there is no device running you will just get a 404 at the moment.
To make things easier to access, set up a Dynamic DNS name (e.g. https://www.noip.com/), which in many cases can be entered on the Router directly, then you can access it via a more memorable URL such as http://visualmicrohomeproject.ddns.net:9090
Step 2: Register With If This Then That (IFTTT)
IFThis Then That provides a linking service between a wide variety of tools such as Google Assistant / Alexa, as well as being able to link plain Web hooks as well.
First register with IFTTT with the same Google Account as on your Assistant device.
(If you don't use the same account it will not work)
Step 3: Create an Applet
Now Click on Create
Then Search for Google > Select Google Assistant
At this point you can configure different phrase types, in this example we will use a word "On" or "Off" as our variable, in the phrase "Turn the light On/Off" which we enter as "Turn the light $"
Now we need to link this Trigger by clicking on That
Search for Webhooks > Select Webhooks
Here we configure our URL and Port as setup in the previous step, and we can add our "ingredient" variable from above.
For simplicity we will add it to the URL here, but if you were sending more data it would generally go in the body, often in a JSON Format, where you can use the Arduino JSON library to parse it on your Arduino.
Once the applet is configured, we can amend the code on our board as needed.
Step 4: Amend and Upload Code
We only need to make some slight amendments to this code to allow it to work here.
This can be downloaded as a ZIP from this location with all changes completed.
1) Add Static IP Configuration before Wifi.begin
// Static IP Configuration
IPAddress local_IP(192, 168, 0, 55);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(192, 168, 0, 1); IPAddress secondaryDNS(8, 8, 4, 4); // fake
WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS);
1) Amend Web Server Port & MDNS service binding to the same port as in step 2
MDNS.addService("http", "tcp", 9090);
2) Amend URL Binding for our On and Off calls from step 3
in "handleFileRead(String path, WiFiClient &client)" there is a new case added for when path.startsWith("/on") and path.startsWith("/off")
3) Build and Upload the code to your board
4) Publish the SPIFFS data files to the board if you want to use the Web UI as well
Step 5: Try It Out
Now you should be able to say to your assistant "Hey Google, turn the light On", and it will respond with the phrase you said.
The light should also be on now as well!
You will also see in your Serial Monitor that there is a request sent in with the On Keyword appended to the URL, and your Arduino will have sent the RF message to turn the light on from the code changes.
From here the sky is the limit, as you can now have your own custom web interface on the device itself, as well as send data to and from it using IFTTT, and control your projects from your Voice Enabled Assistant whether it be a phone or physical device.