Well here is my latest little fun project.
The Problem - Recent bad weather coupled with new developments around us has seen an influx of mice into our house. While the cats quickly catch any that make it into the house proper, they can't catch the ones living in the roof space. The mice seem to love burrowing under the insulation bats and the scratching noises above your bed can keep you awake at night. A couple of mousetraps in the roof cavity helped improve the problem, but getting up in the roof and crawling from end to end twice a day to check the traps is hot, dusty, dangerous and time consuming.
The solution - No, it wasn't throw a cat in the roof, it was connect the mousetraps to an etherten so it would tell me whenever I had caught something, removing the unnecessary trips into the roof. It now sends me an email 4 times a day telling me whether any traps have gone off, and if so which ones. Now I know when I have to go into the roof, and which traps need checking.
The electronics side was pretty easy. Total parts list was 1 X Freetronics etherten, 1 X Freetronics protosheild short, 3 X mousetraps, 3 X 10k resistors, 1 X short screw terminal block, 3 micro switches saved from an old microwave and about 50 metres of cheap speaker cable.
Using the protoshield the 3 resistors were linked between the digital input pins and ground to bias the pins towards a low state. The switches were hot glued into the mousetraps so that they would close whenever the mousetrap was locked in the 'armed' position. Each was then wired between the 5 volt pin and a digital input via the screw terminal block which was also mounted on the protoshield to make disconnecting the traps a lot easier. I had to use microswitches as the modern mousetraps I had are made of plastic. If you have some of the old fashioned metal traps on a wooden base then the trap itself can form the switch.
For me the interesting part was getting the Etherten to successfully send me an email. Despite several days of googling I couldn't find a single example of someone who had succeeded to do this directly from their arduino just lots of people who seemed to be stuck at different points or were actually sending the email via a script on their PC. Over a couple of days I was able to work through the steps of sending a simple email, and was able to get it going without any additional equipment. One drawback of my implementation is that the email is sent out blindly by the arduino. It doesn't know if the mail server has received the email or not and doesn't read any of the messages sent back. In my case this doesn't really matter as I send a new email every 6 hours anyway and the timing isn't critical.
Although I got this going with bigpond.com in Australia (Telstra), depending on the ISP you use this code may or not work for you. There are many different ways that email can be implemented – just have a look at all the options in the settings for your email client! Also it will probably only work from within your ISPs network as most of them won’t accept mail in this way via another ISP.
So here is the working code. I have only edited out my personal information and substituted it as follows;
***1 = My personal domain that resolves to my external IP address. This is one of those 'extra' bits of information some email servers can require.
***2 = Sending Email Address
***3 = Recipient 1 email address
***4 = Recipient 2 email address
************************************************************************************
Code: Select all
/*
Arduino Email Mousetrap
By David Blake Sept 2012
*/
#include <SPI.h>
#include <Ethernet.h>
// 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++ ;
}
Dave