Normally, getting splashed in the face with beer is not a good thing. I am going to show you how using your face with beer can be a good thing. This blog is a continuation of my previous blog on using IoT with a Beer Kegerator (link).
Deploying the first version of the IoT app was well received, and there were a lot of great feedback suggestions on how to improve the experience. I wanted to showcase some of the newer Microsoft technologies, and ended up choosing facial recognition support for the app. This would have great benefits, as it would allow accurate tracking of who is pouring the beer and when they requested a pour. Coupled with the volume tracking of the selected beer, this would provide excellent data capabilities on who is drinking beer, when the beer is poured, along with the consumption rates of the different beers provided.
I also wanted to expand the app to include all four beer lines that we have available, so this required a bit of a redesign of the app. In order to do this, I needed to have the app enable/disable any of the four lines when someone requests to pour a specific beer.
First off, I purchased the additional three flow sensors so that I had the ability to track volume on all four beer lines. Modifying the code to account for this was simple, as it was just a matter of designating additional GPIO pins to receive the digital pulses.
More interesting is how I was going to handle the selection of beer when a pour is requested. I needed to verify that only the selected beer was able to be poured, while the other lines would not work. This meant providing the ability to disable each of the beer lines when necessary. I solved this problem with a food-grade quality solenoid valve, which allows opening/closing control of the valve via an electrical signal.
This valve is normally closed, which means that when powered off the valve is closed, thereby stopping the flow of beer in the line it’s connected to. Providing power to the valve (12 volts) opens the valve as long as the power is applied to it.
The next question is how do I control the 12 volt signal to each of the solenoid valves connected to the four beer lines? This can be done with a simple relay switch, which allows a control signal to toggle the 12v on or off to the solenoid valve. I found a simple circuit board with four relays on it:
This will work perfectly for my situation, as I can use 5v control signals from the raspberry pi to turn on/off the different beer lines (activating a solenoid via the relay board) when a pour is requested.
With the hardware issues resolved, we now turn to the changes needed in the software. In order to support and control four beer lines, changes are needed in both the IoT app on the Raspberry Pi, as well as creating a web app to handle administration.
IoT Kegerator App
The main changes to the IoT app are centered around how to display the four different beer lines that a user can choose from, along with the process to select a beer and have the facial recognition routines recognize the person, so the selected beer line can be opened for the pour.
The previous version of this app only handled one line, so I needed to leverage the touchscreen I am using with the Raspberry Pi, in order that a person can touch an icon of the beer they want.
This will start the facial recognition process so they can be recognized. Once the app recognizes them, it will open their selected choice for 30 seconds for them to pour their beer. After 30 seconds, the system will close the line and be ready for the next person.
The following code snippet shows how the app processes a request from a person, when they touch one of the beer icons:
The first thing this method does is display the associated web camera feed and then calls the facial recognition routine to take a snapshot of the webcam and process it for recognition. If it returns with a valid registered person, the routine will open the solenoid valve for that beer line ONLY, by setting a GPIO pin which will drive a specific relay, which in turn enables the 12v signal to the solenoid valve to open it. The method will then set the 30 seconds and start a timer to count down to 0, where it will then reset for the next time.
This timer method runs every second, and records the digital pulses coming from the open beer line flow sensor, for the 30 seconds the beer line is open for. After it reaches the end of the 30 seconds, it will then take the total of the recorded digital pulses and send that info to Azure for saving into the database. The method will then reset some counter variables and turn itself off.
The final change needed for the IoT Kegerator App is adding support for facial recognition. Since I wanted to showcase Microsoft technology as part of this app, it made sense to leverage the Azure Cognitive Services suite of functionality that they provide (more info here). Specifically, I will be using the Face API that is part of the Computer Vision area.
The Face API is designed to detect and analyze faces/facial features in images. This will work for my implementation, as I can save the picture that is taken by the webcam when a person is requesting a beer. The trick then is to compare that image taken from the webcam against a “trained” model of images for a registered user. If recognized, the API reports that back to the app, which in turn will open the beer line for the person. Here is a code snippet showing how the Face API is called and leveraged to recognize a face when the app takes a picture.
Please note that the CustomPerson class is based off the Face API Person class. The UploadandDetectFaces method calls the Face API to detect any faces in the webcam picture, and returns an array of detected faces. I then call the IdentifyAsync method against the array of detected faces to see if any correspond to any registered user face models that I trained previously. If the Face API determines there is a match I get additional details about the recognized face and return that to the calling method.
Here is a screenshot of the final redesigned app (with hardware):
In conclusion, now that I’ve implemented this latest version of the IoT Kegerator app, I am able to track the consumption of each beer line, along with how much beer each registered user is consuming. I also have the ability to track time series data on when beer is being consumed, along with consumption rates of each beer on the four lines. Here is a quick example of a monthly consumption report:
Here’s to getting a beer to the face!