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.

  • The sensor in this case needs voltage, ground, and a free input pin - I'm using GPIO 14 labelled as 'D5' on this board.
  • The LEDs are optional but handy for debugging, using D6 and D7 (GPIO 12, yellow, and 13, red) for their positive lead
  • I was lazy and just use a single a shared resistor from their negative leads to ground.

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...

Read more »