Google Assistant controlled IoT based Pet Feeder using NodeMCU

Google Assistant controlled IoT Pet Feeder

If you have a pet in your home and you don’t have anyone to feed it when you are away from home then you definitely need some kind of machine to do this job. So here we are building a IoT Pet Feeder machine which is simple, efficient and cost effective. Using this machine you can feed your Pet by Google Assistant from anywhere in the world. You just have to say "Okay Google. Feed my pet" and rest of the things will be done by this machine. You can also set a specific time using Google Assistant to feed your Pet. For example Say "Okay Google. Feed my pet Today Morning" and it will feed your pet at previously specified time. Like this you can also set a specific time for noon and evening.

 

In this project, we are using a NodeMCU ESP8266 as the main controller, a Servo motor to open & close feeding bottle, and a 16*2 LCD to display the time. We will get the time from NTP servers. Instead of using RTC module for Time and Date, here we used NTP server to reduce the hardware components. NTP servers are better solution for getting time compare to RTC as it is more accurate and can provide the time of any geographical area in the world. Here is the complete tutorial on building a Internet time clock.

 

Components Required

  • NodeMCU ESP8266
  • 16x2 LCD Module
  • LCD I2C Module
  • Servo Motor

 

Circuit Diagram

Circuit diagram for this IOT based Pet Feeder project is given below. In this circuit diagram, a servo motor and LCD module is connected with NodeMCU ESP8266.

 

IoT Pet Feeder Circuit Diagram

 

The Vcc & GND pin of Servo motor and LCD I2C module are connected with Vin & GND pin of NodeMCU. While SCL and SDA pins of I2C module is connected with D1 and D2 pin of NodeMCU respectively.

Setup For Google Assistant controlled Pet Feeder

 

Here we have used a plastic bottle for the pet food container:

Container for Google Assistant controlled Pet Feeder

 

Adafruit IO Setup for IoT Pet Feeder

Adafruit IO is an open data platform that allows you to aggregate, visualize, and analyze live data on the cloud. Using Adafruit IO, you can upload, display, and monitor your data over the internet, and make your project IoT enabled. You can control motors, read sensor data, and make cool IoT applications over the internet using Adafruit IO. For test and try, with some limitation, Adafruit IO is free to use. We have also used Adafruit IO with Raspberry Pi previously.

 

1. For Adafruit IO setup, the first thing you will need to do is to sign up to Adafruit IO. To sign up go to Adafruit IO’s site https://io.adafruit.com and click on ‘Get started for Free’ on the top right of the screen.

Adafruit IO Setup for Raspberry Pi GPIO Control

 

2. After this, a window will pop up where you need to fill your details

 Sign up on AdafruitIO for Controlling Raspberry Pi GPIO

 

In sign up window fill your details like your name, mail id, username, etc. Then click on save settings, and your account is created. To get your AIO key click on ‘View AIO Key.’

 Set AIO key on AdafruitIO for Controlling Raspberry Pi GPIO

 

3. A window will pop up with your Adafruit IO AIO Key. Copy this key you'll need it later in your code.

 AdafruitIO AIO key for Controlling Raspberry Pi GPIO

 

4. Now, after this, you need to create a feed. To create a feed click on ‘Feed.’ Then click on ‘Actions,’ you will see some options from them click on ‘Create a New Feed.’

 Create New Feeds on Adafruit IO for Controlling Raspberry Pi GPIO

 

5. After this a new window will open where you need to input:

Name – In name, option write a short descriptive name of your feed. You can use

Letters, numbers, and spaces.

Description - A long-form description of your data. This field is not required, but you    

can write a description of your data.

 New Feeds on AdafruitIO for Controlling Raspberry Pi GPIO

 

6. Click on ‘Create,’ you will then be redirected to your new feed.

 

IFTTT Setup for IoT Pet Feeder

IFTTT aka ‘If This Then That’ is a free web-based service to create chains of simple conditional statements, called applets. IFTTT allows users to create triggers and execute actions based on the triggers.

In this IoT cat feeder project, we are using IFTTT to create a trigger when we say a specific line using Google Assistant. For that we have to create an applet in which we will integrate the Google Assistant with Adafruit IO. We previously used Google assistant to build IoT projects.

