[136] | 1 | /*
|
---|
| 2 | SendAnEmail
|
---|
| 3 |
|
---|
| 4 | Demonstrates sending an email via a Google Gmail account using Temboo from an Arduino Y炭n.
|
---|
| 5 |
|
---|
| 6 | Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
---|
| 7 |
|
---|
| 8 | A Temboo account and application key are necessary to run all Temboo examples.
|
---|
| 9 | If you don't already have one, you can register for a free Temboo account at
|
---|
| 10 | http://www.temboo.com
|
---|
| 11 |
|
---|
| 12 | Since this sketch uses Gmail to send the email, you'll also need a valid
|
---|
| 13 | Google Gmail account. The sketch needs the username and password you use
|
---|
| 14 | to log into your Gmail account - substitute the placeholders below for these values.
|
---|
| 15 |
|
---|
| 16 | This example assumes basic familiarity with Arduino sketches, and that your Y炭n is connected
|
---|
| 17 | to the Internet.
|
---|
| 18 |
|
---|
| 19 | Looking for another API to use with your Arduino Y炭n? We've got over 100 in our Library!
|
---|
| 20 |
|
---|
| 21 | This example code is in the public domain.
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | #include <Bridge.h>
|
---|
| 25 | #include <Temboo.h>
|
---|
| 26 | #include "TembooAccount.h" // contains Temboo account information
|
---|
| 27 | // as described in the footer comment below
|
---|
| 28 |
|
---|
| 29 | /*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
---|
| 30 |
|
---|
| 31 | // Note that for additional security and reusability, you could
|
---|
| 32 | // use #define statements to specify these values in a .h file.
|
---|
| 33 |
|
---|
| 34 | // your Gmail username, formatted as a complete email address, eg "bob.smith@gmail.com"
|
---|
| 35 | const String GMAIL_USER_NAME = "xxxxxxxxxx";
|
---|
| 36 |
|
---|
| 37 | // your Gmail password
|
---|
| 38 | const String GMAIL_PASSWORD = "xxxxxxxxxx";
|
---|
| 39 |
|
---|
| 40 | // the email address you want to send the email to, eg "jane.doe@temboo.com"
|
---|
| 41 | const String TO_EMAIL_ADDRESS = "xxxxxxxxxx";
|
---|
| 42 |
|
---|
| 43 | // a flag to indicate whether we've tried to send the email yet or not
|
---|
| 44 | boolean attempted = false;
|
---|
| 45 |
|
---|
| 46 | void setup() {
|
---|
| 47 | Serial.begin(9600);
|
---|
| 48 |
|
---|
| 49 | // for debugging, wait until a serial console is connected
|
---|
| 50 | delay(4000);
|
---|
| 51 | while(!Serial);
|
---|
| 52 |
|
---|
| 53 | Bridge.begin();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void loop()
|
---|
| 57 | {
|
---|
| 58 | // only try to send the email if we haven't already tried
|
---|
| 59 | if (!attempted) {
|
---|
| 60 |
|
---|
| 61 | Serial.println("Running SendAnEmail...");
|
---|
| 62 |
|
---|
| 63 | TembooChoreo SendEmailChoreo;
|
---|
| 64 |
|
---|
| 65 | // invoke the Temboo client
|
---|
| 66 | // NOTE that the client must be reinvoked, and repopulated with
|
---|
| 67 | // appropriate arguments, each time its run() method is called.
|
---|
| 68 | SendEmailChoreo.begin();
|
---|
| 69 |
|
---|
| 70 | // set Temboo account credentials
|
---|
| 71 | SendEmailChoreo.setAccountName(TEMBOO_ACCOUNT);
|
---|
| 72 | SendEmailChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
---|
| 73 | SendEmailChoreo.setAppKey(TEMBOO_APP_KEY);
|
---|
| 74 |
|
---|
| 75 | // identify the Temboo Library choreo to run (Google > Gmail > SendEmail)
|
---|
| 76 | SendEmailChoreo.setChoreo("/Library/Google/Gmail/SendEmail");
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | // set the required choreo inputs
|
---|
| 80 | // see https://www.temboo.com/library/Library/Google/Gmail/SendEmail/
|
---|
| 81 | // for complete details about the inputs for this Choreo
|
---|
| 82 |
|
---|
| 83 | // the first input is your Gmail email address.
|
---|
| 84 | SendEmailChoreo.addInput("Username", GMAIL_USER_NAME);
|
---|
| 85 | // next is your Gmail password.
|
---|
| 86 | SendEmailChoreo.addInput("Password", GMAIL_PASSWORD);
|
---|
| 87 | // who to send the email to
|
---|
| 88 | SendEmailChoreo.addInput("ToAddress", TO_EMAIL_ADDRESS);
|
---|
| 89 | // then a subject line
|
---|
| 90 | SendEmailChoreo.addInput("Subject", "ALERT: Greenhouse Temperature");
|
---|
| 91 |
|
---|
| 92 | // next comes the message body, the main content of the email
|
---|
| 93 | SendEmailChoreo.addInput("MessageBody", "Hey! The greenhouse is too cold!");
|
---|
| 94 |
|
---|
| 95 | // tell the Choreo to run and wait for the results. The
|
---|
| 96 | // return code (returnCode) will tell us whether the Temboo client
|
---|
| 97 | // was able to send our request to the Temboo servers
|
---|
| 98 | unsigned int returnCode = SendEmailChoreo.run();
|
---|
| 99 |
|
---|
| 100 | // a return code of zero (0) means everything worked
|
---|
| 101 | if (returnCode == 0) {
|
---|
| 102 | Serial.println("Success! Email sent!");
|
---|
| 103 | } else {
|
---|
| 104 | // a non-zero return code means there was an error
|
---|
| 105 | // read and print the error message
|
---|
| 106 | while (SendEmailChoreo.available()) {
|
---|
| 107 | char c = SendEmailChoreo.read();
|
---|
| 108 | Serial.print(c);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | SendEmailChoreo.close();
|
---|
| 112 |
|
---|
| 113 | // set the flag showing we've tried
|
---|
| 114 | attempted = true;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /*
|
---|
| 119 | IMPORTANT NOTE: TembooAccount.h:
|
---|
| 120 |
|
---|
| 121 | TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
---|
| 122 | You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
---|
| 123 | by inserting your own Temboo account name and app key information. The contents of the file should
|
---|
| 124 | look like:
|
---|
| 125 |
|
---|
| 126 | #define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
---|
| 127 | #define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
---|
| 128 | #define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
---|
| 129 |
|
---|
| 130 | You can find your Temboo App Key information on the Temboo website,
|
---|
| 131 | under My Account > Application Keys
|
---|
| 132 |
|
---|
| 133 | The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
---|
| 134 |
|
---|
| 135 | Keeping your account information in a separate file means you can share the main .ino file without worrying
|
---|
| 136 | that you forgot to delete your credentials.
|
---|
| 137 | */
|
---|