At Yoctopuce we have a small refrigerator that is used to keep the stock of solder paste, a few bottles of water and occasional picnics cool. We've noticed that the little flick of the wrist we all usually give the door isn't always enough to close it. It then stays open for hours before anyone notices. We suspect this is a result of the heavy water bottles stored in the door. We could probably solve the problem by slightly tilting the fridge with wedges, but a Yoctopuce overkill was much more fun...
Objet the crime
The heart of the problem is to detect if the fridge door is properly closed or not. Rather than using a mechanical switch, we decided to use an optical solution, which is easier to develop and probably more reliable over time.
First tests
The idea is to use a Yocto-Proximity to detect the door. We separated the sensor part and used Yocto-Visualization to determine the best location to measure if there is a gap between the fridge body and the door seal.
We finally chose to place the sensor in the upper corner of the fridge body, on the opposite side of the hinge, and facing downwards. At this point, in theory, we measure the distance between the top of the fridge and the door seal when it is closed or the floor when the door is open.
the best position for the sensor
The reality is a little more nuanced because of light reflections, but we can still see perfectly when the door is left open.
One can easily detect if the door is properly closed or not
Moreover, mounted in this place the sensor remains hidden and protected from shocks and dust.
Realization
After a closer examination of the fridge, we noticed that not only the sort of worktop on top of it can be easily removed, but also that there is quite a lot of free space under it, space in which we can easily hide most of the electronics. Placing the main part of the Yocto-Proximity and an old YoctoHub-Wireless-SR that was lying around was a piece of cake. Note that we could have very well used a YoctoHub-Wireless-n with a small patch antenna.
The electronic parts, nothing fancy
To signal that the door is not closed we chose to use a Yocto-MaxiBuzzer that we simply magnetized on the side of the fridge, it is a little too big to be hidden under the worktop.
Optional improvement
It turns out that Yocto-MaxiBuzzer has an RGB LED driver. We couldn't resist adding an LED strip in the free space just above the door. We only have one fridge, but we imagine if we had a whole row of them, we'd be pretty happy to be able to tell at a glance which one has a problem.
We added some LEDS, just for fun
Programmation
The programming part is relatively trivial: a simple python script of four dozen lines is enough to handle the problem.
from yocto_api import *
from yocto_proximity import *
from yocto_buzzer import *
from yocto_colorledcluster import *
# IP address of the hub handling the devices
hubAddr = "192.168.1.98"
# Some quick and dirty global variables
maxOpenTime = 15
doorIsOpen = False
openSince = datetime.datetime.now()
# automatically called when the sensor value changes
def proximityValueChangeCallback(fct, value):
global doorIsOpen
global openSince
isOpen = int(value) >10
if not (doorIsOpen) and isOpen:
print("Door is open!")
openSince = datetime.datetime.now()
doorIsOpen =True
elif not (isOpen) and doorIsOpen:
print("Door is closed.")
doorIsOpen =False
print("Starting fridge's door monitoring...")
YAPI.DisableExceptions()
errmsg = YRefParam()
# Setup the API to use the remote hub
if YAPI.RegisterHub(hubAddr, errmsg) != YAPI.SUCCESS:
sys.exit(hubAddr+"init error" + errmsg.value)
# look for proximity sensor
proximitySensor = YProximity.FirstProximity()
if proximitySensor is None:
sys.exit("No proximity sensor found on "+hubAddr)
# look for buzzer
buzzer = YBuzzer.FirstBuzzer()
if buzzer is None:
sys.exit("No buzzer found sensor on "+hubAddr)
# look for led driver, but don't complain if not found
ledCluster = YColorLedCluster.FirstColorLedCluster()
# We definitely want to hear the buzzer -> full power
buzzer.set_volume(100)
# install a callback, automatically called when the door moves
proximitySensor.registerValueCallback(proximityValueChangeCallback)
# main loop
while True:
if doorIsOpen and (datetime.datetime.now()-openSince).total_seconds() >maxOpenTime:
if not(ledCluster is None ): ledCluster.set_rgbColor(0,12, 0xFF0000 );
buzzer.playNotes(",C32^ C32^ C8")
openSince = datetime.datetime.now()
if not(ledCluster is None ): ledCluster.rgb_move(0, 12, 0x000000, 5000);
YAPI.UpdateDeviceList(errmsg) # traps plug/unplug events
YAPI.Sleep(500, errmsg) # traps others events
The result is quite effective, as long as the door remains open, a resounding beep is heard every fifteen seconds and a red flash is clearly visible above the door.
The door was left slightly open, the fridge is not happy
Conclusion
Thanks to this little tinkering done in a few hours, the fridge door is now closed properly. If we were a bit curious we could use the Yocto-Proximity data logger and Yocto-Visualization to know when the door was opened. Finally, let's note that if the temperature inside the fridge were mission critical, we could have easily added a temperature sensor to the system.