/* Arduino Email Mousetrap By David Blake Sept 2012 */ #include #include // Network Details byte mac[] = { 0x12, 0x34, 0xab, 0x54, 0x2e, 0xa7 }; //Give your Arduino a mac address byte ip[] = { 192,168,1,84 }; //and an IP address suitable for your network byte dns[] = { 192, 168, 1, 199 }; //a dns server address. It's not used but we need to put something in there for the Ethernet.begin command byte gateway[] = { 192,168,1,199 }; // Default Gateway or Router address byte server[] = { 61,9,189,249 }; // mail.bigpond.com (This is my isp's email server IP address) byte subnet[] = { 255,255,255,0 }; // subnet mask // These intergers are used as constants to control timing and delays in the script int wait = 1000; //used for a basic 1 second delay in places unsigned long hour = 3600000; // For long delay between loops. 3600000 = 1 hour int delaytime = 6; //Time to wait between emails int interval = 0; // initial value int nextinterval = 1; //target value int period = 0; // These integers represent pin numbers and initial states for working out which traps are armed or have fired int trap2 = 2; int trapread2 = 1; int trapstatus2 = 1; //1 for armed int oldtrapstatus2 = 1; int trap3 = 3; int trapread3 = 1; int trapstatus3 = 1; //1 for armed int oldtrapstatus3 = 1; int trap4 = 4; int trapread4 = 1; int trapstatus4 = 1; //1 for armed int oldtrapstatus4 = 1; int armed = 3; int totalarmed = 3; int oldtotalarmed = 3; Client client(server, 25); void setup() { // Specify the input pins for the traps pinMode(trap2,INPUT); pinMode(trap3,INPUT); pinMode(trap4,INPUT); delay(wait); // Think about things.... Ethernet.begin(mac, ip, dns, gateway ); } void loop() { // Check the status of all traps. HIGH = Armed, LOW = Fired trapread2 = digitalRead(trap2); trapread3 = digitalRead(trap3); trapread4 = digitalRead(trap4); if (trapread2 == HIGH) { trapstatus2 = 1; } else { trapstatus2 = 0; } if (trapread3 == HIGH) { trapstatus3 = 1; } else { trapstatus3 = 0; } if (trapread4 == HIGH) { trapstatus4 = 1; } else { trapstatus4 = 0; } // Count how many traps are armed totalarmed = (trapstatus2 + trapstatus3 + trapstatus4); // And work out if it has changed since last time. If it has changed send an email saying so if (oldtotalarmed != totalarmed) { delay(wait); /* allow the router to identify the Arduino before the Arduino connects to the internet */ Ethernet.begin(mac, ip, dns, gateway ); delay(1000); if (client.connect()) { delay(wait); delay(wait); client.println("helo ***1"); // say hello to the SMTP server. delay(1000); // Give time for response client.println("MAIL From ***2"); // identify sender delay(1000); client.println("RCPT To: ***3"); // identify 1st recipient delay(1000); client.println("RCPT To: ***4"); // identify 2nd recipient delay(1000); client.println("DATA"); // announce start of message delay(1000); client.println("Subject: Mousey Mousey"); // insert subject client.println(""); // insert 2 blank lines between subject and body client.println(""); // insert 2 blank lines between subject and body client.println(""); // insert 1 blank line to start of body client.println("The mousetraps status has changed."); // message body client.println(); // blank line in message //Now work out how many traps have fired and include the appropriate line in the message if (totalarmed == 2) { client.print(" There is 1 mousetrap that has gone off "); } else { client.println("There are "); client.print(3 - totalarmed); client.println(" mousetraps that have gone off"); } client.println();// blank line in message // Now tell them which traps have fired or are still armed if (trapstatus2 == 0) {client.println("Trap 2 has gone off");} else {client.println("Trap 2 is still armed");} if (trapstatus3 == 0) {client.println("Trap 3 has gone off");} else {client.println("Trap 3 is still armed");} if (trapstatus4 == 0) {client.println("Trap 4 has gone off");} else {client.println("Trap 4 is still armed");} client.println("");// blank line in message client.println("."); /* Notifys mail server of message end */ client.println("QUIT"); /* tells mail server to close connection */ client.println(); client.stop(); //Disconnect from the mail server } } else { Ethernet.begin(mac, ip, dns, gateway ); delay(1000); if (client.connect()) { delay(wait); delay(wait); client.println("helo ***1"); // say hello delay(1000); client.println("MAIL From: ***2"); // identify sender delay(1000); client.println("RCPT To: ***3"); // identify 1st recipient delay(1000); client.println("RCPT To: ***4"); // identify 2nd recipient delay(1000); client.println("DATA"); // Announce start of message body delay(1000); client.println("Subject: The mousetraps are online"); // insert subject client.println(""); // insert 2 blank lines between subject and body client.println(""); // insert 2 blank lines between subject and body / client.println(""); // insert 1 blank line to start of body client.println(" The mousetraps are online."); // message client.println(); // insert 1 blank line client.println("No traps have been triggered"); // message client.println(""); // insert 1 blank line client.println("."); // Notifys mail server of message end client.println("QUIT"); // tells mail server to terminate connection client.println(); client.stop(); //Close the connection from our end } } /* This section builds a big delay in the loop to stop you getting bombarded with emails. The Arduino will pause here before going back to check the status of the traps again and deciding what email to send. The value of delaytime should be the number of hours between emails. */ while (interval != nextinterval) { delay(hour); period = period++ ; if (period == delaytime) { interval = interval++ ; period = 0 ; } } nextinterval = nextinterval++ ; }