Google Assistant controlled home Light with a Raspberry Pi with IFTTT and Particle.io

Basavaraj PN
6 min readMar 28, 2019

https://www.youtube.com/watch?v=IsxdWJNqLPk

Smart home Lights in recent times have become quite common thanks to the growing IoT market. But more recently, we are getting to see these lights being controlled with just the power of voice (instead of apps) through AI assistants on people’s phones, speakers, computers, etc. However, these Smart Lights haven’t yet beaten the conventional lights in terms of price and hence require a bit of an investment. So, if you have a Raspberry Pi lying around, we will show you how you can make your own Smartlight with a dumb one that can be controlled using Google Assistant. The project will involve playing around with IFTTT and Particle services so below is a bit of background about them.

IFTTT — (if this then that) is a free platform that helps your apps and devices work together in new ways. It has become the room of automation service for improvement. IFTTT supports social media sites, online tools, and email and service such as Twitter, Gmail, Facebook. The automation is accomplished via applets that connect multiple apps to run automated tasks. You can turn on or off an applet using IFTTT’s website or the IFTTT widgets. You can also create your applets or make variations of existing ones. It’s actually quite simple once you start playing around with it.

PARTICLE — An IoT company that produces internet-connected microcontroller boards. The company also has a cloud service called Particle cloud which makes it easy to connect particle devices over the internet. From robust security to reliable infrastructure, to a flexible integration system, the particle cloud has everything you need to succeed and can move quickly. In recent times, particle cloud added raspberry pi to their service i,e, you can connect your Raspberry Pi to the particle cloud for free. The development is currently in beta but is still very functional for most parts.

  1. Prerequisites:
  • Raspberry Pi (with Raspbian OS).
  • Relay module
  • Jumper Wires.
  • Lamp/Bulb with Bulb Holder.
  • 1mm or higher Guage wires.
  • 5V Power Supply (For the Pi).

2. Circuit Diagram and Wiring:

The Particle Agent follows its own GPIO labeling and doesn’t follow the conventional Raspberry Pi Labeling which can be found below

In the Particle firmware, pins are labeled from D0 to D15. The Broadcom pin numbers, also known as the BCM or GPIO pin numbers, are also available from GPIO0 to GPIO27.

We will be using GPIO17 which is D1 according to Particle to control our Relay module as shown in the circuit below

This circuit diagram can easily be designed using fritzing software.

3. Particle agent Setup:

Before this first, sign up for a Particle cloud account from here. Then give your Pi a name when asked. IIf you want to roll back to sign in type particle-agent setup which will restore the Particle package to default values. Now type particle-agent status to know if your Pi is connected to the Particle Cloud.

Now, Install the particle agent by typing or pasting the following command in a terminal on your Raspberry Pi

bash <( curl -sL https://particle.io/install-pi )

After the download and install process is complete, you will need be prompted to sign-in to your Particle account on the Raspberry Pi.

4. Create a Particle App:

Over on the browser, go to particle website, that is build.particle.io. You will be doing all the coding on Particle’s online IDE and then remotely flashing it on your Particle connected Raspberry Pi. Particle IDE is broken into three sections- The Coding environment where you write your code, Examples app where you can also make your own app and finally the Menu section where you have got many options like checking the connected Devices, activities, etc.

Create a “New App” by typing or pasting the following code.

int relay = D1; //pin to which relay is connected
int boardLed = D7;
bool vin = LOW; //a virtual boolean variable

// setup() is run only once, it's where we set up GPIO and //initialise peripherals
void setup() {

// Setup GPIO
pinMode(relay,OUTPUT); // relay pin is set as output
digitalWrite(relay,HIGH);
// Subscribe to events published by IFTTT using Particle.subscribe Particle.subscribe("Name_of_The_Event-1", myHandler); //turning off function declarationParticle.subscribe("Name_of_The_Event-2", thisHandler); //turning on function declaration
}

// loop() runs continuously, it's our infinite loop.

void loop() {
if (vin==HIGH){
digitalWrite(relay,LOW);
}
else if (vin==LOW){
digitalWrite(relay,HIGH);
}

}

//our events are called when IFTTT applets are triggered

void myHandler(const char *event, const char *data){
vin=LOW;
}
void thisHandler(const char *event, const char *data){
vin=HIGH;
}

Enter your own unique name of the event names( i.e, Name_of_The_Event-1 and Name_of_The_Event-2 ) inside the Particle.subscribe functions declaration as you will be needing later to set up the IFTTT events. example- “mylight_assistant_on” and “mylight_assistant_off”. Click the flash icon to flash the code onto your Pi. Make sure that there is no compilation error shown at the bottom of the screen.

5. IFTTT Setup

Download the IFTTT app on your device from Appstore or Playstore or head over to ifttt.com. The first time you open the app, you’ll have to signup for a new account. Once that’s done, on the bottom of the screen tap on the icon at the extreme right which will take you to the My Applet section

Tap on the “+” icon at the top to create a new applet. And then you will be taken to a new screen. Tap on the blue “+this” text and then search for Google Assistant and tap on the first result. If this is your first time with IFTTT and Google Assistant, then IFTTT will ask you to login to your Google account connected to the Google assistant and prompt for access to the service.

Then select “Say a simple phrase” and enter the phrase to which you would want your Google Assistant to get triggered and turn on the light. You can also add two alternate ways to trigger the same event. Also, type in what you would want the Google Assistant to respond back when you say this command.

Now you have set up the “that” part of your IFTTT. SO tap on “+that” and then search for Particle and open the first result. Then select “Publish an event” after that IFTTT will prompt you to connect to your Particle account and authorize access.

Now, Enter the Unique event name you had given earlier at the Particle Build IDE for the function which controlled the turning on the process. Then change it to a Public event and tap on the tick mark at the right top and then tap on Finish on the next screen.

In the same way, create another Applet for the turning Off (Name_of_The_Event-2)process by setting up the this and then part with Google Assistant and Particle respectively.

And you’re done! Now you can invoke your Google Assistant to control your self-made Raspberry Pi based Smarthome light.

--

--