DIY ESP8266 WiFi Water Sensor With Text and Email Alerts




About: Analog Mixed Signal Electrical Engineer

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 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 account and observe live readings and trends to see if a potential leak may be happening.

What you will need:

1. A 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 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.

Automation Contest 2017

Participated in the
Automation Contest 2017



    • Sensors Contest

      Sensors Contest
    • Planter Challenge

      Planter Challenge
    • Beauty Tips Contest

      Beauty Tips Contest

    13 Discussions


    Question 15 days ago

    Is ubidots free? When I go to the website it looks like only a 30 day trial is free?

    3 answers

    Reply 14 days ago

    Ah, thanks! Looks like it could still end up costing over time depending on the number of texts and how long you have it connected right? First device is free, next device is 500 credits per month. Looks like they front load 5000 credits for free. Then it's 20 credits per text. Wonder if setting it up with webhooks and IFTTT would work. Any thoughts?


    Reply 13 days ago

    Yes you could but it will change the code as those systems do not play well with the UbiDots API. In general I found the first device is nearly free. I have more than one device so yes it charges for the next devices. Unless your sensor is alerting often the education account should be more than adequate for a water sensor.

    I made it! I have the sensor dangling in a sump pit in my basement, with the ESP8266 sitting on the floor next to the hole. I hope I won't ever need to receive that text message though...

    2 replies

    Reply 14 days ago

    That error typically comes from not having the Arduino environment setup properly to recognize the ESP8266 (NodeMCU) module. Read over the setup section carefully and reboot your system before compiling. From time to time I will have issues myself with programming and more often it's a fault with the board selected or possibly a bad USB cable.


    7 months ago

    Running into an issue with the sketch - apparently a library is missing PubSubClient.h and this sketch is dependent on it... Stuck for now.

    UPDATE - got it working - you need to have an additional library installed that is not mentioned in this tutorial.

    If you run into my same issue, please install this library and you should be good to go!

    2 replies

    Reply 14 days ago

    Thanks. Didn't realize that library was even needed.


    Question 2 months ago on Introduction

    Love it! I'm working on building my own now, however I do have a question. Where I install the fundunio water sensor, there is a chance that the sensor could be completely submerged. If that happens I assume there could be a risk of the 3.3v connector shorting out to the ground connector. Any suggestions for modifying the circuit to keep it from shorting out? (circuit protection, fuse, or run power or ground through an I/O pin to test votage?) I'm a complete beginner, so the answer may be obvious, but I'm afraid it currently alludes me. Thanks again!

    1 answer

    Answer 14 days ago

    Sorry for being late with a reply. Normally water is not all that conductive and even if the sensor is fully submerged the amount of current that will flow between the 3.3V and GND is going to be fairly small (in the microamps to perhaps a few milliamps). If you want to be careful you could insulate the wires at the top of the FUNDUINO board. Try soldered connections and paint "Plasti-DIP" on the small transistor amplifier and the resistors on the board. Cover the connector and wires as well. That will minimize any issues. You could fuse the item but the ESP has such a small low drop out regulator at most you may see a couple hundred mA even if dead shorted. A fuse for the sensor in the 0.1A or so range woud do well. Or you could fuse the entire board. Again it's not going to be very high and 0.1A or so will likely work fine. I believe the active current of the ESP is in the 70-80mA range while communicating on the WiFi.


    1 year ago

    Thanks for sharing :)