Google Assistant(3/4):  Voice-Activated Light Control with IFFFT, Adafruit IO, NodeMCU, and Raspberry Pi

Overview

This post is the third tutorial in a 4-part series on how to set up Google Assistant with the Raspberry Pi. In this tutorial you will learn how to control a relay switch connected to a light bulb using NodeMCU, Adafruit IO, IFTTT, together with Raspberry Pi and ReSpeaker 4-Mics Pi Hat. Of course, we're going to control the light turning on and off using voice commands :D

 

Hardware

  • Raspberry Pi 3B+ (Compatible on Raspberry Pi Zero and Zero W, Raspberry Pi B+, Raspberry Pi 2 B, Raspberry Pi 3 BRaspberry Pi 3 B+, Raspberry Pi 3 A+)

  • ReSpeaker 4-Mics Pi HAT
  • 
    
  • Speaker with 3.5mm audio jack
  • NodeMCU V3 development board
  • 
    
  • Breadboard
  • 
    
  • Relay Module
  • 
    
  • Light Bulb
  • Router (For WiFi Connection)
  •  

    Software

    • Arduino IDE
    • RPI terminal
    • Internet Browser (Example Chrome)

     

    Application Discussion

    We basically know that to turn ON and OFF a light bulb, we need a switch connected to its electrical wire. To control it remotely and online, we need a microcontroller to do that. In this tutorial we will use the NodeMCU Wi-fi development board as the microcontroller and a relay module to control the light status.

    NodeMCU is a low-cost open-source IoT platform. It has firmware which runs on the ESP8266 Wi-Fi SoC from Espressif Systems and hardware which was based on the ESP-12 module. It has support for Arduino IDE for programming.

    To control the light bulb online we will use the Adafruit IO service and library to set up a simple API to control it.

    Adafruit IO is a system that makes data useful. It is easy to use and allows simple data connections with little programming required. Adafruit IO includes client libraries that wrap its REST and MQTT APIs. 

    To control the light bulb using Google Assistant we will use IFTTT to create a simple conditional statement so that Google Assistant and Adafruit IO API can interact.

    IFTTT derives its name from the programming conditional statement “If This, Then That.” It provides a software platform that connects apps, devices, and services from different developers in order to trigger one or more automation involving those apps, devices, and services.

     

    Setup the Hardware

    NodeMCU Setup

    Follow the schematic diagram below.

    CAUTION: Special care must be taken when working with high AC Voltage!!

     

    Google Assistant on Raspberry Pi Setup

    Mount the ReSpeaker 4-Mics Pi HAT to Raspberry pi like this.

     

    Setup the Software

    Google Assistant Setup

    • To set up the Google Assistant with Raspberry Pi, follow the first tutorial. You can check it here.

    Raspberry Pi with ReSpeaker 4-Mic Pi Hat Integration

     

    NodeMCU with Arduino IDE Setup

    • First, we will program the NodeMCU. We need to download and install the newest version of Arduino IDE, you can download it here.

    https://www.arduino.cc/en/software

    • In the Arduino IDE, go to Preferences. You can press "CTRL+Comma", or click "File" then "Preferences" as shown in the screenshot below.

     

    • On the Preferences window, copy the text below and paste it on "Additional Boards Manager URLs" field. This is for adding the ESP8266 board driver. Then click "OK".

    http://arduino.esp8266.com/stable/package_esp8266com_index.json

     

    • Proceed to download the ESP8266 library on the Boards Manager. Click "Tools", then "Boards:(Your Current Board)", and "Boards Manager...". 

     

    • In the "Boards Manager" first select "All" on the Type drop-down box, then search for esp8266. Download the esp8266 by ESP8266 Community, by selecting the latest version and clicking Install. After installation, you may close it.

     

    void setup() {
      pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
    }
    
    // the loop function runs over and over again forever
    void loop() {
      digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level
      // but actually the LED is on; this is because
      // it is active low on the ESP-01)
      delay(1000);                      // Wait for a second
      digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
      delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
    }  
    • Paste it to your Arduino code editor (or open the downloaded file), then click Upload by clicking the -> icon as shown below.

    • If you see your NodeMCU's built-in LED blinking, we're good at setting up our NodeMCU. Next is to test your Relay Ssetup.

     

    Relay Setup

    // Relay State Changing test
    // Written by jeminico (Createlabz)
    /************************ Relay Configuration *******************************/
    // Relay pin
    int relayPin = 15;
    // Relay State
    int state = LOW;
    const long delayChangeState = 5000;
    
    void setup() {
      // put your setup code here, to run once:
      
      // Start of initialization
      Serial.println("Initializing Setup..");
      // Initialize Serial
      Serial.begin(9600);
      
      // Initialize relay
      Serial.println("Initializing Relay..");
      pinMode(relayPin, OUTPUT);
      
      Serial.println("Done Initializing Setup!");
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      changeStateRelay();
    }
    
    // Change state of the Relay
    void changeStateRelay(){
      // Set state of relay
      if(state == LOW) state = HIGH;
      else state = LOW;
      digitalWrite(relayPin, state);
      Serial.println("Relay Change State");
      delay(delayChangeState);
    }
    • Paste it to your Arduino code editor (or open the downloaded file), then upload it to your NodeMCU.
    • After successfully uploading, you can observe the light bulb is blinking within 5 seconds delay. Next is to proceed to set up Adafruit IO with your NodeMCU.

     

    Adafruit IO Setup

    • For setting up Adafruit IO first we need to sign in if you have already an account, or if not, click the "SIGN UP" button to create an account. 

    https://accounts.adafruit.com/users/sign_in

    • After signing in, you can see your dashboard as shown below. 

    • Click "My Key" then take note of the defined value of "IO_USERNAME" and "IO_KEY" from Arduino option, as pointed in the screenshot below. We will need it later for code implementation. Click "X" to close the dialog box.

    • Next click the "Feeds" tab and click the highlighted "View all" text beside "Feeds" as shown below.

    • Then proceed with "+ New Feed" button to create a Feed.

    • Fill in the "Name"  and "Description" of your Feed. For example, you can check the values below. After filling up the values you can proceed by clicking "Create".

    • Now that we have a Feed, go back to your Arduino IDE. We need to install the Adafruit IO library to do that. Select "Sketch", then click "Include Library", then click "Manage Libraries".

    • On the Library Manager, search for "adafruit io arduino". Look for Adafruit IO Arduino by Adafruit then install as shown below.
    • After clicking Install, you will be prompted with this.
    • Click "Install All" to install the other needed libraries for Adafruit IO.

     

    // Light and temperature control via AdaFruit
    // Written by jeminico (Createlabz)
    /************************ Relay Configuration *******************************/ // Relay pin int relayPin = 15; /************************ Adafruit IO Configuration *******************************/ #include <ESP8266WiFi.h>
    #include <AdafruitIO.h>
    #include <Adafruit_MQTT.h>
    #include <ArduinoHttpClient.h>
    #include "AdafruitIO_WiFi.h" // visit io.adafruit.com if you need to create an account, // or if you need your Adafruit IO key. #define IO_USERNAME "YOUR_IO_USERNAME" #define IO_KEY "YOUR_IO_KEY" // Wifi Configuration #define WIFI_SSID "YOUR_WIFI_SSID" #define WIFI_PASS "YOUR_WIFI_PASS" // Initialize Adafruit IO wifi AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS); // Set up the 'command' feed AdafruitIO_Feed *command = io.feed("COMMAND FEED"); void setup() { // put your setup code here, to run once: // Start of initialization Serial.println("Initializing Setup.."); // Initialize Serial Serial.begin(9600); // Initialize relay Serial.println("Initializing Relay.."); pinMode(relayPin, OUTPUT); // connect to io.adafruit.com Serial.println("Initializing Adafruit IO.."); io.connect(); // set up a message handler for the 'command' feed. // the handleMessage function (defined below) // will be called whenever a message is // received from adafruit io. command->onMessage(handleMessage); // wait for a connection while(io.status() < AIO_CONNECTED) { Serial.print("."); delay(500); } // we are connected Serial.println(); Serial.println(io.statusText()); Serial.println("Done Initializing Setup!"); } void loop() { // put your main code here, to run repeatedly: // Run Adafruit IO io.run(); } // Handle Adafruit message. void handleMessage(AdafruitIO_Data *data) { int command = data->toInt(); if (command == 1){ //light up the LED Serial.print("received <- "); Serial.println(command); digitalWrite(relayPin, HIGH); } else { Serial.print("received <- "); Serial.println(command); digitalWrite(relayPin, LOW); } }
    • Paste it to your Arduino code editor (or open the downloaded file). Look for the part with #define as specified below:
    // visit io.adafruit.com if you need to create an account,
    // or if you need your Adafruit IO key.
    #define IO_USERNAME    "YOUR_IO_USERNAME"
    #define IO_KEY         "YOUR_IO_KEY"
    
    // Wifi Configuration
    #define WIFI_SSID       "YOUR_WIFI_SSID"
    #define WIFI_PASS       "YOUR_WIFI_PASS"

    • Change the values for "YOUR_IO_USERNAME", and "YOUR_IO_KEY" with the values in your Adafruit IO account. Also, change the values for "YOUR_WIFI_SSID" to your actual WiFi SSID, and "YOUR_WIFI_PASS" to your actual WiFi password.
    • There's one more thing to change, look for this part: 
    // Set up the 'command' feed
    AdafruitIO_Feed *command = io.feed("COMMAND FEED");
    • Change the value of "COMMAND FEED" to the name of the Feed we created earlier. For this example, we named it "Light Control".
    • Next, proceed to upload the Arduino code to NodeMCU.
    • For testing, we will go back to your Feed tab on Adafruit IO. Click your Feed (Light Control) as shown below.

    • Then click the "+ Add Data" button as shown below.
    • Fill in the text box with a value of "1" as shown below. This should turn ON the light bulb.

    • To turn the light bulb OFF click the "+Add Data" again then fill in a value of "0".
    • Let's proceed to link your Google Assistant to your Adafruit IO by setting up IFTTT.

     

    IFTT Setup

    • Create a new tab, and proceed to the IFTTT website and sign in if you have already an account, if not, go sign up.

    https://ifttt.com/login?wp_=1

    • After signing in, you will be directed to the "IFTTT home screen". Then click "Create" tab at the top right corner.

    • In the "IFTTT create screen", click the "Add" button beside the "If This" text as shown below.

    • Now search for "Google Assistant" in the search bar, then click the "Google Assistant" image button.

    • If  Google Assistant is not yet connected to your account, you will be shown this. Click the "Connect" button, then connect it to your Google Assistant Gmail account.

    • Click the first option "Say a simple phrase" as shown below.

    • Next, we will provide the necessary information for Google Assistant to use as trigger and response. Fill in the needed values below (you may copy them as is). Afterwards, click "Create trigger".

    • Then you will be shown the "IFTTT home screen" again, now you can add on "Then That" by pressing the "Add" button beside it.
    • Then search for "adafruit" in the search bar, then click the "Adafruit" image button.

    •  Click the "Send data to Adafruit IO" image button as shown below.

    •  If Adafruit is not yet connected to your account, you will be shown this. Click the "Connect" button, then connect it to your Adafruit account, by scrolling down on the prompt tab, then click the "AUTHORIZE" button.

    • After connecting your account, proceed by filling in the action fields. On the "Feed name" field, select "Light Control" we created earlier. Then fill in the field "Data to save" with "1" to turn on the Light Bulb as shown below. Click the button "Create action" to proceed.
    • Now you will go back to the "IFTTT home screen", this time click the button "Continue".

    • After continue, you will be prompted to "Review and finish screen". Click the button "Finish" to proceed. You will be prompted as you finish creating your applet.

    • Now you can test on your RPi-based Google Assistant by saying "Ok Google" then say "Turn on the light" the phrase we created earlier. The light bulb will turn ON. To turn OFF the light we need to create another applet.
    • To create an IFFFT applet to turn OFF the light, go through the IFTTT setup process again, but this time change the values needed as shown below.
    For Google Assistant phrase (Light Off):
    For Adafruit IO Setup(Light Off):
     
    • Now you can test your RPi-based Google Assistant by saying "Ok Google" then say "Turn off the light" the phrase we created earlier. The light bulb will turn off.

     

    Demo Video

       

      Conclusion

      While demonstrating that a light bulb can be controlled using voice-commands via Google Assistant, we can extend this project to other and more electrical devices that involve switching (turning ON and OFF). You can create your own voice-activated Smart Home :D

      But remember to always be cautious when working with high voltages as the risk of being electrocuted is always there. Check always your wirings that they are not going to short or better to ask help from people who know how to handle high voltage electricity if you are not sure.

       

      Check other tutorials in this series: Integrating Google Assistant to Raspberry Pi

      Raspberry Pi with ReSpeaker 4-Mic Pi Hat Integration

      Integration of Youtube and Spotify with ReSpeaker 4-Mics Pi Hat on Raspberry Pi

      Voice-Activated Sensor Reading and Light Control using NodeMCU and Raspberry Pi with Google Assistant and Dialogflow

       

      References

      https://www.instructables.com/Quick-Start-to-Nodemcu-ESP8266-on-Arduino-IDE/

      https://www.instructables.com/How-to-Control-You-Room-With-Your-Voice-and-Google/

       

      AdafruitAdafruit ioGoogle assistantIftttNodemcuRaspberry piRespeakerRespeaker 4-mics piRpiSmart homeVoice controlVoice-activatedWi-fi

      Leave a comment

      All comments are moderated before being published