A smart cat flap

Hello and welcome to a new and exciting two-part blog series. This short series is about our 4-legged (favorite) roommates. We mean cats, even if dogs are of course also part of it 😊

This time a colleague gave me the idea for this blog. He reads my blogs from time to time and is a big smart home fan himself. Now he recently asked me that he would like to have a notification on his cell phone when his cat comes home from your “forays” or is out on the streets . He told me that he was already looking for a cat flap with the appropriate functionality on the market but hadn't found anything suitable. Couldn't I build a solution to the problem? Of course I can and here it is:

The principle of our smart cat flap is based on the fact that our cat has to "push up" the flap in the door in a different direction (to the right or left) each time to pass through the flap when it comes in or out through the cat flap.

Since the direction of the flap movement depends on the path of the cat, we can get all the necessary information by simply arranging 2 buttons on the flap. The following diagram illustrates this in the basic mechanical / electrical functionway.


It can be seen that when walking through the cat flap, the cat either presses the "cat is coming" button or the "cat is going" button, depending on the direction in which it passes the flap.

These two "direction" buttons are wired according to the mechanical structure as in the following Fritzing plan:


Fritzing plan

Both buttons close shortly after plus (3.3 volts) when they are pressed. Pull down resistors of 330 KOhm otherwise pull the ports down.

The hardware list for this project is pleasantly clear:


  • 2x 330 KOhm resistance
  • 1x ESP32
  • 2x buttons
  • 1x power supply or power bank

Now that we have set up the hardware and the mechanics, it is time to configure or prepare our cell phone. To do this, we download the “Blynk” app from the App Store onto our mobile phone. The APP itself is free of charge and can also be operated for the described application without additional costs for the app.

Regardless of this, there are costs for data transmission from our cat flap to the cell phone. Please take these costs into account when building the project!

So after we have downloaded the Blynk app from the store and started it for the first time, we first have to create an account. To do this, we tap on "Create New Account"


Register Blynk account

We register with our own email address and assign a password:


Create a Blynk Account - 2


Then we click on new project:


Blynk - Create a new project


We end up in the dialogue about creating a new project. The basic data of the project must be entered here, e.g. the project name, our ESP32 development platform and the desired connection form. Various connection parameters can be given, such as Bluetooth or WLAN.

However, in order to be able to receive data while on the move, the GSM connection type must be selected. The settings that I have chosen for the project are shown below:


Blynk - Create new project - Project details


We confirm the information with "Create" and end up in an empty project.

We now add our first active element to our app using the "Plus" symbol in the title bar:


Add Blynk element


We select "Notfication" as the first element and configure the element.


Blynk - Add widget

Here are the details of the element's settings:


Blynk - notification settings

We confirm the settings with the arrow on the left, and then return to the main view of the APP:

Blynk - That's it!

That’s it!

We can now close the app again. Now we come to the microcontroller part. We now have to adapt our Arduino IDE and ESP code to our APP.

To do this, we first install the latest version of the Blynk library in the Arduino IDE via the library manager:

Install Blynk library in Arduino


Then we adapt the code to our project. Our code is adjusted in the following places:

// Global Variables
char auth [] = "11111111111111111111111111"; // Enter the auth token of your Blynk app according to the instructions (email). Auth token is a dummy!

// Your WiFi access data.
char ssid [] = "***********";
char pass [] = "**************"; // Set password to" "for open networks.

After the code has been adapted to your own WLAN and auth token parameters, it can be uploaded to the ESP:


#include <WiFi.H>
#include <BlynkSimpleEsp32.H>

// Port definition button
#define Cat_Go_SW  15    // cat goes
#define Cat_Comes_SW 2    // cat is coming

#define BLYNK_PRINT Serial
#define Switch_Poll_Interval 200
// # define BLYNK_DEBUG

// Global Variables
char auth[] = "11111111111111111111111111"; // Enter the auth token of your Blynk app here (email).

// Your WiFi access data.
char ssid[] = "***********";
char passport[] = "**************";  // Set password to "" for open networks.
unsigned long Switch_ServiceCall_Handler = 0;  // Delay Variable for Delay between Moisure Readings
bool State_Cat_Go_old = false;
bool State_Cat_Comes_old = false ;

