drapeau fr

chatiere If you like pets, maybe you have a cat. What can it do when I work or the night ? I had this question that's why I wanted to monitor the cat flap.

For this system I used


Below, the synoptic
synoptic

Differents parts

The sensor

chatiere For the position sensor, there are many possibilities. I made a choice but now I think that it was not the best. I just took a sensor in my possession in my garage. The aim was to avoid mechanical sensors. With a dirty cat that it come in and out, dust may easily block the system.

The power supply of the IC is 5V. The output is centered on 2.5V. The signal increase when it sees the North Pole and decreases when it sees the South Pole. The main problem with this solution is : When the flap is fully opened, there are no magnetic field and the signal is 2.5V (the same that a closed flap).

The magnetic position on the flap gives the following transfer function
fonction de transfert

As we can see, the function is not bijective. In true language, it means that for a given voltage, there are several positions possible. By example at 2.5V, the flap could be fully opened or closed. I had some issues because of this phenomenon. I did not plan that the cat likes staying inside more than 2 minutes before go out.


The interface board

The PCB is a prototype board.
carte microcontroleur schéma carte uc
Input : analogic signal 0-5V centered on 2.5V

Output: two digitals signals 3.3V S1 and S2. : S1 is high when the cat goes out , et S2 when the cat comes.

The truth table is below
U S1
Output
 S2
Input
>4.5V 1 1
3V to 4.5V0 1
2V to 3V 0 0
0.5V to 2V 1 0
< 0.5 V 1 1

When an event occurs, the signal is hold on during one second. I use this feature to remove the unwanted glitchs on the raspberry Pi

The raspberry pi

For this part, I thank a person, he made a very good job on his blog : Blog d'Idleman (in french).

The microcontroller board is connected to the expansion connector of the raspberry pi
S1 on GPIO24
S2 on GPIO25
The ground of each board are connected together

I wrote the script in python, based onRasPi.Tv
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# script based on by Alex Eames http://RasPi.tv
# http://RasPi.tv/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-                                                                         gpio-part-3
import RPi.GPIO as GPIO
import time
from subprocess import Popen

PinIn = 25
PinOut = 24

GPIO.setmode(GPIO.BCM)

# GPIO 24 & 25 set up as inputs, pulled up to avoid false detection. 
# In case of no signal input # We'll be setting up rising edge detection for both
GPIO.setup(PinIn , GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(PinOut , GPIO.IN, pull_up_down=GPIO.PUD_UP)


# now we'll define two threaded callback functions
# these will run in another thread when our events are detected
def my_callback(channel):
    print "Sortie %d" % PinIn
    my_upload(0)

def my_callback2(channel):
    print "Entree %d" % PinOut
    my_upload(1)


## envoie les donnees le serveur free
def my_upload(value):
   cmd = "wget -q -O - http://lyonl.free.fr/pi/chatiere.php?type=%d" %value
   p   = Popen(cmd , shell=True )

# when a rising edge is detected on Pin_In, regardless of whatever
# else is happening in the program, the function my_callback will be run
# we add a bouncetime but it should be useless as we have no press button  
GPIO.add_event_detect(PinIn , GPIO.RISING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(PinOut , GPIO.RISING, callback=my_callback2, bouncetime=300)


try:
      while True:
         time.sleep(1)


except KeyboardInterrupt:
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
    print "fin de programme"
GPIO.cleanup()           # clean up GPIO on normal exit


The script is based on interrupt vector. It does not need CPU ressources. It calls a PHP script on an external website. It logs the event in a database. It is possible to record it on the raspberry itself, however for a safety reason; I don't want to have a website reachable from internet.


The website

I built two php scripts

Conclusion

The process is simple but long. I had to program with different languages. C for microcontroller, python for the raspberry pi and PHP for the website. If you have better ideas, you can write a message on the blog.

I had some issue because of the cat behavior. Sometimes it stays in the flap for a while. The sensors sees then two outputs. I made a software filter to debounce.