Products for USB Sensing and Control

Products for USB Sensing and Control

Water Leak Detector

Leak detection system


by Lucas

Source Code

Introduction

If you've ever worried about coming home to a flooded basement, this project is for you. We will be creating a water leak detection system that will send you a text message if a water leak is detected.

Hardware/Component List

Prepping Hardware

Our sensor will likely be coming into contact with water (especially during testing), so it is important that we protect the PCB components. To do this, we used a conformal coating on the PCB.

Applying conformal coating to the top of the PCB.

Applying conformal coating to the bottom of the PCB.

Software

Libraries and Drivers

This project assumes that you are somewhat familiar with the basic operation of Phidgets. If this is your first time building a Phidgets project with a Phidget SBC, please refer to the SBC page for instructions on how to setup your environment and get started.

Overview

A simple C program will run on the Phidget SBC and perform the following tasks:

  • Create a VoltageInput object and map it to the water sensor. If the voltage from the water sensor indicates that there is water present, send a text to a pre-defined phone number

Texts

In order to send text messages from the Phidget SBC, we will be using the email to text feature that most wireless carries support. For more information on sending an email from the SBC, check out this project

Create simple C program on Phidget SBC

You should create a project on the SBC that looks like this:

Next, you will need to set up libcurl on your SBC. Open the terminal and enter this command:

  
sudo apt-get install libcurl4-gnutls-dev

After some minor changes to waterLeakDetector.c (serial numbers and email information for SSMTP), you can compile the program:

Logging and remote connections

As always, we want to create a log file that will let us know what is happening in our project. We write this line at the top of our main loop:
PhidgetLog_enable(PHIDGET_LOG_INFO, "/path/to/log/file");
We also need to connect to the PhidgetDictionary that we defined. This will be a remote connection so we need to add the following line at the top of our main loop:
PhidgetNet_enableServerDiscovery(PHIDGETSERVER_DEVICE);

Interface with water sensor

The water sensor simply outputs a voltage that roughly corresponds to the amount of water on it. We can use the built-in VINT hub on the SBC4 in order to read the voltage and determine whether or not a leak is occuring. In order to do this, we must create a VoltageInput channel, which is shown below:

  
#define VINTHUB_SERIALNUM		370465
#define WATER_SENSOR_HUBPORT	1 
...
PhidgetVoltageInputHandle waterSensor;
...
//create waterSensor Voltage Input
result = PhidgetVoltageInput_create(&waterSensor);
if (result != EPHIDGET_OK) {
	PhidgetLog_log(PHIDGET_LOG_ERROR, "failed to create voltage input object");
	return 1;
}
result = Phidget_setDeviceSerialNumber((PhidgetHandle)waterSensor, VINTHUB_SERIALNUM);
if (result != EPHIDGET_OK) {
	PhidgetLog_log(PHIDGET_LOG_ERROR, "failed to set device serial number");
	return 1;
}
result = Phidget_setHubPort((PhidgetHandle)waterSensor, WATER_SENSOR_HUBPORT);
if (result != EPHIDGET_OK) {
	PhidgetLog_log(PHIDGET_LOG_ERROR, "failed to set hub port");
	return 1;
}
result = Phidget_setIsHubPortDevice((PhidgetHandle)waterSensor, 1);//we are using the built-in functionality of the VINT Hub
if (result != EPHIDGET_OK) {
	PhidgetLog_log(PHIDGET_LOG_ERROR, "failed to set is hub port device");
	return 1;
}
result = Phidget_openWaitForAttachment((PhidgetHandle)waterSensor, 2000);
if (result != EPHIDGET_OK) {
	PhidgetLog_log(PHIDGET_LOG_ERROR, "failed to open water channel");
	return 1;
}

Main loop

  
while (1) {
	result = PhidgetVoltageInput_getVoltage(waterSensor,&sensorValue);
	if (result != EPHIDGET_OK) {
		PhidgetLog_log(PHIDGET_LOG_ERROR, "Failed to get sensor value");
		sensorValue = UNKNOWN_VALUE;
	}
	else{
		snprintf(value, 20, "%f", sensorValue);
		PhidgetLog_log(PHIDGET_LOG_INFO, "Got sensor value: %f", sensorValue);
	}
	if (sensorValue > SENSOR_VALUE_DANGEROUS) {
		time_t currentTime;
		time(& currentTime);
		if(initialMessage || difftime(currentTime,lastTime) > MINS_30){
			PhidgetLog_log(PHIDGET_LOG_INFO, "Dangerous water level detected, sending text message");
			initialMessage = 0;
			time(&lastTime);
			sendMessage();
		}
	}
	sleep(SLEEP_TIME); //sleep in seconds
}

Here is a breakdown of the main loop:

  • Call PhidgetVoltageInput_getVoltage to get the latest voltage value from the water sensor.
  • Check if the sensorValue is at a dangerous level. If it is, send a text message.
  • Sleep for one second and repeat

This is a simple program, and there are some improvements that could be made. If you have any suggestions, let us know!