You've already generated all of the code you need to send an SMS through Twilio using your Arduino, so you only need to add a couple of extra lines to your sketch to trigger those messages with your push button. First, initialize the pin that you will be reading (recall that we chose to use pin 8) by adding the following lines of code to what you already have in void setup():
// Initialize pin pinMode(8, INPUT);
Your setup should now look something like this:
void setup() { Serial.begin(9600); // For debugging, wait until the serial console is connected. delay(4000); while(!Serial); status_t wifiStatus = STATUS_DISCONNECTED; while (wifiStatus != STATUS_CONNECTED) { Serial.print("WiFi:"); if (cc3k.begin()) { if (cc3k.connectToAP(WIFI_SSID, WPA_PASSWORD, WLAN_SEC_WPA2)) { wifiStatus = cc3k.getStatus(); } } if (wifiStatus == STATUS_CONNECTED) { Serial.println("OK"); } else { Serial.println("FAIL"); } delay(5000); } // Initialize pin pinMode(8, INPUT); Serial.println("Setup complete.\n"); }
Then, add the following two lines of code to void loop() to set the SendSMS Choreo to run only when the button is pressed:
int sensorValue = digitalRead(8); if (sensorValue == HIGH) {
You should nest the code that you already have in void loop() within this new conditional, so that it should now look something like this:
void loop() { int sensorValue = digitalRead(8); if (sensorValue == HIGH) { if (numRuns <= maxRuns) { Serial.println("Running SendSMS - Run #" + String(numRuns++)); TembooChoreo SendSMSChoreo(client); // Invoke the Temboo client SendSMSChoreo.begin(); // Set Temboo account credentials SendSMSChoreo.setAccountName(TEMBOO_ACCOUNT); SendSMSChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); SendSMSChoreo.setAppKey(TEMBOO_APP_KEY); // Set Choreo inputs String AuthTokenValue = "PLACEHOLDER"; SendSMSChoreo.addInput("AuthToken", AuthTokenValue); String BodyValue = "PLACEHOLDER"; SendSMSChoreo.addInput("Body", BodyValue); String ToValue = "PLACEHOLDER"; SendSMSChoreo.addInput("To", ToValue); String AccountSIDValue = "PLACEHOLDER"; SendSMSChoreo.addInput("AccountSID", AccountSIDValue); String FromValue = "PLACEHOLDER"; SendSMSChoreo.addInput("From", FromValue); // Identify the Choreo to run SendSMSChoreo.setChoreo("/Library/Twilio/SMSMessages/SendSMS"); // Run the Choreo; when results are available, print them to serial SendSMSChoreo.run(); while(SendSMSChoreo.available()) { char c = SendSMSChoreo.read(); Serial.print(c); } SendSMSChoreo.close(); } Serial.println("\nWaiting...\n"); delay(30000); // wait 30 seconds between SendSMS calls } }
Don't forget to add a curly bracket at the end of void loop() to close out the additional conditional! Note that in the code example above, the Choreo inputs have been replaced with placeholders. The code that you generate on the Temboo website will automatically have these input values filled in, so there's no need to worry about replacing them manually.
With that, your sketch is once again ready to go—you're free to tinker with it as you wish (for example, you might choose to comment out the 30 second delay at the end if you want to send messages more rapidly), but no further alterations are required. Save it, upload it to your Arduino, open the serial monitor, and push the button to dispatch your SMS!
Page last edited March 08, 2024
Text editor powered by tinymce.