Here is the code:
/ Tweeting when the garage door opens and closes using two reed switches
#if defined(ARDUINO) && ARDUINO > 18 // Arduino 0019 or later
#include <SPI.h>
#endif
#include <Ethernet.h>
#include <EthernetDNS.h>
#include <Twitter.h>
#define LED 13 // Define LED pin
#define SWT 7 // Define the input pin for switch 1 Top Switch
#define SWB 8 // Define the input pin for switch 2 Bottom Switch
// Ethernet Shield Settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x3F, 0x99 };
// substitute an address on your own network here
byte ip[] = { 192,168,1,100 };
// Your Token to Tweet (get it from
http://arduino-tweet.appspot.com/)
Twitter twitter("3xxxxxxxxxxxxxxxxxxx");
const char* msg ="/meh/"; //Placeholder message otherwise compiler will error
int count = 0; //Random integer to insert into Tweets
int valT = 0; // Variable used to store the state of the top switch
int valT_old; // Variable used to store old value and prevent repeat tweets
int valB = 0; // Variable used to store the state of the bottom switch
int valB_old; // Variable used to store old value and prevent repeat tweets
int state = 0;
void setup() {
delay(1000);
Serial.begin(9600);
Ethernet.begin(mac, ip);
pinMode(LED, OUTPUT);
pinMode(SWT, INPUT);
pinMode(SWB, INPUT);
}
void loop(){
valT = digitalRead(SWT); // Read the top switch
valB = digitalRead(SWB); // Read the bottom switch
if ((valT == HIGH) & (valB == LOW)){ // If top switch gets triggered first we know it is going down
if ((valT == HIGH) & (valT_old == LOW)){ // This prevents retweets if garage door stops on the switch
state = 0; // Give the status of the door as closed for LED
msg = "Garage Door is Closed"; // Message to tweet
Tweet(); // Run tweeting program
delay(5000); // Wait for the door to pass both switches before checking again
}}
if ((valT == LOW) & (valB == HIGH)){ // If bottom switch gets triggered first we know it is going up
if ((valB == HIGH) & (valB_old == LOW)){ // This prevents retweets if the garage door stops on the switch
state = 1; // Give the status of the door as open for the LED
msg = "Garage Door is Open"; // Message to tweet
Tweet(); // Run tweeting program
delay(5000); // Wait for the door to pass both switches before checking again
}}
valT_old = valT;
valB_old = valB;
if (state == 1){ // Check the state of the garage door
digitalWrite(LED, HIGH);} // If the garage is open turn ont he LED
else {digitalWrite(LED, LOW);} // Otherwise turn off the LED
}
// Tweeting Program
void Tweet(){
char S[50]; //Define twitter message as "S"
/**********
* SprintF "stitches" together a string(msg) and a variable(randomVariable) into
* another string, "S".
**********/
count = count +1;
sprintf(S, "%s %i",msg, count); //
Serial.println(S);
Serial.println("connecting ...");
//The rest is lifted from the Twitter post example.
if (twitter.post(S))
{int status = twitter.wait();
if (status == 200)
{Serial.print("OK. code ");
Serial.println(status);
}
else
{Serial.print("failed : code ");
Serial.println(status);
}
}
else
{Serial.println("connection failed.");
}}