Smart Mousetrap

Smart Mousetrap
Photo by Mert Guller / Unsplash

I got tired of hearing critters running around in my walls. Ordered some traps and quickly got tired of going into the attic every day to check them. I ended up adding small reed switches and magnets to the traps and wiring them to an ESP32. Now I just wait for the "A πŸͺ€ went off! Another 🐁 bites the dust!" message, grab a Ziploc bag and head up to the attic hoping it was a merciful death.

πŸͺ€ Running tally πŸͺ€ - 🐁🐁🐁🐁🐁🐁🐁🐁🐁🐁🐁🐁🐁🐁🐁🐁🐁


Supplies

When you purchase through links on this site, I may earn an affiliate commission.


Wiring and Assembly

You need 2 wires between each trap and ESP32. Depending on the spread you might be better off having multiple ESP32s and shorter runs of wire. One wire from each trap needs to go to ground and the other to an input pin. Among others, pins 16 - 33 are safe to use.

The ends of the wires on the trap side are connected to the reed switch. Test the reed switch before connecting the wires as some are likely DOA. Hot glue the reed switch to the bottom of the trap opposite the teeth and hot glue the magnet above that on the part you press down. I used a multimeter while gluing the magnet to ensure it was in a good spot to trigger the reed switch.

Magnet and reed switch hot glued in place

Code

Getting Started With Micropython

1. Getting started with MicroPython on the ESP32 β€” MicroPython latest documentation

micropython-mqtt

GitHub - peterhinch/micropython-mqtt: A β€˜resilient’ asynchronous MQTT driver. Plus a means of using an ESP8266 to bring MQTT to non-networked targets.
A 'resilient' asynchronous MQTT driver. Plus a means of using an ESP8266 to bring MQTT to non-networked targets. - GitHub - peterhinch/micropython-mqtt: A 'resilient' asynchronous M…

Grab mqtt_as/mqtt_as.py and mqtt_as/mqtt_local.py and upload them to the root of the ESP32 without the subdirs. We'll need to tweak mqtt_local.py with your WiFi and MQTT broker settings.

OTA library (optional)

GitHub - scarey/simple-ota-updater: Micropython code to read updated code via MQTT and update the ESP32 filesystem
Micropython code to read updated code via MQTT and update the ESP32 filesystem - GitHub - scarey/simple-ota-updater: Micropython code to read updated code via MQTT and update the ESP32 filesystem

If you want to be able to remotely update the code, grab ota.py and upload to the root of the ESP32. See this post for more details.

This project's code

Copy main.py to the root of the ESP32.

GitHub - scarey/smart-mousetrap: Micropython code for monitoring mousetraps using ESP32.
Micropython code for monitoring mousetraps using ESP32. - scarey/smart-mousetrap

Configuration

You just need to let the software know what pins the trap(s) are connected to via MQTT configuration data in the 'esp32/mousetrap/config' topic (set retain for the config). It looks something like:

{
  "activePins": [25,26,27]
}

Sample esp32/mousetrap/config

To use multiple ESP32s just change the BASE_TOPIC in the code for each to a unique value, e.g. 'esp32/mousetrap2'.


Notifications

When the trap is sprung an MQTT message with a comma separated list of all sprung trap pin numbers will be sent. Use Home Assistant automation or Node-RED etc to let you know traps went off. Home Assistant phone notifications work well and are free. Twilio costs a few bucks a month and let's you send text messages.


Notes

I've got an outlet near the air handler for the AC so I just power the ESP32s from that. If you have a light socket you could get a socket adapter to add an outlet there. If there's any interest I could look into running off a battery and going into deep sleep mode, etc, etc.

Technology icons created by Freepik - Flaticon