Mouse Trap Alert Project
Our live mouse trap alert system is simple to make. It’s based on Arduino Scratch programming using a Sparkfun Electronics ESP8266 Thing board to connect to the Internet. Make your live mouse trap into an Internet of Things (IoT) device!
First, you’ll need the parts from Sparkfun. We have a project set up on their website for easy shopping:
Tilt Sensor – AT407 SEN-10289


#include <SimpleTimer.h> SimpleTimer timer; // Include the ESP8266 WiFi library. (Works a lot like the // Arduino WiFi library.) #include <ESP8266WiFi.h> // Include the SparkFun Phant library. #include <Phant.h> //////////////////////////////// // BLYNK // //////////////////////////////// // include Blynk ESP8266 Library #include <BlynkSimpleEsp8266.h> // Blynk Authorization Code char auth[] = "<ENTER YOUR BLYNK AUTHORIZATION CODE>"; ////////////////////// // WiFi Definitions // ////////////////////// const char WiFiSSID[] = "<ENTER YOUR WIFI SSID>"; const char WiFiPSK[] = "<ENTER YOUR WIFI PASSWORD>"; ///////////////////// // Pin Definitions // ///////////////////// const int LED_PIN = 5; // Thing's onboard, green LED //////////////// // Phant Keys // //////////////// const char PhantHost[] = "data.sparkfun.com"; const char PublicKey[] = "<ENTER YOUR PUBLIC KEY FROM SPARKFUN>"; const char PrivateKey[] = "<ENTER YOUR PRIVATE KEY FROM SPARKFUN>"; ///////////////// // Post Timing // ///////////////// const unsigned long postRate = 30000; unsigned long lastPost = 0; // Pithons variables setup int state = 0; // holds state of light in case tilt switch bounces int sms = 0; // only send one sms message per cycle int val = 0; // to check tilt switch status String trigger = "Online"; // status of mousetrap switch String currentStatus = ""; // String to display on Blynk virtual LCD (Line 1) String currentStatus2 = ""; // String to display on Blynk virtual LCD (Line 2) String macID = ""; // Mac Address WidgetLCD lcd(V1); bool Connected2Blynk = false; #define LED 12 // Pin to use LED to show status of tilt switch trigger #define CONTACTSWITCH 4 // Tilt switch connects to pin 4 void setup() { initHardware(); connectWiFi(); digitalWrite(LED_PIN, HIGH); pinMode(LED, OUTPUT); // tell Arduino LED is an output pinMode(CONTACTSWITCH, INPUT); // and CONTACTSWITCH is an input Blynk.config(auth); // in place of Blynk.begin(auth, ssid, pass); Blynk.connect(3333); // timeout set to 10 seconds and then continue without Blynk while (Blynk.connect() == false) { // Wait until connected } Serial.println("Connected to Blynk server"); timer.setInterval(11000L, CheckConnection); // check if still connected every 11 seconds // Do a little work to get a unique-ish name. Append the // last two bytes of the MAC (HEX'd) to "Trap-": uint8_t mac[WL_MAC_ADDR_LENGTH]; WiFi.macAddress(mac); macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) + String(mac[WL_MAC_ADDR_LENGTH - 1], HEX); macID.toUpperCase(); } void loop() { val = digitalRead(CONTACTSWITCH); if (val == HIGH) { state = 1; digitalWrite(LED, HIGH); // turn LED ON trigger = "Triggered"; } if (lastPost + postRate <= millis()) { if (postToPhant()) lastPost = millis(); else delay(100); } if (Connected2Blynk) { Blynk.run(); lcd.clear(); currentStatus = "Trap-" + macID + " " + trigger + " @ " + millis()/1000; lcd.print(0, 0, currentStatus); if (trigger == "Triggered") { Blynk.virtualWrite(0, 1023); // 100% brightness } else { Blynk.virtualWrite(0, 1); // LED OFF } } timer.run(); if (state == 1) { // BLINK THE LED digitalWrite(LED, HIGH); // turn LED ON delay(200); digitalWrite(LED, LOW); // turn LED OFF delay(400); digitalWrite(LED, HIGH); // turn LED ON delay(200); digitalWrite(LED, LOW); // turn LED OFF delay(100); digitalWrite(LED, HIGH); // turn LED ON delay(200); digitalWrite(LED, LOW); // turn LED OFF delay(100); digitalWrite(LED, HIGH); // turn LED ON } } void connectWiFi() { byte ledStatus = LOW; // Set WiFi mode to station (as opposed to AP or AP_STA) WiFi.mode(WIFI_STA); // WiFI.begin([ssid], [passkey]) initiates a WiFI connection // to the stated [ssid], using the [passkey] as a WPA, WPA2, // or WEP passphrase. WiFi.begin(WiFiSSID, WiFiPSK); // Use the WiFi.status() function to check if the ESP8266 // is connected to a WiFi network. while (WiFi.status() != WL_CONNECTED) { // Blink the LED digitalWrite(LED_PIN, ledStatus); // Write LED high/low ledStatus = (ledStatus == HIGH) ? LOW : HIGH; // Delays allow the ESP8266 to perform critical tasks // defined outside of the sketch. These tasks include // setting up, and maintaining, a WiFi connection. delay(100); // Potentially infinite loops are generally dangerous. // Add delays -- allowing the processor to perform other // tasks -- wherever possible. } } void CheckConnection() { Connected2Blynk = Blynk.connected(); if (!Connected2Blynk) { Serial.println("Not connected to Blynk server"); Blynk.connect(3333); // timeout set to 10 seconds and then continue without Blynk } else { Serial.println("Connected to Blynk server"); } } void initHardware() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); // Don't need to set ANALOG_PIN as input, // that's all it can be. } int postToPhant() { // LED turns on when we enter, it'll go off when we // successfully post. digitalWrite(LED_PIN, HIGH); // Declare an object from the Phant library - phant Phant phant(PhantHost, PublicKey, PrivateKey); // ENTER THE NAME OF THE TRAP HERE String postedID = "Trap-" + macID + "-HAVAHART"; // Add the four field/value pairs defined by our stream: phant.add("id", postedID); phant.add("time", millis()); phant.add("trigger", trigger); // Now connect to data.sparkfun.com, and post our data: WiFiClient client; const int httpPort = 80; if (!client.connect(PhantHost, httpPort)) { // If we fail to connect, return 0. return 0; } // If we successfully connected, print our Phant post: client.print(phant.post()); // Read all the lines of the reply from server and print them to Serial while (client.available()) { String line = client.readStringUntil('\r'); //Serial.print(line); // Trying to avoid using serial } // Before we exit, turn the LED off. digitalWrite(LED_PIN, LOW); return 1; // Return success }