This sketch connects to Twitter using an Ethernet shield. It parses the XML returned, and looks for <text>this is a tweet</text>.

Make sure your SIM is data enabled and you have the necessary information to connect to the network. You'll need the Access Point Name (APN), login, and password of the carrier. To obtain this information, contact your network provider for the most up to date information. This page has some information about various carrier settings, but it may not be current.

Hardware Required

Arduino board

Arduino + Telefonica GSM/GPRS Shield

SIM card with a data connection

Circuit

image of the Arduino GSM Shield on top of an Arduino Uno

First, import the GSM library

#include <GSM.h>

SIM cards may have a PIN number that enables their functionality. Define the PIN for your SIM. If your SIM has no PIN, you can leave it blank :

#define PINNUMBER ""

Define a number of constants that contain information about the GPRS network you're going to connect to. You'll need the Access Point Name (APN), login, and password. To obtain this information, contact your network provider for the most up to date information. This page has some information about various carrier settings, but it may not be current.

Connect to the network by calling gsmAccess.begin(). It takes the SIM card's PIN as an argument. You'll also connect to the GPRS network using gprs.attachGPRS(). This requires the APN, login, and password you declared earlier. By placing this inside a while() loop, you can continually check the status of the connection and wait for them to both become true before proceeding.

When the modem does connect and has attached itself to the GPRS network, gsmAccess() will return GSM_READY. Use this as a flag to set the notConnected variable to true or false. Once connected, the remainder of setup will run.

Finally, check to see how long it has been since the last time a connection was made. If it has been longer than the request interval, call connectToServer() again and read the most recent message. Close up the loop.

// 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();}}}}elseif(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=arduinoteam HTTP/1.1");
client.println("HOST: api.twitter.com");
client.println();}// note the time of this connect attempt:
lastAttemptTime =millis();}