void set up() {   pinMode(Cat_Go_SW, INPUT);   pinMode(Cat_Comes_SW, INPUT);   Serial.begin(9600);   // initialize serial communication at 115200 bits per second:   Serial.print(F("Connection to WLAN"));   delay(500);   Blynk.begin(auth, ssid, passport);  // Initalize WiFi connection over Blynk Library   Serial.println(F(" successful."));

void Run_Check_Switches ()   //
{   bool State_Cat_Go = false;   bool State_Cat_Comes = false;   if (millis() -  Switch_ServiceCall_Handler >= Switch_Poll_Interval)   {     Switch_ServiceCall_Handler = millis();     State_Cat_Go = digitalRead(Cat_Go_SW);     State_Cat_Comes = digitalRead(Cat_Comes_SW);     if (State_Cat_Go && (!State_Cat_Go_old))     {       State_Cat_Go_old = true;       Blynk.notify ("Note: cat goes");       delay(1000);     } else if ((!State_Cat_Go) && State_Cat_Go_old)     {       State_Cat_Go_old = false;       delay(1000);     } else if (State_Cat_Comes && (!State_Cat_Comes_old))     {       State_Cat_Comes_old = true;       Blynk.notify ("Note: cat is coming");       delay(1000);     } else if ((!State_Cat_Comes) && State_Cat_Comes_old)     {       State_Cat_Comes_old = false;       delay(1000);     }   }

// Main loop
void loop()
{   Run_Check_Switches();   Blynk.run();   // Execute Blync basic functions




If we now briefly connect port 2 to Plus for testing, we receive the following message on the cell phone:

Function test: cat is coming

Now we connect port 15 briefly with plus for testing. We should now see the following message on the phone:

Function test - cat goes. : '(

Now all we have to do is attach the electronics or mechanics to the cat flap and now we always get a message when our room tiger uses the flap. I hope you will enjoy copying.

Until the next part of the series and have fun building it!


Esp-32Projects for advancedSmart home




Stichwort Smarte Katzenidentifikation

Hallo zusammen,
zu uns kommen mehrer Katzen durch eine Katzenklappe (Klappe ausgebaut) zum Fressen in den Schuppen, eigene und fremde. Ich würde nun gerne wissen wollen, welche Katze wann in den Schuppen kommt (raus ist erst einmal egal). Als Idee hätte ich, dass 1. Fotos von der hereinkommenden Katze gemacht werden (Bewegungsmelder), auch nachts, und 2. mittels RFID-Scanner an der Öffnung der Chipcode ausgelesen wird. Fotos, RF-ID-Code und Uhrzeit sollten auf einem Webserver gespeichert werden, wo ich sie über Handy auslesen kann (bestenfalls auch per ESP im eigenen WLAN umgesetzt). Zur Not auch über eine Iot-Cloud.
auch mit Zeitstempel zuordnenbar sein.
Mit dieser Methode könnte ich nämlich auch Katzen identifizieren, die nicht gechipt sind und ggf. Anwärter auf eine Kastration/Chippung wären.
Vielleicht habt ihr ja eine schnuckelige Idee für eine Umsetzung per ESP32-CAM mit OV2640 und weiteres Zubehör (Bewegungsmelder, RFID-Reader) dafür? Würde mich freuen.
Viele Grüße, Thorsten



Echt eine tolle Sache!
Würde es auch gerne mit dem RFID Chip am Halsband realisieren, da die Katze eh schon einen hat, um die Klappe zu öffnen.
Wenn es dafür auch noch eine Lösung gäbe, wäre es echt ein Traum.
Vllt. mit 2 RFID Lesern. Einer kommt ganz an den Anfang der Klappe und einer an das Ende.
Je nach dem in welcher Reihenfolge die Leser ausgelöst werden, ist die Katze dann eben drin oder draußen.

Dirk Zwijas

Dirk Zwijas

Wäre das ganze nicht leichter mit einem RFID Chip im Katzenhalsband zu realisieren ?

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN