How to Build a Raspberry Pi Smart Beer Fridge
How do you take a perfectly good mini fridge and make it smart? One way is to have the refrigerator tell you how many bottles are in it and how many bottles you’ve drank. You can view this information on your laptop, tablet, and phone. It can send you notifications when your fridge is getting low. This is the smart fridge of the future!
This project is a way to build your first smart fridge that is easy, low cost, and hopefully fun.
What you’ll need to get started:
- (2) Wii Balance Boards
- (2) Wii Fit Rechargeable Battery Packs
- (2) Raspberry Pi’s (3, 4, or WH) with micro SD cards & chargers
- Initial State account
- a mini fridge full of beer or wine or soda
Wii Balances Boards
We need more than just a scale. We need a scale that we can control and read with a Raspberry Pi and that will fit neatly under your refrigerator while keeping it stable. We need a scale with bluetooth that can communicate with the Raspberry Pi. We need a Wii Balance Board! We actually need two.
Let’s test connecting both Wii Balance Boards with the Raspberry Pi’s. Decide which board will go with which Pi. You’ll connect each board & Pi individually.
Take your first pair and let’s start. Type the following command into the Pi:
$ cd ~
$ git clone https://github.com/InitialState/smartbeerfridge.git
Cloning into 'smartbeerfridge'...
remote: Counting objects: 12, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 12 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (12/12), done.
You should see two python files in the new smartbeerfridge directory — smartbeerfridge.py and wiiboard_test.py.
$ cd smartbeerfridge
$ ls
README.md smartbeerfridge.py wiiboard_test.py
Run the wiiboard_test.py script to test communication and take weight readings from the Wii Balance Board:
$ python wiiboard_test.py
You will see the following response:
Discovering board...
Press the red sync button on the board now
Remove the battery cover underneath the Wii Balance Board to locate the red sync button. Make sure you press the button within a few seconds of running the script or a timeout will occur. Once successful, you will see something similar to the following:
Found Wiiboard at address 00:23:CC:2E:E1:44
Trying to connect...
Connected to Wiiboard at address 00:23:CC:2E:E1:44
Wiiboard connected
ACK to data write received
84.9185297 lbs
84.8826412 lbs
84.9275927 lbs
The wiiboard_test.py script is taking the number of weight measurements specified on line 10 and outputting the average:
# --------- User Settings ---------
WEIGHT_SAMPLES = 500
# ---------------------------------
You can play with this number by changing the value and re-running the script to see the variation in weight measured and time required for each measurement. Weigh yourself, weigh your dog, weigh whatever and see if the measurements make sense. To stop the script, press CTRL+C.
You’ll need to repeat this step on your second set of Pi & Wii Balance Board.
Nintendo assumed you would always power your Wii Balance Board with four AA batteries and included no AC power adapter. Having only battery power would be a non-starter for this project considering the inconvenience of getting to the battery pack once it is under an 80+ lbs refrigerator. Luckily, there are several third-party adapters made for the Wii Balance Board that we can use to provide constant power from a wall outlet. The Wii Fit Rechargeable Battery Pack is a perfect solution to our power problem. Replace the batteries with this battery pack, and plug the ac adapter into a wall outlet.
Having to pair the Wii Balance Board and Raspberry Pi every time we run our Python script presents a problem due to the location of the sync button. The sync button is inaccessible with a refrigerator sitting on top of it. We can fix this by making a hacky little lever using a pencil and three 3/8" felt pads as shown above. The rechargeable battery pack exposes the sync button to the underneath surface of the Board. Tape a pencil (or something similar) that spans from the sync button to the outside front of the Board. Stack three 3/8" felt pads (or something similar) on the center (ish) of the pencil to create a stationary pivot. Be careful to not expose too much of the pencil out from the Board as you don’t want someone to accidentally kick it out. Flip the Board over and you can press the sync button by simply pressing down on the lever. Hacky but effective.
Depending on the surface that your refrigerator sits on, you may need to remove the rubber grip pads from the feet of the Board (the pads are simply stickers you can pry off). If you slide your refrigerator on a hardwood or tile floor to put it into place, the 3/8" felt pads can be placed on the Board’s feet for easy sliding.
Once you have both boards connected and working it’s time to install them!
Fridge Setup
Place your empty refrigerator on top of the two Wii Balance Board and plug the AC adapter from the battery pack into a wall outlet. You’ll want to make sure your fridge is stable on top of the two Wii Balance Boards or else your weight measurements won’t be accurate. Run the wiiboard_test.py script from a terminal on both of your Raspberry Pi’s. This will allow us to get the base weight of the refrigerator.
$ sudo python wiiboard_test.py
Discovering board...
Press the red sync button on the board now
Found Wiiboard at address 00:23:CC:2E:E1:44
Trying to connect...
Connected to Wiiboard at address 00:23:CC:2E:E1:44
Wiiboard connected
ACK to data write received
83.011212 lbs
82.962819 lbs
83.165323 lbs
83.068293 lbs
83.182738 lbs
82.981102 lbs
The measurements are a bit noisy varying +/- .25 lbs. To get the base weight of your refrigerator, allow the script to run and take several measurements. Each measurement output is really 500 measurements (or whatever you changed the WEIGHT_SAMPLES variable to in the script). Add together the weight from both Wii Balance Boards to get your total fridge weight. Write down your refrigerator’s base weight, this will be used later.
Fill your refrigerator up with as many bottles as you have (do not mix wine and beer bottles and soda cans because they have very different weights; we are making the assumption that each bottle has approximately the same weight). Observe the average weight of the refrigerator plus the additional bottles. Subtract the base weight of the refrigerator from the full weight and divide this weight by the number of bottles in the refrigerator. This is the average weight of a bottle. I measured an average weight of 1.266 lbs for each bottle using 30 beer bottles. Write down your measured average bottle weight.
We have everything we need to detect the number of bottles of beer in our refrigerator. Next, let’s add a couple of important sensors to our refrigerator and then put it all together.
Initial State
We want the weight data to a cloud service and have that service turn our data into a nice dashboard that we can access from our laptop or mobile device. Our data needs a destination. We will use Initial State as that destination.
Step 1: Register for Initial State Account
Go to https://iot.app.initialstate.com and create a new account or login into your account if you already have one.
Step 2: Install the ISStreamer
Install the Initial State Python module onto both of your Raspberry Pi’s.
At a command prompt (don’t forget to SSH into your Pi first), run the following command:
$ cd /home/pi/
$ \curl -sSL https://get.initialstate.com/python -o - | sudo bash
Step 3: Make some Automagic
After Step 2 you will see something similar to the following output to the screen:
pi@raspberrypi ~ $ \curl -sSL https://get.initialstate.com/python -o - | sudo bash
Password:
Beginning ISStreamer Python Easy Installation!
This may take a couple minutes to install, grab some coffee :)
But don't forget to come back, I'll have questions later!Found easy_install: setuptools 1.1.6
Found pip: pip 1.5.6 from /Library/Python/2.7/site-packages/pip-1.5.6- py2.7.egg (python 2.7)
pip major version: 1
pip minor version: 5
ISStreamer found, updating...
Requirement already up-to-date: ISStreamer in /Library/Python/2.7/site-packages
Cleaning up...
Do you want automagically get an example script? [y/N]
Where do you want to save the example? [default: ./is_example.py]Please select which Initial State app you're using:
1. app.initialstate.com
2. [NEW!] iot.app.initialstate.com
Enter choice 1 or 2:
Enter iot.app.initialstate.com user name:
Enter iot.app.initialstate.com password:
When prompted to automatically get an example script, type y. This will create a test script that we can run to ensure that we can stream data to Initial State. The next prompt will ask where you want to save the example file. You can either type a custom local path or hit enter to accept the default location. Finally, you’ll be asked which Initial State app you are using. If you’ve recently created an account, select option 2, enter your user name and password. After that the installation will be complete.
Step 4: Access Keys
Let’s take a look at the example script that was created.
$ nano is_example.py
On line 15, you will see a line that starts with streamer = Streamer(bucket_ ...
. This lines creates a new data bucket named “Python Stream Example” and is associated with your account. This association happens because of the access_key=”...”
parameter on that same line. That long series of letters and numbers is your Initial State account access key. If you go to your Initial State account in your web browser, click on your username in the top right, then go to “my settings”, you will find that same access key at the bottom of the page under “Streaming Access Keys”.
Step 5: Run the Example
Run the test script to make sure we can create a data stream to your Initial State account. Run the following:
$ python is_example.py
Step 6: Profit
Go back to your Initial State account in your web browser. A new data bucket called “Python Stream Example” should have shown up on the left in your log shelf (you may have to refresh the page). Click on this bucket and then click on the Waves icon to view the test data.
Now we are ready to setup the final script and send our data to an Initial State dashboard.
SmartBeerFridge Script
Assuming you ran the git clone command earlier in the tutorial, the final script that puts everything together is called smartbeerfridge.py in your ~/smartbeerfridge directory.
A few settings need to be set in the script before you can run it. Open up smartbeerfridge.py in the text editor.
$ cd ~
$ cd smartbeerfridge
$ nano smartbeerfridge.py
Near the top of this file, there is a User Settings section.
# --------- User Settings ---------
WEIGHT_SAMPLES = 500
BUCKET_NAME = ":beer: Beer Fridge"
BUCKET_KEY = "beer2019"
ACCESS_KEY = "ist_9l88ABg2jnZZgbaDTMoBTYfZVmh8WU43"
WIIBOARD_NAME = "w2"
# ---------------------------------
- WEIGHT_SAMPLES sets how many weight measurements the Wii Balance Board takes and averages before computing how many bottles are present. 500 seemed to provide a nice balance of accuracy while not taking too long to complete. Feel free to tweak this setting to your liking.
- BUCKET_NAME specifies the name of the data bucket that will show up in your Initial State account.
- BUCKET_KEY should be set to a unique name. The BUCKET_KEY parameter allows you to append to an existing bucket or create a new bucket. Simply pick a name for this parameter (i.e. beerfridge1234).
- ACCESS_KEY is your Initial State account key that can be found on your account page.
- WII BOARD NAME is the name for the board you are connected to. Each of the two Wii Boards should be named differently so you can differentiate their signals in your Initial State dashboard.
BUCKET_KEY, BUCKEY_NAME, and ACCESS_KEY should be the same for both of your Pi’s. Once you have specified each parameter in this section and saved your changes, you are ready to run the final script. Before we run the script, let’s go through what it is going to do.
- At the start of the script, you will be asked to pair your Wii Balance Board with your Raspberry Pi. Use the lever that you hacked together in to press the sync button when prompted.
- If the script notices a weight change it will wait for it to stabilize before sending the new weight data. So if you open the door, take a beer, and close the door it will wait until the weight is stabilized and not send a bunch of changing weight data.
- Weight from both your Wii Board will be send in from each Pi.
Run the script to start the magic.
$ sudo python smartbeerfridge.py
Remotely Monitor the Pi Process and IP Address
Before we go to view the data there is an optional step that will make monitoring your beer fridge a lot easier. We can monitor the beer fridge process and send the IP address of each Pi to our Initial State dashboard. That way you can get alerts if a Pi stops running or reboots. It can also allow your process to launch and monitor when the Pi reboots, although its necessary to set an alert so you can reconnect your Wii Balance Board.
You can follow the tutorial here to get and send data to the same bucket as your smart beer fridge script. Once you have this running you can go to your Initial State dashboard to start customizing it.
Dashboard
You now have data in your dashboard! What we need to do next is write some Real-Time Expressions to display number of drinks in the fridge, total weight, and beer drank over time.
Number of Drinks in the fridge: =math.round(([w1_weight]+[w2_weight]-49.01)/0.80) & ‘🍺’
Total Weight: =’⚖️ ‘ & math.round([w1_weight]+[w2_weight],2) & “ lbs”
Total Beers Drank: =math.round((math.sum([w1_decremented_weight]) + math.sum([w2_decremented_weight]))/0.80) & ‘🍻’
You can make Number of Drinks & Total Beers Drank summary tiles and line graph tiles. Total Weight, Wii Status, Pi+Wii Status, and Pi IP Address are all summary tiles. This is how we customized it but there is an infinite number of ways to customize your Tiles to your liking. You can view the below dashboard here: https://go.init.st/17lgb9n.
You can add a custom background image to your dashboard to give your data more context and personality. Whether that is a picture of a beer fridge or wine or sodas or a bar.
Now that your dashboard is fully setup and customized, let’s set some Triggers so we can get notified when the beer fridge starts to go low!
Triggered Alerts
Now that you have your dashboard built, you’ll want to set Triggers to be alerted when your beer goes low or your Pi’s stop running. That is easy to do.
Go to your bucket shelf and click the settings under your Smart Beer Fridge bucket. Go to the Triggers tab. For the Trigger alert for beer going low set the Stream Key to w1_weight or w2_weight, select the less than or equal to operator, and set the value to 10 or less, depending on how low you want your fridge to go before you get alerted. Click the plus sign. Enter your email or phone number and click the plus sign.
For the Trigger alert when the Pi stops running, set the Stream Key to Pi+Wii1 Monitor, set the operator to equal, and set the value to Exited. Click the plus sign. Add your email or phone number and click the plus sign. Repeat this same setup with the Stream Key equal to Pi+Wii2 Monitor so you can be alert for both Pi’s. Once you are done click the Done button on the bottom and your Triggers are now set!
You can now enjoy all the beers you want and you’ll never have to worry about running out again. Cheers!