/* Arduino Projects for Dummies * by Brock Craft * * Chapter 11: Building a Tweeting Pet Door * A system that uses hall effect sensors to * detect the movement of a pet flap and then * posts a message to your (or your pet's) twitter account * * Uses the default SPI, Ethernet, EEPROM libraries, * the SHA and Time libraries, * and the Arduino Twitter libraries by Markku Rossi * * v0.1 30.04.2013 */ #include #include #include #include #include #include // Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; IPAddress ip(192,168,1,43); // the IP address may be different on your network EthernetClient client; IPAddress twitter_ip(199, 59, 149, 232); uint16_t twitter_port = 80; // Replace the text below with your CONSUMER KEY and SECRET from Twitter! // Otherwise the code will not work! const static char consumer_key[] PROGMEM = "*****YOUR CONSUMER KEY*****"; const static char consumer_secret[] PROGMEM = "*****YOUR CONSUMER SECRET*****"; char buffer[512]; Twitter twitter(buffer, sizeof(buffer)); unsigned long timestamp; boolean entering = false; char* entryMessage = "I'm baaaack!"; char* exitMessage = "I'm outta here!"; const int entryPin = A0; // the number of the pushbutton pin const int exitPin = A1; // the number of the pushbutton pin int entryValue = 0; // Define variable to store the entry value int exitValue = 0; // Define variable to store the exit value void setup() { Serial.begin(9600); Serial.println("Attempting to get an IP address using DHCP:"); if (!Ethernet.begin(mac)) { //if DHCP fails, start with a hard-coded address: Serial.println("Failed to get an IP address using DHCP, trying the static IP"); Ethernet.begin(mac, ip); } Serial.print("My address:"); Serial.println(Ethernet.localIP()); delay(3000); // delay so we can read stuff on the Serial Monitor pinMode(entryPin, INPUT); pinMode(exitPin, INPUT); // connect to Twitter: twitter.set_twitter_endpoint(PSTR("api.twitter.com"), PSTR("/1/statuses/update.json"), twitter_ip, twitter_port, false); // Reads OAuth account identification from EEPROM. twitter.set_client_id(consumer_key, consumer_secret); // Set OAuth account identification from program memory. // Replace the text below with your ACCESS TOKEN and SECRET from Twitter! // Otherwise the code will not work! twitter.set_account_id(PSTR("******YOUR ACCESS TOKEN******"), PSTR("******YOUR ACCESS TOKEN SECRET******")); } void loop(){ entryValue = analogRead(entryPin); Serial.println(entryValue); // Uncomment this line to monitor the readings if (entryValue < 50){ entering=true; sendTweet(); } exitValue = analogRead(exitPin); if (exitValue < 50){ entering=false; sendTweet(); } delay(10); } void sendTweet(){ if (twitter.is_ready()) { char tweet[140]; timestamp = twitter.get_time(); if(entering){ sprintf(tweet, "%02d:%02d:%02d: %s", hour(timestamp), minute(timestamp), second(timestamp), entryMessage); } else { sprintf(tweet, "%02d:%02d:%02d: %s", hour(timestamp), minute(timestamp), second(timestamp), exitMessage); } Serial.println(tweet); Serial.print("Posting to Twitter: "); if (twitter.post_status(tweet)){ Serial.println("Status updated"); } else{ Serial.println("Update failed"); } } delay(10000); // wait 10 seconds to avoid double triggering (a "pet debounce") }