First of all, create an account on IFTTT. To do so navigate to IFTTT website and click on signup. Then fill out the required details and click on create an account.

Now, as you are signed in to your account, click on your profile and then click on ‘Create.’

 

NOTE: Sign in to IFTTT using the same Email ID as used in your Android phone’s Google account. For example, if your phone is signed using the xyz@gmail.com Email ID, then sign in to IFTTT using the same Email ID.

 

IFTTT Setup for IoT Based Pet Feeder

 

 

Now create an applet using If ‘This’ Than ‘That.’ Here ‘This’ is the service name by which we will give input and ‘That’ will generate trigger according to input. So in this project we will use Google Assistant as ‘This’ and Adafruit IO as ‘That.’

So to create an applet, click on the ‘This’ icon. And search for ‘Google Assistant.’ Here IFTTT will ask for permission for using your Google account.

IFTTT Applet Setup for Google Assistant controlled Pet Feeder

 

Now in Google Assistant click on ‘Say a simple phrase.’

IFTTT setup  for Google Assistant controlled Pet Feeder

 

Now in the next window, it will ask you about the phrase that you want to say your Google Assistant and what you want to hear in response to that phrase. You can also add some optional phrases. Now click on Create Trigger.

IFTTT Trigger field Setup for IoT Pet Feeder

 

Now one part of this applet is complete. For the second part click on the ‘That.’

IFTTT setup for Google Assistant controlled Pet Feeder

 

Now in the search window, search for ‘Adafruit’

 

 

IFTTT Adafruit Setup for Google Assistant controlled Pet Feeder

 

In the next window choose the feed name for which you want to create trigger and then enter the word/Data that you want to send to the feed when you say your specified phrase. After that click on ‘Create Action.’ After this it will ask you to finish the setup by clicking on the finish button. Now your applet is ready to use.

IFTTT Action Setup For Google Assistant controlled Pet Feeder

 

This applet was to feed the pet when we say a phrase to Google Assistant.

Now we will create another applet by which we can feed our pet at a specific time by saying a phrase with the text, for example, “Ok Google Feed My Pet Today Morning.” By using this applet we can send a trigger to ESP8266 at a specific time. For this applet follow the same steps: click on profile and then ‘Create.’ After that in ‘This’ option choose ‘Google assistant’. Now in google assistant click on ‘Say phrase with a Text ingredient’.

IFTTT Command Setup For IoT Pet Feeder

 

In the next window click on ‘That’ and choose Adafruit. In Adafruit select the same feed name as previous applet. Then click on ‘Create action’ and ‘Finish’ to finish the process

IFTTT Setup for Google Assistant controlled Pet Feeder

 

Code Explanation

Complete code for this Pet feeder along with a demonstration video is given at the end of this project, here we are explaining few parts of it.

Begin the code by including all the required libraries for this project. Arduino IDE has pre-installed libraries for servo motor and LCD module. But you have to download and install the rest of the libraries from the below given links:

Adafruit MQTT

NTP Client

Wire.h

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include<Servo.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

 

To get accurate time from NTP server initialize the server location. Here I am using “pool.ntp.org” which is a pool of worldwide servers.

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800,60000);

Here in this part of code, we are defining the Adafruit server address, port number, Adafruit account username and unique AIO key that you copied from your Adafruit account. This is used to establish the MQTT connection between ESP and Adafruit IO.

#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME "Username"
#define MQTT_PASS "AIO Key"

In this part of code, set up the MQTT and WiFi clients using previous information, i.e. adafruit username, AIO key, server address, and port no.

WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);

 

Now here we set up the feed with a name “onoff” and subscribing to it.

Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/onoff");

 

timeClient.begin(); function is used to start the NTP client so the it can send data to ESP8266.

timeClient.begin();

 

Inside the void loop, timeClient.update() function is used to update the date and time whenever we request to NTP servers. After getting the data, we store the hour, minute and second in three different integers. 

timeClient.update();
hh = timeClient.getHours();
mm = timeClient.getMinutes();
ss = timeClient.getSeconds();

 

