Introduction: Water Usage Monitor

In our first episode of Deconstructing IoT, we talked about how the Internet of Things can provide solutions to the problems posed by water waste, and built a simple application to measure water flow from a household faucet. Here, we'll go through the steps behind building that water usage monitoring device with Temboo and Texas Instruments hardware. We’ll log data in a Google Spreadsheet, and then use Zendesk to create an alert if water is being wasted.

Step 1: Get Set Up

To build this project, you’ll need:

  • A TI CC3200 LaunchPad
  • A USB A – Micro B cable
  • A potentiometer
  • Some wires
  • A faucet with a rotating handle

You'll also need a Temboo account, a Google spreadsheet, a Zendesk account, and the latest version of the open source Energia IDE. You can create a Temboo account here, create a Google account here, create a Zendesk account here, and download Energia here.

For this example to work, the columns of the Google spreadsheet that you are using must be labeled. It doesn’t matter what the labels are as long as the first cell in each column that you are using has some text in it, so for now, let’s use one column and label it “Water Flow.”

Step 2: Generate Your Code

Once you're set up, log in to Temboo and go to the Google > Spreadsheets > AppendRow Choreo in the Temboo Library. Turn on IoT Mode and select “Texas Instruments LaunchPad” and “TI CC3200 LaunchPad (WiFi)” from the dropdown menus. In the pop-up that appears, give a name to your board (so that you can find it again for future projects), and also fill in your wireless network information (this will enable Temboo to generate code that is preconfigured to work with your network).

Fill out the Input fields using the information from your Google account and the title of the spreadsheet that you are using. For now, we’re going to hard-code the values to be added to the spreadsheet; later on, we’ll adjust this so that the values being added are coming from the potentiometer that you will attach to your LaunchPad. Since we’re only logging data in one column of the spreadsheet ("Water Flow"), we can just put a single character into the RowData Input field as a placeholder.

Step 3: Test Your Code

Once you've filled out all of the Input fields on the Temboo AppendRow Choreo page, test the Choreo from your browser by clicking Run at the bottom of the window. Now, if you check the spreadsheet you created to store the data, you should see that the first row beneath the column header has been filled in with the character that you provided in the RowData Input field. Back on the AppendRow Choreo page, you should also see that a box entitled Response and containing the string “success” has appeared under Output—it probably will not surprise you to learn that this indicates that your Choreo has executed successfully.

If your test run fails, an error message will appear explaining why. One common error occurs when Google’s security checks flag the API call that you are making as an unauthorized attempt to access your account. If you see this error, you should turn on 2-step verification on your Google account and create an application-specific password for Temboo. Use this password in the Password Input field, and you will be able to access Google’s APIs using Temboo.

Step 4: Create Your Sketch

Now that you've tested the Choreo successfully, you can scroll down a bit more to find the Code section of the page. When you're in IoT Mode and you run a Choreo, Temboo automatically generates code that can be used to make the same API call from your LaunchPad. Copy the code, and paste it into a new sketch in the Energia IDE.

In order to run this sketch on your LaunchPad, it needs to be configured with an appropriate TembooAccount.h header file that contains your Temboo account information. To create the header file, make a new tab in Energia and name it TembooAccount.h.

On the AppendRow Choreo page beneath the sketch code that you previously copied and pasted into Energia, you’ll find another block of generated code containing several #define statements. This is your header file. Copy the contents of the header into the TembooAccount.h tab in Energia.

Step 5: Integrate Your Sensor

With both files in place, you are just about ready to start logging data values to your spreadsheet—you only need to make a few changes to your sketch to have that data be read from your sensor rather than taken from a hard-coded input. First, initialize the pin that you will be reading on the LaunchPad (we’ve chosen to use pin labeled "P60" on the board, which is identified in the Energia IDE as "A3") by adding the following line of code to what you already have in void setup():

pinMode(A3, INPUT);

Then, in void loop(), replace

String RowDataValue = "hard-coded input";
AppendRowChoreo.addInput("RowData", RowDataValue);

with

int flow = analogRead(A3);
String rowData(flow);
AppendRowChoreo.addInput("RowData", rowData);

