Google Assistant Automated Toilet Flush

Project Photograph

As the spread of the Novel Corona Virus is increasing drastically, the flush knob became one of the most infected places in the house as everyone touches it a lot of time with dirty hands! The highest probability of getting the COVID-19 is there. The project uses google assistant to flush the toilet, so one does not have to manually press the flush knob. Just add a Google Home or Google Assistant-enabled Smart Speaker in your washroom and ask it to ‘flush the toilet’.On receiving a command from Google Assistant,the IFTTT applet sends data in an Adafruit field which in turn changes the state of the momentary switch, this change is sent to the bolt via UART from the NodeMcu(which receives this data from Adafruit) and the servo motor is controlled using the Bolt servo API(Bolt Cloud Pro Subscription Required), flushing the toilet.

 

Things used in this project

Hardware components -

  1. Bolt WiFi module
  2. USB-A to Micro USB-B
  3. NodeMcu 1.0(Based on ESP8266)
  4. SG90 Servo Motor
  5. 10cm Male to Female jumper cables
  6. 10cm Male to Male jumper cables
  7. A 5V DC power source(that can at least deliver 1A of current)
  8. 25 cm Insulated Copper Wire(1mm or 2 mm thick)
  9. Google Home or any Google Assistant enabled Smart Speaker

Software components -

  1. Bolt IoT Android App
  2. Ubuntu
  3. Adafruit
  4. IFTTT
  5. Bolt Cloud Pro
  6. Google Assistant

Hand tools and fabrication machine -

  • Wire Stripper
  • Hot Glue Gun

Demonstration

Although complete demonstration cannot be shown in the picture, the video will be more appropriate for it, here is a screenshot of the command given by me to
the google assistant on my phone.

flush_the_toilet

 

Hardware Setup

Step 1: Automated Controlling of toilet flush Assistant

In this section, we study the 'AUTO' functionality of the project i.e. flushing the toilet by sending a command from google assistant. We will use a servo motor to pull the lever mechanism of the flush. The command for flushing the to received by a NodeMcu and will be relayed to B Wifi Module over UART. make sure your latest firmware.

Step 2: Automated Controlling of toilet flush using Google Assistant

Given below are the connections of Bolt WiFi Module to the servo motor and NodeMcu.

connections of Bolt WiFi

Power supply to the NodeMcu is given by Bolt Wifi Module. The 3.3V pin of the Bolt Wifi Module is connected to the 3.3V pin of NodeMcu.The ground of the Bolt Wifi Module is connected to the ground of the NodeMcu.The servo motor’s(SG90) signal line is connected to Bolt Digital Pin 0, Red wire(+ve) is connected to +5V on the Bolt Wifi Module and ground to the black wire(-ve) on the NodeMcu since Bolt Wifi Module and NodeMcu share the same ground. The Tx of the NodeMcu is connected to Rx of the Bolt Wifi Module, no need to connect Rx of the NodeMcu to Tx of the Bolt WiFi Module as we only need to send data to Bolt WiFi Module from NodeMcu. Only power the Bolt WiFi Module with a USB cable, NodeMcu will turn
on automatically.

Software Programming

Step 1: Getting the Bolt Cloud Pro Subscription

To control the Servo Motor(SG90) using Bolt Wifi Module,we need Bolt Cloud Pro subscription.

1)Go to https://cloud.boltiot.com/ and login and then click on the three dots in the upper right corner as indicated in the picture below.

Bolt Cloud Pro - 1 2)Click on my account

2)Click on my account

3)Go to my plan section and click on the renew button

3)Go to my plan section and click on the renew button

4)Click on the upgrade button

4)Click on the upgrade button5)Fill in your details and select whether you want a 1 month(for Rs.50) subscription or 6 subscription and then click on next and pay using your desired payment method.

Fill in your details

 

Step 2:Configuring the bolt for auto functionality

In the further section, we will use ubuntu to use python for auto functionality. 1)The API key and Device ID of the Bolt module can be determined as follows:

Connect your Bolt Device to the Bolt Cloud as per instructions given at https://cloud.boltiot.com/

The following screen will appear after that. The Bolt Device ID is highlighted in yellow

Configuring the bolt for auto - 1

2)Go to the API section to know the API Key. Click on the ‘eye’ button to see the API key.

Go to the API section

Note the device ID and API key, it will be used for configuring the auto functionality.

 

Step 3:Setting up Adafruit IO

1)We will use Adafruit IO to receive the commands from Google Assistant (via IFTTT) to NodeMcu.
Go to https://io.adafruit.com/ for Free in the upper right corner and create your account.

Setting up Adafruit IO - 1

2)Log in with your account to Adafruit IO and go to the Dashboards section

Log in with your account to Adafruit

3)Click on the Actions button and select Create New Dashboard

Click on the Actions

4)Fill in the details to create a new DashBoard, you can give it any name of your choice.

Fill in the details

5)Click on the name of the newly created Dashboard.

Click on the name

6. Click on the 'key' icon in the upper right corner to get Adafruit API key.

