How to add Twitter feature into my sketch? {PLEASE HELP!}

I am new to the whole Arduino World, I am still in the learning phase of how to write my own code and I am doing pretty well. However I still don’t fully understand how to include things like Twitter into my code. Basically I have a Laser Trip Wire which I have built ( Simple I know), but I would like to add the option for it to “Tweet” when a certain threshold is met. basically I want it to flow like this… (Laser Beam Broken> Arduino reads that photocell Value fell below 100> Pin 11 goes to HIGH for 1 Sec (Sounds Alarm)(back to LOW if above Threshold)> “tweets” the words “Laser has been Tripped!”. I have an Arduino Uno and an Ethernet Shield and I have been able to use the “SimplePost” example forTwitter to successfully connect. I just don’t understand how to add the Tweet part to my sketch. Please see the Code below and if someone could point me in the direction of how to add this feature it would be greatly appreciated!

/*

Trip Laser

*/

int buzzPin = 11; // buzzer connected to digital pin 11

void setup() {
pinMode(buzzPin, OUTPUT); // sets the digital pin as output
}

void loop(){
if(analogRead(0) < 100){ // this number depends on calibration of the photocell
digitalWrite(buzzPin, HIGH); // turns buzzer on
delay(1000); // waits for 1 second
digitalWrite(buzzPin, LOW); // turns buzzer off

} else{
digitalWrite(buzzPin, LOW);
}
}

Search the Forum. There have been a few threads on twittering in the last few weeks.

I have been searching the forum for a few days and still cant find anything to help. Especially since the changes to the IDE to 1.0 it has been causing alot of problems for people with twitter integration which is just adding to my confusion. I find more often then not people are just asking how to be able to post to twitter using only usb w/o Ethernet shield or by using processing to have the computer do the actual tweeting. this isn’t what I am trying to accomplish… Do you know any posts in particular that may help me? it seems like it should be a simple thing I just don’t understand how to code it… any help is greatly appreciated!

I have an Arduino Uno and an Ethernet Shield and I have been able to use the "SimplePost" example forTwitter to successfully connect. I just don't understand how to add the Tweet part to my sketch.

Why don't you post the working code you have? I would think you would put the tweeting in a conditional code segment and have the motion detection in a main loop.

zoomkat:

I have an Arduino Uno and an Ethernet Shield and I have been able to use the “SimplePost” example forTwitter to successfully connect. I just don’t understand how to add the Tweet part to my sketch.

Why don’t you post the working code you have? I would think you would put the tweeting in a conditional code segment and have the motion detection in a main loop.

Thanks for the reply zoomkat! The code that I used to connect to twitter successfully was the Twitter Library example sketch by Neocat. Its just being new at this I don’t understand how the code gets intertwined into my code for the laser…

/* Post a simple message to Twitter  */
#if defined(ARDUINO) && ARDUINO > 18
#include <SPI.h>
#endif
#include <Ethernet.h>
//#include <EthernetDNS.h> // only for IDE 0022 or earlier
#include <Twitter.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte gateway[] = { 10, 0, 0, 1 };
byte subnet[] = { 255, 255, 0, 0 };

Twitter twitter("YOUR-TOKEN");
char msg[] = "Laser has been Tripped!";

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);

  delay(1000);

  Serial.println("connecting ...");
  if (twitter.post(msg)) {
    int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }
}

void loop()
{
}

The below is not twitter stuff, but it shows how to put an internet connection in a conditional statement based on an event, in this case the event is the reception of an “e” on the serial port. You will also need to figure out if/how you want to include a variable in your tweet message.

//zoomkat 12-08-11
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, subnet, gateway);
  Serial.begin(9600); 
  Serial.println("Better client test 12/01/11"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Well zoomcat you will either get a good laugh out of this “coding” or I will be moving in the right direction… (please be gentle if this is completely wrong) I am trying to add the 2 codes together…

/*

Trip Laser
version 2.5
2012

*/
#if defined(ARDUINO) && ARDUINO > 18
#include <SPI.h>
#endif
#include <Ethernet.h>
#include <Twitter.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 120 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };

Twitter twitter("YOUR-TOKEN"); //taken out for security reasons
char msg[] = "Laser has been Tripped!";

int buzzPin = 11; // buzzer connected to digital pin 11

void setup() {
pinMode(buzzPin, OUTPUT); // sets the digital pin as output
Ethernet.begin(mac, ip, gateway, subnet);
 Serial.begin(9600);

  delay(1000);
// Serial display to see if its actually connecting or not
  Serial.println("connecting ..."); 
  if (twitter.post(msg)) {
    int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }
}

This is just the Setup portion… I am still trying to figure out how to add another step or “if” statement to start the tweet if buzzpin 11 went (HIGH) in the Loop section.

Guess I scared him away with whats probably, an utterly useless code… :fearful: