Introduction: DIY ESP8266 WiFi Water Sensor With Text and Email Alerts
Have you ever had a water leak in your home that went un-noticed until damage was done? Sadly this type of problem happens all too often. According to Homeadvisor.com the average water damage repair bill is around $2438 in US dollars. Most of these leaks are from hot water heater tanks that spring a leak, faulty water pipes, flooding, or air conditioning units that do not drain properly.
This instructable will show you how to create an inexpensive water leak detector that can connect to your home's WiFi router and can send you a text or email if it detects a leak. In addition you will be able to log into a Ubiodots.com account and observe live readings and trends to see if a potential leak may be happening.
What you will need:
1. A Ubidots.com account. This is a free Internet of Things web interface that will collect your water leak devices data and format various alerts based on the data. We will go into some detail in this instuctable but a good starting point is to create an account and have a look around.
2. An ESP8266 module. I used a WeMos Mini or a NodeMCU type board
3. The Arduino IDE with the NODEMCU enabled (see other Instructables on how to setup the IDE for this board)
4. A Funduino type water sensor board
5. Hook up wires
6. A 5V USB power source and a USB micro cable
Step 1: Hook Up the Hardware
The hardware hookup is extremely easy. There are only three wires that need to be connected. The ground (-) from the NodeMCU goes to the minus connection on the sensor. The 3.3V pin from the NodeMCU goes to the PLUS (+) pin of the sensor. Finally the A0 (Analog channel 0) pin of the NodeMCU is wired to the Sensor (S) pin. That is all there is to do!
BTW - If you want or need to monitor multiple sources of water from one NodeMCU you can parallel the water sensors. By default you can add about 4 sensors in parallel without changing the resistors on the sensor board. If you go over 4 sensors then you will need to remove the 100 ohm pull-down resistor and change it to 500 ohm to 1K ohm instead. Use caution with the length of the wires to your sensors. This water sensor works best with shorter wires between the sensor and the NodeMCU (ESP8266). I installed the sensor with double sided tape (VHB by 3M) with the sensor wires facing down inside a pan with the connector at the top for my water heater. The wires to the ESP8266 board were kept under 6" and the NODEMCU was also double side tape attached to the side of my water heater. A USB cord and AC adapter supplies the needed power for the device.
Step 2: Software Setup
1. Download the Arduino IDE if you have not done so already
2. Setup the Arduino IDE to recognize the NodeMCU (ESP8266) board.
3. Create an account on the Ubidots.com web site
Download and install the Ubidots ESP8266 library in the Arduino IDE. We will use MQTT (Message Queue Telemetry Transport) that is supported on the Ubidots system. This is a very lightweight Internet of Things messaging protocol that will use very little bandwidth. It's perfect for data such as a water sensor.
4. Once you have an account on Ubidots you will be assigned a default token. This token is a software key that will allow your water sensor to communicate with the Ubidots server and log the data for analysis.
5. Next you need to create a Ubidots device. The device in this case is the water sensor you are building. Click on Devices then Add a device. You can rename the device "Water Sensor" by clicking on the title of the device block. You should get a confirmation that a new device was created. See picture.
6. Edit your new device by selecting it and make sure the api label is "water-sensor". The Arduino code on the next page will use this name along with a variable name and the token to communicate the water sensor data to the Ubiodots server. Once the device is created you then need to attach a variable to this device. For this demo the variable name is "h20_heater". Congratulations you just created a Ubidots device by the name of Water Sensor and it has an api label of "water-sensor" and the variable "h2o_heater" is associated with it.
7. Next you will need to tell Ubidots what to do when the data arrives. Click on "Events" at the top of the page. You will want to create a new event. Select the device you just created. Next select the variable you want to test. In this case there is only one variable "h2o_heater". Next you will want to check if the value of the water sensor is greater than 200. Select the greater than symbol ">" then put in 200 for the value. Continue to add a text alert by adding your mobile number and a message that will let you know the water sensor triggered. Make sure to click "Finish" at the bottom of the page to save it. That's all you need to do on the Ubidots website for now.
Step 3: Setting Up the NodeMCU
To program the NodeMCU you will need to use the earlier installed Arduino IDE and the Ubidots MQTT library.
You will need to load this sketch into the NodeMCU.
Make sure you edit the sketch to use your custom Ubidots Token, your router SSID and password.
Once the Sketch is loaded and running the LED on the NodeMCU should start to slowly glow and get brighter then fade out followed by a 10 second delay. The water sensor is now working and you can verify your data is being passed to the Ubidots server next.
Step 4: Verifying Your Water Sensor Data Is Being Published to Ubidots
If you got this far you should have created a Ubidots device with a api name of water-sensor. A variable was then created called "h20_heater". You also created an event on the Ubidots system to create a "text alert" in case the value is higher than 200 (water is present).
To validate all of this is working log into Ubidots and create a dashboard. This is where you can go to visualize the data. I created a dashboard that plots the sensor data over time as a line graph. I used a 24 hour time frame for the graph. If your NodeMCU is reporting data you should see the beginnings of a graph with a few data points.
Next you can test the sensor by taking a wet paper towel and placing it over the exposed wire fingers on the board. You should soon see a data point appear much higher than the earlier data and if you setup the alert properly a text message will soon appear on your mobile phone.
BTW - If your sensor is sensitive with an alert value of 200 you can raise or lower the value as you desire. I found that high humidity in the air and the oils in your skin can easily create a reading that is about 100. A single drop of water typically created a value in the 250 range or higher.
Step 5: What If My Water Sensor Fails?
The power of the Ubidots system not only allows for you to test the data that arrives but you can also verify that your sensor continues to communicate with the server. I created a text alert if the sensor was not reporting at least once every 30 minutes. To do this just create a new alert based on your variable being inactive. In the odd case that the NodeMCU does have a fault then the Ubidots system will alert you that the sensor has not communicated recently. In testing I have had the system running full time for several weeks in a row without errors or reports of communication lost. I did have a prototype of this sensor sitting on a bench and accidentally knocked the USB cord out f the unit. 30 minutes later I was notified of the error so it does work.
Remember you can also log into your dashboard and view the data live as well. Good luck.
Participated in the
Automation Contest 2017