Click on the key icon

Note your Adafruit API Key.

7)Now go to the feeds section

go to the feeds

8)Click on the Actions button and select create a new feed

go to the feeds

9)Fill in the name of the field. I’ll name it flush, you can name it as per your choice.

Fill in the name of the field

10)Click on the plus(+) button to add a block

button to add a block

11) Select momentary switch from the options

Select momentary

12)Link the feed you just created to the momentary switch. Data sent to the feed will change the state of the momentary button. Then click on the next step button.

Link the feed13)On the next page, set the button text to ‘Flush’ or whatever you want to and set press value to ‘ON’.Leave all the other fields empty and create the block.

set the button text 

Step 4: Setting up IFTTT to send commands from google assistant to activate the Adafruit momentary switch.

 

1)We will use IFTTT(if this then that) to applet that will take the command assistant and send it to a momentary switch, which in turn will be sent to NodeMcu, which will then command the Bolt Wifi Module to flush the toilet

  • Go to https://ifttt.com/ and login.
  • After signing in click on the explore button in the upper right corner.

 

Setting up IFTTT

2) Click on the plus(+) button to create a new applet

 

Click on the plus

3)Click on the add button(+) before ‘this’.

 

Click on the add button

4)Select ‘Google Assistant’.Then select ‘Say a simple phrase’.

Select ‘Google Assistant

5) In the next step, add ‘Flush the toilet’ in the ‘What do you want to say?’ field. When you will say the phrase “Ok Google, Flush the toilet” the applet will be called.

In the ‘What do you want the Assistant to say in response’ field type ‘OK’ or whatever you want.

Flush the toilet

6)Click on the add button(+) before ‘that’.

Click on the add button-17)Then select ‘Adafruit’

Then select ‘Adafruit8)Now select ‘Send data to Adafruit IO’

Send data to Adafruit IO

9)Then select the feed you linked your block with and type ‘ON’ in the ‘Data to save’ field and click on create.

block with and type10)Click on the ‘Finish’ button to create the applet.

button to create the applet

Step 4: Receiving the data sent by google assistant from Adafruit IO feed when triggered.

  • We will use NodeMcu Version 1.0 to receive the
    data from Adafruit IO.
  • The code will be uploaded using Arduino IDE to the node MCU.
  • Go to https://circuits4you.com/2018/06/21/addnodemcu-esp8266-to arduino-ide/ and set up your NodeMcu in the Arduino IDE.
  • Hold the flash button on the NodeMcu while uploading the code to the NodeMcu.

Code for NodeMcu to establish the same.


#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <ESP8266WiFi.h> 

/*Defines your WiFi SSID,WiFi Password and Adafruit details as constants for further connecting the
NodeMcu to WiFi and connecting to Adafruit IO server for receiving the data.*/
#define wifi_ssid "Your WiFi SSID"
#define wifi_pass "Your WiFi Password"
#define Adafruit_server "io.adafruit.com"
#define Adafruit_port 1883
#define Adafruit_user "Your Adafruit uername"
#define Adafruit_key "Your Adafruit API key"


WiFiClient client; // Create an ESP8266 WiFiClient class to connect to the MQTT server.
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt( & client, Adafruit_server, Adafruit_port, Adafruit_user, Adafruit_key);
/*Subscribing to the feed ‘flush’ or whatever feed name you have given to receive the data sent to it by IFTTT.*/
Adafruit_MQTT_Subscribe flush1 = Adafruit_MQTT_Subscribe( & mqtt, Adafruit_user "/feeds/Your feed name");
//defining the function MQTT_connect() to be used later for connecting to Adafruit MQTT server
void MQTT_connect();

void setup() {
Serial.begin(9600); /*Start Serial communication(using UART) with Bolt WiFi Module at baud rate 9600*/
WiFi.begin(wifi_ssid, wifi_pass); //connects the NodeMcu to your WiFi Network
while (WiFi.status() != WL_CONNECTED) /*keeps running till the NodeMcu is connected to your WiFi Networks*/ {
Serial.println("CONNECTING"); //Keeps printing till WiFi is connected
delay(500); //waits for 0.5 second
}

Serial.println("CONNECTED"); //Prints once the while loop ends(WiFi connection is established)
}
void loop() {
mqtt.subscribe( & flush1); //Subscribes to the feed
MQTT_connect(); //Connects to the Adafruit MQTT server
Adafruit_MQTT_Subscribe * subscription; /*Subscription linked to Adafruit MQTT Server to start getting the data*/
while ((subscription = mqtt.readSubscription(20000))) /*Stores the incoming value in the subscription.*/ {
if (subscription == & flush1) //checks if data is sent to the feed(from subscription)
{
Serial.println("flush"); //Sends the text to the Bolt over UART,for flushing the toilet
}

}
}
void MQTT_connect() {
byte ret;

// Stop if already connected.
if (mqtt.connected()) {
return;
}

Serial.print("Connecting to MQTT... ");

byte retries = 3;

while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret)); //prints the error
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect(); /*disconnects(although it has not connected,this command is necessary before trying to connect again)*/
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically wait for Watchdog Timer(a timer that automatically resets the microcontroller in case it hangs or malfunctions) to reset it, as there is some trouble connecting to the Adafruit MQTT Server.
while (1);
}
}
Serial.println("MQTT Connected!"); //Prints once while loop ends

}
 

 

