Components

This one is extremely basic, but it gets the job done and makes use of a microcontroller I wasn't really using for anything else - a "NodeMCU" ESP8266 board. Any ESP should work although you might have to tweak the wifi headers to compile for ESP32.

The other major component is one of those IR 'line sensor' modules sometimes used to make robots. They're dirt cheap, and seem to go by many names - MH-B is a search term that I've had success with. The pinout is OUT - GND - VCC and the output goes low when it detects something reflective in IR close to the sensor - up to about 10cm.

I did experiment with a tiny PIR sensor I had,the AM312, but it was more sensitive than I needed and drew considerably more power - between 20 and 70 mA in my testing, compared to the 6mA of the setup using the MH-B sensor. Still, depending on your use-case, you might want to hook a PIR sensor up instead.

I also added two LEDs because I like LEDs and they were handy to debug the state of the device in action. The yellow LED blinks while the code waits for Poppy to ... finish her business, and the red LED echoes what activity the sensor is reporting.

Assembly

Literally just poking things into a breadboard, no soldering needed if the MCU has pins already.

Diagram

Here's a quick goofy schematic if you want:

Schematic
Schematic showing an ESP12 hooked up to an IR sensor and two LEDs.

Software

Microcontroller side

I've never had much luck with the Lua stuff so I used the Arduino IDE and its ESP board support. I'm also using my LazySerial library that I've added to the Arduino library hub, so you'll want to install that, too. The library makes it very easy to add a bunch of callbacks in the code that are mapped to basic commands over the USB serial port, and for this project I used them to configure the wifi details and what to do when it detects activity.

  1. Get the esp8266-sentinel sketch and update globals.h with the pins appropriate for your microcontroller. If you are using a sensor other than the MH-B, it might pull the output HIGH rather than LOW for the 'detected' signal, as was the case for my AM312 - you can flip the logic on those #defines in that case.
  2. Flash the code to your board.
  3. Open the serial port at 9600 baud. Type the command OHAI and it should respond. I find this extremely handy when I have multiple projects connected over USB.
  4. The full documentation of all the commands is on the bitbucket repository but type HELP and it'll list 'em so you can experiment. The ones you'll want to use while testing are `WIFI CONNECT <ssid> <pass>`, `SHOW_WIFI`, and `POST <url> <payload>`
  5. Once you have the software side configured to your liking, you can use the `ON STARTUP` / `ON TRIGGER` / `ON SLEEP` commands to tell the microcontroller what to do in each event. You can separate multiple commands with semicolons when using these three. `SAVE` will store the command sequences to flash so they survive the next boot.

Server side

This is where the configuration will vary considerably based on your own home setup. What do you want to trigger when the sensor detects litterbox activity? The code supports a `POST <url> <payload>` command that can send a message to a waiting webhook. In my case, I want to trigger another project of mine, my 'ring light', to do a spinny 'alert' effect.

My ring light is not hooked up to the wifi directly, but via a Perl daemon I run on my desktop. It lets me control it via commands from my Twitch chat when I'm streaming. It may be overkill for what you want to install it and also build my ring light, which is still very much a work-in-progress, but I bet you have something that you could send a simple webhook-style message to.

I'll explain my own setup for reference, at any rate:

The ESP is configured with the following command sequence for the trigger and sleep events:

ON TRIGGER WIFI CONNECT Nekonet2 password;POST http://10.8.8.10:2366/ring-light command=alert+12+2+0;WIFI OFF;
ON SLEEP WIFI CONNECT Nekonet2 password;POST http://10.8.8.10:2366/ring-light command=idle;WIFI OFF;SLEEP;
SAVE

That server running on port 2366 is my lazy-serial-daemon project, which I use to try and wrangle all my USB serial projects that are connected to assorted random `/dev/ttyUSB` devices. It is written in Perl for my Linux desktop - it may work for a Mac too, but I have no idea how serial ports on Windows work so this part is very much Batteries Not Included.

If I wanted to skip the desktop PC / dedicated server requirement, I might hook it up to my lazy-esp32-banner project, a scrolling text LED banner that exposes a web interface. To send a message to that, I'd configure the Poopopticon with the command:

POST http://10.8.8.23/scroll text=Time+to+clean+the+kitty+litter!

Other considerations

This setup works for me because I use an old cardboard box to enclose the litter tray, and Poppy generally brushes up quite close to the sensor when ... selecting a spot. As mentioned, I did try a mini PIR sensor which could work at much longer range, but I noticed several false alarms when testing it out, and it used much more power, surprisingly.

Since it's in the bathroom there aren't any power points nearby, so I run the whole thing off a large USB power bank. At 6mA it's not a big drain, my 10,000mAh battery goes for multiple weeks since the MCU goes into a sleep mode after the 'event' is detected. Note that some power banks will shut off if they detect very low power draw, concluding that whatever you're trying to charge with them is now full - quite a handy feature normally, but it will mess up our low-power project here.

I'm sure I could get the power usage even lower with some sort of dedicated circuit around the sensor, and just power up the MCU when using wifi, switching it off entirely when it's not needed - but the sleep mode is 'good enough' for this project. Using Bluetooth or Zigbee etc. could also reduce power requirements, but most of the device's time is spent asleep, so it wouldn't save that much overall.