Have you ever found yourself mindlessly snacking not even realizing what, or how frequently you are eating? Even worse, have you ever had something in the fridge that you are excited to eat and when you go to take it out you realize someone else already beat you to it?
Consider these problems of the past with this new refrigerator alarm. By utilizing the Texas Instruments Launchpad, CC3100 WiFi BoosterPack, Educational BoosterPack MKII, and services provided by Temboo, we will be able to track every time the refrigerator is opened and document it to a spreadsheet on google drive.
Getting Temboo and Google Talking:
The first step in getting our refrigerator monitor up and running is to register for a Temboo account. Temboo is a service that makes interacting with many APIs seamless and is great for IoT development. For more depth on getting set up with Temboo, check out this other project. For our refrigerator monitor, we will be using the Google spreadsheet API which can be found by navigating to "Google" on the left pane and selecting "Spreadsheets". In order to use this, and any, Google Choreos, there a few steps to set up OAuth so that Temboo can talk to the Google API. The steps for setting up OAuth are found on the Spreadsheets Choreo page (this requires a google account). Make sure to take note of the ClientID, ClientSecret, and RefreshToken as you need all of these in the next step.
Create the Google Spreadsheet:
In the same Google account used to set up the API, navigate to the Google Drive page. Create a new Google Sheet and give it a title that is easy to remember (you are going to need it later). Go ahead and open up the spreadsheet so we can make a few changes. We are going to be using the AppendRow Choreo which adds information below the last full row using comma separated values and inserting information into only columns that have a title. Because of this, we want to go ahead and set up our column titles so we know what our imported data is. For This example, we import a timestamp using epoch time (more about this later) in the first column and the LUX (measurement of light) value read by the sensor in the second column. One last step to make sure the timestamp works correctly is to select the whole first column by clicking the letter 'A'. Go to Format --> Number and select "Date Time". This will cause google to change the imported epoch time value into a readable date and time. Now the spreadsheet is ready to receive data and should look something like the image below.
Getting The Choreo Running:
After finishing up the OAuth portion and spreadsheet set up, it is time to get our Choreo talking to Google. Navigate to the AppendRow Choreo from the Spreadsheets Choreo page. Fill in all the information obtained when running through the OAuth section. For "RowData", you can enter a place holder so that it can be edited in code later with the timestamp and LUX values. "SpreadsheetTitle" has to match the spreadsheet that we created in the previous section. Go ahead and try and run the Choreo. You should get a "success" return at this point and see the spreadsheet update with whatever was in "RowData". If it did not work, it is most likely an issue with the OAuth process that can be solved by running through the steps again. Once we have successfully run the Choreo on the web, it is time to get it set up on the LaunchPad.
Coding the LaunchPad:
At this point, Temboo provides us the base code to append a row to an existing spreadsheet. This takes a lot of the work out of setting up the API, but the fun of actually using Temboo to solve a problem still remains. In order to create this refrigerator monitor we are going to be using the OPT3001 optical sensor on the Educational BoosterPack and we are also going to be using multi tasking which is new on the MSP432. Multi tasking allows us to have to individual sketches running simultaneously on the same launchpad and even lets them interact with each other. In this case we are going to have one tab called fridgeAlarm which is set up to monitor the optical sensor and append a row to the Google sheet on an event trigger. The other tab, called epochTime, is going to use WiFi to ping a time server and return the current time in epoch time. Epoch time is simply the number of seconds that has passed since January 1, 1970. More information can be found here.
fridgeAlaram:
The code below, called fridgeAlarm.ino, is the main function of the multi tasked program. It initializes all of the variables and sets up the WiFi connection as well as the optical sensor. Go ahead and download then open the code, or copy and paste it into a blank Energia sketch. Read through it and compare it to the code provided on the Temboo site. The code is very similar with a few exceptions such as allowing the optical sensor to trigger the event and actually setting up the data to be appended to the spreadsheet.
The maxLux value may need to be changed based on how bright your refrigerator light is. Make sure that it doesn't trigger when it is closed and it does trigger when it is open! A value of 100 should be okay as long as the optical sensor is exposed to the light bulb.
Line 86 begins a block of variables that you will need to fill in using the values provided by the Temboo AppendRow Choreo page. The lines that need to be changed are clarified with an "Edit Needed" commented along side.
Line 90 actually does the math on the epoch time to convert it to a value that google can use. Notice the variable timeZoneShift declared at the top of the page. This needs to be changed to match your difference from GMT (I used -5 to for CDT).
epochTime:
Now it is time to initialize the second task called epochTime. First, create a new tab in the Energia sketch as shown below.
Rename the tab epochTime to keep everything straight as seen below:
Now we should have two separate tabs that can run side by side! Copy the code listed below under epochTime.ino and paste it into this new tab. This code shouldn't need any changes, but if the time server is down the IP address can be changed to a different server. A list of servers can be found here.
TembooAccount.h:
Temboo uses a library to fill in your account information and communicate with your LaunchPad. Make sure to copy TembooAccount.h to where your local Energia libraries are stored prior to trying to run the code. Another example can be found here.
After getting both tabs set up the code should be ready to run! Flash your LaunchPad and set up your alarm in the refrigerator and every time someone opens it and the optical sensor triggers an event, a row will be appended to the google spreadsheet containing the time that the refrigerator was opened as well as the LUX value detected when the refrigerator was opened. No more night time pests stealing all your favorite leftovers!
Comments