Step 4: Serial Output of NodeMcu Bolt WiFi Module

Serial Output of NodeMcu

Step 4: Creating a web URL for servo motor control

Parameters

  • pin: It is the Bolt GPIO connected to the servo motor that you want to control. It can take any of the following values 0, 1, 2, 3, 4.(In this case Digital pin 0)
  • value:It is the angle to which you want to set the servo motor. It can take a value between 0-180. Note - Some servo motors may not be calibrated and may give a different output angle than expected.
  • deviceName: The name of the Bolt device(Device id) you want to control. (Already noted in the previous section)
  • Your API key:The Bolt API Key you have Already noted in the previous section

https://cloud.boltiot.com/remote/Your API
key/servoWrite?pin=pinNumber&value=value
&deviceName=BOLT#######

For turning 180 degree for pulling the lever mechanism - 

https://cloud.boltiot.com/remote/Your API
key/servoWrite?pin=0&value=180&deviceNam
e=BOLT#######

For turning back the mechanism (back to 0 degree)

https://cloud.boltiot.com/remote/Your API
key/servoWrite?pin=0&value=0&deviceName=
BOLT#######

 

Step 5: Complete Code for 'AUTO' functionality


from boltiot import Bolt
import time
import urllib.request
api_key = "Your Bolt API key(Noted in previous section)"
device_id = "Your Bolt device ID(Noted in previous section)"
mybolt = Bolt(api_key, device_id) # to identify your bolt
mybolt.serialBegin(9600) # start serial communication(using UART) with the NodeMcu at baud #rate 9600
c=0
while c==0: # running the infinite loop to continuously read the incoming data from NodeMcu and controlling the servo if commanded
    a= mybolt.serialRead('127') # calls the Bolt API for reading the incoming data(using UART) from NodeMcu (till the character ASCII value 127 i.e. all the characters supported)
    b=a[11]+a[12]+a[13]+a[14]+a[15] # extracting the characters and concating them from response which may contain the trigger data(if sent) i.e. “flush” from NodeMcu
    if b=="flush":#if trigger data is sent(“flush”)
    # calls the link(Bolt API) that(created in the previous section) commands the servo motor to turn 180 degrees(to pull the lever to activate the flush)
    urllib.request.urlopen('https://cloud.boltiot.com/remote/Your API Key/servoWrite?pin=0&amp;value=180&amp;deviceName=Your Bolt ID')
    time.sleep(5) # waits for 5 seconds in that position
    # calls the link(Bolt API) that(created in the previous section) commands the servo motor to turn back to 0 degrees (to set back the lever to its initial position)
    urllib.request.urlopen('https://cloud.boltiot.com/remote/Your API Key/servoWrite?pin=0&amp;value=0&amp;deviceName=Your Bolt ID')

    print("Flush command received")
    time.sleep(0.3) # the code waits for 0.3 second(300 milliseconds)

 

 

 

Output Screenshots of Python Code (AUTO Functionality)

output

Step 6: Setting up the flush(hardware) and Servo Motor

Step 6.1: Setting Hardware

1)Gently lift the Cistern’s lid from the side.

Gently lift the Cistern’s lid

2)Tie the 25 cm (1 or 2 mm thick) copper wire to the lever as shown it the figure.

copper wire to the lever

3)Strip the other end of the wire, insert it through the last hole on the longer side of the servo attachment used and tie it as shown in the figure.

Strip the other end of the wire

4)Hot glue the SG90 servo motor(in an inverted orientation, top(rotary part) below and body above) approximately 20-centimeter above the flush(the longer the tether, the more torque servo motor can provide, so put it as high as possible.).

Hot glue the SG90

5)Now connect the attachment to the servo with the wire facing downward(when the servo is turned off and is at 0 degrees) at an angle of around 5 degrees to pull the lever to its fullest,

Hot glue the SG90

6)I have clipped the USB cable of the Bolt WiFi Module on a wall to hang all the components in midair, but it is recommended to hot glue them to the wall. (which (Hot Glue) I ran out of stock and cannot get new because of lockdown).Now connect power to the Bolt WiFi Module using the 5V power source capable of delivering 1A of current. Just add a Google Home/Nest or any Google Home/Nest speaker to your bathroom and you are done.

USB cable of the Bolt WiFi

Schematics

Schematics-1

Demonstration

 

 

Want to build more such IoT and ML projects? Want to learn IoT and ML from basics?

Check out the Bolt IoT and ML training. This online video training is excellent for those who want to start with IoT and ML because it teaches you to build projects from the basics. Click on the button below to know more about the training.

Learn more about Bolt IoT & ML Training

Bolt IoT Home Automation smart lock