Recipe for Controlling an XBee Smart Plug Using Text/Email MessagesThis Recipe demonstrates how to control an XBee Smart Plug using text/email messages. Each time a message is sent, it will be acknowledged with a return message, so the sender knows what happened. IngredientsFor this Recipe, you will need:
Before installing the Smart Plug, inspect the Smart Plug case until you find its serial number label. It often has a bar code at the top, and has a string underneath that begins with "SN:(S)". The serial number follows the "SN:(S)" and is a string with the digits 0-9 and the letters A-F. The serial number is a 16 digit (64 bit) hexadecimal number (a number in base 16). Write the serial number down, as you will need it for this Recipe. Plug the Smart Plug into an electrical socket where you wish to control a light or appliance, and plug the XStick into a USB port on your host computer. Keep the XStick and the Smart Plug less then a few tens of feet apart to begin with, as the two are wireless devices and can get out of range. Once you have things working, you can experiment over greater distances. 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 "smtp.provider.com" and "pop.provider.com". 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. ScriptHere is the Script you need to run for this Recipe:
To run this Script, you need 9 parameters: coordinator_port, plug_address, pop_host, smtp_host, smtp_port, ssl_enable, user, password, and messaging_address. The plug_address parameter is the serial number of the Smart Plug. To convert the serial number to the plug_address parameter, strip any leading zeros from the serial number and replace them with "0x". The coordinator_port parameter is the name of the USB port of the Coordinator. 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 Coordinator'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 Smart Plug. As an example, for a Coordinator at USB port "/dev/cu.usbserial-00002006" and a Smart Plug with serial number "0013A200406B0263", 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:
Running the ScriptRun the Script above with your own parameters (remember, if you make a mistake, you can edit what you've done on the Console page). Go to the Device Explorer, click on the Smart Plug's "1" terminal, and enter
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 Smart Plug off, send a message back to the device that looks like this:
Depending on your polling interval (4 minutes in this Script), it may take a few minutes for this command to take effect. Email Devices, by default, quote their output values. In this Recipe, we don't want quoted values, we want unquoted Try sending something to a nonsensical output terminal (Device). For example, send the message:
Enjoy controlling your Smart Plug with your email/texting device. |