Software Overview  |  Sitemap  |  Downloads  |  Developers  |  Forums

Recipe that Beeps a Beeper and Sends a Text/Email Message When It Senses Water

This recipe uses an Arduino analog input pin to sense the voltage on a Grove Water Sensor. If the voltage from the sensor goes low, it beeps a beeper and sends a text/email message.


For this recipe you will need:

  • an Arduino (running Firmata)
  • a Grove Water Sensor (or equivalent)
  • a Grove Buzzer (or equivalent)
  • an email account

If you are not using Grove devices, any 5V buzzer that requires 40mA of current or less can be connected to the Arduino output and ground. Instead of a Grove Water Sensor, you can use two wires. One should be connected to ground, and the other pulled up with a ~1Meg resistor to 5V and connected to the Arduino analog input.

You will need an email account to which you can send messages via SMTP. Most email accounts support an SMTP interface. If you look on your provider's support page, you should find an address for sending messages to such as "". Once you have that, you can use your account name and password to send messages.

The address where your messages will be sent can be any email address. It can also be an email address for a text message (see the Email Addresses for Texting section). You can add additional addresses using the addEmailAddress Script.

Note that the Arduino analog input is a Schmitt trigger input. Schmitt triggers have good noise immunity, and this weak ~1Meg pullup resistor input is susceptible to noise.


You can find the Script in the Scripts "examples" area. The Script is called "moisture_detector_texting.script". The Script is as follows:

# Makes a moisture detector that turns on an LED or buzzes a buzzer when
# there's moisture, and it sends a text/email message as well.
# Requires an Arduino with a Grove board, a Grove moisture detector, and
# an LED or buzzer (or equivalents).  You'll also need a mail account you
# can SMTP messages to.
# This Script has defaults for the location of the moisture detector and
# the LED/buzzer.  The moisture detector is on A0, and the LED is on pin 3.
# You can override the defaults when you run this Script.

# Set up defaults

# LED/buzzer location
# moisture detector pin

# parameters for Arduino and its peripherals 

# arduino_port should be the file handle of the Arduino

optional_script_parameters(buzzer_pin:BUZZER_PIN, moisture_sensor_pin:MOISTURE_SENSOR_PIN)

# parameters for text/email Device
# to_address is the text/email address where messages will be sent
# smtp_host is host name for SMTP - e.g. ""
# smtp_user is the user's email address on the host - e.g ""
# password is the user's password on the SMTP host

script_parameters("to_address", "smtp_host", "password", "smtp_user")

optional_script_parameters(ssl_enable:true, smtp_port:587)

# will build on top of our buzzer based system, so start with it
run_script("Scripts/examples/arduino/moisture_detector_warning.script", arduino_port:arduino_port, buzzer_pin:buzzer_pin, moisture_sensor_pin:moisture_sensor_pin)

# add an alarm generator (which self resets after an hour)
run_script("Scripts/Device/Virtual/Alarm/Alarm", duration:3600, id:"alarm")

# give it an input which looks for a moisture alarm
run_script("Scripts/Device/Virtual/Alarm/InputTypes/Equal", alarm_id:"alarm", input_name:"moisture", value:"on")

# Create a text messaging device.
# No need for a "host" parameter, since are never polling this host for email.
run_script("Scripts/Device/EMail/TextMessagingPOP", email_address:to_address, gmail_polling:nil, host:"", id:"text_messenger", mail_subject:"Virtual Wiring Text Messenger", password:password, poll_interval:nil, smtp_host:smtp_host, smtp_port:smtp_port, ssl_enable:ssl_enable, user_name:smtp_user)

# wire in the alarm and email device
wire("inverter:out", "alarm:moisture")
wire("alarm:messages", "text_messenger:in")

To run this Script, you need 9 parameters: arduino_port, buzzer_pin, moisture_sensor_pin, smtp_host, smtp_port, smtp_user, ssl_enable, password, and to_address.

The arduino_port parameter is the name of the USB port of the Arduino. On *nix hosts, the USB port name is in the "/dev" directory. To see the devices in order of their creation, type "ls -lrt /dev" at a shell prompt and look near the bottom of the list for your Arduino's USB port.

The moisture_sensor_pin and buzzer_pin are the pin numbers on the Arduino where the moisture sensor and buzzer are connected.

The smtp_host and smtp_port are the name and port of the SMTP server hosting the email account. ssl_enable determines whether SSL will be used when talking to the server. The smtp_user parameter is the user name of the email account on the server and the password parameter is the password for the email account. The to_address is the email/texting address of the device that will be receiving text/email messages.

As an example, for an Arduino at USB port "/dev/ttyUSB0" with a buzzer at pin 3 and a moisture sensor at pin 2, an email server using SSL with name "email_server" with an email account of "user" and a password of "secret", and a text receiving device which is a Verizon cell phone with phone number 111-555-1212, the Script parameters are:

arduino_port: "/dev/ttyUSB0"
moisture_sensor_pin: 2
buzzer_pin: 3
smtp_host: "smtp.email_server"
smtp_port: 587
ssl_enable: true
smtp_user: "user"
password: "secret"
to_address: ""

Running the Script

Run the Script and squeeze the sensor between your fingers. If your fingers are a little moist, the buzzer will sound and you'll get a message. You can also make the buzzer sound by sticking the sensor in water. Make sure the water is not distilled water, however, as it is a poor conductor.

When you trip your moisture detector, here is what your message will look like:

 Alarm is on

 moisture: ALARM!

Catalina Computing, LLC.

Copyright © Catalina Computing, LLC. (2013-2018)

Page last updated: Mon May 14 03:01:02 2018 (UTC)