mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-30 16:24:09 +03:00
Temboo: library and examples update
This commit is contained in:
@ -8,26 +8,26 @@
|
||||
|
||||
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
||||
|
||||
A Temboo account and application key are necessary to run all Temboo examples.
|
||||
If you don't already have one, you can register for a free Temboo account at
|
||||
A Temboo account and application key are necessary to run all Temboo examples.
|
||||
If you don't already have one, you can register for a free Temboo account at
|
||||
http://www.temboo.com
|
||||
|
||||
Since this sketch uses Twilio to retrieve the SMS, you'll also need a valid
|
||||
Since this sketch uses Twilio to retrieve the SMS, you'll also need a valid
|
||||
Twilio account. You can create one for free at https://www.twilio.com.
|
||||
|
||||
The sketch needs your Twilio Account SID and Auth Token you get when you
|
||||
register with Twilio. Make sure to use the Account SID and Auth Token from
|
||||
|
||||
The sketch needs your Twilio Account SID and Auth Token you get when you
|
||||
register with Twilio. Make sure to use the Account SID and Auth Token from
|
||||
your Twilio Dashboard (not your test credentials from the Dev Tools panel).
|
||||
|
||||
Normally, Twilio expects to contact a web site you provide to get a response
|
||||
when an SMS message is received for your Twilio number. In this case, we
|
||||
when an SMS message is received for your Twilio number. In this case, we
|
||||
don't want to send any response (and we don't want to have to set up a web
|
||||
site just to receive SMS messages.) You can use a URL that Twilio provides
|
||||
for this purpose. When a message is received and sent to the Twilio "twimlets"
|
||||
URL, it returns a code meaning "no response required." To set this up:
|
||||
|
||||
1. Log in to your Twilio account and go to this URL:
|
||||
|
||||
|
||||
https://www.twilio.com/user/account/phone-numbers/incoming
|
||||
|
||||
2. Select the Twilio number you want to receive SMS messages at.
|
||||
@ -40,7 +40,7 @@
|
||||
|
||||
https://www.twilio.com/help/faq/sms/how-can-i-receive-sms-messages-without-responding
|
||||
|
||||
4. Click the "Save Changes" button at the bottom of the page.
|
||||
4. Click the "Save Changes" button at the bottom of the page.
|
||||
|
||||
Your account will now receive SMS messages, but won't send any responses.
|
||||
|
||||
@ -48,14 +48,14 @@
|
||||
to the Internet.
|
||||
|
||||
Looking for another API? We've got over 100 in our Library!
|
||||
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
#include <Bridge.h>
|
||||
#include <Temboo.h>
|
||||
#include "TembooAccount.h" // contains Temboo account information
|
||||
// as described in the footer comment below
|
||||
// as described in the footer comment below
|
||||
|
||||
|
||||
|
||||
@ -85,7 +85,7 @@ unsigned long lastSMSCheckTime = -SMS_CHECK_PERIOD;
|
||||
// (we only need to process newer messages)
|
||||
String lastSid;
|
||||
|
||||
// we'll be turning the LED built in to the Yun on and off
|
||||
// we'll be turning the LED built in to the Yun on and off
|
||||
// to simulate controlling some device. That LED is on pin 13.
|
||||
int LED_PIN = 13;
|
||||
|
||||
@ -98,7 +98,7 @@ void setup() {
|
||||
|
||||
// for debugging, wait until a serial console is connected
|
||||
delay(4000);
|
||||
while (!Serial);
|
||||
while(!Serial);
|
||||
|
||||
// tell the board to treat the LED pin as an output.
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
@ -109,7 +109,7 @@ void setup() {
|
||||
// initialize the connection to the Linino processor.
|
||||
Bridge.begin();
|
||||
|
||||
// Twilio will report old SMS messages. We want to
|
||||
// Twilio will report old SMS messages. We want to
|
||||
// ignore any existing control messages when we start.
|
||||
Serial.println("Ignoring any existing control messages...");
|
||||
checkForMessages(true);
|
||||
@ -124,15 +124,15 @@ void loop()
|
||||
|
||||
// see if it's time to check for new SMS messages.
|
||||
if (now - lastSMSCheckTime >= SMS_CHECK_PERIOD) {
|
||||
|
||||
|
||||
// it's time to check for new messages
|
||||
// save this time so we know when to check next
|
||||
lastSMSCheckTime = now;
|
||||
|
||||
|
||||
if (numRuns <= maxRuns) {
|
||||
Serial.println("Checking for new SMS messages - Run #" + String(numRuns++));
|
||||
|
||||
// execute the choreo and don't ignore control messages.
|
||||
|
||||
// execute the choreo and don't ignore control messages.
|
||||
checkForMessages(false);
|
||||
} else {
|
||||
Serial.println("Already ran " + String(maxRuns) + " times.");
|
||||
@ -145,7 +145,7 @@ This function executes the Twilio > SMSMessages > ListMessages choreo
|
||||
and processes the results.
|
||||
|
||||
If ignoreCommands is 'true', this function will read and process messages
|
||||
updating 'lastSid', but will not actually take any action on any commands
|
||||
updating 'lastSid', but will not actually take any action on any commands
|
||||
found. This is so we can ignore any old control messages when we start.
|
||||
|
||||
If ignoreCommands is 'false', control messages WILL be acted on.
|
||||
@ -156,7 +156,7 @@ void checkForMessages(bool ignoreCommands) {
|
||||
TembooChoreo ListMessagesChoreo;
|
||||
|
||||
ListMessagesChoreo.begin();
|
||||
|
||||
|
||||
// set Temboo account credentials
|
||||
ListMessagesChoreo.setAccountName(TEMBOO_ACCOUNT);
|
||||
ListMessagesChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
||||
@ -166,12 +166,12 @@ void checkForMessages(bool ignoreCommands) {
|
||||
ListMessagesChoreo.setChoreo("/Library/Twilio/SMSMessages/ListMessages");
|
||||
|
||||
// set the choreo inputs
|
||||
// see https://www.temboo.com/library/Library/Twilio/SMSMessages/ListMessages/
|
||||
// see https://www.temboo.com/library/Library/Twilio/SMSMessages/ListMessages/
|
||||
// for complete details about the inputs for this Choreo
|
||||
|
||||
// the first input is a your Twilio AccountSID
|
||||
ListMessagesChoreo.addInput("AccountSID", TWILIO_ACCOUNT_SID);
|
||||
|
||||
|
||||
// next is your Twilio Auth Token
|
||||
ListMessagesChoreo.addInput("AuthToken", TWILIO_AUTH_TOKEN);
|
||||
|
||||
@ -179,24 +179,24 @@ void checkForMessages(bool ignoreCommands) {
|
||||
ListMessagesChoreo.addInput("From", FROM_PHONE_NUMBER);
|
||||
|
||||
// Twilio can return information about up to 1000 messages at a time.
|
||||
// we're only interested in the 3 most recent ones. Note that if
|
||||
// this account receives lots of messages in quick succession,
|
||||
// we're only interested in the 3 most recent ones. Note that if
|
||||
// this account receives lots of messages in quick succession,
|
||||
// (more than 3 per minute in this case), we might miss some control
|
||||
// messages. But if we request too many messages, we might run out of
|
||||
// messages. But if we request too many messages, we might run out of
|
||||
// memory on the Arduino side of the Yun.
|
||||
ListMessagesChoreo.addInput("PageSize", "3");
|
||||
|
||||
// We want the response in XML format to process with our
|
||||
// We want the response in XML format to process with our
|
||||
// XPath output filters.
|
||||
ListMessagesChoreo.addInput("ResponseFormat", "xml");
|
||||
|
||||
// we don't want everything from the output, just the
|
||||
// we don't want everything from the output, just the
|
||||
// message IDs (the Sids) and the message texts
|
||||
ListMessagesChoreo.addOutputFilter("sid", "Sid", "Response");
|
||||
ListMessagesChoreo.addOutputFilter("text", "Body", "Response");
|
||||
|
||||
// tell the Choreo to run and wait for the results. The
|
||||
// return code (returnCode) will tell us whether the Temboo client
|
||||
// tell the Choreo to run and wait for the results. The
|
||||
// return code (returnCode) will tell us whether the Temboo client
|
||||
// was able to send our request to the Temboo servers
|
||||
unsigned int returnCode = ListMessagesChoreo.run();
|
||||
|
||||
@ -204,7 +204,7 @@ void checkForMessages(bool ignoreCommands) {
|
||||
if (returnCode == 0) {
|
||||
|
||||
// Need a string to hold the list of message IDs.
|
||||
String messageSids;
|
||||
String messageSids;
|
||||
|
||||
// Need a string to hold the texts of the messages.
|
||||
String messageTexts;
|
||||
@ -214,8 +214,8 @@ void checkForMessages(bool ignoreCommands) {
|
||||
// lists containing the Sids and texts of the messages
|
||||
// from our designated phone number.
|
||||
|
||||
while (ListMessagesChoreo.available()) {
|
||||
|
||||
while(ListMessagesChoreo.available()) {
|
||||
|
||||
// output names are terminated with '\x1F' characters.
|
||||
String name = ListMessagesChoreo.readStringUntil('\x1F');
|
||||
name.trim();
|
||||
@ -236,46 +236,46 @@ void checkForMessages(bool ignoreCommands) {
|
||||
|
||||
// done reading output, close the Choreo to free up resources.
|
||||
ListMessagesChoreo.close();
|
||||
|
||||
// parse the comma delimited lists of messages and Sids
|
||||
|
||||
// parse the comma delimited lists of messages and Sids
|
||||
processMessages(messageTexts, messageSids, ignoreCommands);
|
||||
|
||||
} else {
|
||||
// a non-zero return code means there was an error
|
||||
// read and print the error message
|
||||
while (ListMessagesChoreo.available()) {
|
||||
while(ListMessagesChoreo.available()) {
|
||||
char c = ListMessagesChoreo.read();
|
||||
Serial.print(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
This function processes the lists of message texts and Sids.
|
||||
If a message contains a comma as part of the
|
||||
If a message contains a comma as part of the
|
||||
message text, that message will be enclosed in double quotes
|
||||
(") in the list. Example:
|
||||
|
||||
A message,"Hey, now",Another message text
|
||||
|
||||
If the message contains double quotes, it will be enclosed in
|
||||
double quotes AND the internal quotes will be doubled.
|
||||
double quotes AND the internal quotes will be doubled.
|
||||
Example:
|
||||
|
||||
"Hi ""Sam"" the man", Led on
|
||||
|
||||
NOTE! We are assuming that Twilio returns more recent messages
|
||||
first. This isn't officially documented by Twilio, but we've
|
||||
first. This isn't officially documented by Twilio, but we've
|
||||
not seen any other case.
|
||||
|
||||
'messageTexts' is a String containing a comma separated list of
|
||||
message texts with commas and quotes escaped as described above.
|
||||
|
||||
'messageSids' is a String containing a comma separated list of
|
||||
'messageSids' is a String containing a comma separated list of
|
||||
message Sids. Sids should not contain embedded commas or quotes.
|
||||
|
||||
'ignoreCommands' is a boolean. 'true' means and control messages
|
||||
will not be acted upon. 'false' means control messages will be
|
||||
'ignoreCommands' is a boolean. 'true' means and control messages
|
||||
will not be acted upon. 'false' means control messages will be
|
||||
acted upon in the usual way.
|
||||
*/
|
||||
void processMessages(String messageTexts, String messageSids, bool ignoreCommands) {
|
||||
@ -297,14 +297,14 @@ void processMessages(String messageTexts, String messageSids, bool ignoreCommand
|
||||
// find the start of the next item in the list
|
||||
i = messageSids.indexOf(',', sidsStart);
|
||||
if (i >= 0) {
|
||||
|
||||
|
||||
//extract a single Sid from the list.
|
||||
sid = messageSids.substring(sidsStart, i);
|
||||
sidsStart = i + 1;
|
||||
|
||||
// find the start of the next text in the list.
|
||||
// find the start of the next text in the list.
|
||||
// Note that we have to be prepared to handle embedded
|
||||
// quotes and commans in the message texts.
|
||||
// quotes and commans in the message texts.
|
||||
// The standard Arduino String class doesn't handle
|
||||
// this, so we have to write our own function to do it.
|
||||
i = quotedIndexOf(messageTexts, ',', textsStart);
|
||||
@ -314,12 +314,12 @@ void processMessages(String messageTexts, String messageSids, bool ignoreCommand
|
||||
text = messageTexts.substring(textsStart, i);
|
||||
textsStart = i + 1;
|
||||
|
||||
// process the Sid and text to see if it's a
|
||||
// process the Sid and text to see if it's a
|
||||
// control message.
|
||||
ledUpdated = processMessage(sid, text, ignoreCommands);
|
||||
}
|
||||
} else {
|
||||
|
||||
|
||||
// the last item in the lists won't have a comma at the end,
|
||||
// so we have to handle them specially.
|
||||
// Since we know this is the last item, we can just
|
||||
@ -333,9 +333,9 @@ void processMessages(String messageTexts, String messageSids, bool ignoreCommand
|
||||
|
||||
// keep going until either we run out of list items
|
||||
// or we run into a message we processed on a previous run.
|
||||
} while ((i >= 0) && (sid != lastSid));
|
||||
|
||||
// print what we've found to the serial monitor,
|
||||
} while ((i >=0) && (sid != lastSid));
|
||||
|
||||
// print what we've found to the serial monitor,
|
||||
// just so we can see what's going on.
|
||||
if (sid == lastSid) {
|
||||
if (ledUpdated)
|
||||
@ -359,17 +359,17 @@ A message with the text "LED ON" turns the LED on.
|
||||
A message with the text "LED OFF" turns the LED off.
|
||||
(Case is ignored.)
|
||||
|
||||
If 'ignoreCommands' is true, the actions described above will NOT
|
||||
take place.
|
||||
If 'ignoreCommands' is true, the actions described above will NOT
|
||||
take place.
|
||||
|
||||
It also updates the 'lastSid' global variable when
|
||||
It also updates the 'lastSid' global variable when
|
||||
a control message is processed.
|
||||
|
||||
It returns 'true' if the message was a control message, and
|
||||
'false' if it wasn't or if we've already processed this message.
|
||||
*/
|
||||
bool processMessage(String sid, String text, bool ignoreCommands) {
|
||||
|
||||
|
||||
// a flag to indicate whether this was a control message or not
|
||||
bool ledUpdated = false;
|
||||
|
||||
@ -377,7 +377,7 @@ bool processMessage(String sid, String text, bool ignoreCommands) {
|
||||
if (sid != lastSid) {
|
||||
|
||||
if (text.equalsIgnoreCase("LED ON")) {
|
||||
|
||||
|
||||
if (!ignoreCommands) {
|
||||
//turn on the LED
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
@ -394,7 +394,7 @@ bool processMessage(String sid, String text, bool ignoreCommands) {
|
||||
}
|
||||
|
||||
// If the LED state was updated, remember the Sid if this message.
|
||||
if (ledUpdated)
|
||||
if (ledUpdated)
|
||||
lastSid = sid;
|
||||
}
|
||||
return ledUpdated;
|
||||
@ -428,16 +428,16 @@ int quotedIndexOf(String s, char delim, int start) {
|
||||
by inserting your own Temboo account name and app key information. The contents of the file should
|
||||
look like:
|
||||
|
||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
||||
|
||||
You can find your Temboo App Key information on the Temboo website,
|
||||
You can find your Temboo App Key information on the Temboo website,
|
||||
under My Account > Application Keys
|
||||
|
||||
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
||||
|
||||
Keeping your account information in a separate file means you can save it once,
|
||||
Keeping your account information in a separate file means you can save it once,
|
||||
then just distribute the main .ino file without worrying that you forgot to delete your credentials.
|
||||
*/
|
||||
|
||||
|
Reference in New Issue
Block a user