0

I'm trying to get a specific user's tweets into Processing and then have them spoken out using the TTS Library, but only have them spoken when a specific value is detected from Arduino over Serial = 491310

I've got the tweets coming into Processing and can have them printed and spoken, and the value 491310 is picked up by Processing, BUT it's the placement of the if Statement ( 'if (sensor == 491310) {') that I'm struggling with, as it currently has no effect - Can anyone solve this one?

Absolute novice here, any help would be great. Thanks.

import twitter4j.util.*;
import twitter4j.*;
import twitter4j.management.*;
import twitter4j.api.*;
import twitter4j.conf.*;
import twitter4j.json.*;
import twitter4j.auth.*;
import guru.ttslib.*;
import processing.serial.*;

TTS tts;
Serial myPort;

int sensor = 0;

void setup() {
  tts = new TTS();
  
  myPort = new Serial(this, Serial.list()[0], 9600); 
  
}

void draw() {
  
ConfigurationBuilder cb = new ConfigurationBuilder();

cb.setOAuthConsumerKey("XXXX");
cb.setOAuthConsumerSecret("XXXX");
cb.setOAuthAccessToken("XXXX");
cb.setOAuthAccessTokenSecret("XXXX");

java.util.List statuses = null;

Twitter twitter = new TwitterFactory(cb.build()).getInstance();

String userName ="TWITTER HANDLE";
int numTweets = 19;
String[] twArray = new String[numTweets];

  try {
    statuses = twitter.getUserTimeline(userName);
  }
  catch(TwitterException e) {
  }

if( statuses != null) {
  for (int i=0; i<statuses.size(); i++) {
    Status status = (Status)statuses.get(i);
    
    if (sensor == 491310) {
    println(status.getUser().getName() + ": " + status.getText());
 tts.speak(status.getUser().getName() + ": " + status.getText());
  }
}
}

}

void serialEvent (Serial myPort) {
 int inByte = myPort.read();
   sensor = inByte;
  print(sensor);
}

2 Answers 2

1

Reading from a serial port returns a byte( 8 bit) not a 16 bit integer. The value of 'sensor" cannot be above 255 so never matches 491310. You'll have to do 2 reads to form the 16 bit int.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply! Any more information (or links) you can provide would be great - Complete novice, so I wouldn't know where to start with this. The data that's sent to Serial Port from Arduino is only ever 1s and 2s, but Processing picks them up as 6 digit numbers, so an alternative may be for me to find out how to fix that instead. Thanks!
0

My guess is that you're hitting twitter's rate limit. Twitter only allows a certain amount of API calls in a given 15 minute window. And since you're calling getUserTimeline() in the draw() function (which happens 60 times per second), you're going to hit that limit pretty fast.

So you're probably getting a TwitterException, but you're just ignoring it. Never use an empty catch block! At least put a call to e.printStackTrace() in there:

catch(TwitterException e) {
   e.printStackTrace();
}

To fix the problem, you're going to have to modify your code to only check for tweets once at the beginning of the program. Move all of your logic for fetching the tweets into the setup() function, and then move the logic for printing them out into the serialEvent() function.

If you still can't get it working, then you're going to have to do some debugging: what is the value of every single variable in your sketch? Use the println() function to help figure that out. Is statuses == null? What is the value of statuses.size()? What is the value of sensor? Once you know that, you'll be able to figure out exactly what's going wrong with your code. But my bet would be it's the twitter rate limit, so check that first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.