Ok. New Update.
Now i've made a small GUI so the user can insert the Username and password of his Twitter account. This makes it easy for anyone user "Birduino"
http://cg-levelseven.blogspot.com/search/label/prt.sc wich is our school project. (videos and images available).
Code:
//############ Vars para Interface ###################
import controlP5.*;
ControlP5 controlP5;
int myColorBackground = color(30,30,30);
int corTitulo = color(40,100,155);
int corSubtitulo = color(54,132,55);
Textfield usernameTXB;
Textfield passwordTXB;
Textlabel tituloLABEL;
Textlabel subtituloLABEL;
//############ Vars para ligar ao Arduino e para o twitter ################
import simpleML.*;
import processing.serial.*;
int time;
String lasttweet;
String tweet;
String[] check;
String[] titulo;
Serial port;
int rate;
String bla;
Twitter twitter;
User[] friends;
String username;
String password;
//String username = "USERNAME";
//String password = "PASSWORD";
java.util.List statuses = null;
//FUNÇÃO PARA O SUBMIT DOS VALORES DAS CAIXAS DE TEXTO
void controlEvent(ControlEvent theEvent) {
username = usernameTXB.getText();
password = passwordTXB.getText();
//println("\n\n Username: "+ usernameTXB.getText() + "\nPassword: " + passwordTXB.getText());
}
void setup(){
//#########CONFIGURA O INTERFACE###############
size(400,300);
frameRate(30);
controlP5 = new ControlP5(this);
usernameTXB = controlP5.addTextfield("Username",100,160,200,20);
usernameTXB.setFocus(true);
passwordTXB = controlP5.addTextfield("Password",100,200,200,20);
passwordTXB.setPasswordMode(true);
subtituloLABEL = controlP5.addTextlabel("label","complete the following fields.",20,134);
subtituloLABEL.setColorValue(corSubtitulo);
tituloLABEL = new Textlabel(this, "Welcome to Birduino.",20,100,400,200,0xffff0000,ControlP5.synt24);
tituloLABEL.setLetterSpacing(3);
tituloLABEL.setColorValue(corTitulo);
//########## CONFIGURA A LIGAÇÃO AO TWITTER ###############
twitter = new Twitter(username,password);
println("Starting timer...");
int time = 0;
lasttweet = " ";
port = new Serial(this, Serial.list()[1], 9600);
}
void draw() {
//########## Desenhar o interface #################
background(myColorBackground);
tituloLABEL.draw(this);
//se as variáveis de utilizar e password não estiverem preenchidas, nao corre o timer
if(username != null && password != null){
//######## Timer para correr a função tweets(); ##########
//println("tic-tac: " + time + "sec.");
time++;
if (time>=120)
{
tweets();
time=0;
}
}
else
{
println("introduzir dados");
}
}
void tweets(){
//Coloca a friend time line na var statuses
try
{
statuses = twitter.getFriendsTimeline();
}
catch( TwitterException e)
{
println(e.getStatusCode());
}
Status status = (Status)statuses.get(0);
tweet = status.getText();
println("Tweet actual: " + tweet);
println("Tweet Anterior: " + lasttweet);
//Mostra o rate limit
try
{
rate = twitter.rateLimitStatus().getRemainingHits();
println("Rate limit: " + rate);
}
catch( TwitterException e)
{
println(e.getStatusCode());
}
if (tweet.length() != lasttweet.length())
{
println("==New Tweet==\n\n");
port.write('H');
}
else
{
println("==No Activity==\n\n");
port.write('L');
}
Status Lstatus = (Status)statuses.get(0);
lasttweet = Lstatus.getText();
}
Im having a small problem and i know why it is happening.
According to the debug, im getting a "no username or password was sent for authentication" or something like that to the following line
statuses = twitter.getFriendsTimeline(); This works fine if i uncomment the following lines:
//String username = "USERNAME";
//String password = "PASSWORD";
and give them a valid username / password. These vars will then be global to the program and when needed they will be set and sent to the function and everything works fine.
However whats happening is, the GUI i made should put the Username and Password on those variables in the following function:
void controlEvent(ControlEvent theEvent) {
username = usernameTXB.getText();
password = passwordTXB.getText();
//println("\n\n Username: "+ usernameTXB.getText() + "\nPassword: " + passwordTXB.getText());
}
Problem is somewhere here... teh vars
username and
password arent "global" or lose theyr value when they are requested by the function that needs them...
Any idea how i could make them global? and still be set by the user on the GUI?
Thank you