Tutorial.WiFiTwitterClient History

Hide minor edits - Show changes to output

November 08, 2013, at 01:23 PM by Scott Fitzgerald -
Added lines 6-7:
''The [[Products/ArduinoYUN | Arduino Yún]] supports OAuth through [[https://temboo.com/ | Temboo]]. Learn more in the [[https://temboo.com/arduino | Temboo documentation]].''
November 08, 2013, at 09:29 AM by Scott Fitzgerald -
Changed lines 4-5 from:
*''This example doesn't work anymore because the Twitter authentication mode is now different and not compatible with Arduino libraries.''
to:
'''This example no longer works as the Twitter authentication mode has changed, and is no longer compatible with the Arduino library.'''
July 01, 2013, at 05:51 PM by Alberto Cicchi -
Changed lines 4-5 from:
to:
*''This example doesn't work anymore because the Twitter authentication mode is now different and not compatible with Arduino libraries.''
April 11, 2013, at 07:30 PM by Federico -
Changed lines 41-191 from:
(:source lang=arduino tabwidth=4:)
/*
Wifi Twitter Client with Strings

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

This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.

This example uses the String library, which is part of the Arduino core from
version 0019.

Circuit:
* WiFi shield attached to pins 10, 11, 12, 13

created 23 apr 2012
by Tom Igoe

This code is in the public domain.

*/
#include <SPI.h>
#include <WiFi.h>

char ssid[] = "YourNetwork"; // your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS; // status of the wifi connection

// initialize the library instance:
WiFiClient client;

const unsigned long requestInterval = 30*1000; // delay between requests; 30 seconds

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(199,59,149,200); // numeric IP for api.twitter.com
char server[] = "api.twitter.com"; // name address for twitter API

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);
// start serial port:
Serial.begin(9600);

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the status:
printWifiStatus();
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 = "";
// break out of the loop so this character isn't added to the tweet:
return;
}
// 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();
}
}

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


void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}



(:sourceend:)
to:
(:source http://arduino.cc/en/pub/code/master/libraries/WiFi/examples/WiFiTwitterClient/WiFiTwitterClient.ino lang=arduino tabwidth=4:)
July 31, 2012, at 11:02 PM by Scott Fitzgerald -
Changed lines 41-191 from:
(:source http://arduino.cc/en/pub/code/master/libraries/WiFi/examples/WiFiTwitterClient/WiFiTwitterClient.ino lang=arduino tabwidth=4:)
to:
(:source lang=arduino tabwidth=4:)
/*
Wifi Twitter Client with Strings

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

This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.

This example uses the String library, which is part of the Arduino core from
version 0019.

Circuit:
* WiFi shield attached to pins 10, 11, 12, 13

created 23 apr 2012
by Tom Igoe

This code is in the public domain.

*/
#include <SPI.h>
#include <WiFi.h>

char ssid[] = "YourNetwork"; // your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS; // status of the wifi connection

// initialize the library instance:
WiFiClient client;

const unsigned long requestInterval = 30*1000; // delay between requests; 30 seconds

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(199,59,149,200); // numeric IP for api.twitter.com
char server[] = "api.twitter.com"; // name address for twitter API

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);
// start serial port:
Serial.begin(9600);

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the status:
printWifiStatus();
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 = "";
// break out of the loop so this character isn't added to the tweet:
return;
}
// 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();
}
}

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


void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}



(:sourceend:)
April 25, 2012, at 06:23 PM by Scott Fitzgerald -
Changed lines 27-28 from:
%height=300px%[[Attach:PachubeClient_bb.png.png | Attach:PachubeClient_bb.png.png]]
to:
%height=300px%[[Attach:WiFiShield_bb.png | Attach:WiFiShield_bb.png]]

Changed lines 36-40 from:
!!!Schematic
(:div class=circuit :)
%height=300px%[[Attach:EthernetShield_sch.png | Attach:EthernetShield_sch.png]]
(:divend:)
to:

April 21, 2012, at 12:26 AM by Scott Fitzgerald -
Changed lines 5-6 from:
This example shows you how to answer a HTTP request using a WiFi shield. Specifically, it connects to [[(twitter.com)]], The example requires you to set up a twitter.com account. Your WiFi shield will then connect to that account and send a tweet.
to:
This example shows you how to answer a HTTP request using a WiFi shield. It parses the XML returned, and looks for <text>this is a tweet</text>.

This example is written for a network using WPA encryption. For WEP or WPA, change the Wifi.begin() call accordingly.
Deleted lines 13-14:
!!!!Software Required
* twitter.com account
April 21, 2012, at 12:23 AM by Scott Fitzgerald -
Added lines 1-48:
''Examples > WiFi Library''

!!!WiFi Twitter Client

This example shows you how to answer a HTTP request using a WiFi shield. Specifically, it connects to [[(twitter.com)]], The example requires you to set up a twitter.com account. Your WiFi shield will then connect to that account and send a tweet.

(:div class=BOM :)
!!!Hardware Required
* Arduino WiFi Shield
* Shield-compatible Arduino board

!!!!Software Required
* twitter.com account
(:divend:)

!!!Circuit
(:div class=circuit :)

The WiFi shield uses pins 10, 11, 12, and 13 for the SPI connection to the HDG104 module. Digital pin 4 is used to control the slave select pin on the SD card.

You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.

For networks using WPA/WPA2 Personal encryption, you need the SSID and password. The shield will not connect to networks using WPA2 Enterprise encryption.

WEP network passwords are hexadecimal strings known as keys. A WEP network can have 4 different keys; each key is assigned a "Key Index" value. For WEP encrypted networks, you need the SSID, the key, and key number.

%height=300px%[[Attach:PachubeClient_bb.png.png | Attach:PachubeClient_bb.png.png]]

[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]

'''''In the above image, the Arduino would be stacked below the WiFi shield.'''''

(:divend:)

!!!Schematic
(:div class=circuit :)
%height=300px%[[Attach:EthernetShield_sch.png | Attach:EthernetShield_sch.png]]
(:divend:)

'''Code:'''

(:div class=code :)
(:source http://arduino.cc/en/pub/code/master/libraries/WiFi/examples/WiFiTwitterClient/WiFiTwitterClient.ino lang=arduino tabwidth=4:)
(:divend:)


!!!See Also:
(:include WiFiTutorialSeeAlsoInclude:)

Share