Mixing Electronics & Water

Enter Your Electronics & Design Project for Your Chance to Win a $100 Shopping Cart!

Back to The Project14 homepage

Project14 Home
Monthly Themes
Monthly Theme Poll

 

For the Mixing Electronics & Water project, we made a shower timing device that uses the bathroom lights to signal the person when it's time to get out.  If they stay in too long, then the lights turn off and stay off.

 

This is a great project for those who want to learn how to use inexpensive parts with the power of IFTTT.com to trigger physical world tasks.

 

The following parts are required:

  • ESP8266 wifi microcontroller
  • about 4"x4" of perfboard
  • Waterproof Temperature Probe (DS18B20 chip) such as by Elenker.
  • 4.7K resistor to pull the probe chip data line high
  • two tactile switches (preferably one tactile switch and one three-way)
  • soldering iron, solder, and solid wire
  • 3.3V power source such as a LiON battery pack or wall wart
  • Software:  Arduino IDE with ESP8266
  • 3.3V FTDI Cable
  • A smart light switch or bulb such as a Wemo switch

 

Step 1:  Build a flash device (programmer) for the ESP8266

Our model of the ESP8266 did not have built in programming hardware.  To program it we need an FTDI cable.  Also, when you flash the program to the ESP8266, you have to pull GPIO0 to ground.  To aid in this, we designed a programming circuit using the software KiCAD.  You can view a step-by-step tutorial here:

 

Here is the schematic for our ESP8266 programmer:

ESP8266 Programmer Circuit

Step 2:  Build the ESP8266 Programmer Board

Having made the board in KiCAD, we could have sent it off to a fab shop such as OSHPARK.com.  However, we were eager to get the project going, so we did it by hand.

You can view the step-by-step build here:https://www.youtube.com/watch?v=PJ5f82W8uUs

 

 

Step 3:  Set up IFTTT.com WebHooks

First, you have to set up an account on IFTTT.com.

Then click My Applet-->New Applet.

For the This, click on WebHook and follow the prompts.

For the That, choose your light switch and follow the prompts.

After you get it set up, click on My Applets-->Webhooks and then Documentation to see the "hooks" you will need in the code in Step 4.

See the video at the end of this post for a step-by-step IFTTT.com setup guide.

 

Step 4:  Writing the Code in the Arduino IDE

To get the Arduino IDE to talk to the ESP8266, we added a reference to the board manager for the ESP8266 boards.  We show how to do this in the video at the end of this post.

 

Once we had the board recognized in the Arduino IDE, we imported the OneWire and Dallas temperature libraries.  These allow us to read the temperature probe.

 

We opened the Dallas temperature example "Simple" and the ESP8266 example "HTTPSRequest" and then morphed them into the code we needed.

 

The end result of the code is here:

// Include the libraries we need
#include "OneWire.h"
#include "DallasTemperature.h"
#include "ESP8266WiFi.h"
#include "WiFiClientSecure.h"

// Data wire is plugged into GPIO2 of the ESP8266
#define ONE_WIRE_BUS 2
const char* ssid = "........";//your home wifi router SSID
const char* password = "........"; //your home wifi router password
const char* host = "maker.ifttt.com";//we'll use IFTTT.com to trigger our Wemo Light Switch
const int httpsPort = 443;//this is the SSL default port
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
float myTemp=0; unsigned long timer; unsigned long timeout;
//used to monitor the temperature of the hot water plumbing.  Right now, 2 seconds for testing.  
unsigned long duration=2000;//value is in milliseconds, so this means 2 seconds
bool showering=false;// used to keep track of assumed shower is happening.
/*
 * The setup function. We only start the sensors here
 */
void setup(void)
{
    // Start up the Dallas temperature library
  sensors.begin();
}
/*
 * Main function, get and show the temperature
 */
void loop(void)
{   
  sensors.requestTemperatures(); // Send the command to get temperatures.  You can have many temperature probes on "one wire".
 
  myTemp=sensors.getTempCByIndex(0)*1.8+32; //convert to degrees F  
  if (myTemp>80&!showering) {//first time detecting someone is showering due to the temp going up.  Temperature of pipe went above 80F
    showering=true;timer=millis()+duration; timeout=millis()+10000;//snap shot when this happened.  Set a timeout to turn the light completely off.  Keeping it low for testing for now.
  }
  if (showering&&(millis()>timer)){//blink the light if beyond the duration
    ToggleLight();
    duration=duration/4;//blink the light again in 1/4th the duration of the previous wait time.
    timer=millis()+duration;//reset the timer
  }
  
  delay(1000);//just rest 1 second between polls for temperature
}
void ToggleLight()
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  if (!client.connect(host, httpsPort)) {
    return;
  }
  //don't know if I actually needed this code from the example, so I kept the call anyway.
  if (client.verify(fingerprint, host)) {
  } else {
  }
  
  //this just blinks the light
  String url = "/trigger/YourBlinkerEvent/with/key/YourWebHook";//change this to the IFTTT URL that toggles the lightswitch.  
  
  //if in too long, then turn the light out for good.
  if (showering&&((millis()>timeout))) {
    url = "/trigger/YourKillTheLightEvent/with/key/YourWebHook";//change this to the IFTTT URL that turns off the lightswitch
  }
  
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");
  //     Send the hit to IFTTT.com at the above URL     
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      break;
    }
  }
  String line = client.readStringUntil('\n');
}

Step 5:  We then needed a circuit board to mount the ESP8266, the power connection, and the temperature probe.  This was done just like the programmer board in step 2.  Here is our resulting schematic that would be compatible to a 9V or 5V wall wart.

Shower Timer ESP8266 Circuit

Step 6:  We installed it on our hot water line going to the shower of the most demanding user - Walnut.

 

This was a very fun project that has opened up infinite new projects for DIY IoT devices.  We thank Element14 for the opportunity!

 

Here is the video of our build in "Raising Awesome" style: