Arduino Yun, Temboo and a light - HELP

Hi,

Can someone please check my code and help me to identify what I am doing wrong. I cannot manage to turn on an LED connected on pin 13? I am using an Arduino YUN and a Temboo choreo. I know the temboo it is sending a valid response. I assume the issue is on the code to read the response from temboo and activate the light. Thank you,

#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 = 3;   // Maximum number of times the Choreo should be executed

void setup() {
  Serial.begin(9600);

  // For debugging, wait until the serial console is connected.
  delay(4000);
  while (!Serial);
  Bridge.begin();
}

void loop() {
  if (numRuns <= maxRuns) {
    Serial.println("Hold on, I am checking for Tweets - Run #" + String(numRuns++));

    TembooChoreo TweetsChoreo;

    // Invoke the Temboo client
    TweetsChoreo.begin();

    // Set Temboo account credentials
    TweetsChoreo.setAccountName(TEMBOO_ACCOUNT);
    TweetsChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    TweetsChoreo.setAppKey(TEMBOO_APP_KEY);

    // Set Choreo inputs
    TweetsChoreo.addInput("Count", "20");
    TweetsChoreo.addInput("AccessToken", "");
    TweetsChoreo.addInput("Query", "goodsir_plinko");
    TweetsChoreo.addInput("AccessTokenSecret", "");
    TweetsChoreo.addInput("ConsumerSecret", "");
    TweetsChoreo.addInput("ConsumerKey", "");

    // Identify the Choreo to run
    TweetsChoreo.setChoreo("/Library/Twitter/Search/Tweets");

    // Run the Choreo; when results are available, print them to serial
    TweetsChoreo.run();

  unsigned int returnCode = TweetsChoreo.run();
  
  // a response code of 0 means success; print the API response
    if(returnCode == 0) {
      
      String tweet; // a String to hold the text of the tweet

      
      while(TweetsChoreo.available()) {
        // read the name of the output item
        String name = TweetsChoreo.readStringUntil('\x1F');
        name.trim();

        // read the value of the output item
        String data = TweetsChoreo.readStringUntil('\x1E');
        data.trim();

        // assign the value to the appropriate String
        if (name == "goodsir_plinko") {
          tweet = data;
          Serial.print(tweet + "\n\n");
          int len = tweet.length();
          if (len > 0) {
            digitalWrite(13, HIGH); 
          } else {
            digitalWrite(13, LOW); 
        }
      }
    }
  } else {
      // there was an error
      // print the raw output from the choreo
      while(TweetsChoreo.available()) {
        char c = TweetsChoreo.read();
        Serial.print(c);
      }
    }
    TweetsChoreo.close();
  }

  Serial.println("Waiting...");
  delay(30000); // wait 30 seconds between Tweets calls
}

There is no

pinMode(pin, OUTPUT)

In your setup to let the Arduino know it is an output. Therefore it defaults to an input.

Weedpharma

Is this better? I still not getting the light to turn on

#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 = 3;   // Maximum number of times the Choreo should be executed

void setup() {
  Serial.begin(9600);

  // For debugging, wait until the serial console is connected.
  delay(4000);
  while (!Serial);
  Bridge.begin();


  // Initialize pin
  pinMode(13, OUTPUT);
}

void loop() {
  if (numRuns <= maxRuns) {
    Serial.println("Hold on, I am checking for Tweets - Run #" + String(numRuns++));

    TembooChoreo TweetsChoreo;

    // Invoke the Temboo client
    TweetsChoreo.begin();

    // Set Temboo account credentials
    TweetsChoreo.setAccountName(TEMBOO_ACCOUNT);
    TweetsChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    TweetsChoreo.setAppKey(TEMBOO_APP_KEY);

    // Set Choreo inputs
    TweetsChoreo.addInput("Count", "20");
    TweetsChoreo.addInput("AccessToken", "");
    TweetsChoreo.addInput("Query", "goodsir_plinko");
    TweetsChoreo.addInput("AccessTokenSecret", "");
    TweetsChoreo.addInput("ConsumerSecret", "");
    TweetsChoreo.addInput("ConsumerKey", "");

    // Identify the Choreo to run
    TweetsChoreo.setChoreo("/Library/Twitter/Search/Tweets");

    // Run the Choreo; when results are available, print them to serial
    TweetsChoreo.run();

  unsigned int returnCode = TweetsChoreo.run();
  
  // a response code of 0 means success; print the API response
    if(returnCode == 0) {
      
      String tweet; // a String to hold the text of the tweet

      
      while(TweetsChoreo.available()) {
        // read the name of the output item
        String name = TweetsChoreo.readStringUntil('\x1F');
        name.trim();

        // read the value of the output item
        String data = TweetsChoreo.readStringUntil('\x1E');
        data.trim();

        // assign the value to the appropriate String
        if (name == "goodsir_plinko") {
          tweet = data;
          Serial.print(tweet + "\n\n");
          int len = tweet.length();
          if (len > 0) {
            digitalWrite(13, HIGH); 
          } else {
            digitalWrite(13, LOW); 
        }
      }
    }
  } else {
      // there was an error
      // print the raw output from the choreo
      while(TweetsChoreo.available()) {
        char c = TweetsChoreo.read();
        Serial.print(c);
      }
    }
    TweetsChoreo.close();
  }

  Serial.println("Waiting...");
  delay(30000); // wait 30 seconds between Tweets calls
}

arkkitekktura:
Is this better? I still not getting the light to turn on
::::SNIP::::

@arkkitekktura,
in your Arduino IDE chose: Files->Examples->01.Basics->Blink
Run that and see if you can get the LED to blink.

Next, move your variable declarations outside of the loop.
I know this is okay in Java, but it’s NOT a good idea in C/C++.
C/C++ does NOT have garbage collection.

void loop() {
  if (numRuns <= maxRuns) {
    Serial.println("Hold on, I am checking for Tweets - Run #" + String(numRuns++));

    TembooChoreo TweetsChoreo;  //<======== MOVE ALL DECLARATION like this, out of the loop()

Jesse

arkkitekktura: you have several print statements showing progress, what do they say? Are there any error messages? Are you getting the expected tweets? Can you post the output?

It looks like you may be collecting up to 20 tweets at a time. Is it possible that one tweet is turning the light on, and the last one is quickly turning it off?

I would add a couple temporary print statements right next to the digitalWrite statements. Explicitly print that you are turning the light on or off. This will help you follow the logic and see if you're even getting to the right sections of code. I would also explicitly print the values of name, data, and tweet.lentgh.

Speaking if which, why do you copy data into a new tweet string? All you are doing with it is take the length and print it. Why not just do that with data directly?

jessemonroy650:
C/C++ does NOT have garbage collection.

C++ doesn't need garbage collection. Those local variables are allocated on the stack, and simply popped off the stack when the function returns. I don't see a problem there, nothing that would prevent it from working. Now, that being said, it would be more efficient to not allocate/deallocate those variables each time, but given that the sketch us spending most if it's time in a 30 second delay, that's not really a concern here. If this were a more complicated sketch, there would actually be some benefit to "hiding" these local variables inside the function: if the program is complicated enough, having everything global can cause issues.

ShapeShifter:
::::SNIP::::
C++ doesn’t need garbage collection. Those local variables are allocated on the stack, and simply popped off the stack when the function returns.
::::SNIP::::

@ShapeShifter,
Yes you are indeed correct. However, it’s a bad habit. I’ve seen too much code where “new” programmers were either unaware or had completely forgotten the “destacking” rule. Similar rules are applied in Javascript and other “C” like languages.

FWIW: I could program in any one of 20 languages I know. I can tell you my biggest error are remembering things like “destacking” rules.
[soapbox] I could devolve the conversation in to a diatribe about the moral hazzards of inner declarations, but let’s stick to the problems at hand AND be aware that for all programmers generality in coding is a plus.[/soapbox]

TIA
Jesse