Introduction: Group Text Message Button (SMS)

About: Experience Designer

News Team Assemmmmbbbllle!!!

Hit a button and send a group mass text message. You can have it randomize through different messages and have the recipient's name right in the SMS too.

This tut is a Build, Build, Build style tut... You should have everything you need here to get it to work minus why it is working. To know why it is working the way it is, you'll have to dig deeper.
e.g. The role of the resistor... You won't find that here.


This uses:

  1. Particle - A WiFi Connected Arduino-like microcontroller - $19.00
  2. Parse cloud computing - The brains (Backend as a Service (BaaS)) - FREE (Limited)
  3. Twilio - The SMS sender - FREE (Limited)

Step 1: Prep

Prepare because road bumps can hamper your flow...
In this step, you should already have purchased and received your Particle Photon.

Prep. A - Computer tools :: Programs/Software that you'll need

Prep. B - Physical tools :: Bare minimum to get the button functioning

Prep. C - Accounts :: Sign-up/Open these free accounts

Prep. D - Particle smartphone App

Step 2: Get the Keys, Credentials, and Pertinent Info... Keep It Handy.

Get this account info/credentials and paste it into a non-formatted document, we'll call it NOTES.rtf, you'll need this in coding parts coming up later:

Parse

API Credentials (Found here: https://parse.com/apps > Settings > Keys)

  1. Application ID
  2. REST API Key

Twilio

API Credentials (Found here: Getting Started Page - Show API Credentials)

  1. AccountSID
  2. AuthToken

Particle

  1. Device Credential (Found here: https://build.particle.io/build) Device ID (aka coreID)


You have to claim your device before you can get the Device ID this is in the next step =>

Step 3: Particle - 1 of 4 :: Photon to WiFi

The Particle Photon is the IoT device with built-in WiFi. It is like a mini Arduino that connects to the internet and has a web based IDE for writing code into it wirelessly. Claiming your Photon and connecting it to the WiFi can be done with your smartphone very quickly.

The instructions for connecting it and all the troubleshooting already exist. Instead of rewriting it all here, we'll just link you there and wait for you here.

  1. Connecting your Photon instructions (You'll only seed steps 1 and 2 in those instructions)

Now write your Device Credentials into your NOTES.rtf
(Found here: https://build.particle.io/build) Device ID (aka coreID)

Step 4: Particle - 2 of 4 :: Button Hardware

Put your electronic together using this schematics image above.

Pop your Particle Photon (w/Headers), button, and resistor onto the breadboard as shown on the image. Place your jumper wires as shown. Power it up with your micro USB cable connected to your computer.

Notes: Make sure your momentary push button is connected properly. The button has four header pins but it's really just like a two pin button (see image)

Step 5: Particle - 3 of 4 :: Button Sketch

Your Photon button's light should be "Breathing cyan" indicating that it is connected to the internet.

Now we can use the Particle Build online IDE to upload a sketch to it.
This link is a quick pass at how to 'Flash' your app/sketch

The sketch/app:

static int button1 = A5; // Button signal
int led1 = D7; // Onboard LED for a button press feedback int prevState = 0;

void setup() { pinMode(button1, INPUT); pinMode(led1, OUTPUT);

Serial.begin(9600); }

void loop() { int currState = digitalRead(button1); if (currState == 0 && prevState == 1) { digitalWrite(led1, HIGH);

Serial.println("Photon button pressed!"); Spark.publish("myHook01"); // Calls the webhook living in Particle's cloud to talk to the Parse cloud delay(1000); } digitalWrite(led1, LOW); prevState = currState; }


Note:: Line: "Spark.publish("myHook01")"... myHook01 is case sensitive and will be the name of your webhook. We'll get to this later.

Flash this sketch into your Particle Photon

Step 6: Parse - 1 of 3 :: CLI

BeforeParticle 4 of 4 - Creating a webhookwe need to set up the Parse App

(node.js should be installed at this point)

Using Terminal we need to install the Parse CLI, login, and create an app.

  1. Using Terminal navigate to the directory where you want your project to live
  2. Install the CLI <= Follow these steps to create your app. Name your app SMSbutton
  3. Confirm the files were created in the directory Terminal is pointing to

The directory structure should look like the image. Instead of it saying 'myapp' it should say 'SMSbutton'. Now find the file: cloud > main.js. ...

Step 7: Parse - 2 of 3 :: App/Sketch

You've found the file main.js now delete everything inside of it and replace it with:

var TWILIO_ACCOUNT_SID = "YOURCREDENTIALS";
var TWILIO_AUTH_TOKEN = "YOURCREDENTIALS"; var twilio = require('twilio')(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);

var r_text = new Array (); r_text[0] = "Random message text here."; r_text[1] = "Random message text here."; r_text[2] = "Random message text here."; r_text[3] = "Random message text here."; r_text[4] = "Random message text here."; var i = Math.floor(5*Math.random())

// "myHook01_function" is what is triggered by the Particle webhook

Parse.Cloud.define("myHook01", function(request, response) { SMS.sendMessage({ success: function() { response.success("Eureka!"); }, error: function() { response.error("¡akeruE"); } }); });

// SMS

// make an associative array of people we know, indexed by phone number var people = [ ["+12341234567","Name1"], ["+12341234567","Name2"], ["+12341234567","Name3"], ["+12341234567","Name4"], ["+12341234567","Name5"] ];

var counter = 0;

var SMS = { sendMessage: function(response) {

people.forEach(function(person) { twilio.sms.messages.create({ to: person[0], from: "+12341234567", //Twilio number body: "Hey " + person[1] + "! " + r_text[i] + " #SMSbutton" }, function(error, msg) { if (error) { console.log(error); // callback.error(); } else { counter++; if (counter >= people.length) { response.success(); } } }); }

)} };


Donezo... Let's send it to the cloud.

Step 8: Parse - 3 of 3 :: App/Sketch Upload

Those changes are made locally. Now upload them to the Parse cloud using Terminal.

1. Make sure Terminal is pointing the Parse project directory i.e. SMSbutton that you created in Step 6

2. Run the Terminal command:

$ parse deploy

The Parse App SMSbutton is now waiting in the cloud for the Particle webhook to trigger the function inside of it.

Step 9: Particle - 4 of 4 :: Particle Webhook

Let's make a webhook that will live in the Particle Cloud.

  1. You'll need to install the Particle CLI
$ npm install -g particle-cli
$ particle cloud login

2. With Sublime text make a json file. 'File' > 'New'.

This code will live in there:

{
"event": "myHook01", "url": "https://api.parse.com/1/functions/myHook01", "coreID": "1a345f24534513833a8", "requestType": "POST", "headers": { "X-Parse-Application-Id" : "ZH4DVsxe60j32tIHCUWpKisrGLHZGPmYW8Asz6SW", "X-Parse-REST-API-Key" : "X2C2DQ8QWRyxR3Ju2snwY10KeLNMptHktNHXYqIn", "Content-Type" : "application/json" }, "query": null, "json": null, "auth": null, "mydevices": true }

'Save As' rename to myHook01.json and save it in your Parse App folder since terminal is already pointing there.

3. If Terminal is pointing to the folder where the file you just created lives it is time to run the command to create the webhook in the Particle Cloud

$ particle webhook create myHook01.json

The JSON file HAS to be in the older your Terminal is pointing to.

Step 10: Twilio

Twilio setup basics

When looking at your admin console this is where you find the things you'll need:

  • Get your first Twilio phone number on the 'Getting Started Page' - Enable SMS/MMS
  • Getting Started Page - Show API Credentials
  • Verified Caller IDs - The numbers you want to send to (Free version requires verification)

Step 11: Hit the Button

Start sending text messages