Sergio Mendoza
Published © GPL3+

A gallon a day will keep the drought away.

Save your shower water by diverting it to the toilet tank.

IntermediateFull instructions providedOver 1 day619
A gallon a day will keep the drought away.

Things used in this project

Hardware components

SparkFun ESP8266 Thing - Dev Board
SparkFun ESP8266 Thing - Dev Board
×1
Shower Diverter
×1
Magnetically Latching Valve
For the toilet tank.
×1
Magnetically Latching Valve
For the shower diverter.
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
Used pre- cut ones.
×1
5 VDC Reed Relay
I bought these because they fit nicely into the breadboard. You can substitute any SPDT relay that fits your requirement. If you will be using non-latching valves, you will only need two or you can get rid of the relay altogether.
×4
N-Channel MOSFET
If you are using non-latching valves, you will only need two.
×4
Quick Disconnect Adapter
×1
1/2" coupling
×2
1/4" tubing
For this project I got the clear tubing, 20 ft.
×1
Mounting Base
Comes in 10 pack
×1
Thread seal
×1
Cable ties
40 pack (or less)
×1
Battery holder
4XAAA
×1
Lithium batteries
You can probably use regular alkaline batteries, I just wanted to play it safe with the valves
×1
3/8" to 1/2" braided hose
×1
1/2" to 7/8" braided toilet hose
×1
Heat shrink tubing assortment
×1
22 AWG 2 wire cable
I bought 20 feet just in case but I would recommend to measure out what you need.
×1

Software apps and online services

Cayenne
myDevices Cayenne

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
including solder
Electronics Helping Hand
Is not necessary, but definitely makes soldering fast and easy.
Crescent wrench
Channel locks
You can also use pliers
Wire strippers
Needs to go down into the smaller gauges like (e.g AWG 22)
Utility knife
You can use any sharp blade.
Heat gun
A hair dryer also works.

Story

Read more

Schematics

Fritzing Water Saving Sketch

Use Fritzing software to open the file. Will contain the breadboard layout and the schematic.

Code

Code for Water Saving

C/C++
Used in the Arduino IDE.
/* 
 *  This sketch will use the Cayenne MQTT ESP8266 to connect to the Cayenne Dashboard.
 *
 *  The sketch incoroporates 3 channels for the event handlers:
 *  - Channel 0 is set for the Shower Valve
 *  - Channel 1 is set for the Toilet Valve
 *  - Channel 2 is set for System Enable/Disable
 *
 *  If you are using non-latching valves you will have to modify the contents of each channel by
 *  removing the PulsePin method and inserting your own method to signal the pins for your setup.
 *
 *  In the Cayenne Dasboard, you will have to setup two triggers based off the timer to get the valves to shutoff automatically.
 *
 *  Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.
 */

#include <CayenneMQTTESP8266.h>


//Defines the pins for the ESP8266 Dev Thing
#define ESP8266_LED 5  //Pin 5
#define ESP8266_D4 4   //Pin 4
#define ESP8266_D13 13 //Pin 13
#define ESP8266_D12 12 //Pin 12
#define ESP8266_D16 16 //Pin 16


//Defines for times
#define PULSE_MILLI 500 // 500 millisec pulse for the latching valves
#define PUBLISH_ TIME 500// amount of time to publich the data to the dashboard


// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "wifiPassword";


// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";


//Used to update the values on the dashboard
unsigned long lastMillis = 0; 


//Used to set the flags for system and first connect
bool isSystemEnabled = false;
bool isFirstConnect = true;


//Setup Loop
void setup() 
{
  pinMode(ESP8266_LED, OUTPUT);
  pinMode(ESP8266_D4, OUTPUT);
  pinMode(ESP8266_D13, OUTPUT);
  pinMode(ESP8266_D12, OUTPUT);
  pinMode(ESP8266_D16, OUTPUT);
  digitalWrite(ESP8266_LED, HIGH);//Turns off the LED
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword); 
}


//Main loop
void loop() 
{
  Cayenne.loop();
}


// This function will send a pulse signal to the desired pin for the amount of time specified.
void pulsePin(unsigned int pin, unsigned int delayTime)
{
  digitalWrite(pin, HIGH);
  delay(delayTime);
  digitalWrite(pin, LOW);
}


// This function will run every time the Cayenne connection is established.
CAYENNE_CONNECTED()
{
  CAYENNE_LOG("Connection established");
  if (isFirstConnect)
  {
    // This causes Cayenne to resend data for any virtual pins.
    Cayenne.syncAll();
    isFirstConnect = false;
  }
}


// This event handler will take the signal from channel 0 and either turn off or
// turn on the shower valve. This will allow the user to fill the toilet tank with 
// the shower head water.
CAYENNE_IN(0)
{
  if(getValue.asInt() == 0) {
    if(isSystemEnabled) { pulsePin(ESP8266_D13, PULSE_MILLI); }//Turn off shower valve.
    else { return;}
  }
  
  if(getValue.asInt() == 1) {
    if(isSystemEnabled) { pulsePin(ESP8266_D4, PULSE_MILLI); }//Turn on shower valve.
    else {return;}
  }
}


// This event handler will take the signal from channel 1 and either turn off or turn on 
// the toilet valve. This will allow the user to fill the toilet tank for a normal flush.
CAYENNE_IN(1)
{ 
   if(getValue.asInt() == 0) {
      if(isSystemEnabled) { pulsePin(ESP8266_D16, PULSE_MILLI); }//Turn off toilet valve.
      else{ return; }
    }
    
    if(getValue.asInt() == 1) {
      if(isSystemEnabled){ pulsePin(ESP8266_D12, PULSE_MILLI); }//Turn on toilet valve.
      else{ return; }
    }
}


// This event handler will enable or disable the system.
CAYENNE_IN(2) {
  if (getValue.asInt() == 0) {
    isSystemEnabled = false;
    digitalWrite(ESP8266_LED, HIGH);//Turns off the LED
    pulsePin(ESP8266_D12, PULSE_MILLI);//Turn on toilet valve.
    pulsePin(ESP8266_D13, PULSE_MILLI);//Turn off shower valve. This ensures the valve is shut.
  }

  if (getValue.asInt() == 1) {
    isSystemEnabled = true;
    digitalWrite(ESP8266_LED, LOW);//Turns off the LED
    pulsePin(ESP8266_D16, PULSE_MILLI);//Turn off toilet valve.
    //Don't need to turn on shower valve here since it is already in the ready position.
  }
}

Credits

Sergio Mendoza

Sergio Mendoza

0 projects • 0 followers
Electrical Engineer

Comments