Email Alert Mousetrap

Showcase your projects! We love to see what people come up with, so please share your creations here.
Posts: 22
Joined: Wed Nov 02, 2011 10:38 am
Location: Hoppers Crossing

Email Alert Mousetrap

Postby dave » Thu Sep 13, 2012 11:12 am

Email Alert Mousetrap

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.

Finished Mousetrap in the roof - unarmed
Finished Mousetrap armed and closing the circuit
No need to get up in the roof today

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 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 }; // (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
  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 );
  if (client.connect()) {
                         client.println("helo ***1"); // say hello to the SMTP server.
                         delay(1000); // Give time for response
                         client.println("MAIL From ***2"); // identify sender
                         client.println("RCPT To: ***3"); // identify 1st recipient
                         client.println("RCPT To: ***4"); // identify 2nd recipient
                         client.println("DATA"); // announce start of message

                         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.stop(); //Disconnect from the mail server
   else {
    Ethernet.begin(mac, ip, dns, gateway );

  if (client.connect()) {
                         client.println("helo ***1"); // say hello
                         client.println("MAIL From: ***2"); // identify sender
                         client.println("RCPT To: ***3"); // identify 1st recipient
                         client.println("RCPT To: ***4"); // identify 2nd recipient

                         client.println("DATA"); // Announce start of message body

                         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.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) {
  period = period++ ;
 if (period == delaytime) {
   interval = interval++ ;
   period = 0 ;

nextinterval = nextinterval++ ;



User avatar
Freetronics Staff
Freetronics Staff
Posts: 296
Joined: Sat Oct 15, 2011 11:31 am

Re: Email Alert Mousetrap

Postby jonoxer » Thu Sep 20, 2012 1:26 pm

That's really clever, Dave!

Now that I think about it I can't remember seeing anyone send email directly from an Arduino before. Hopefully this example will provide some inspiration to other people trying to get it working.

Posts: 3
Joined: Fri Oct 18, 2013 6:24 am

Re: Email Alert Mousetrap

Postby dube68 » Fri Oct 18, 2013 10:01 am

Hi Dave,
I'm a complete noob to everything IT & Electronics but I'm interested in learning.

I've bought myself a few Arduino items with the view to making something a little practical as a way of learning.

I've got an Arduino Duemilanove, coupled to an Arduino Ethernet Shield, coupled to an irTemp module.

I'd like the irTemp module to continually check temperature, log it to the Arduino Ethernet Shield's SD card every minute, then email the log once a day. But, if the temperature gets over a critical threshold (eg 41deg C) then IMMEDIATELY send an alert email.

I see this is do-able with your project, but don't really know how to mould 2 different sketches (yours + irTemp to achieve that.

Is this do-able?
I'm very grateful for any help / advice.

Posts: 7
Joined: Wed Feb 26, 2014 2:50 am

Re: Email Alert Mousetrap

Postby chupps » Mon Mar 20, 2017 9:12 pm


I know this is an old post but hope you are still able to help out.

The Ethernet.h library has been updated and I am not able to verify your code as some classes have been renamed.

Do you have an updated code for the new library? or would the freetronic staffs that replied be able to help me?

Kind regards,


Return to “Project Showcase”

Who is online

Users browsing this forum: No registered users and 1 guest