
Control ESP by Google Assistant
Introduction
Don’t forget to check out the video tutorial :
In this tutorial,we will use Google Assistant to control devices :
- LED – switching on / off, that is, sending Boolean values by Google Assistant
- Servo mechanism – after the a voice command the servo mechanism moves to that position – by Google Assistant
Assumption
We will use IFTTT and RemoteMe. In IFTTT, we can easily add voice commands for Google Assistant, which will call a “trigger” in IFTTT and this will call the http query to change the variable in RemoteMe. The RemoteMe will forward command to the ESP. And the ESP will change LED or servo mechanism state
Let’s begin
RemoteMe
The Google Assistant will control two variables
- Boolean type – LED diode
- Integer type – servo mechanism
After adding the variables, ESP will be “observing” it, when a variable value is changed by the Google Assistant our ESP will be notified
Adding Variables :
Name of Boolean variable is RELAY_01 – with this name it will be easier to add android application later to control it. More about variables here
The same way we are creating integer type variable with the name “servo”
Now we have to send variable change into ESP
ESP
Connections
- LED to D5 through resistor to ground
- Servo mechanism
- Vcc to ESP 5V
- GND to the GND of the ESP
- Control wire to D1 ( check before if your servo mechanism can be connected to 3.3V pin, otherwise use logic level converter)
After we change variable the ESP will set LED or servo mechanism to given state.
We will start by generating the code with the functions needed to collaborate with variables .
To add a device to RemoteMe, go to the “Devices” tab, then “New Device” and “New Network Device” in the window enter:
Then click “Submit” more here
To generate the code, click on the Burger Menu and the “Code Generator Wizard”:
In the first step, we select all variables, this will generate the appropriate code where the function will be created for the variable manipulation and called when variable is changed more about the wizard here
In the second step, enter the name of our WiFi network and the password to it (we can leave it empty and complete it later in the code).
In the last step, we display the generated code and paste it into the Arduino IDE. Below the code completed with the implementation of the servo control mechanism and the LED.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#define WIFI_NAME "nameofwidi" #define WIFI_PASSWORD "wifi password" #define DEVICE_ID 1 #define DEVICE_NAME "ESP" #define TOKEN "token" #include <RemoteMe.h> #include <RemoteMeSocketConnector.h> #include <ESP8266WiFi.h> #include <Servo.h>//ADDED uint8_t LEDpin = D5;//ADDED Servo myservo; //ADDED RemoteMe& remoteMe = RemoteMe::getInstance(TOKEN, DEVICE_ID); //*************** CODE FOR CONFORTABLE VARIABLE SET ********************* inline void setRELAY_01(boolean b) {remoteMe.getVariables()->setBoolean("RELAY_01", b); } inline void setServo(int32_t i) {remoteMe.getVariables()->setInteger("servo", i); } //*************** IMPLEMENT FUNCTIONS BELOW ********************* void onRELAY_01Change(boolean b) { digitalWrite(LEDpin,b?HIGH:LOW);//ADDED } void onServoChange(int32_t i) { myservo.write(i);//ADDED } void setup() { WiFi.begin(WIFI_NAME, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(100); } remoteMe.getVariables()->observeBoolean("RELAY_01" ,onRELAY_01Change); remoteMe.getVariables()->observeInteger("servo" ,onServoChange); remoteMe.setConnector(new RemoteMeSocketConnector()); remoteMe.sendRegisterDeviceMessage(DEVICE_NAME); myservo.attach(D1);//ADDED pinMode(LEDpin, OUTPUT);//ADDED digitalWrite(LEDpin, LOW);//ADDED } void loop() { remoteMe.loop(); } |
new lines are mark by //ADDED
1 |
#include <Servo.h> |
a library for servo control mechanism – it must be added before, as well as the RemoteMe library more here
1 2 3 4 5 6 |
void onRELAY_01Change(boolean b) { digitalWrite(LEDpin,b?HIGH:LOW); } void onServoChange(int32_t i) { myservo.write(i); } |
Function called when the Google Assistant changes our variables.
1 2 3 |
myservo.attach(D1); pinMode(LEDpin, OUTPUT); digitalWrite(LEDpin, LOW); |
setting the servo mechanism and pins for controlling the LED .
After these modifications we upload our sketch
Testing
Our ESP after uploading the sketch and powering on will connect to RemoteMe:
and after going to the variables tab and expanding them:
ESP – in variables means that our device is connected and is ready to receive data .
let’s change the variables and see if the diode and servo mechanism reacts accordingly. From the burger menu select the “Set …” option
And in the window that appears we change the value of the variable and click Submit. Our LEDe and servo mechanism will react.
Generating links to change variables
IFTTT will be like the “Set …” function, change the value of a variable, but IFTTT will do it by calling the http query appropriately. Let’s generate a simple link to the GET method:
From the variable menu burger, click on “Generate Set Variable Link”:
generated link:
1 |
https://app.remoteme.org/api/~155_D49LDj@aBFhK./rest/v1/variable/set/variableValue/RELAY_01/BOOLEAN/false/ |
After copying the link and opening it in the browser (even in incognito mode – where we are not logged in ), our LED will turn off, the URL is using one of our tokens, that’s why it works even if you are not logged in
To generate a link to light up with a diode – change “boolean” to on and click “Generate” we will get a link after calling which our diode will light up.
IFTTT
Let’s create an Applet for voice control
- Adding the Google Assistant with the “turn light on” command
- Adding a connected “Webhook”, which will call the appropriate query on the RemoteMe, just like the links from the example above will turn on the diode.
In IFTTT, we go to My Applets, then New Applet, after clicking on “+ this”
we choose Google Assistant:
And then “Say a simple phrase”, we complete:
(Note: phrases such as “turn light on” can already be assigned to the default command in the Google Assistant – so when we get a different response after calling a voice command than “OK preforming action” we have to choose another voice command)
and click create trigger, then click “+ that” and choose webhooks:
and “Make a web Request” and fill with data generated by RemoteMe
let’s generate :
in this case we chose the “use Post Method” option (the POST method is more elegant to change the variables state than the GET method) and generated a link and POST query content that will enable the LED
after pasting into our webhook it should look like this:
(don’t forget to change Method to POST and Content Type to application/json)
we click “create action” and “finish”. After a while, when our account on Google Assistant is updated, we can say “okay google, turn on light” and our diode will be turned on.
Similarly, we create actions for the phrase “turn off light”, remembering to generate a link in remoteMe with the “boolean” option set to false. Now we can turn the diode on and off with the help of the Google Assistant.
IFTTT – servo
Like a diode, we can generate a command to control the servo mechanism, but in this case we will say in the command a number that will be properly recognized.
We create a new applet similarly to the previous one, but now in the Google assistant we choose “Say Phrase with a number” and we complete:
in the “Set servo to #” command instead of # we will tell the number that will be recognized. Let’s insert the URL, modifying the “body” generated by RemoteMe with the appropriate “tag” after pasting the webhook should look like this:
IFTTT will replace in the called {{NumberField}} json by the number we say to the Google Assistant.
Our Applets look like this:
Then from the Google Assistant, we can change our diode and the position of the servo mechanism. The movie from 10:40 shows the action:
Bonus – control via a website
We can also easily create a website – hosted in the RemoteMe cloud to control our ESP,
We choose “new Device”, “New WebPage” then fill
After adding, extend the belt website, click on index.html and choose “Edit With wizard”
then click on the Insert Component and complete:
the name property insert by clicking the “magnifying glass” we will minimize the mistake when entering the names of the variables. After clicking “Insert” add the component to control the servo mechanism:
after adding, close the wizard, click on index.html and choose “Open in new Tab”
by clicking the switch we turn off our led, and by moving the slider we change the positions of servo mechanism.
We can open the website on a mobile phone, the easiest way to scan the QR code generated by clicking on index.html -> “Get anonymous link” and clicking the QR code icon, after display the QR code we scan it with a smartphone and we have access to website control
Bonus – controlled by mobile applications
There is an application – provided by Nguyễn Duy Hiếu available here
Which, when connected to our account, allows us to control our diode – from the Relays tab (of course, because we called our variable RELAY_01 diode at the beginning)
Summary
This simple tutorial shows the basics how by invoking http queries change variable states i.e. by IFTTT.
I encourage you to create your own examples 🙂