SmsWebhookRK
|
Library for Particle devices to easily send SMS via a webhook to Twilio
Note that both setup and loop calls are required! You should make the loop call on every loop; if there is nothing to do it returns quickly.
+1
.This queues the SMS message to send. If the device is online and connected to the Particle cloud, it will be sent immediately. Otherwise, it will be queued to send later.
If an error occurs and the publish fails, the message will be retried later.
If you are only sending SMS to yourself from your own devices, you can leave the recipient phone number out and instead encode it in the webhook, that way you don't need to code your phone number in your application code.
There are more options available as described in the Examples section, below.
This method could easily be updated to work with other SMS providers, however you may need to change authentication methods and the keys where the data is stored.
withEventName()
method to reconfigure the library. They must match and follow event naming rules (1 - 64 ASCII characters; only use letters, numbers, underscores, dashes and slashes. Spaces and special characters should not be used). Also note that the event name is a prefix, so any event name beginning with that string will trigger the webhook.$TWILIO_ACCOUNT_SID
!+
then country code, so it will begin with +1
in the United States.{{{t}}}
(case-sensitive). The triple curly brackets are required. If you are only going to ever send to your own phone, you could enter your phone number here instead of {{{t}}}
but make sure it begins with a +
.{{{b}}}
(case-sensitive).application/x-www-form-urlencoded
is correct.Here's the full code for the simple example. Tapping the MODE (or SETUP) button once will send a SMS.
Digging into the code:
Include the header file. Note that you must add the library as well, just adding the include is not sufficient.
This is so log message can be seen by the USB serial debugging output. The library uses "sms" as the logging category so you can turn off the messages if desired.
System threading enabled is recommended for this library. The code can be used in both AUTOMATIC
and SEMI_AUTOMATIC
mode.
This is a forward declaration, because we use the function in a call to System.on()
before it's implemented in the .cpp file.
The button handler can be called at interrupt time, so it's best to just set a flag and handle it from loop.
You must call the setup method for the library from global app setup()
. The library object is a singleton, which you always get using SmsWebhook::instance()
. You never create the object as a global or allocate it with new.
Add the button handler:
Since we used SYSTEM_MODE(SEMI_AUTOMATIC)
it's necessary to call Particle.connect()
to connect to the cloud. If you use AUTOMATIC
you don't need this.
You must call SmsWebhook::instance().loop()
from global application loop()
!
This checks the flag set by the button handler.
And this is the part that sends the SMS.
SmsMessage
object on the stack (it's small)withRecipient()
. Note that the phone number must be in + country code format, so in the United States it will begin with +1
.withMessage()
. In this case, we format a message with a counter that increases each time a message is sent.SmsWebhook::instance().queueSms(mesg)
.When the MODE/SETUP button is tapped once, this handles it and sets a flag to be handled from loop().
This example uses the CloudConfigRK library to store the recipient phone number in the emulated EEPROM on the device. This allows a per-device phone number, without requiring the phone number be hardcoded in the user firmware. This is also helpful if you are creating a product, so you can have individual customer phone numbers and have one firmware binary and one webhook.
The library has many options, including:
The code to hook the two libraries together and set the phone number with a function call (once), store it in EEPROM, then use it for any SMS notification is mostly just this:
To set the phone number make a function call setConfig
with the data
Remember that the phone number must be in + country code format, so in the United States it will begin with +1
.
If you want to use the Particle CLI, the command to set the phone number for the device named "test2" is:
Tapping the MODE (or SETUP) button sends the SMS to the configured phone number.