loading

Step 18: Firmware

This firmware will read incoming twitter data so that if you tweet "feed me!" from the username omnomnom the arduino will light up the two LEDs attached to pins 2 and 3 for 1 second, causing the food delivery sequence to start.


I am using Arduino 1.0 for this project, I recommend downloading this version (or later) for this project. If you do not want to use the new version, make sure you have the following Arduino libraries (they are bundled with v1.0):
- Ethernet (for the Ethernet Shield)
- EthernetDHCP (for self-configuring the IP address is you use DHCP at home)

Turn on the power switch on the bottom of the feeder, the LCD should flash 12:00 and the motors should run through the food delivery sequence once. Refer to the manual if you would like to set the clock or set additional food timers, this won't be necessary for the project. Press the rec button and record a personalized message for your pet, this will play at the end of each food delivery sequence.

Here is the Firmware:
<pre>/*
  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
 modified by Amanda Ghassaei June 2012
 http://www.instructables.com/id/Twitter-Controlled-Pet-Feeder/
 
 This code is in the public domain.
 
 */
#include <SPI.h>
#include <Ethernet.h>


//variable to prevent overfeeding
boolean justFed = 1;

// 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(190,298,34,132); //<<< ENTER YOUR IP ADDRESS HERE!!!

// initialize the library instance:
EthernetClient client;

const int requestInterval = 60000;  // delay between requests = 1min

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);
  pinMode(3, 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();
  
  testing();
}



void loop()
{
  if (justFed){
    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 == ">feed me!"){
             digitalWrite(2, HIGH);
             digitalWrite(3, HIGH);
             Serial.println("LED ON!");
             delay(1000);//turn on for 1 sec
             digitalWrite(2, LOW);
             digitalWrite(3, LOW);
             justFed = 0;
            }
            if(tweet != ">feed me!"){
             digitalWrite(2, LOW);
             digitalWrite(3, 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();
    }
  }
  else if (millis() - lastAttemptTime > 14400000){//if four hours has passed since last feeding
    justFed = 1;
  }
}


void testing(){
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  delay(1000);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  
}

  

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

You will need to make a two edits before this firmware is ready to use.

1. Insert your IP address in the following line at the top of the firmware:
IPAddress ip(190,298,34,132); //<< ENTER YOUR IP ADDRESS HERE!!!
if you don't know your IP address open File>>Examples>>Ethernet>>DhcpAddressPrinter, upload this code to your arduino with the ethernet shield plugged in (and ethernet cable connected), and open the serial monitor Tools>>SerialMonitor

2. Change the username of your twitter account in the following line (from the function connectToServer():
client.println("GET /1/statuses/user_timeline.xml?screen_name=nomnomnomfeeder&count=1 HTTP/1.1");

Upload firmware on your arduino board, plug in the ethernet modem/router. Tweet "feed me!" from your twitter account and you should see your pet feeder dispense food in a minute or less. The feeder will not receive any further tweets for four hours (to prevent over feeding), during this time, tweet another message to replace "feed me!" as your most recent message.

<p>Genius!</p>
<p>. I featured this Instructable in one of my collections:<a href="http://www.instructables.com/id/8-Twittering-Thingamajigs/" style="">http://www.instructables.com/id/8-Twittering-Thingamajigs/</a></p>
not trying to be rude but can't you just set the automatic feeder to idk automatically feed your dogs... but fun looking project
<p>Hi Amanda,</p><p>Could I ask you some questions in private?</p>
Hi Amanda, great project! I thought I would do this for my senior project at school. Instead of using photoresistors and leds, using a simple transistor with a 2.2K resistor on the base, connecting the emitter to the ground side of the button pad and the collector to the positive side of the button pad would be a more cost-effective and time sparing solution. Also, I would put in piece of code that would post a tweet after the feeding procedure was completed to prevent overfeeding. But thank you, great idea, and this instructable will certainly come in handy.
That's what I was thinking, why not some transistor, maybe because of the power flowing through it to control the pet feeder.
I have gotten to the step where you attached the Ground from the D Battery on the feeder to the PCB board. I must have put it on the wrong area as one of the LEDs is constantly lit. Do you have an exact spot to put it? <br> <br>Also, do you have a part number for the Test Buttons? I can't find them. Great project though!!! I am almost done with mine.
Is the USB cable just for the initial program load? You can disconnect it after that, right? <br>
Definitely a cool project, but 1 question, why use photo resistors and LEDs in stead of just straight transistors or some form of switching circuit? Seems like a bit of a roundabout solution
I would love to see more of an explanation of how the arduino code works. Great project!
This is a nifty bit of work......however......I would NEVER be too busy to get home to your dog! I love English Bull Terrriers. Sadly ours died last year but we are now ready to invite another Bully into our home. <br> <br>If it isn't an English Bull Terrier......it's just a dog! <br> <br>Apologies to owners of other breeds, I'm sure you feel just as passionate about yours.
Great project! But what prevents the dog from shredding it into pieces? :P
just dont loose your phone or your dog might famish:-)
This is too cool! I am really bad with electronics but I will have to learn the basics so I will be able to make this soon.
that is one awesome project! My dog Buckley would love this.
i have been wanting to do something like this

About This Instructable

63,322views

224favorites

License:

Bio: I'm a grad student at the Center for Bits and Atoms at MIT Media Lab. Before that I worked at Instructables, writing code for ... More »
More by amandaghassaei:OTCA Metapixel - Conway's Game of Life "9 Degrees of Freedom" IMU Twitter Controlled Pet Feeder 
Add instructable to: