8
8
9
9
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
10
10
11
- A Temboo account and application key are necessary to run all Temboo examples.
12
- If you don't already have one, you can register for a free Temboo account at
11
+ A Temboo account and application key are necessary to run all Temboo examples.
12
+ If you don't already have one, you can register for a free Temboo account at
13
13
http://www.temboo.com
14
14
15
- Since this sketch uses Twilio to retrieve the SMS, you'll also need a valid
15
+ Since this sketch uses Twilio to retrieve the SMS, you'll also need a valid
16
16
Twilio account. You can create one for free at https://www.twilio.com.
17
-
18
- The sketch needs your Twilio Account SID and Auth Token you get when you
19
- register with Twilio. Make sure to use the Account SID and Auth Token from
17
+
18
+ The sketch needs your Twilio Account SID and Auth Token you get when you
19
+ register with Twilio. Make sure to use the Account SID and Auth Token from
20
20
your Twilio Dashboard (not your test credentials from the Dev Tools panel).
21
21
22
22
Normally, Twilio expects to contact a web site you provide to get a response
23
- when an SMS message is received for your Twilio number. In this case, we
23
+ when an SMS message is received for your Twilio number. In this case, we
24
24
don't want to send any response (and we don't want to have to set up a web
25
25
site just to receive SMS messages.) You can use a URL that Twilio provides
26
26
for this purpose. When a message is received and sent to the Twilio "twimlets"
27
27
URL, it returns a code meaning "no response required." To set this up:
28
28
29
29
1. Log in to your Twilio account and go to this URL:
30
-
30
+
31
31
https://www.twilio.com/user/account/phone-numbers/incoming
32
32
33
33
2. Select the Twilio number you want to receive SMS messages at.
40
40
41
41
https://www.twilio.com/help/faq/sms/how-can-i-receive-sms-messages-without-responding
42
42
43
- 4. Click the "Save Changes" button at the bottom of the page.
43
+ 4. Click the "Save Changes" button at the bottom of the page.
44
44
45
45
Your account will now receive SMS messages, but won't send any responses.
46
46
47
47
This example assumes basic familiarity with Arduino sketches, and that your Yun is connected
48
48
to the Internet.
49
49
50
50
Looking for another API? We've got over 100 in our Library!
51
-
51
+
52
52
This example code is in the public domain.
53
53
*/
54
54
55
55
#include < Bridge.h>
56
56
#include < Temboo.h>
57
57
#include " TembooAccount.h" // contains Temboo account information
58
- // as described in the footer comment below
58
+ // as described in the footer comment below
59
59
60
60
61
61
@@ -85,7 +85,7 @@ unsigned long lastSMSCheckTime = -SMS_CHECK_PERIOD;
85
85
// (we only need to process newer messages)
86
86
String lastSid;
87
87
88
- // we'll be turning the LED built in to the Yun on and off
88
+ // we'll be turning the LED built in to the Yun on and off
89
89
// to simulate controlling some device. That LED is on pin 13.
90
90
int LED_PIN = 13 ;
91
91
@@ -98,7 +98,7 @@ void setup() {
98
98
99
99
// for debugging, wait until a serial console is connected
100
100
delay (4000 );
101
- while (!Serial);
101
+ while (!Serial);
102
102
103
103
// tell the board to treat the LED pin as an output.
104
104
pinMode (LED_PIN, OUTPUT);
@@ -109,7 +109,7 @@ void setup() {
109
109
// initialize the connection to the Linino processor.
110
110
Bridge.begin ();
111
111
112
- // Twilio will report old SMS messages. We want to
112
+ // Twilio will report old SMS messages. We want to
113
113
// ignore any existing control messages when we start.
114
114
Serial.println (" Ignoring any existing control messages..." );
115
115
checkForMessages (true );
@@ -124,15 +124,15 @@ void loop()
124
124
125
125
// see if it's time to check for new SMS messages.
126
126
if (now - lastSMSCheckTime >= SMS_CHECK_PERIOD) {
127
-
127
+
128
128
// it's time to check for new messages
129
129
// save this time so we know when to check next
130
130
lastSMSCheckTime = now;
131
-
131
+
132
132
if (numRuns <= maxRuns) {
133
133
Serial.println (" Checking for new SMS messages - Run #" + String (numRuns++));
134
-
135
- // execute the choreo and don't ignore control messages.
134
+
135
+ // execute the choreo and don't ignore control messages.
136
136
checkForMessages (false );
137
137
} else {
138
138
Serial.println (" Already ran " + String (maxRuns) + " times." );
@@ -145,7 +145,7 @@ This function executes the Twilio > SMSMessages > ListMessages choreo
145
145
and processes the results.
146
146
147
147
If ignoreCommands is 'true', this function will read and process messages
148
- updating 'lastSid', but will not actually take any action on any commands
148
+ updating 'lastSid', but will not actually take any action on any commands
149
149
found. This is so we can ignore any old control messages when we start.
150
150
151
151
If ignoreCommands is 'false', control messages WILL be acted on.
@@ -156,7 +156,7 @@ void checkForMessages(bool ignoreCommands) {
156
156
TembooChoreo ListMessagesChoreo;
157
157
158
158
ListMessagesChoreo.begin ();
159
-
159
+
160
160
// set Temboo account credentials
161
161
ListMessagesChoreo.setAccountName (TEMBOO_ACCOUNT);
162
162
ListMessagesChoreo.setAppKeyName (TEMBOO_APP_KEY_NAME);
@@ -166,45 +166,45 @@ void checkForMessages(bool ignoreCommands) {
166
166
ListMessagesChoreo.setChoreo (" /Library/Twilio/SMSMessages/ListMessages" );
167
167
168
168
// set the choreo inputs
169
- // see https://www.temboo.com/library/Library/Twilio/SMSMessages/ListMessages/
169
+ // see https://www.temboo.com/library/Library/Twilio/SMSMessages/ListMessages/
170
170
// for complete details about the inputs for this Choreo
171
171
172
172
// the first input is a your Twilio AccountSID
173
173
ListMessagesChoreo.addInput (" AccountSID" , TWILIO_ACCOUNT_SID);
174
-
174
+
175
175
// next is your Twilio Auth Token
176
176
ListMessagesChoreo.addInput (" AuthToken" , TWILIO_AUTH_TOKEN);
177
177
178
178
// we only want to know about messages sent from our designated phone number
179
179
ListMessagesChoreo.addInput (" From" , FROM_PHONE_NUMBER);
180
180
181
181
// Twilio can return information about up to 1000 messages at a time.
182
- // we're only interested in the 3 most recent ones. Note that if
183
- // this account receives lots of messages in quick succession,
182
+ // we're only interested in the 3 most recent ones. Note that if
183
+ // this account receives lots of messages in quick succession,
184
184
// (more than 3 per minute in this case), we might miss some control
185
- // messages. But if we request too many messages, we might run out of
185
+ // messages. But if we request too many messages, we might run out of
186
186
// memory on the Arduino side of the Yun.
187
187
ListMessagesChoreo.addInput (" PageSize" , " 3" );
188
188
189
- // We want the response in XML format to process with our
189
+ // We want the response in XML format to process with our
190
190
// XPath output filters.
191
191
ListMessagesChoreo.addInput (" ResponseFormat" , " xml" );
192
192
193
- // we don't want everything from the output, just the
193
+ // we don't want everything from the output, just the
194
194
// message IDs (the Sids) and the message texts
195
195
ListMessagesChoreo.addOutputFilter (" sid" , " Sid" , " Response" );
196
196
ListMessagesChoreo.addOutputFilter (" text" , " Body" , " Response" );
197
197
198
- // tell the Choreo to run and wait for the results. The
199
- // return code (returnCode) will tell us whether the Temboo client
198
+ // tell the Choreo to run and wait for the results. The
199
+ // return code (returnCode) will tell us whether the Temboo client
200
200
// was able to send our request to the Temboo servers
201
201
unsigned int returnCode = ListMessagesChoreo.run ();
202
202
203
203
// a return code of zero (0) means success
204
204
if (returnCode == 0 ) {
205
205
206
206
// Need a string to hold the list of message IDs.
207
- String messageSids;
207
+ String messageSids;
208
208
209
209
// Need a string to hold the texts of the messages.
210
210
String messageTexts;
@@ -214,8 +214,8 @@ void checkForMessages(bool ignoreCommands) {
214
214
// lists containing the Sids and texts of the messages
215
215
// from our designated phone number.
216
216
217
- while (ListMessagesChoreo.available ()) {
218
-
217
+ while (ListMessagesChoreo.available ()) {
218
+
219
219
// output names are terminated with '\x1F' characters.
220
220
String name = ListMessagesChoreo.readStringUntil (' \x1F ' );
221
221
name.trim ();
@@ -236,46 +236,46 @@ void checkForMessages(bool ignoreCommands) {
236
236
237
237
// done reading output, close the Choreo to free up resources.
238
238
ListMessagesChoreo.close ();
239
-
240
- // parse the comma delimited lists of messages and Sids
239
+
240
+ // parse the comma delimited lists of messages and Sids
241
241
processMessages (messageTexts, messageSids, ignoreCommands);
242
242
243
243
} else {
244
244
// a non-zero return code means there was an error
245
245
// read and print the error message
246
- while (ListMessagesChoreo.available ()) {
246
+ while (ListMessagesChoreo.available ()) {
247
247
char c = ListMessagesChoreo.read ();
248
248
Serial.print (c);
249
249
}
250
250
}
251
251
}
252
252
253
- /*
253
+ /*
254
254
This function processes the lists of message texts and Sids.
255
- If a message contains a comma as part of the
255
+ If a message contains a comma as part of the
256
256
message text, that message will be enclosed in double quotes
257
257
(") in the list. Example:
258
258
259
259
A message,"Hey, now",Another message text
260
260
261
261
If the message contains double quotes, it will be enclosed in
262
- double quotes AND the internal quotes will be doubled.
262
+ double quotes AND the internal quotes will be doubled.
263
263
Example:
264
264
265
265
"Hi ""Sam"" the man", Led on
266
266
267
267
NOTE! We are assuming that Twilio returns more recent messages
268
- first. This isn't officially documented by Twilio, but we've
268
+ first. This isn't officially documented by Twilio, but we've
269
269
not seen any other case.
270
270
271
271
'messageTexts' is a String containing a comma separated list of
272
272
message texts with commas and quotes escaped as described above.
273
273
274
- 'messageSids' is a String containing a comma separated list of
274
+ 'messageSids' is a String containing a comma separated list of
275
275
message Sids. Sids should not contain embedded commas or quotes.
276
276
277
- 'ignoreCommands' is a boolean. 'true' means and control messages
278
- will not be acted upon. 'false' means control messages will be
277
+ 'ignoreCommands' is a boolean. 'true' means and control messages
278
+ will not be acted upon. 'false' means control messages will be
279
279
acted upon in the usual way.
280
280
*/
281
281
void processMessages (String messageTexts, String messageSids, bool ignoreCommands) {
@@ -297,14 +297,14 @@ void processMessages(String messageTexts, String messageSids, bool ignoreCommand
297
297
// find the start of the next item in the list
298
298
i = messageSids.indexOf (' ,' , sidsStart);
299
299
if (i >= 0 ) {
300
-
300
+
301
301
// extract a single Sid from the list.
302
302
sid = messageSids.substring (sidsStart, i);
303
303
sidsStart = i + 1 ;
304
304
305
- // find the start of the next text in the list.
305
+ // find the start of the next text in the list.
306
306
// Note that we have to be prepared to handle embedded
307
- // quotes and commans in the message texts.
307
+ // quotes and commans in the message texts.
308
308
// The standard Arduino String class doesn't handle
309
309
// this, so we have to write our own function to do it.
310
310
i = quotedIndexOf (messageTexts, ' ,' , textsStart);
@@ -314,12 +314,12 @@ void processMessages(String messageTexts, String messageSids, bool ignoreCommand
314
314
text = messageTexts.substring (textsStart, i);
315
315
textsStart = i + 1 ;
316
316
317
- // process the Sid and text to see if it's a
317
+ // process the Sid and text to see if it's a
318
318
// control message.
319
319
ledUpdated = processMessage (sid, text, ignoreCommands);
320
320
}
321
321
} else {
322
-
322
+
323
323
// the last item in the lists won't have a comma at the end,
324
324
// so we have to handle them specially.
325
325
// Since we know this is the last item, we can just
@@ -333,9 +333,9 @@ void processMessages(String messageTexts, String messageSids, bool ignoreCommand
333
333
334
334
// keep going until either we run out of list items
335
335
// or we run into a message we processed on a previous run.
336
- } while ((i >= 0 ) && (sid != lastSid));
337
-
338
- // print what we've found to the serial monitor,
336
+ } while ((i >=0 ) && (sid != lastSid));
337
+
338
+ // print what we've found to the serial monitor,
339
339
// just so we can see what's going on.
340
340
if (sid == lastSid) {
341
341
if (ledUpdated)
@@ -359,25 +359,25 @@ A message with the text "LED ON" turns the LED on.
359
359
A message with the text "LED OFF" turns the LED off.
360
360
(Case is ignored.)
361
361
362
- If 'ignoreCommands' is true, the actions described above will NOT
363
- take place.
362
+ If 'ignoreCommands' is true, the actions described above will NOT
363
+ take place.
364
364
365
- It also updates the 'lastSid' global variable when
365
+ It also updates the 'lastSid' global variable when
366
366
a control message is processed.
367
367
368
368
It returns 'true' if the message was a control message, and
369
369
'false' if it wasn't or if we've already processed this message.
370
370
*/
371
371
bool processMessage (String sid, String text, bool ignoreCommands) {
372
-
372
+
373
373
// a flag to indicate whether this was a control message or not
374
374
bool ledUpdated = false ;
375
375
376
376
// if we haven't already processed this message
377
377
if (sid != lastSid) {
378
378
379
379
if (text.equalsIgnoreCase (" LED ON" )) {
380
-
380
+
381
381
if (!ignoreCommands) {
382
382
// turn on the LED
383
383
digitalWrite (LED_PIN, HIGH);
@@ -394,7 +394,7 @@ bool processMessage(String sid, String text, bool ignoreCommands) {
394
394
}
395
395
396
396
// If the LED state was updated, remember the Sid if this message.
397
- if (ledUpdated)
397
+ if (ledUpdated)
398
398
lastSid = sid;
399
399
}
400
400
return ledUpdated;
@@ -428,16 +428,16 @@ int quotedIndexOf(String s, char delim, int start) {
428
428
by inserting your own Temboo account name and app key information. The contents of the file should
429
429
look like:
430
430
431
- #define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
431
+ #define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
432
432
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
433
433
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
434
434
435
- You can find your Temboo App Key information on the Temboo website,
435
+ You can find your Temboo App Key information on the Temboo website,
436
436
under My Account > Application Keys
437
437
438
438
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
439
439
440
- Keeping your account information in a separate file means you can save it once,
440
+ Keeping your account information in a separate file means you can save it once,
441
441
then just distribute the main .ino file without worrying that you forgot to delete your credentials.
442
442
*/
443
443
0 commit comments