You've already generated all of the code you need to check Twitter using your Yún, so you only need to make a few changes to your sketch to have the Yún determine whether a Tweet is new. First, let’s add an Output Filter so that we only have to worry about the Tweet ID among all the different information that the Twitter API will return. The Output Filter will filter the API response in the cloud so that only the Tweet ID of your most recent Twitter mention is returned to the board. Before
// Run the Choreo; when results are available, print them to serial MentionsChoreo.run();
add
MentionsChoreo.addOutputFilter("TweetID", "/[]/id", "Response");
and in void loop(), replace
while(MentionsChoreo.available()) { char c = MentionsChoreo.read(); Serial.print(c);
with
while(MentionsChoreo.available()) { String TweetIDName = MentionsChoreo.readStringUntil('\x1F'); TweetIDName.trim(); String TweetIDNumber = MentionsChoreo.readStringUntil('\x1E'); TweetIDNumber.trim();
Then, we’ll add some code that will let your Yún check the newly retrieved Tweet ID against the previously returned ID to determine whether or not there’s been a new mention. To do it, first initialize a new variable at the top of your code:
int LastID = 0;
Then, add the following lines of code after TweetIDNumber.trim();
if(TweetIDName == "TweetID"){ int NewID = TweetIDNumber.toInt(); if(LastID != NewID){ digitalWrite(13, HIGH); delay(5000); digitalWrite(13, LOW); LastID = NewID; } }
Next, we'll introduce the push button that will trigger your sketch. First, initialize the pin that you will be reading (recall that we chose to use pin 2) by adding a few lines of code to what you already have in void setup(). While we're at it, let's also initialize the output pin to which the LED is attached (we've selected pin 13 for this):
// Initialize pin pinMode(2, INPUT); pinMode(13, OUTPUT);
Then, add the following two lines of code to void loop() to set the Mentions Choreo to run only when the button is pressed:
int sensorValue = digitalRead(13); if (sensorValue == LOW) {
You should nest the code that you already have in void loop() within this new conditional.
Finally, delete the lines
Serial.println("Waiting..."); delay(30000); // wait 30 seconds between Mentions calls
Since you’re calling the Choreo only when you press the button, that line is no longer necessary.
With that, your sketch is once again ready to go! It should look something like this (note that the Input values in the code below have been replaced by placeholders):
#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 = 10; // Maximum number of times the Choreo should be executed int LastID = 0; void setup() { Serial.begin(9600); // For debugging, wait until the serial console is connected. delay(4000); while(!Serial); Bridge.begin(); // Initialize pin pinMode(2, INPUT); pinMode(13, OUTPUT); } void loop() { int sensorValue = analogRead(2); if (sensorValue == LOW) { if (numRuns <= maxRuns) { Serial.println("Running Mentions - Run #" + String(numRuns++)); TembooChoreo MentionsChoreo; // Invoke the Temboo client MentionsChoreo.begin(); // Set Temboo account credentials MentionsChoreo.setAccountName(TEMBOO_ACCOUNT); MentionsChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); MentionsChoreo.setAppKey(TEMBOO_APP_KEY); // Set Choreo inputs MentionsChoreo.addInput("Count", "1"); MentionsChoreo.addInput("AccessToken", "PLACEHOLDER"); MentionsChoreo.addInput("AccessTokenSecret", "PLACEHOLDER"); MentionsChoreo.addInput("ConsumerSecret", "PLACEHOLDER"); MentionsChoreo.addInput("ConsumerKey", "PLACEHOLDER"); // Identify the Choreo to run MentionsChoreo.setChoreo("/Library/Twitter/Timelines/Mentions"); MentionsChoreo.addOutputFilter("TweetID", "[]/id", "Response"); // Run the Choreo; when results are available, print them to serial MentionsChoreo.run(); while(MentionsChoreo.available()) { String TweetIDName = MentionsChoreo.readStringUntil('\x1F'); TweetIDName.trim(); String TweetIDNumber = MentionsChoreo.readStringUntil('\x1E'); TweetIDNumber.trim(); if(TweetIDName == "TweetID"){ int NewID = TweetIDNumber.toInt(); if(LastID != NewID){ digitalWrite(13, HIGH); delay(5000); digitalWrite(13, LOW); LastID = NewID; } } } MentionsChoreo.close(); } } }
You can continue to make changes if you’d like, but no further alterations are required. Save it, upload it to your Arduino, open the serial monitor, and press the button to begin checking your Tweets.
Page last edited March 08, 2024
Text editor powered by tinymce.