Software Overview  |  Sitemap  |  Downloads  |  Developers  |  Forums

Recipe for Controlling an LED and Relay Using Text/Email Messages

This recipe demonstrates how to control multiple Devices using text/email messages. Here we will control and LED and a relay, but the recipe could be changed to support most any Device and any number of Devices. Each time a message is sent, it will be acknowledged with a return message, so the sender knows what happened.


For this recipe, you will need:

  • an Arduino (running Firmata)
  • a Grove LED (or equivalent)
  • a Grove Relay (or any 5V <40mA relay)
  • a dedicated email account

You need an email account to which you can send messages to via SMTP and receive from using POP. Most email accounts support SMTP and POP interfaces. If you look on your provider's support page, you should find addresses for sending and receiving messages such as "" and "". Once you know these addresses, you can use your account name and password to send and receive 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.

Unlike configurations which only send messages, this Recipe can receive and respond to messages as well. Receiving requires that the Recipe poll your email account for email. Don't use an email account which has email you wish to keep, or which is receiving email you wish to read. When this Recipe picks up the mail, it cleans out the mailbox. It will act upon messages from users it recognizes, but all the messages it receives will be deleted.


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

# Control an LED and a relay using email/text messages.
# Returns a status message for each control message.
# Requires an Arduino, an LED, and a relay.\
# The pin location of the LED is pin 3, and the relay is pin 5.

# set up device pin locations

# Get the file name of the Arduino device.
# On *nix systems, the file will be in the "/dev" directory with
# a name like "/dev/cu.usbmodem411" or "/dev/ttyUSB0".
# On Windows systems, the name will be something like "COM1", "COM2", ....

# parameters for the Wired Messaging Device
# messaging_address is the text/email address to which and from where messages
# will be sent and received
# pop_host is the host name for POP polling - e.g. ""
# smtp_host is host name for SMTP - e.g. ""
# user is the user's email address on the host - e.g ""
# password is the user's password for the email account

script_parameters("messaging_address", "pop_host", "smtp_host", "password", "user")

optional_script_parameters(ssl_enable:true, smtp_port:587)

# arduino device
run_script("Scripts/Device/Arduino/Arduino", port_location:arduino_port, id:"arduino")

# create LED and relay outputs
run_script("Scripts/Device/Arduino/PinTypes/DigitalOutputs", names:"led", pins:LED_PIN, arduino_id:"arduino")
run_script("Scripts/Device/Arduino/PinTypes/DigitalOutputs", names:"relay", pins:RELAY_PIN, arduino_id:"arduino")

# create a wired messaging device
# Poll every 240 seconds (4 minutes)
run_script("Scripts/Device/EMail/WiredMessagingPOP", email_address:messaging_address, host:pop_host, id:"wired_messenger", inputs:"led_status, relay_status", mail_subject:"Virtual Wired Text Messenger", outputs:"led, relay", password:password, poll_interval:240, smtp_host:smtp_host, smtp_port:smtp_port, ssl_enable:ssl_enable, user_name:user)

# don't add quotes to what comes from our output terminals
run_script("", text:"define_avoid_quotes_terminals(\"led, relay\")", on_device:"wired_messenger")

# wire things together
wire("wired_messenger:led", "arduino:led")
wire("wired_messenger:relay", "arduino:relay")
wire("wired_messenger:led", "wired_messenger:led_status")
wire("wired_messenger:relay", "wired_messenger:relay_status")

To run this Script, you need 8 parameters: arduino_port, pop_host, smtp_host, smtp_port, ssl_enable, user, password, and messaging_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 pop_host is the name of the POP server hosting the email account, and 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 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 messaging_address is the email/texting address of the device that will be controlling the system.

As an example, for an Arduino at USB port "/dev/cu.usbserial-00002006", an email server using SSL with name "email_server" with an email account of "user" and a password of "secret", and a controlling device which is a Verizon cell phone with phone number 111-555-1212, the Script parameters are:

arduino_port: "/dev/cu.usbserial-00002006"
pop_host: "pop.email_server"
smtp_host: "smtp.email_server"
smtp_port: 587
ssl_enable: true
user: "user"
password: "secret"
messaging_address: ""

Running the Script

Run the Script above. Go to the Device Explorer, click on the Arduino's "led" terminal, and enter on. The LED should turn on. Next check the device you are using for messaging. You should receive a message that looks like this:

led_status: on

This recipe uses a Wired Messaging POP Device. The Wired Messaging Device supports multiple outputs and inputs, so we can control multiple devices with it. In order to tell the Device which output terminal we are addressing, messages to the Device have 2 values - a terminal name and a value. To turn the LED off, send a message back to the device that looks like this:

led off

Email Devices, by default, quote their output values. In this recipe, we don't want quoted values, we want unquoted on and off values - the only values the "led" and "relay" understand. To make the "led" and "relay" values be unquoted, we call the "define_avoid_quotes_terminals" method (see Script below).

Try sending something to a nonsensical Device. For example, send the message: xx off. You should get back a message:

Error, unknown destination in command: "xx off"
Possible destinations are: led, relay

Catalina Computing, LLC.

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

Page last updated: Tue Oct 13 12:56:49 2015 (UTC)