Go Down

Topic: Twitter controlled plant feeder - advice sought (Read 993 times) previous topic - next topic

kazimier

hello

just working on a little art project with a friend and was hoping to get some advice.
The aim is to have a plant watered everytime the artists On Kawara tweets (https://twitter.com/on_kawara)his daily message "I AM STILL ALIVE #art" the idea being that his daily tweet keeps this plant alive. I have worked out the electronics using a simple electronic valve from sparkfun.. however the code is proving a bit more tricky. There are plenty of fanstastic examples of using twitter for home automation or for generally reading a tweet.. however the problem we have encountered is that the tweet from On Kawara is always the same so the method of seeing wether or not a new *different* tweet has been posted does not work so well as the content does not change, the only real variable is the time and date of the tweet, i am not sure how to extract that information from twitter.. it is essential for the integrity of the work that the water is controlled directly by the tweet in question and only when On Kawara tweets it. I was wondering if anyone had any suggestions.. the example code i was working with looks like this:

Code: [Select]
/*
  Twitter Client with Strings

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

You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield,
either one will work, as long as it's got a Wiznet Ethernet module on board.

This example uses the DHCP routines in the Ethernet library which is part of the
Arduino core from version 1.0 beta 1

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

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

created 21 May 2011
by Tom Igoe

This code is in the public domain.

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


// 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(192,168,1,30); //<<< ENTER YOUR IP ADDRESS HERE!!!

// initialize the library instance:
EthernetClient client;

const int requestInterval = 60000;  // delay between requests

char serverName[] = "api.twitter.com";  // twitter URL

boolean requested;                   // whether you've made a request since connecting
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() {
 
  pinMode(2, OUTPUT);
 
  // reserve space for the strings:
  currentLine.reserve(256);
  tweet.reserve(150);

// initialize serial:
  Serial.begin(9600);
  // attempt a DHCP connection:
  if (!Ethernet.begin(mac)) {
    // if DHCP fails, start with a hard-coded address:
    Ethernet.begin(mac, ip);
  }
  // connect to Twitter:
  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 = "";
      }
      // 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);
         
          if(tweet == ">I AM STILL ALIVE #art"){
           digitalWrite(2, HIGH);
           Serial.println("LED ON!");
          }
          if(tweet != ">I AM STILL ALIVE #art"){
           digitalWrite(2, LOW);
           Serial.println("LED OFF!");
          }
         
          // 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=on_kawara&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
  }
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}

UKHeliBob

What does the code do ?  I assume that it does not do what you want.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

kazimier

currently it does not do anything really - this is just a sample code that i took off the internet to connect to twitter and see how it works.. i was looking to see how it is possible to extract the relevant time stamp from the twitter message itself, but not sure how to go about it..

PaulS

What are you reading? It hardly makes sense to try to debug code to parse the input when you have no idea what the input is.

kazimier

i want to read On Kawara's twitter feed - https://twitter.com/on_kawara and every time he tweets the message "I AM STILL ALIVE #art" (which he does daily and is the only thing he does with the twitter account) i want the plant to be watered.


spatula

You should check the twitter API, it allows you to get only the tweets for a certain user and inspect the date. You will need to perform authentication in order to use the API.

You may start from this: https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline

PaulS

Quote
i want to read On Kawara's twitter feed - https://twitter.com/on_kawara and every time he tweets the message "I AM STILL ALIVE #art" (which he does daily and is the only thing he does with the twitter account) i want the plant to be watered.

You've said that. Any you are, in theory at least, reading something. What ARE you reading? Serial.print() the characters to find out.

Only when you have a clue that the parsing code is parsing the right data will you have a hope of debugging it.

Go Up