Stap 9: Laten we in de schets
- Ik veronderstel dat je je Arduino verbonden met uw wifi.
- Als u problemen ondervindt, kunt u proberen de videohandleidingen en hier en hier.
- Al heb je de nieuwe tab voor TembooAccount.h, recht?
- Schets tabblad Kopieer de onderstaande code in de main. Het wordt van aantekeningen voorzien en moet easy(ish) te begrijpen. Email me voor de verduidelijkingen en vragen.
- Zet uw Auth Token-ID en SID account ID hieronder, waar deze tijdelijke aanduidingen zijn:
- PUT_AUTH_TOKEN_HERE
- PUT_ACCOUNT_SID_HERE
#include <Bridge.h> #include <Temboo.h> #include "TembooAccount.h" // contains Temboo account information, as described below int numRuns = 1; // Execution count, so this doesn't run forever int maxRuns = 4320; // Maximum number of times the Choreo should be executed - 4320 is Every 10 minutes, for a month. int ledPin = 13; // Led for debug int buzzerPin = 2; // buzzer's connected to pin 2 int lightsPin = 7; // The pin where your Xmas lights are connected to. int timeToWait = 600000; //Delay between calls String bodyMsgLast = "none"; // This variable holds the last text message read. void setup() { //DEBUG MODE - comment out this segment for production //maxRuns = 10; //debug maxruns //timeToWait = 10000; //debug time to wait //END DEBUG MODE //Set Outputs pinMode(ledPin, OUTPUT); pinMode(lightsPin, OUTPUT); pinMode(buzzerPin, OUTPUT); //Turn lights off on boot up digitalWrite(lightsPin, HIGH); delay(5000); //simulates a button press for 5 seconds digitalWrite(lightsPin, LOW); delay(1000); //Console setup (Should be serial for non Yun arduinos) Bridge.begin(); Console.begin(); // while (!Console); //Waits for Console to connect before starting. Disabled by default. } void loop() { if (numRuns <= maxRuns) { //Run only if haven't passed the max amount of runs Console.println("Running GetLastMessageThatContains - Run #" + String(numRuns++)); // prints Run number TembooChoreo GetLastMessageThatContainsChoreo; // Invoke the Temboo client GetLastMessageThatContainsChoreo.begin(); // Set Temboo account credentials GetLastMessageThatContainsChoreo.setAccountName(TEMBOO_ACCOUNT); GetLastMessageThatContainsChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); GetLastMessageThatContainsChoreo.setAppKey(TEMBOO_APP_KEY); // Set Choreo inputs GetLastMessageThatContainsChoreo.addInput("AuthToken", "PUT_AUTH_TOKEN_HERE"); //Twilio Authentication Token GetLastMessageThatContainsChoreo.addInput("Filter", "ights"); // Filter for incoming messages holding this word GetLastMessageThatContainsChoreo.addInput("AccountSID", "PUT_ACCOUNT_SID_HERE"); //Twilio account ID GetLastMessageThatContainsChoreo.addInput("ResponseMode", "simple"); //Response Mode // Identify the Choreo to run GetLastMessageThatContainsChoreo.setChoreo("/Library/Twilio/SMSMessages/GetLastMessageThatContains"); // Run the Choreo; when results are available, print them to Console GetLastMessageThatContainsChoreo.run(); String bodyMsg; // This contains the whole Message while (GetLastMessageThatContainsChoreo.available()) { char c = GetLastMessageThatContainsChoreo.read(); Console.print(c); //c is one character at at time from the whole message. It's being printed to the console. bodyMsg += c; //The characters are being fed to the bodyMsg string } if (bodyMsg != bodyMsgLast) { //Only runs if this message is different than the one stored. if (bodyMsg.substring(17, 19) == "on") { //This only works if the 17th to 19 letters are "on"". // This works if you're seinding the message "Lights on" // Characters before Lights on are other info from Twilio // Turn lights on digitalWrite(ledPin, HIGH); //turns on debug LED digitalWrite(lightsPin, HIGH); delay(800); digitalWrite(lightsPin, LOW); //Simulated button press for less than a second Console.println("Lights are on"); tone(buzzerPin, 2000, 3000); //beeps for 3 seconds } else if (bodyMsg.substring (17, 20) == "off") { //reads "off" from a message saying "Lights off" digitalWrite(ledPin, LOW); //turns off debug LED tone(buzzerPin, 4200, 1000); //beeps digitalWrite(lightsPin, HIGH); delay(5000); //simulates a 5 second button press to turn the lights off digitalWrite(lightsPin, LOW); delay(1000); Console.println("Lights are off"); } bodyMsgLast = bodyMsg; //Copies this message to the Last message variable } else { Console.println("Identical to Last"); //if identical, do nothing. } Console.println(); Console.println("Waiting..."); delay(timeToWait); // wait a period between GetLastMessageThatContains calls } else { Console.println ("Done. Restart me for another run"); abort(); } }