Introduction: IoT Cat Feeder Using Particle Photon Integrated With Alexa, SmartThings, IFTTT, Google Sheets

The need for an automatic cat feeder is self explanatory. Cats (our cat's name is Bella) can be obnoxious when hungry and if your cat is like mine will eat the bowl dry every time. I needed a way to dispense a controlled amount of food automatically by 3 methods - 1. Alexa voice command, 2. by a schedule, and 3. by a button in SmartThings. I also wanted a way to track how and when the cat gets fed so we don't feed twice (sometimes when when my wife feeds the cat and then I feed the cat again 15 minutes later).

This project is not overly difficult, I am no developer, nor an engineer, but there was many resources online that helped me. I'm compiling everything I used and all the steps necessary for you to complete this project too. Enjoy, and happy building!

Step 1: Get All Your Needed Materials - Roughly $100

Here's everything I purchased to make this cat feeder. It was about $100 which is a little more than my planned $75 but went for some better quality parts to ensure it will last. Some of materials I already had to make the box like wood glue, hot glue gun, 18ga nailer, sand paper, wood filler, etc. These fall under "tools" in my opinion and are not all necessary. I just go above and beyond sometimes on making things pretty.

If you deviate from these exact products, just make sure your servo has a metal gear and note the number of teeth because your servo arm/horn will need to have the same number of teeth. The servo and arm listed below are 25T, meaning 25 teeth. Also, I used a plastic arm on the first go-around and the teeth stripped within a week of use so don't go the plastic route, get metal.

1. Particle Photon $19.99 http://a.co/flaQM0Z

2. Breadboard and Jumper wires $7.69 http://a.co/iJuFpXp

3. Servo $19.99 https://www.frys.com/product/7027291

4. Metal servo arm/horn $8.98 http://a.co/e9eWOvx

5. Piezo Buzzer $5.05 http://a.co/9iOP2hq

6. Cereal Dispenser $14.95 http://a.co/6c2xpVq

7. Fiber board from hardware store for the box, 2 pieces just in case you mess up $20 https://www.homedepot.com/p/1-2-in-x-2-ft-x-4ft-Me...

8. Rustoleum Hammered Spray Paint $5.97 http://a.co/alqqR8n

8. Misc. items like screws, wood glue, nails, sand paper, wood filler, etc.

Step 2: Set Up Particle Photon

The first step is connecting your Particle Photon to the internet. This is very easy, especially with the guides provided by Particle. Download the Particle app onto your phone, create a particle account, and folllow the guide to getting it connected to your wifi. Watch the Video and follow these instructions!

https://docs.particle.io/guide/getting-started/start/photon/

Step 3: Connect Particle to Servo, Breadboard, and Buzzer

Ok, now we need to wire up the particle to the servo and buzzer using a breadboard. Attach your particle to the breadboard leaving 2 pin holes on each side, it doesn't really matter where the particle is placed outside that. I soldered my wires to the buzzer and then used hot glue to secure it. A simpler method would be to use terminal connectors or butt connectors and crimp them with pliers. Up to you.

Connect the servo wires:

VIN Red servo wire

A5 Yellow servo wire

GND Black servo wire

Connect the buzzer wires (doesn't matter which is which):

D0 Buzzer wire

GND Buzzer wire

Step 4: Flashing Code to the Photon

Now that you have it wired up, lets flash the firmware code to the photon and test that the buzzer plays a melody and the servo turns.

Login to the particle build console using your account https://build.particle.io

Click the code <> button on the left side toolbar. Copy and paste the firmware code provided (cat_feeder.c) into the build console, replace any existing code there by default.

A little explanation on the feed function. Since feeding can be invoked by 3 different methods (button, voice, schedule) we need 3 different commands in order to publish each event so we know how the cat was fed. These commands are "bfeed" when fed by the button, "sfeed" when fed by schedule and "vfeed" when fed by voice command.

Click the Folder looking button to save your code, give it a name of your choosing.

Click the verify button (it looks like a check mark with a circle around it). After a few moments you should see in the bottom status bar:

Compiling code... Code verified. Great work! Ready.

Now, Click the Flash button on the left toolbar (looks like a lightning bolt). After a few moments you should see in the bottom status bar:

Flashing code... Flash successful! Your device is being updated. Ready.

Next, login to the particle console https://console.particle.io/

Click on your Particle Photon. This is a status page for your device, you can see in the Event Logs when actions are taken, such as flashing code or when events are published from invoking the feed commands. To initiate a feed command you will use the Function tile on the right side of this console. Since the function is called "feed" in the code, you should see it and an argument input box.

Type "bfeed" in the argument input box and click Call.

This should play the melody and then turn the servo. If you want, for testing purposes, change the delay time in the code from 330 MS to something longer like 5000 if you want to verify the servo turning. You need to click the Flash button every time you make any changes to the code. You will probably need to adjust the delay later on to get the desired amount of turn (such as feeding a dog, you might want to dispense more food).

Step 5: Make a Box to House Everything

This step consists of your good ol fashion carpentry skills. I purchased 2 -2x4ft fiber boards to make the box because its strong, smooth to the touch, relatively cheap, and easy to cut/work with. You can choose any material. This step is really at your discretion in how it's built because everyone has different needs. I won't have a detailed step by step for making the box but I can share the thought process and methods I used.

There was a number of factors I took into consideration for designing the box. I wanted this thing as compact as possible. I wanted it easily serviceable for cleaning and/or fixing it if it ever malfunctioned. Lastly, I wanted the dispenser tube exposed so I can visibly see when the food is running low. With this in mind I made the box first and then measured the inside dimensions to make a square frame which the the dispenser unit, servo, and board are attached. This way the entire unit can be lifted right out of the box for servicing. See the pictures.

Step 6: SmartThings Device Handlers

This step explains how to implement calling the feed function using SmartThings and Alexa. To do this I had to write 2 groovy device handlers. One of the handlers is for "bfeed" when I press the button in SmartThings and the other handler is for "vfeed" when I ask Alexa to feed Bella by voice command. Another reason to have 2 different handlers is that Alexa only recognizes devices that are switches, not buttons. So I had to trick Alexa into recognizing a switch in order to invoke the function (off and on perform the same action). Ok, let's get started.

Login (or create an account) to the IDE SmartThings portal at http://ide.smartthings.com using either your Samsung Account or SmartThings account.

Confirm your SmartThings hub shows up under "My Locations" and "My Hubs". If it doesn't show, troubleshoot this before continuing.

We will create the Button Handler first.

Click on "My Device Handlers" on the top navigation bar.

Click on blue button "Create New Device Handler"

Select "From Code" tab and paste the code in from the file attached button_handler.groovy. Then Click Create.

Click "Save" and "Publish" --> "For me". You should see a green notification appear that says, "Device type published successfully."

Now, jump over to your phone and open the SmartThings app. You should see the handler appear as a new device. You'll need to configure the handler so it posts to your particle device. Select the newly created device and open the Settings (looks like a gear icon).

Enter Device Name - Name the device whatever you choose, I'm sure your cat is also not named Bella.

Enter Access Token - The access token is going to be found in the Particle Build console https://build.particle.io/ under the Settings (gear icon on bottom left). The Access Token will be a 40 character string.

Enter Device ID - The device ID is found on the Particle Console page as a 24 character string and is also seen in all the particle URL's when you are logged into your account working on your particle device. Device ID seen BOLD in the URL: https://console.particle.io/devices/83900082840c382000132441

Click Save.

Click the Feed button!

By clicking the feed button you should hear the melody and get the servo to turn. You should also be seeing events published in the particle console event log.

Next, we will create the Device Handler for Alexa.

Follow the same procedure as above. Go to the IDE SmartThings portal and Click on "My Device Handlers" on the top navigation bar.
Click on blue button "Create New Device Handler"

Select "From Code" tab and paste the code in from the file attached alexa_handler.groovy. Then Click Create.

Click "Save" and "Publish" --> "For me". You should see a green notification appear that says, "Device type published successfully."

Now, jump back over to your phone and open the SmartThings app. You should see the handler appear as a new device but this time without any button. You'll need to configure the handler with your Access Token and Device ID just like you did previously.

Select the newly created device in SmartThings and open the Settings (looks like a gear icon).

Enter Device Name - Name the device whatever you choose, I called it "Cat Feeder Handler for Alexa" so my wife knows its not the Cat Feeder button.

Enter Access Token and Enter Device ID, then click Save.

Now, open your Alexa App on your phone. If you haven't already done so before, enable the SmartThings Alexa Skill and link your SmartThings account to the Alexa App. Then go to the Smart Home screen and click "Add Device". This can also be done by saying "Alexa, discover devices". A device should show up with a switch icon, it will be named whatever you named the handler in SmartThings. You can change the name of it in the Alexa app if you want but remember that this is a switch type so it will require saying "Alexa, turn on" or "Alexa, turn off". I know this isn't nice to say "Turn on the cat feeder" so an alternative is to create a new routine in the Alexa app and call it with the phrase you want exactly. I made a routine called "Feed Bella" which turns on the cat feeder switch.

By clicking the switch in the Alexa app, you should now be successfully calling the feed function. The Particle Console event log should also post the events indicating so.

Once you have everything named the way you want it go ahead and give it a try!

Step 7: Putting It All Together

The last piece I haven't covered is how to schedule the feeds and record the published events in a Google Sheet. You are probably thinking the Alexa App can be used to make a routine for the schedule but this wouldn't allow us to log schedule vs voice command or button. So instead, there is an IFTTT applet for particle already that we will use.

Let's create the IFTTT applet for scheduling feeds.

Go to https://ifttt.com or download the IFTTT smartphone app and login (create an account if needed)

If you are new to IFTTT its very straight forward. Every applet consists of an IF (a condition or trigger) and a THAT (some action to do).

So once logged in to IFTTT, create a new applet, select IF and search for or select "Date and Time". You configure this to whatever schedule you prefer. Then proceed through the steps and once you get to the THAT you will search for "Particle" and select "Call a Function". Fill out the fields as in the screenshot above, make sure to use "sfeed" as the input as this is the command needed to publish a "Fed by schedule" event. You may need to connect or login to your particle account while setting up this applet. Save the applet and you should be good to go. If you want to test, just change the date and time to near in the future and see if it triggers properly.

Now to create the IFTTT applets for handling the 3 event types.

There are 3 commands we need to account for when the cat feeder runs. These are hard coded in the particle firmware:

Bella fed by schedule
Bella fed by button
Bella fed by voice command

If your pet is not named "Bella" you probably want to change this in the firmware, and re-flash. Change the text in the Particle.publish line of the particle code.

Create a new IFTTT applet. (need to do this 3 times, for each event type)

Starting with the IF, search for Particle and select "New event published". The Event Name will need to be the exact Published Event Name that you have in your firmware. For example "Bella fed by schedule" is the exact event name in the code I provided you. Check out the screenshot as I filled it in for reference. You will need to create 2 more IFTTT to handle the other events too. This applet will create a new sheet in your google drive if it doesn't exist already and log new events to the next empty row.