Introduction: Control ESP8266 From Google Home Using GBridge.io
There are different ways to control ESP8266 from Google Home, but most of the solutions you can find on Internet use IFTT, which is not really user-friendly to setup.
gBridge.io allows to make the process easier and act seamlessly.
In this how-to guide, I’m gonna show you how I setup my ESP01 module to answer to commands such as “Turn the lamp on” and “Is the lamp turned on?”. The project only turn on and off the built-in LED, but it easy to go further after that.
Materials needed:
- 1 * ESP8266 module (https://www.sparkfun.com/products/13678)
- 2 * push-buttons (https://www.sparkfun.com/products/97)
- 1 * 10k resistor
- 1 * FTDI cable 3.3V (https://www.sparkfun.com/products/14909)
Step 1: FTDI Cable to ESP8266
To communicate between ESP8266 and your PC, wou will have to make a FTDI to ESP8266 adapter.
- You will have to build the circuit shown in the linked image if you have a 5V FTDI cable:
- If you have a 3.3V FTDI cable, you can avoid the 78xxl chip, and plug 3.3V directly to ESP8266.
- The left button is the “programing” button and the right one is the “reset” button
- When you want to put it in “programing” mode, you have to keep the two buttons pressed and first release the reset button, and after that, the second one.
- The program button will be used in this project to turn on and off the built-in LED manually.
Step 2: Programing ESP8266 With Arduino IDE
The second step is to be able to program ESP01 module with Arduino IDE. This will make it easy after that to use the MQTT Library from Adafruit. I was inspired by this guide for these steps: http://www.whatimade.today/esp8266-easiest-way-to-program-so-far/
- Install the latest Arduino IDE version. In my case it was v1.8.8.
- Go to File --> Preferences and add the link http://arduino.esp8266.com/stable/package_esp8266com_index.json to the Additional Boards Manager URLS.
- Go to Tools --> Board --> Boards manager
- You should now have the esp8266 as an option there since you've added it to the Additional Boards.
- Select it and press Install.
- You should now have ESP8266 module listed as “Generic ESP8266” module.
- In my case, I had to choose some parameters as shown in the linked image.
- Choose the Port where your FTDI cable is plugged.
- You can test the “Blink example” (File --> Examples --> ESP8266 --> Blink).
- Put your ESP8266 in “programing” mode by keeping the two buttons pressed and first release the reset button, and after that, the second one.
Step 3: Setting Up GBridge
- Go to https://about.gbridge.io/
- Register an account
- Login to your account
- Create a new device
- Press Add.
In your device list, you should have your new device listed.
You will need the two feeds address for later.
To connect Google Assistant, you can follow the guide available in gBridge documentation: https://doc.gbridge.io/firstSteps/gettingStarted.html
Step 4: Getting Adafruit MQTT Library to Work With GBridge
The Adafruit MQTT library will be used to communication between the ESP866 and gBridge.io
- In Arduino IDE, Go to Tools -> Library Manager
- Install Adafruit MQTT Library
- Enter informations in the first part of the code and upload it. You shoud be up and running.
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "Your SSID name"
#define WLAN_PASS "Your SSID Password"
/************************* Gbridge Setup *********************************/
#define AIO_SERVER "mqtt.gbridge.kappelt.net"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "your gBridge username"
#define AIO_KEY "your gBridge password"
/****************************** Feeds ***************************************/ Adafruit_MQTT_Publish onoffset = Adafruit_MQTT_Publish(&mqtt, "gBridge/u341/d984/onoff/set"); //Replace by your feedname Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, "gBridge/u341/d984/onoff"); //Replace by your feedname
7 Comments
3 years ago
it was working first 5 min after boot and not responding after that. I appreciate if someone explain why.
But now unfortunately GBridge shutdowned
Question 3 years ago on Step 4
how to make 4 wifi switches using node MCU, 4 channel relay module and GBRIDGE.io...?
I have all the components... I just want the ide code....with specified pin details which are connecting the relay and act as a switch....plz help me...
3 years ago
3 years ago
for secure connection to new gbridge and ISR problem code should be like following
/***************************************************
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "Your SSID name"
#define WLAN_PASS "Your SSID Password"
/************************* Gbridge Setup *********************************/
#define AIO_SERVER "mqtt.gbridge.io"
#define AIO_SERVERPORT 8883 // use 8883 for SSL
#define AIO_USERNAME "your gBridge username"
#define AIO_KEY "your gBridge password"
char outputState = 0;
char lastState = 0;
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
static const char *fingerprint PROGMEM = "9F B6 69 93 2B C3 E6 31 48 F7 BC 60 FB A8 79 FB DA FB 2A EA";
/****************************** Feeds ***************************************/
Adafruit_MQTT_Publish onoffset = Adafruit_MQTT_Publish(&mqtt, "gBridge/u0000/d1111/onoff/set"); //Replace by your feedname
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, "gBridge/u0000/d1111/onoff"); //Replace by your feedname
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(F("Controle de lampe - Google Home"));
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
digitalWrite(LED_BUILTIN, HIGH);
pinMode(0, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(0), handleInterrupt, FALLING);
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
client.setFingerprint(fingerprint);
// Setup MQTT subscription for onoff feed.
mqtt.subscribe(&onoffbutton);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(3000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
if (strcmp((char *)onoffbutton.lastread, "1") == 0) {
digitalWrite(LED_BUILTIN, LOW);
outputState = 1;
}
if (strcmp((char *)onoffbutton.lastread, "0") == 0) {
digitalWrite(LED_BUILTIN, HIGH);
outputState = 0;
}
}
}
// Now we can publish stuff!
if (outputState != lastState) {
lastState = outputState;
Serial.print(F("\nSending state val "));
Serial.print(outputState, BIN);
Serial.print("...");
if (! onoffset.publish(outputState)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
}
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
}
ICACHE_RAM_ATTR void handleInterrupt() {
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 200)
{
if (outputState == 0) {
outputState = 1;
digitalWrite(LED_BUILTIN, LOW);
}
else {
outputState = 0;
digitalWrite(LED_BUILTIN, HIGH);
}
}
last_interrupt_time = interrupt_time;
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
4 years ago
I get "ISR not in IRAM!" on the serial monitor and the ESP restarts...
Reply 4 years ago
He forgot to put ICACHE_RAM_ATTR before void handleInterrupt()
It should be like this-
ICACHE_RAM_ATTR void handleInterrupt() {
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 200)
{
if (outputState == 0) {
outputState = 1;
digitalWrite(LED_BUILTIN, LOW);
}
else {
outputState = 0;
digitalWrite(LED_BUILTIN, HIGH);
}
}
last_interrupt_time = interrupt_time;
}
4 years ago on Step 4
Uncomplete Instructions, waste of time.