diff options
author | Cristian Maglie <c.maglie@bug.st> | 2013-04-03 13:51:04 +0200 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2013-04-03 13:51:04 +0200 |
commit | ee90e68e86dd61d86f5d17b69080338328765b22 (patch) | |
tree | e620c0edc2690ab789b665e567910640597aa6fe /libraries/GSM/examples/GsmTwitterClient/GsmTwitterClient.ino | |
parent | 0ecdc5ebc96ad4c7c548c438a03d9ce00679db8b (diff) | |
parent | f50c307be280dc6ece9e70c43b301c1db36291a0 (diff) |
Merged 1.0.5
Merge remote-tracking branch 'arduino/master' into ide-1.5.x
Conflicts:
app/src/processing/app/Base.java
build/shared/revisions.txt
hardware/arduino/avr/cores/arduino/malloc.c
hardware/arduino/cores/arduino/avr-libc/malloc.c
hardware/arduino/cores/arduino/malloc.c
todo.txt
Diffstat (limited to 'libraries/GSM/examples/GsmTwitterClient/GsmTwitterClient.ino')
-rw-r--r-- | libraries/GSM/examples/GsmTwitterClient/GsmTwitterClient.ino | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/libraries/GSM/examples/GsmTwitterClient/GsmTwitterClient.ino b/libraries/GSM/examples/GsmTwitterClient/GsmTwitterClient.ino new file mode 100644 index 0000000..3032141 --- /dev/null +++ b/libraries/GSM/examples/GsmTwitterClient/GsmTwitterClient.ino @@ -0,0 +1,162 @@ +/* + GSM Twitter Client with Strings + + This sketch connects to Twitter using an Arduino GSM shield. + It parses the XML returned, and looks for the string <text>this is a tweet</text> + + This example uses the String library, which is part of the Arduino core from + version 0019. + + Circuit: + * GSM shield attached to an Arduino + * SIM card with a data plan + + created 8 Mar 2012 + by Tom Igoe + + http://arduino.cc/en/Tutorial/GSMExamplesTwitterClient + + This code is in the public domain. + + */ + +// libraries +#include <GSM.h> + +// PIN Number +#define PINNUMBER "" + +// APN data +#define GPRS_APN "APN" // replace your GPRS APN +#define GPRS_LOGIN "LOGIN" // replace with your GPRS login +#define GPRS_PASSWORD "PASSWORD" // replace with your GPRS password + +// initialize the library instance +GSMClient client; +GPRS gprs; +GSM gsmAccess; + +const unsigned long requestInterval = 30*1000; // delay between requests: 30 seconds + +// API Twitter URL +char server[] = "api.twitter.com"; + +boolean requested; // whether you've made a request since connecting +unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds + +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: + currentLine.reserve(256); + tweet.reserve(150); + + // initialize serial communications and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + + // connection state + boolean notConnected = true; + + // After starting the modem with GSM.begin() + // attach the shield to the GPRS network with the APN, login and password + while(notConnected) + { + if((gsmAccess.begin(PINNUMBER)==GSM_READY) & + (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY)) + notConnected = false; + else + { + Serial.println("Not connected"); + delay(1000); + } + } + + Serial.println("Connected to GPRS network"); + + Serial.println("connecting..."); + connectToServer(); +} + + + +void loop() +{ + char c; + 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); + + // 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(); + } +} + +/* + Connect to API Twitter server and do a request for timeline +*/ +void connectToServer() +{ + // attempt to connect, and wait a millisecond: + Serial.println("connecting to server..."); + if (client.connect(server, 80)) + { + Serial.println("making HTTP request..."); + // make HTTP GET request to twitter: + client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1"); + client.println("HOST: api.twitter.com"); + client.println(); + } + // note the time of this connect attempt: + lastAttemptTime = millis(); +} |