Communicate with your IoT device using SMS

Internet Button Internet of Things Prototyping

We demonstrate an easy setup that allows anyone to send commands to your Internet of Things (IoT) device and get feedback. Our device of choice is Particle’s Internet Button (11 LEDs & 4 buttons) and we use an Android phone as SMS gateway. IFTTT‘s simple if-this-then-that recipes connect the Internet Button and the gateway phone.

The following illustration shows the steps in a complete command/confirmation loop.

  1. Someone sends a text message with a command (e.g. LED 1 on/off) to your phone
  2. Your phone receives the message and triggers an IFTTT recipe
  3. IFTTT passes the message on to the Internet Button by calling a function
  4. The Internet Button interprets the message, executes the command and publishes a confirmation event
  5. The confirmation event triggers another IFTTT recipe that sends an SMS from your phone
  6. The original sender receives the command confirmation and is happy

The following sections explain how to make that happen.

Get your Internet Button ready

internet-button

Particle’s Internet Button is a Wi-Fi enabled IoT device that features 4 buttons, 11 RGB LEDs, a speaker as well as an accelerometer and is programmed over the air.

Your Internet Button needs to be able to do two things:

  • Expose an API that can be called from IFTTT
  • Publish events that trigger IFTTT recipes

Particle’s so-called Cloud Functions make this a breeze and the following code shows how:

// Include this lib with many useful functions
#include "InternetButton.h"
InternetButton b = InternetButton();

void setup() {
  // Tell b to get everything ready to go
  b.begin();
  // Expose function that can be called by IFTTT (Cloud Function)
  Particle.function("execute", executeAndInform);
}

void loop(){ /* nothing to do here */ }

// This is the implementation of the exposed function,
// it interprets a command, executes it and
// sends a confirmation to the sender
int executeAndInform(String senderAndCommand) {
  // Separate the sender from the command
  const String delimiter = ":-*-:";
  String sender = senderAndCommand.substring(0, senderAndCommand.indexOf(delimiter)).trim();
  String command = senderAndCommand.substring(senderAndCommand.indexOf(delimiter)+delimiter.length()).trim().toLowerCase();

  // Parse the command, e.g. LED 7 on
  int nofCommandsFound = 0;
  if (command.startsWith("led")) {
    command = command.substring(3).trim();
    int ledNr = command.toInt();
    if (command.endsWith("off")) {
      b.ledOff(ledNr);
    } else {
      b.ledOn(ledNr, 0, 30, 30);
    }
    nofCommandsFound++;
  }

  // Publish an event that can trigger an IFTT recipe (Cloud Function)
  if (nofCommandsFound > 0) {
    Particle.publish("commandexecuted", sender);
  } else {
    Particle.publish("commandnotfound", sender);
  }

  return nofCommandsFound;
}

That’s all there’s to it, just run the above code on your Internet Button and it’s ready. (Please refer to Particle’s documentation for getting started with the Internet Button. It’s straightforward and it shouldn’t take long until you can run your code.)

Now, let’s create the IFTTT recipes to connect your Internet Button to your phone.

Create the if-this-then-that (IFTTT) recipes

IFTTT recipes

IFTTT is a platform which lets you create simple rules (so-called recipes) connecting two services/devices without the need to write any code.

To create the three necessary recipes, you need:

  • An IFTTT account
  • The Android SMS Channel connected to your account
  • The Particle Channel connected to your account

The first recipe passes the command from the SMS gateway, i.e. your phone, to your Internet Button:

if any new SMS received,
then call a function "execute" on Internet Button
with input "{{FromNumber}}:-*-:{{Text}}"

The input will then be split and parsed as shown in the Internet Button code above.

The second recipe sends a confirmation SMS upon an event published by the Internet Button:

if your Internet Button published event "commandexecuted",
then send a confirmation SMS to "{{EventContents}}"

{{EventContents}} contains the original sender’s number as can be seen in the Internet Button code above.

The third recipe is a mere courtesy in case the Internet Button doesn’t understand the command:

if your Internet Button published event "commandnotfound",
then send an apology SMS to "{{EventContents}}"

That’s all. Now, your friends can talk to your Internet Button.

This is just one of a myriad of use cases you can achieve with these techniques – the possibilities for prototyping and real world applications are almost endless.

Interested?

Drop us a line if you’re interested in a hands-on experience. We’ll organize a workshop in the near future.

No Thoughts to Communicate with your IoT device using SMS

Leave a Reply