Here we are directly checking for a specific word in our subscribed feed and if the word matches with our specified word, i.e. ‘ON’ it will call the open_door() and close_door() function.

      if (!strcmp((char*) onoff.lastread, "ON"))
      {
        open_door();
        delay(1000);
        close_door();
       }

 

By using the if condition assign a specific time to the “Morning”, “Afternoon” and “Evening” strings. After that use another if condition to match the current time and feed time. So whenever we say “Feed My Pet Today Morning” it will call the open_door() and close_door() function when the current time is equal to 10:30. It will follow the same procedure for “Evening” and “Afternoon” strings.

if (!strcmp((char*) onoff.lastread, "Morning"))
      {
        feed_hour = 10;
        feed_minute = 30;
      }
     }
   }
   if( hh == feed_hour && mm == feed_minute &&feed==true) //Comparing the current time with the Alarm time
    {
      open_door();
      delay(1000);
      close_door();
      feed= false;
      }
}

 

void open_door() and void close_door() function are used to send the command to the servo motor to open the trap door. Open angle is defined as 60° while the close angle is defined as 0°.

void open_door(){
    servo.write(OPEN_ANGLE)
void close_door(){
    servo.write(CLOSE_ANGLE);  

 

Working of the IoT Pet Feeder

After uploading the code to the NodeMCU, the current time and Feed time will be displayed on the 16*2 LCD. Initially, it will show 0:0 in feed time as we didn’t enter any feed time. Now Say "Okay Google. Feed my pet" to your Google Assistant. Google Assistant will recognize the phrase and respond with "Feeding your pet." After that it rotates the servo motor from its initial position 0⁰ to 60⁰ and after a delay, it returns to its initial position. You can also feed your pet at a specific time. For that we have used three strings i.e., “Morning”, “Afternoon” and “Evening”. These strings are assigned with different times, so when you say “Feed My Pet Today Morning” it will take the time assigned to “Morning” string as feeding time and when the entered time matches with current time it rotates the servo motor from its initial position 0⁰ to 60⁰ and after a delay it returns to its initial position.

Working Of Google Assistant controlled IoT Pet Feeder

 

This is how you can feed your pet using Google Assistant. If you want to feed your pet at a specific time instead of morning, afternoon and evening, go to IFTTT and change the Google assistant settings from ‘Say a phrase with text ingredient’ to ‘Say a phrase with a number’.

Complete process is shown in the video given below.

Code

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include<Servo.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h> 

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800,60000);

Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define WIFI_SSID "Galaxy-M20"
#define WIFI_PASS "ac312124"

#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME "aschoudhary"
#define MQTT_PASS "1ac95cb8580b4271bbb6d9f75d0668f1"

int SERVO_PIN = D3;    // The pin which the servo is attached to
int CLOSE_ANGLE = 0;  // The closing angle of the servo motor arm
int OPEN_ANGLE = 60;  // The opening angle of the servo motor arm
int  hh, mm, ss;
int feed_hour = 0;
int feed_minute = 0;

//Set up MQTT and WiFi clients
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);

//Set up the feed you're subscribing to
Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/onoff");
boolean feed = true; // condition for alarm

void setup()
{
  Serial.begin(9600);
  timeClient.begin();
  Wire.begin(D2, D1);
  lcd.begin();
  
  //Connect to WiFi
  Serial.print("\n\nConnecting Wifi... ");
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
  }

  Serial.println("OK!");

  //Subscribe to the onoff feed
  mqtt.subscribe(&onoff);
  servo.attach(SERVO_PIN);
  servo.write(CLOSE_ANGLE);
  
  
}

