Smart Pet Feeder Part 2 – Feeding App with Speech Recognition

automatic pet feeder

automatic pet feeder

In Part 1 of Smart Pet Feeder, Make a Smart Automatic Pet Feeder with Arduino Uno, we set up an automatic platform that determines whether your pet is fed or not and counts the hours to the next feed. In Part 2, we’ll try to make the system even more “smarter” by adding speech recognition feature through an app made with MIT App Inventor. We’ve used MIT App Inventor to create apps in our previous tutorials. App Inventor is an easy-to-use block-based language for designing Android Apps.

Hardware

  • Arduino UNO
  • RFID RC522
  • HC-05 Bluetooth Module

From Part 1:

  • Arduino Uno
  • Light sensor TEMT6000
  • Distance Sensor Sharp GP2Y0A21YK
  • RFID RC522
  • Buzzer
  • Motor SG90
  • RTC DS1307

Software

 

Step 1: Working with RFID tag EEPROM

The tags we set up in Part 1 wanted to EEPROM – in this memory, the tags will be saved until we clear it; this feature helps us to differentiate our pets from others, giving food only to the pets with the assigned tags.

We set 2 tags in Part 1 and used EEPROM to store data. RFID tags help identify and differentiate your pets from other pets, giving food only to the pets with the assigned tags. Using EEPROM ensured that the data is securely stored in the memory even after system reboots. The following code allows you to change tag information from EEPROM:

 

automatic pet feeder

Figure 1: Our pet tags in the EEPROM

Last time, our pet was assigned to a red tag. This time, we changed the tag to a green tag.

Step 2: How to control the servo

A servo motor uses PWM (pulse width modulation) from the microcontroller in order to change its positions. The servo needs to be calibrated and it will be set to 90 degrees for opening the door.  

To control the servo, we’ll use Arduino Sweep code. The code sweeps the servo motor shaft across 180 degrees. We’re going to change the degrees, so instead of 0-180 degrees we’ll use 10-170 degrees.

Notes about the servo:

Sometimes, your servo may behave erratically. When you can’t execute commands because the Arduino restarts itself, it may be due to the USB ports not providing enough power to drive servos. In this case, the Arduino resets and the application becomes useless. Here are two options to avoid this problem:

  1. You can add a high value capacitor (470uF or greater) between GND and 5V on the breadboard. This capacitor is used as a temporary supply when the Arduino doesn’t have enough power to sustain it with current. The longer terminal must be connected to VDD = 5V and the short one to the GND.
  1. You can program the board from USB and disconnect it afterward. Then, you can use a phone charger to supply the setup from the plug as this has larger current capability.

Let’s briefly look at how <Servo.h> library works.

#include <Servo.h>

This command has to be included to use Servo.h library. The 2 examples that are given in the Servo library are Knob and Sweep. These two are very good for testing the servos. Using Knob, you can use a potentiometer to move your servo to a specific degree. Using Sweep, you can sweep the shaft of your servo back and forth across 180 degrees.

Servo servo;

This is a declaration of a type. It defines a variable servo of type Servo. It is similar to other types like int or float for servo.

servo.attach(servoPin);

In the block setup of the code you need to assign your servo to a specific pin. This command is used to assigns the servo variable to the pin.

servo.write(angle);  

This command sets the angle of the shaft of your servo, from 0 to 180 degrees, and actually move the servo to that degree.

 

Step 3: Adding HC-05 Bluetooth module

———-

About Bluetooth HC-05 – User Instructional Manual

Bluetooth Serial module’s operation doesn’t need drive and can communicate with the other Bluetooth device that has the serial. Communication between two Bluetooth modules requires at least two conditions:

(1) The communication must be between master and slave.
(2) The password must be correct.

Properties of this module:

  • Master and slave mode can be switched
  • Bluetooth name: HC-05
  • Password:1234

Pairing: The master device can not only make pair with the specified Bluetooth address, like cell-phone, computer adapter, slave device, but also can search and make pair with the slave device automatically.

Typical method: On some specific conditions, master device and slave device can make a pair with each other automatically. (This is the default method.)

———-

In this project, we opted for a Bluetooth connection because it’s simple to configure. This works as a serial terminal for Arduino and it will be connected to the pins TX and RX.

There are some rules for successful data transmission via Bluetooth. We need to have:

  • A start bit (usually has the value of logic 0)
  • Data bits
  • Parity bits (sum of data bits; we will compare the sum from beginning end end)
  • A stop bit (in most cases has the value of logic 1)

PIN connection:

  1. Connect the TX pin from HC-05 to the RX pin of the Arduino.
  2. Connect the RX pin from HC-05 to the TX pin of the Arduino.
  3. Connect the GND pins together.

 

Detailed instructions on HC-05 setup is covered in our previous tutorial Make Your Own Arduino RFID Door Lock – Part 2: Unlock Using Your Smartphone. If you have trouble connecting the Bluetooth module, please refer back to the above tutorial.

automatic pet feeder

Figure 2: Connection between HC-05 and Arduino Uno.

Code for Bluetooth Transmission:

 

 

The algorithm for this code is very simple: we initialize serial and wait for the port to open. We’ll send our commands through this. If it doesn’t become available, the program will not follow its flow and the “feed” command will not be sent to the microcontroller and not be processed.

The program also compares the string from the “voice” variable and from the string read on the serial. If the two are equal, a command is sent to the motor and it triggers the SG90 servo motor.

automatic pet feeder

Figure 3: HC-05 Bluetooth module connected to Part 1 setup

Step 3: Designing the app

