1 /* Arduino Projects for Dummies
4 * Chapter 11: Building a Tweeting Pet Door
5 * A system that uses hall effect sensors to
6 * detect the movement of a pet flap and then
7 * posts a message to your (or your pet's) twitter account
9 * Uses the default SPI, Ethernet, EEPROM libraries,
10 * the SHA and Time libraries,
11 * and the Arduino Twitter libraries by Markku Rossi
23 // Enter a MAC address for your controller below.
24 // Newer Ethernet shields have a MAC address printed on a sticker on the shield
25 byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
26 IPAddress ip(192,168,1,43); // the IP address may be different on your network
27 EthernetClient client;
29 IPAddress twitter_ip(199, 59, 149, 232);
30 uint16_t twitter_port = 80;
32 // Replace the text below with your CONSUMER KEY and SECRET from Twitter!
33 // Otherwise the code will not work!
34 const static char consumer_key[] PROGMEM = "*****YOUR CONSUMER KEY*****";
35 const static char consumer_secret[] PROGMEM = "*****YOUR CONSUMER SECRET*****";
38 Twitter twitter(buffer, sizeof(buffer));
40 unsigned long timestamp;
41 boolean entering = false;
42 char* entryMessage = "I'm baaaack!";
43 char* exitMessage = "I'm outta here!";
45 const int entryPin = A0; // the number of the pushbutton pin
46 const int exitPin = A1; // the number of the pushbutton pin
47 int entryValue = 0; // Define variable to store the entry value
48 int exitValue = 0; // Define variable to store the exit value
52 Serial.println("Attempting to get an IP address using DHCP:");
53 if (!Ethernet.begin(mac)) {
54 //if DHCP fails, start with a hard-coded address:
55 Serial.println("Failed to get an IP address using DHCP, trying the static IP");
56 Ethernet.begin(mac, ip);
58 Serial.print("My address:");
59 Serial.println(Ethernet.localIP());
60 delay(3000); // delay so we can read stuff on the Serial Monitor
62 pinMode(entryPin, INPUT);
63 pinMode(exitPin, INPUT);
65 // connect to Twitter:
66 twitter.set_twitter_endpoint(PSTR("api.twitter.com"),
67 PSTR("/1/statuses/update.json"), twitter_ip, twitter_port, false);
68 // Reads OAuth account identification from EEPROM.
69 twitter.set_client_id(consumer_key, consumer_secret);
70 // Set OAuth account identification from program memory.
72 // Replace the text below with your ACCESS TOKEN and SECRET from Twitter!
73 // Otherwise the code will not work!
74 twitter.set_account_id(PSTR("******YOUR ACCESS TOKEN******"),
75 PSTR("******YOUR ACCESS TOKEN SECRET******"));
79 entryValue = analogRead(entryPin);
81 Serial.println(entryValue); // Uncomment this line to monitor the readings
88 exitValue = analogRead(exitPin);
98 if (twitter.is_ready()) {
100 timestamp = twitter.get_time();
103 sprintf(tweet, "%02d:%02d:%02d: %s", hour(timestamp), minute(timestamp), second(timestamp), entryMessage);
105 sprintf(tweet, "%02d:%02d:%02d: %s", hour(timestamp), minute(timestamp), second(timestamp), exitMessage);
108 Serial.println(tweet);
109 Serial.print("Posting to Twitter: ");
110 if (twitter.post_status(tweet)){
111 Serial.println("Status updated");
114 Serial.println("Update failed");
117 delay(10000); // wait 10 seconds to avoid double triggering (a "pet debounce")