Water Detection System


Has this ever happened to you?

Ever find yourself in this situation…

It’s happened to me. But, worry no more with this water detection system which is fun to build and capable of saving you some $$$ when the plumbing busts.

This example shows how to use Node-RED on a RaspberryPi to send an email whenever a moisture reading exceeds a threshold. Node-RED connects to the LightBlue Bean once a minute, requests the moisture and sends an email if water is detected.

Before You Begin

This tutorial assumes you have completed the Getting Started guide and that you understand how to upload a sketch to Bean using Bean Loader.



What do the different nodes do?

Node-RED is a visual programming interface for the Internet of things. If you haven’t used Node-RED with the LightBlue Bean before, here is more information.

This example uses a RaspberryPi to run the Node-RED server. The node red server acts as an internet access point for the LightBlue Bean. Here are the full installation details for using the LightBlue Bean and Node-RED.

This is what the Node-RED flow for the project looks like:

flow diagram

1 min interval: Triggers the nodes it’s connected to on a set interval. In this example the interval is 60 seconds.

Bean serial: This is a node developed specifically for the LightBlue Bean. It connects to a specified Bean and allows you to send and receive serial messages.

Moisture threshold: Compares the current moisture to a high threshold. If it’s too wet it sends a string with the moisture reading to the next node.

Email: Sends the string passed from the previous node to a specified address.

Make It

Get Node-RED up and running on the RaspberryPi

Follow these instructions to set up Node-RED.

Raspberry Pi

Gather Other Parts

other components

Wire the Bean

Solder the wires accordingly and connect the Grove moisture sensor using the 4 pin male jumper to Grove conversion cable.

wiring diagram

And here's how it should look in real life:

how it should look

Program Your Bean

Upload the following sketch to your Bean or Bean+ to measure the moisture:

int sensorPin = A0;
int sensorValue = 0;

void setup() {

void loop() {
    sensorValue = analogRead(sensorPin);

Copy File to Clipboard

Copy the following JSON data to your clipboard:

  "name":"Water Sensor Bean",

  "type":"bean serial",
  "name":"Bean Serial",

  "name":"moisture threshold",
  "func":"context.previousMoisture = context.previousMoisture || 0;\n
            var moisture = msg.payload;\n\n
            var upperThreshold = 400;\n\n
            // Check if the new moisture is different from the previous\n
            if (context.previousMoisture != moisture) {\n
            // Check if moisture outside threshold\n\t
            if ( moisture >= upperThreshold){\n
            // Compose alert email\n\t\t
            msg.payload = \"Water detected! Current moisture is \"
                          + (String(moisture)-upperThreshold)
                          + \" above threshold.\";\n
            return msg;\n\t}\n}",

"name":"1 min interval",


Import Project File to Node-RED

Go to http://<your pi's ip>:1880/ in your browser. Click the menu in the top right corner and choose import > clipboard.

Node-RED import

Add Your Bean

Double click the Bean serial node and click on the pen to edit the Bean settings. Update the name to be the name of your Bean. It is important you write the exact name as it’s the only way for Node-RED to find and connect to the correct Bean.

Bean serial config

Set Up Email

Double click the email node and fill in the account details for your email.

set up email


Click deploy in the top right corner to update server.

Test Water Detection System

Place the sensor in water to send the warning email.