Now, let’s create an app! As before, we’ll be using MIT App Inventor. Our ultimate goal is to create a suite of apps connected in one application (e.g. Smart Home App that integrates with multiple connected devices).

For MIT App Inventor setup guide, please refer back to the previous tutorial Make Your Own Arduino RFID Door Lock – Part 2: Unlock Using Your Smartphone (Step 3: The application). The tutorial walks you through step-by-step on creating your own app using the App Inventor.

The app we’ll be creating here will have a simple interface, which will include the following features:

  • Connecting to the Bluetooth
  • Feed: it allows you to feed your pet remotely
  • Store: it stores the data (as a file) in your phone
  • Show the date: it shows the date information on the screen of your phone
automatic pet feeder

Figure 4: A simple user interface of Pet Feeder App

 

The block diagrams for this program is very simple and easy to understand:

  • 1st Block: The first block is used for the Bluetooth button
    • ListPicker – MIT App Inventor
      A button that, when clicked on, displays a list of texts for the user to choose among. The texts can be specified through the Designer or Blocks Editor by setting the ElementsFromString property to their string-separated concatenation (for example, choice 1, choice 2, choice 3) or by setting the Elements property to a List in the Blocks editor.
    • ListPicker displays all the bluetooth available; this functionality works before you pick an element
automatic pet feeder

Figure 5: 1st Block

  • 2nd Block: this is used to make a connection between the app and the client with a function call BluetoothClient1.Connect address. Your phone’s Bluetooth will search for devices nearby and show them in a ListPicker. You can select the device you’re trying to pair with.
automatic pet feeder

Figure 6: 2nd Block

This also has a label where a message will be displayed after the connection is made. You will see a message “Connected” on the screen if the device is successfully connected.

  • 3rd Block: it sends a message through the client, only in case of connection. This text is sent through the Bluetooth from a device to another via Serial Communication. It’s like typing a text in Arduino’s Serial Monitor.
    When we’re reading from serial, we are comparing the input from a user with a stored string which is stored in Arduino’s memory. It’s how this algorithm works.

 

automatic pet feeder

Figure 7: 3rd Block

We’re going to take a look at another set of blocks:

  • 1st Block: it saves the date from the Bluetooth transmission to a .txt file, which is stored in the phone’s memory.
automatic pet feeder

Figure 8: 1st Block

  • 2nd Block: it reads the data saved in the text file when the button is pressed.
automatic pet feeder

Figure 9: 2nd Block

  • 3rd Block: it writes the time of the feed and the tag on the screen. This information is useful because it helps us keep track of time for feeding and in case we don’t want our pet to be feed anymore, we can check the time.
automatic pet feeder

Figure 10: 3rd Block

  • 4th Block: when error occurs, it will delete all the data from the .txt file. It is important to do that because it will no longer show the previous information.
automatic pet feeder

Figure 11: 4th Block

The second part of the app introduces different feeding modes: a normal feed mode, a feed mode for baby pets and a feed mode for for adult pets. This also offers you information about how much food you need to provide to your pet. One of THE coolest functions it has is the Speech Recognition mode. We’ll discuss more about this function below.

automatic pet feeder

Figure 12: Incorrect date and time shown on app

If you want to find out the day and time of the feeding, you can to press the button ”Show the date”. The app was made in thin mode because not everyone want to see all the information and the screen looks much cleaner that way. As you can see in the picture, the date and time is not shown correctly. In order to get the exact date and time, we need to use the example Set Time from Arduino IDE. The RTC module will now indicate the correct day & time.

automatic pet feeder

Figure 13: How to find SetTime in Arduino IDE



 

Figure 14 shows a final version of the app:

automatic pet feeder

Figure 14: Final version of the app

Overview of the app:

  • Needs to be connected to Bluetooth
  • Features different feeding modes depending on your pet’s age.
  • To activate speech recognition mode, all that is needed is to click on the “Speech Recognition Feed” button and speak.
    • Only when the command is spoken correctly, the feeder works. If the keyword is incorrect (it wasn’t recognized properly), it will only show on the label without executing the command.
    • In case of speech recognition, when the command “feed” is spoken/repeated twice (e.g. You didn’t wait few seconds for the system to process the information), it results in “feedfeed,” which isn’t a valid command. It will remain on the label without executing it.
    • If the command “feed” on the speech recognition works correctly, it will print the time on the label.
  • Also included is a feeding guideline diagram that you can use to find information about the quantity of food provided depending on the pet’s weight.
automatic pet feeder

Figure 15: Pet feeding guidelines / ©Fish4Dogs

All the command will also be found on the Serial Monitor of the Arduino IDE. This helps us debug the app when necessary.

automatic pet feeder

Figure 16: Feed data shown in the serial monitor

For speech recognition, we need a button to activate this mode. We can use SpeechRecognizer component already provided in the App Inventor.

 

automatic pet feeder


Figure 17: Adding SpeechRecognizer component on MIT App Interventor.

After you have those two components, it’s very simple to connect them. You need to process the text originated from the speaker. This is done with Call SpeechRecognizer. GetText. After this, you need to have a label where you can display the text to see what you said – it can be done without it, but you won’t be able to see if you said the correct command or not. In the loop you also need to transfer the speech command to the Arduino via Bluetooth; you need to use SentText text procedure.

automatic pet feeder

Figure 18: Speech recognition blocks

For every mode, you need to have buttons. Each has a different command which will be sent to the Arduino, then different amount of food will be released.

automatic pet feeder


image041Figure 19: Small Pet, Normal Feed and Adult Pet blocks