Smart Mousetrap
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
- ESP32 Mini, ESP32 development board, or whatever ESP32 you prefer
- Mousetraps
- Small reed switches
- Magnets - the tiny magnets that come with the reed switches didn't do a good job
- Wire - the layout of your traps determines how much wire you'll need. See the Wiring section below.
- Dupont connector kit - (optional)
- Dupont Terminal Block (optional, handy for all the ground connections)
- USB cable
- USB wall charger
- Hot glue gun
- Multimeter (optional)
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.
![](https://tech.scarey.net/content/images/2024/05/20240517_122932.jpg)
Code
Getting Started With Micropython
micropython-mqtt
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)
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.
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.