]> git.piffa.net Git - arduino/blob - books/pdummies/APFD_Chapter11_Tweeting_PetDoor/APFD_Chapter11_Tweeting_PetDoor.ino
kit 5
[arduino] / books / pdummies / APFD_Chapter11_Tweeting_PetDoor / APFD_Chapter11_Tweeting_PetDoor.ino
1 /* Arduino Projects for Dummies
2  * by Brock Craft 
3  *
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
8  *
9  * Uses the default SPI, Ethernet, EEPROM libraries,
10  * the SHA and Time libraries, 
11  * and the Arduino Twitter libraries by Markku Rossi
12  *
13  * v0.1 30.04.2013
14 */
15
16 #include <SPI.h>         
17 #include <Ethernet.h>
18 #include <EEPROM.h>
19 #include <sha1.h>
20 #include <Time.h>
21 #include <Twitter.h>
22
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;
28
29 IPAddress twitter_ip(199, 59, 149, 232);
30 uint16_t twitter_port = 80;
31
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*****";
36
37 char buffer[512];
38 Twitter twitter(buffer, sizeof(buffer));
39
40 unsigned long timestamp;
41 boolean entering = false;
42 char* entryMessage = "I'm baaaack!";
43 char* exitMessage = "I'm outta here!";
44
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
49
50 void setup() {
51   Serial.begin(9600);       
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);
57   }
58   Serial.print("My address:");
59   Serial.println(Ethernet.localIP());
60   delay(3000); // delay so we can read stuff on the Serial Monitor 
61   
62   pinMode(entryPin, INPUT); 
63   pinMode(exitPin, INPUT);  
64
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. 
71   
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******"));
76 }
77
78 void loop(){
79   entryValue = analogRead(entryPin);
80   
81   Serial.println(entryValue); // Uncomment this line to monitor the readings
82   
83   if (entryValue < 50){
84     entering=true;
85     sendTweet();
86   }
87
88   exitValue = analogRead(exitPin); 
89   if (exitValue < 50){
90     entering=false;
91     sendTweet();
92   }
93   delay(10);
94 }
95
96 void sendTweet(){
97
98      if (twitter.is_ready())  {
99       char tweet[140];
100       timestamp = twitter.get_time();
101        
102    if(entering){
103       sprintf(tweet, "%02d:%02d:%02d: %s", hour(timestamp), minute(timestamp), second(timestamp), entryMessage);
104    } else {
105       sprintf(tweet, "%02d:%02d:%02d: %s", hour(timestamp), minute(timestamp), second(timestamp), exitMessage);
106    }
107    
108       Serial.println(tweet);
109       Serial.print("Posting to Twitter: ");
110      if (twitter.post_status(tweet)){
111       Serial.println("Status updated");
112     }
113     else{
114       Serial.println("Update failed");
115     }
116   }
117   delay(10000);  // wait 10 seconds to avoid double triggering (a "pet debounce")
118 }
119