Google Voice Assistance + Actions on Google + Google Firebase + NodeMCU
A brief tutorial for a personal home automation device controlling 3 devices.
Developed by: Faraz Ahmad with lots of help from Areeb Jamal
Learner’s Level: Intermediate to Advanced
#InternetOfThings
Introduction
Its quite evident that the devices are getting smarter each day but they all come at a great cost. A Philips Hue (smart lamp) costs INR 2,899.00 while a smart socket/plug costs INR 3870.00 which is in no way pocket friendly.
This led me to find an economic solution for all the DIY Savvy people to make this work under INR 1000 for four devices… That’s right INR 250 per Device.
This is how it looks:
![](http://www.farazahmad.co.in/wp-content/uploads/2018/10/20180801_235000.jpg)
Requirements
The requirements to this DIY are pretty easily available (just amazon):
- Node MCU microcontroller
- Relay interface Board
- OLED (optional)
- USB B type charger (<5Volt output)
- Connector Wires
- Screw Diver
- Wire Stripper
- Temperature Sensor (optional)
Basics
The working is pretty simple.
I have used Google Assistant to receive my commands. Those are then converted to Instructions by the Actions on Google. These actions target the device status in Google Firebase database.
![](http://www.farazahmad.co.in/wp-content/uploads/2018/10/fire-150x150.png)
Simultaneously, the hardware connected to my devices – Fan, Tubelight & CFL, fetches the status from the database and accordingly turns on and off the devise by mere voice commands.
I used references from a lot of different websites and resources but I found this link of Google Codelabs – Smart Washer the most accurate and concise. Concepts are good and will definitely take some time to learn.
Step-by-Step Tutorial
In case of doubts, ping me. You know where to find me.
Hardware Schematic Diagram
![](http://www.farazahmad.co.in/wp-content/uploads/2018/10/schematic.png)
NodeMCU Code:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <FirebaseArduino.h>
#include <dht.h>
dht DHT;
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 display(OLED_RESET);
#define FIREBASE_HOST "<appname>.firebaseio.com" //Your Firebase Project URL goes here without "http:" , "\" and "/"
#define FIREBASE_AUTH "<Firebase Secret>" //Your Firebase Database Secret goes here
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define DHT11_PIN D3 //for using a temperaature sensor
const char* ssid = "<WIFI NAME>"; // SSID i.e. Service Set Identifier is the name of your WIFI
const char* password = "<WIFI PASSWORD>"; // Your Wifi password, in case you have open network comment the whole statement.
int R1=D5; // GPIO13 or for NodeMCU you can directly write D7
int R2=D6;
int R3=D7;
int T=0;
int Fb_Thermos = 0;
String s1 = "";
String s2 = "";
String s3 = "";
String test = "";
WiFiServer server(80); // Creates a server that listens for incoming connections on the specified port, here in this case port is 80.
void setup() {
Serial.begin(115200);
delay(1000);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(R3, OUTPUT);
//pinMode(T1, INPUT);
//pinMode(R4, OUTPUT);
//pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(R1,HIGH);
digitalWrite(R2,HIGH);
digitalWrite(R3,HIGH);
//digitalWrite(R4,HIGH);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
display.print(".");
display.display();
}
display.clearDisplay();
Serial.println("");
Serial.println("WiFi connected");
display.println("");
display.println("WiFi connected");
display.display();
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP()); //Gets the WiFi shield's IP address and Print the IP address of serial monitor
Serial.println("/");
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
if (Firebase.failed()) {
Serial.print("setting /number failed:");
Serial.println(Firebase.error());
return;
}
delay(1000);
}
void loop() {
int chk = DHT.read11(DHT11_PIN);
T=DHT.temperature;
Fb_Thermos = Firebase.getInt("Thermostat/temperature/temperature");
delay(10);
Serial.print("Room Temperature: ");
Serial.println(DHT.temperature);
Serial.print("Fan Status: ");
Serial.println(Firebase.getBool("fan/OnOff/on"));
Serial.print("Light Status: ");
Serial.println(Firebase.getBool("light/OnOff/on"));
Serial.print("CFL Status: ");
Serial.println(Firebase.getBool("CFL/OnOff/on"));
Serial.print("Thermostat: ");
Serial.println(Firebase.getInt("Thermostat/temperature/temperature"));
Serial.println(R1);
Serial.println(R2);
Serial.println(R3);
display.clearDisplay();
display.setCursor(0,0);
display.print("http://");
display.print(WiFi.localIP());
display.println("/");
display.setCursor(0,10);
display.print("FAN ");
display.println(s1);
display.setCursor(55,10);
display.print("LIGHT ");
display.println(s2);
display.setCursor(0,20);
display.print("CFL ");
display.println(s3);
display.setCursor(55,20);
display.print("Temp ");
display.print(T);
display.print("°C");
display.display();
// Match the request
if (Firebase.getBool("fan/OnOff/on") == 1 && T>=Fb_Thermos+2) {
digitalWrite(R1,LOW);
delay(1);
s1 = "On";
}
if (Firebase.getBool("fan/OnOff/on") == 0 || T<=Fb_Thermos) {
digitalWrite(R1, HIGH);
delay(1);
s1 = "Off";
}
if (Firebase.getBool("light/OnOff/on") == 1) {
digitalWrite(R2,LOW);
delay(1);
s2 = "On";
}
if (Firebase.getBool("light/OnOff/on") == 0) {
digitalWrite(R2, HIGH);
delay(1);
s2 = "Off";
}
if (Firebase.getBool("CFL/OnOff/on") == 1) {
digitalWrite(R3,LOW);
delay(1);
s3 = "On";
}
if (Firebase.getBool("CFL/OnOff/on") == 0) {
digitalWrite(R3, HIGH);
delay(1);
s3 = "Off";
}
}
Disclaimer: Codes and Tutorials are sourced from Open-Source resource