void loop()
{
   MQTT_connect();
   timeClient.update();
   hh = timeClient.getHours();
   mm = timeClient.getMinutes();
   ss = timeClient.getSeconds();
   lcd.setCursor(0,0);
   lcd.print("Time:");
    if(hh>12)
  {
    hh=hh-12;
    lcd.print(hh);
    lcd.print(":");
    lcd.print(mm);
    lcd.print(":");
    lcd.print(ss);
    lcd.println(" PM  ");
  }
  else
  {
    lcd.print(hh);
    lcd.print(":");
    lcd.print(mm);
    lcd.print(":");
    lcd.print(ss);
    lcd.println(" AM  ");   
  }
   lcd.setCursor(0,1);
   lcd.print("Feed Time:");
   lcd.print(feed_hour);
   lcd.print(':');
   lcd.print(feed_minute   );

  Adafruit_MQTT_Subscribe * subscription;
  while ((subscription = mqtt.readSubscription(5000)))
  {
    
    if (subscription == &onoff)
    {
      //Print the new value to the serial monitor
      Serial.println((char*) onoff.lastread);
     
    if (!strcmp((char*) onoff.lastread, "ON"))
      {
        
        open_door();
        delay(1000);
        close_door();
       }
      if (!strcmp((char*) onoff.lastread, "Morning"))
      {
        feed_hour = 10;
        feed_minute = 30; 
      }
      if (!strcmp((char*) onoff.lastread, "Afternoon"))
      {
        feed_hour = 1;
        feed_minute = 30; 
      }
      if (!strcmp((char*) onoff.lastread, "Evening"))
      {
        feed_hour = 6;
        feed_minute = 30; 
      }
     }
   }
   if( hh == feed_hour && mm == feed_minute &&feed==true) //Comparing the current time with the Feed time

    { 
      open_door();
      delay(1000);
      close_door();
      feed= false; 
      }
}

void MQTT_connect() 
{
  int8_t ret;

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

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) // connect will return 0 for connected
  { 
       
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) 
       {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  
}

void open_door(){
  
  servo.write(OPEN_ANGLE);   // Send the command to the servo motor to open the trap door
}

void close_door(){
  
  servo.write(CLOSE_ANGLE);   // Send te command to the servo motor to close the trap door
}
 

Video

28 Comments

the interface of IFTTT is changed so the instructions are bit unclear, for feeding in morning, etc that is on a specific time what do we have to add in Data To save tab in the second applet do we have to save "ON" or something else.

My husband and i have been now satisfied when Edward managed to carry out his web research with the precious recommendations he was given from your very own weblog. It is now and again perplexing to simply happen to be freely giving facts which usually most people might have been making money from. We take into account we have got the website owner to appreciate for that. The entire illustrations you have made, the easy site navigation, the relationships you can give support to instill - it is mostly sensational, and it's really leading our son and us do think this content is fun, and that is highly pressing. Many thanks for everything!

Thanks so much for providing individuals with an exceptionally wonderful chance to discover important secrets from this site. It's always so brilliant plus full of fun for me and my office peers to visit your blog more than thrice per week to study the new things you have got. And lastly, I am at all times impressed with the surprising tactics you serve. Some 2 points on this page are really the most efficient I have ever had.

I wanted to compose you a little observation to be able to thank you over again for those great opinions you have contributed at this time. This has been simply pretty open-handed with you to offer unreservedly what exactly a lot of folks would've distributed as an electronic book to help with making some money for their own end, notably considering the fact that you might well have done it in the event you considered necessary. The advice as well worked to provide a fantastic way to be certain that many people have the identical dream the same as my very own to learn a little more when it comes to this condition. I know there are numerous more pleasant situations up front for folks who discover your blog.

After research just a few of the blog posts in your website now, and I actually like your means of blogging. I bookmarked it to my bookmark website checklist and shall be checking back soon. Pls check out my site as well and let me know what you think.

I'm just writing to make you understand what a perfect encounter my wife's girl gained checking your webblog. She learned many things, including how it is like to have a wonderful coaching spirit to have the mediocre ones smoothly master selected tortuous issues. You undoubtedly did more than our own expectations. Thank you for offering the important, safe, explanatory and even easy tips on that topic to Evelyn.

I precisely desired to thank you very much again. I do not know the things I would have achieved without the type of opinions documented by you over my topic. It actually was a real frightening issue in my position, however , being able to see this skilled tactic you managed it took me to cry for delight. Now i'm happy for this information and as well , hope that you know what a great job you are undertaking training people by way of a web site. I know that you've never come across any of us.

I wish to convey my admiration for your kind-heartedness giving support to those who really need guidance on in this situation. Your very own commitment to getting the solution all around turned out to be unbelievably interesting and has without exception enabled individuals much like me to reach their objectives. This interesting tips and hints signifies a whole lot to me and still more to my mates. With thanks; from all of us.

