Skip to content

Instantly share code, notes, and snippets.

@juliangoulding
Created March 25, 2013 06:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliangoulding/5235186 to your computer and use it in GitHub Desktop.
Save juliangoulding/5235186 to your computer and use it in GitHub Desktop.
This is the first part of the project and is an arduino that can auto tweet one of 5 predatemined phrases to is own twitter account depending on what buttons are pressed. It uses a button pad stolen out of a guitar hero controller, an ethernet shield ( in my case a combined etherten from freetronics) and the twitter header file to tweet all by i…
/*
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
//#include <EthernetDNS.h> // only for IDE 0022 or earlier
#include <Twitter.h>
#if defined(ARDUINO) && ARDUINO > 18
#endif
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(130,195,44,97);
// initialize the library instance:
EthernetClient client;
const unsigned long requestInterval = 60000; // delay between requests
char serverName[] = "api.twitter.com"; // twitter URL
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
Twitter twitter("1284887635-xIQyoZlg9fMq9LnFMnK9sjkPaCmmfjex6VbJ8UB");
char msg1[] = "Someone pushed button 1 and I, the arduino, sent this tweet!#arduino #autotweet";
char msg2[] = "Someone pushed button 2 and I, the arduino, sent this tweet!#arduino #autotweet";
char msg3[] = "Someone pushed button 3 and I, the arduino, sent this tweet!#arduino #autotweet";
char msg4[] = "Someone pushed button 4 and I, the arduino, sent this tweet!#arduino #autotweet";
char msg5[] = "Someone pushed button 5 and I, the arduino, sent this tweet!#arduino #autotweet";
const int buttonPin1 = 6; // the number of the pushbutton pin
const int ledPin1 = 1;
const int buttonPin2 = 3; // the number of the pushbutton pin
const int ledPin2 = 8; // the number of the LED pin
const int buttonPin3 = 2; // the number of the pushbutton pin
const int ledPin3 = 9;
const int buttonPin4 = 5; // the number of the pushbutton pin
const int buttonPin5 = 7; // the number of the pushbutton pin
// the number of the pushbutton pin
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
void setup() {
// reserve space for the strings:
currentLine.reserve(256);
tweet.reserve(150);
// Open serial communications and wait for port to open:
Serial.begin(9600);
// attempt a DHCP connection:
Serial.println("Attempting to get an IP address using DHCP:");
Ethernet.begin(mac);
Serial.print("My address:");
Serial.println(Ethernet.localIP());
// connect to Twitter:
connectToServer();
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}
void loop()
{
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
if (buttonState1 == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
Serial.println("btn1");
if (twitter.post(msg1)) {
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("failed.");
}
delay(60000);
}
else {
// turn LED off:
digitalWrite(ledPin1, LOW);
}
if (buttonState2 == HIGH) {
// turn LED on:
digitalWrite(ledPin2, HIGH);
Serial.println("btn2");
if (twitter.post(msg2)) {
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("failed.");
}
delay(60000);
}
else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}
if (buttonState3 == HIGH) {
// turn LED on:
digitalWrite(ledPin3, HIGH);
Serial.println("btn3");
if (twitter.post(msg3)) {
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("failed.");
}
delay(60000);
}
else {
// turn LED off:
digitalWrite(ledPin3, LOW);
}
if (buttonState4 == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
Serial.println("btn4");
if (twitter.post(msg4)) {
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("failed.");
}
delay(60000);
}
else {
// turn LED off:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
if (buttonState5 == HIGH) {
// turn LED on:
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, HIGH);
Serial.println("btn5");
if (twitter.post(msg5)) {
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("failed.");
}
delay(60000);
}
else {
// turn LED off:
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, LOW);
}
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();
}
}
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=tvuino&count=1 HTTP/1.1");
client.println("HOST: api.twitter.com");
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment