Voice Control NodeMCU ESP8266 Over Internet Using Firebase

7.1K1329

Intro: Voice Control NodeMCU ESP8266 Over Internet Using Firebase

In this instructable, I am going to show how to create an Android application to voice control Nodemcu esp8266.

Requirements:-

  1. NodeMCU ESP8266 development board
  2. Breadboard
  3. LED (to check if the project is working)
  4. Firebase (to create a database)
  5. MIT app inventor 2 (to create Android application)

STEP 1: Setup Firebase and Get Secret Key

We are going to use a real-time database by Google firebase. This real-time database will act as a midway broker between Nodemcu and Android device.

  • First of all, navigate to firebase site and log in using your google account.
  • Create a new real-time database.
  • Get real-database URL and secret key to access the database from the app. For a detailed tutorial, you can check out how to use firebase with MIT app inventor.

STEP 2: Create Voice Controllable Android App

We are going to use MIT app inventor 2 to create our Android app. Its very simple to use and easy to integrate win Google firebase.

You can do it in two ways:-

Way one:-

  • Create a new app, and then make the layout (as shown in screenshot 1).
  • After that, create code logic (as shown in screenshot 2).

Way two:-

  • Alternatively, you can download the project (.aia file) attached below
  • Then go to MIT app inventor >> projects >> import project (as shown in screenshot 3). Select the file from your computer and upload it. After that open project and navigate to screen2 (as shown in screenshot 4).

Once layout and code logic is ready, go to layout window, click on firebaseDB1 (located at bottom of the workspace), enter database URL and secret key. Also keep ProjectBucket field empty (as shown in screenshot 5).

After that, click on the build button and save app file (.apk file) to your computer. Later transfer that file to your Android device.

STEP 3: Configure Arduino IDE for Nodemcu ESP8266

  • After that, add these two libraries (reference screenshot) :-
  1. Arduino Json
  2. Firebase Arduino

STEP 4: Code Time

#include "ESP8266WiFi.h"
#include "FirebaseArduino.h"

#define FIREBASE_HOST "test.firebaseio.com"        // Enter Firebase database URL here without 'https://' and last '/' (ex. test.firebaseio.com)
#define FIREBASE_AUTH "secter-key-here"            // Enter Firebase database secret key here
#define WIFI_SSID "Wifi-SSID"            // Enter WiFi SSID to which you want to connect NodeMCU ESP8266
#define WIFI_PASSWORD "Wifi-PassWord"    // Enter password of WiFi
int data,data1,count=0;

void setup()  {

  Serial.begin(9600);                                                   
  pinMode(D2,OUTPUT);
  pinMode(D3,OUTPUT);
 digitalWrite(D2,LOW);
 digitalWrite(D3,LOW);

WiFi.begin(WIFI_SSID,WIFI_PASSWORD);

Serial.print("Connecting to Wifi");
  
  while (WiFi.status()!=WL_CONNECTED){
    Serial.print(".");
    delay(500);
  }

  Serial.println();
  Serial.print("connected:");
  Serial.println(WiFi.localIP());
 Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);                      
}

void loop() {
 
  if (Firebase.failed()) {
    
      Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
      Serial.println(Firebase.error());
      
      delay(1000);
      Serial.println("Error Failed Connection To Firebase!");
    
    count++;
    if (count==10) {count=0; ESP.reset(); }
      return;
      
    }

   else { 

      Serial.println("Everything is ready!");
      delay(30); Serial.println("Everything is ready!");
      delay(30); Serial.println("Everything is ready!");

      delay(30);}

data=Firebase.getString("/data").toInt();  // Fetch data from real-time database

delay(100);
Serial.println(data);
 

data1=data;
Serial.println(data1);    

delay(100);

 if(data1==1)                          // If data is 1, trun on LED 

{                                      
      digitalWrite(D2,HIGH);
       digitalWrite(D3,LOW);
      Serial.println("light 1 ON"); }
    
    else 

    if (data1==0)             // If data is , trun off LED
     {
      digitalWrite(D2,LOW);
       digitalWrite(D3,HIGH);
      Serial.println("light 1 OFF");
    }    delay(300);
}

STEP 5: Upload After Some Changes

You must do some necessary changes in code before uploading to Nodemcu.

  • At line 3, enter database URL without 'https://'.
  • At line 4, enter database secret key.
  • At line 5 and 6, don't forget to update WiFi SSID and Wifi password (to which you want to connect NodeMCU ESP8266)

STEP 6: Getting Everything Ready

  • Install the app on your Android device.
  • Put NodeMCU esp8266 on the breadboard, connect through PC and upload the program.
  • Connect the positive terminal of LED to D2 pin and the negative terminal of LED to D3 pin (Please comment below if you want circuit diagram).
  • Now run the app and say "turn on lights" or "turn off lights".
  • Enjoy The Magic!

STEP 7: Recognize More Languages

Hope everything worked fine for you till step 6.

By default speech recognizer of MIT App Inventor 2 can recognize the English language only. Now if you want to use another language in speech recognition then you need an extension. Just follow a quick workaround.

  • Download Taifun Speech recognizer extension for MIT app inventor.
  • Import this extension in your project.
  • Drag and drop TaifunSpeechRecognizer in place of the default speech recognizer.

That it!

I had tested this extension and it supports 25+ languages including Hindi, Spanish, French, German and many more. For documentation of this extension, you can check its official site.

Need any help regarding this instructable? Please comment below.

21 Comments

thanks for your tutorial, i was follow your tutorial using NodeMCU and i got this, what this mean? thats mean that NodeMCU can't read Firebase?
Hi there,

Yes, it means NodeMCU can't connect to firebase. MAke sure you are using latest firebase SSL fingerprints.

Thanks
Great!!! That was SSL fingerprint issue.

Glad its fixed now :)
no added code in firebase? and how if i add the code "1" for turn on and turn off for "0" in mit app inventor thats have added too in fire base?
Hi there,

Working of project is as follow:-

1. Speech to text - App built using MIT app inventor uses Google speech API to convert voice/speech to text. If text matches ON/OFF then app stores 1/0 to firebase database.

2. Function of firebase realtime database - It just stores whatever value it receives from app.

3. Controller - NodeMCU fetch data from firebase and turn switch ON/OFF accordingly.

You can use any text (like turn on fan), just replace it in app .aia file.
Bro i want to use relay for my nodemcu ? can i use this code or need to edit please help me
Hi

You can use same code for relay.

If relay is 5V then use VV pin of Nodemcu or external battery for relay.
can i control the light when i connect wire from nodemcu D1 pin to relay to light up a bulb ? i have doubt because in the picture you was using two pins in nodemcu to light up the led
yes you can!!!

Ingredients:-
9V battery
5V or 9V Relay board (You can make your own relay board using transistor and diode).
NodeMCU ESP8266


Connect +VE of battery to +VE of relay and -VE of battery to -VE of relay.
Connect -VE of battery to GND of NodeMCU ESP8266.
Connect D1 pin of NodeMCU to Input of Relay borad.
Connect light bulb to output of Relay board.

That's it!!!
ok bro thank you for replying i appreciate it very much!
Gostei muito do projeto, está excelente.
Bom trabalho e continua...
Muito obrigado. Que bom que você gostou.
I get an Exeption (9) in the monitor every 95 Firebase.getString (I even tried Firebase.getInt) execution, does anyone know anything about it?
Why is the program provided by Inventor 2 not running on the phone?
Hello

I had re-uploaded project file (.aia file). It must work fine now.
Can i use another language in speech recognition?
Cool. Do you have any further plans for this tech?
More Comments