So I'm only a few days old into this whole Arduino thing... and I'm addicted. I was trying to adapt a few little script pieces to:
-look for a twitter users's posts (mine)
-read the string
-compare it to something that i have stored in memory
-if it's equal, it triggers an event (in this case, fast blinking of an LED)
-if it's not equal, triggers an event(in this case, slow blinking of an LED)
So I got it working on this code... and for a few minutes, it was quite responsive, did everything I wanted it to. Then I saved, switched projects, and when I tried to load it back up, it wouldn't move from the 'sending HTTP request' entry in the Serial Monitor. Can't figure it out. In the original TwitterClient project that I modded, I bumped the 60s refresh rate down to 10s... is this the problem? Is twitter mad at me for being too needy? Now I can't even access ANY twitter handle when I change the screenname.
/*
Twitter Client with Strings
This sketch connects to Twitter using an Ethernet shield. It parses the XML
returned, and looks for <text>this is a tweet</text>
You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield,
either one will work, as long as it's got a Wiznet Ethernet module on board.
This example uses the DHCP routines in the Ethernet library which is part of the
Arduino core from version 1.0 beta 1
This example uses the String library, which is part of the Arduino core from
version 0019.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 21 May 2011
by Tom Igoe
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0xC2, 0x74 };
IPAddress ip(192,168,2,112);
// initialize the library instance:
EthernetClient client;
const int requestInterval = 10000; // delay between requests
char serverName[] = "api.twitter.com"; // twitter URL
boolean requested; // whether you've made a request since connecting
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
String holder = String(">hello, world"); // string to hold the text we're testing against / looking for
String currentLine = ""; // string to hold the text from server
String tweet = ""; // string to hold the tweet
boolean readingTweet = false; // if you're currently reading the tweet
void setup() {
// reserve space for the strings:
holder.reserve(150);
currentLine.reserve(256);
tweet.reserve(150);
pinMode(8, OUTPUT);
// initialize serial:
Serial.begin(9600);
// attempt a DHCP connection:
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Ethernet.begin(mac, ip);
}
// connect to Twitter:
connectToServer();
}
void loop()
{
if (client.connected()) {
if (client.available()) {
// read incoming bytes:
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
if (inChar == '\n') {
currentLine = "";
}
// if the current line ends with <text>, it will
// be followed by the tweet:
if ( currentLine.endsWith("<text>")) {
// tweet is beginning. Clear the tweet string:
readingTweet = true;
tweet = "";
}
// if you're currently reading the bytes of a tweet,
// add them to the tweet String:
if (readingTweet) {
if (inChar != '<') {
tweet += inChar;
}
else {
// if you got a "<" character,
// you've reached the end of the tweet:
readingTweet = false;
Serial.println(tweet);
if (tweet ==holder) {
Serial.println("THEY ARE EQUAL");
digitalWrite(8, HIGH);
delay(250);
digitalWrite(8, LOW);
delay(250);
digitalWrite(8, HIGH);
delay(250);
digitalWrite(8, LOW);
delay(250);
digitalWrite(8, HIGH);
delay(250);
digitalWrite(8, LOW);
delay(250);
digitalWrite(8, HIGH);
delay(250);
digitalWrite(8, LOW);
delay(250);
}
if (tweet != holder){
Serial.println("THEY ARE NOT EQUAL");
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
delay(1000);
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
delay(1000);
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
delay(1000);
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
delay(1000);
}
// close the connection to the server:
client.stop();
}
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
connectToServer();
}
}
void connectToServer() {
// attempt to connect, and wait a millisecond:
Serial.println("connecting to server...");
if (client.connect(serverName, 80)) {
Serial.println("making HTTP request...");
// make HTTP GET request to twitter:
client.println("GET /1/statuses/user_timeline.xml?screen_name=m2443&count=1 HTTP/1.1");
client.println("HOST: api.twitter.com");
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}