This will create a string called rowData that will contain the water flow value from the potentiometer and pass the flow value to the spreadsheet instead of passing the hard-coded character that we were using before.

Finally, the generated code has some built-in limits that we don’t need. Delete the conditional

if (numRuns <= maxRuns){

from the beginning of void loop (and don't forget to delete the closing curly brace as well). Having a cap on the number of times the Choreo will execute is useful for testing, but this application does not need one. Similarly, the built-in 30-second delay at the end of the sketch can be lengthened or shortened by raising or lowering the number of milliseconds for which it is set to last in the line

delay(30000); // wait 30 seconds between AppendRow calls

Step 6: Add Zendesk

Next, let’s set up your Zendesk alert. First, we’ll add the code that will trigger the alert if too much water is flowing from the faucet. In void loop() after

AppendRowChoreo.close();

add

if (analogRead(A3) > 800){
  // Zendesk Choreo code goes here
}

Note that we've set the threshold for too much water at 800; you should, of course, choose a threshold that best fits your needs. Then, replace the placeholder within the conditional with the code to call the Zendesk Choreo. To add this Zendesk code, go to the Zendesk > Tickets > CreateTicket Choreo page and, as you did for Google Spreadsheets, put in your Zendesk credentials and whatever subject and body text you’d like for your alert. Once again, you’ll find that the code for running the Choreo has been generated further down the page. Since you already have some of the generated code in place as part of the Google Spreadsheets code that you copied earlier, you’ll only need to add a few more lines to integrated the Zendesk functionality into your sketch. From void loop(), copy the following lines:

TembooChoreo CreateTicketChoreo(client);
// Invoke the Temboo client
CreateTicketChoreo.begin();
// Set Temboo account credentials
CreateTicketChoreo.setAccountName(TEMBOO_ACCOUNT);
CreateTicketChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
CreateTicketChoreo.setAppKey(TEMBOO_APP_KEY);
// Set Choreo inputs
String EmailValue = "PLACEHOLDER";
CreateTicketChoreo.addInput("Email", EmailValue);
String SubjectValue = "PLACEHOLDER";
CreateTicketChoreo.addInput("Subject", SubjectValue);
String PasswordValue = "PLACEHOLDER";
CreateTicketChoreo.addInput("Password", PasswordValue);
String CommentValue = "PLACEHOLDER";
CreateTicketChoreo.addInput("Comment", CommentValue);
String ServerValue = "PLACEHOLDER";
CreateTicketChoreo.addInput("Server", ServerValue);
// Identify the Choreo to run
CreateTicketChoreo.setChoreo("/Library/Zendesk/Tickets/CreateTicket");
// Run the Choreo; when results are available, print them to serial
CreateTicketChoreo.run();
while(CreateTicketChoreo.available()) {
  char c = CreateTicketChoreo.read();
  Serial.print(c);
}
CreateTicketChoreo.close();

and paste them into your sketch where it says

// Zendesk Choreo code goes here

With that, your sketch should be ready to go (note that in the example code above, we've replaced the Inputs with placeholder values, as Input values will vary from person to person--in the code that you generate on the Temboo website, these Inputs will automatically be filled in).

Step 7: Set Up Your LaunchPad

Now that you’ve set up your sketch in Energia, let’s set up your LaunchPad as well. The first thing to do is make sure that your firmware is up to date, and that your board is appropriately configured to run a sketch from Energia. There are instructions on how to do that on the Energia website here.

Next, let’s attach the potentiometer. It should have three leads: the outer leads should be wired to the 3.3V power source on the LaunchPad and to ground (it doesn't matter which side goes to which), and the one in the middle should be wired to the pin you’ll be reading from (recall that we earlier selected the pin labeled "P60"). You can refer to the wiring diagram for help. Attach the potentiometer to the inside of your faucet handle so that the two turn together.

Finally, make sure your board is ready to go by going to the Tools > Board menu in Energia and selecting the LaunchPad you’re working with. Then, go to the Tools > Serial Port menu and select the port your board is connected to.

With that, your sketch and LaunchPad are ready to go! Save and upload your sketch, open your serial monitor, and watch your LaunchPad start checking monitoring your water usage.