My spouse and i ended up being really thrilled when Michael could do his web research using the precious recommendations he grabbed in your web pages. It is now and again perplexing just to continually be offering key points people could have been making money from. So we fully grasp we have got the blog owner to appreciate because of that. The main illustrations you've made, the straightforward site menu, the friendships you can assist to promote - it's got mostly impressive, and it's making our son and us believe that that subject matter is satisfying, and that is tremendously pressing. Thank you for the whole thing!

I must voice my admiration for your kind-heartedness for men and women that need assistance with the subject. Your special commitment to getting the message all-around appeared to be really valuable and has consistently made men and women like me to reach their ambitions. Your new informative report means this much a person like me and additionally to my peers. Many thanks; from all of us.

I precisely desired to thank you so much once again. I do not know the things that I would've gone through without these creative ideas revealed by you relating to this question. It seemed to be a hard scenario in my circumstances, but being able to see this skilled mode you dealt with it took me to leap over happiness. I am thankful for your service as well as pray you find out what a great job your are carrying out training men and women thru a blog. More than likely you've never got to know all of us.

I intended to write you this little bit of remark just to give thanks again on your wonderful information you have shared in this case. It's so remarkably generous of people like you to offer publicly just what a number of people could have marketed as an e-book to help make some profit for themselves, and in particular since you could possibly have tried it in the event you desired. Those solutions also acted to become a great way to recognize that some people have similar dreams just as my own to understand more pertaining to this matter. I know there are numerous more fun times up front for those who read carefully your blog.

Thank you so much for giving everyone an exceptionally marvellous possiblity to read articles and blog posts from this blog. It is always so useful and as well , jam-packed with a lot of fun for me and my office friends to search your web site at a minimum 3 times in one week to see the fresh guidance you will have. And of course, I'm also usually happy for the incredible ideas served by you. Selected 1 ideas in this article are particularly the simplest we've had.

I have to express my thanks to the writer for rescuing me from this particular problem. As a result of browsing throughout the internet and meeting notions which were not beneficial, I figured my life was gone. Being alive without the answers to the issues you've solved by means of your entire short post is a serious case, and ones which may have adversely damaged my career if I had not noticed your blog post. That competence and kindness in maneuvering all the stuff was useful. I'm not sure what I would have done if I had not encountered such a step like this. I can at this moment look ahead to my future. Thanks for your time so much for this reliable and sensible help. I will not hesitate to recommend the sites to anybody who would need tips on this problem.

I have to show appreciation to the writer for bailing me out of this particular setting. After researching through the world wide web and obtaining things that were not pleasant, I was thinking my entire life was well over. Existing devoid of the answers to the difficulties you've fixed as a result of your entire guideline is a crucial case, and those that might have adversely affected my career if I hadn't discovered your blog. Your actual skills and kindness in handling a lot of stuff was precious. I am not sure what I would have done if I hadn't come across such a subject like this. It's possible to now relish my future. Thanks for your time very much for your high quality and results-oriented guide. I won't think twice to propose your web page to anybody who ought to have assistance about this subject.

I enjoy you because of all of your efforts on this website. Kate take interest in setting aside time for investigation and it's really obvious why. Almost all know all relating to the dynamic mode you present practical thoughts via your website and as well as cause participation from visitors on the article so our child is actually understanding a great deal. Take pleasure in the rest of the year. You're carrying out a useful job.

I must voice my appreciation for your kindness supporting people that really need assistance with that question. Your special commitment to passing the solution along ended up being exceptionally informative and has really enabled most people much like me to get to their desired goals. Your own invaluable guide signifies so much a person like me and a whole lot more to my office colleagues. Thanks a lot; from all of us.

I would like to express my appreciation to the writer for bailing me out of this problem. Because of checking through the internet and obtaining methods that were not pleasant, I thought my entire life was done. Being alive without the presence of approaches to the problems you have sorted out all through your main post is a serious case, as well as the kind which might have badly damaged my career if I hadn't noticed your web site. Your actual skills and kindness in maneuvering a lot of things was invaluable. I'm not sure what I would have done if I had not discovered such a thing like this. It's possible to at this time look ahead to my future. Thanks so much for your professional and amazing guide. I will not hesitate to propose your web page to anybody who wants and needs care on this topic.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.