
It's always lovely to meet a new bug friend in the fall. This is Stanley. An internet search leads me to believe he is a brown marmorated stink bug. Even though Stanley is technically an invasive species in Vermont, that's not his fault - and I welcome him to winter in my apartment. He seems like a nice bug, and takes up very little space. Wikipedia says marmorated means that his color pattern is like marble.


I see him other places too, like in my closet or even in the hinge of my door. Now I have to check for him before I shut it so he doesn't get squashed!
One day I decided to get to know my new bug friend better. I learned he only lives for between 6 and 12 months. He's trying to survive the winter somewhere warm, but not too warm. He appreciates food, but moisture is even more important to him. I wanted to do my part to help him through the winter in comfort.

The feeding station could have been the end of that project, but then I had another idea...
A stupendous stink bug like Stanley deserves to have even the most routine aspects of his life carefully documented for posterity. Also, it might have helped a little that I already happened to have all the equipment I needed for this on-hand. I set up a makeshift trail camera using a DinoLite Basic USB microscope and a headless Linux box running motion to capture photos whenever Stanley is active around his feeding station.
I chose under my bed as the location for the feeding station because the microscope has to be near the Linux box, and the Linux box has to be near the router. Moving the mattress to change Stanley's food and water is a little annoying though.


The server runs Alpine Linux. Here is how to install motion and its dependencies for what I think are image / video processing and webcam management, respectively.
sudo apk update sudo apk add motion ffmpeg v4l-utils
If the motion package cannot be found, you may need to edit /etc/apk/repositories and make sure the test repository or possibly the community repository is included. It's a cool package, so hopefully it will find its way into main eventually.
https://dl-cdn.alpinelinux.org/alpine/v3.23/main https://dl-cdn.alpinelinux.org/alpine/v3.23/community https://dl-cdn.alpinelinux.org/alpine/edge/testing
The version numbers in these links will likely look slightly different from mine depending on your version of Alpine Linux.
Here is my motion configuration file, located at /etc/motion/motion.conf along with my interpretation of the parameters. I worked with ChatGPT to set some of these, and I'm trusting it especially for noise_tune. I guess I'll really only know that it's working well once I start getting photos of Stanley. That said, it's encouraging there haven't been many false positives and that my finger is enough to trigger photos to be taken.
# I think this is about locking? pid_file /var/run/motion/motion.pid # This is the Dino Lite Basic device video_device /dev/video0 # Capture images picture_output on # No movies movie_output off # Video framerate (not saved image rate), Dino Lite likes 5 it seems? framerate 5 # How many frames in a row must contain motion to count minimum_motion_frames 3 # Seconds to wait after motion to stop capturing event_gap 3 # Where to save images target_dir /var/lib/motion # Timestamped filenames picture_filename stanley-%Y%m%d-%H%M%S-%q # Pixels that need to change to count as motion threshold 4000 # Is it some "color distance" within which pixels are treated the same? noise_level 48 # Might miss Stanley so do not use noise_tune off # Keep only latest 1000 pictures on_picture_save /usr/local/bin/prune-motion-images.sh
The on_picture_save line calls for more explanation. The idea here is to keep only the latest 1000 photos so that the storage on the server does not fill up, in case I don't check for a long time. Every time a photo is saved, the custom script at /usr/local/bin/prune-motion-images.sh gets called. It looks like this.
#!/bin/bash find /var/lib/motion -type f -name '*.jpg' \ -printf '%T@ %p\n' | sort -n | head -n -1000 | cut -d' ' -f2- | xargs rm -f
I think motion tries executing the script directly, so it needs to be executable, probably by anyone. From what I can tell, motion runs at its own user which may and may not share groups in common with my user.
sudo chmod 755 /usr/local/bin/prune_motion_images.sh
The next steps involve communicating with the server over ssh. There are different ways to do this (and arguably key-based authentication is better) but I'll use sshpass. I'll assume there are some environment variables already set up.
HOST: Server IP address or hostnameUSR: My username for the serverPW: Path to a file containing the passwordTechnically, you could start and stop the motion service directly on the server, but it's more convenient to have client-side scripts. I put these lines in stanley-start.sh and stanley-stop.sh respectively. An easy way to tell they're working is that these commands will make the little LEDs on the microscope turn on and off.
sshpass -f $PW ssh $USR@$HOST "sudo rc-service motion start" sshpass -f $PW ssh $USR@$HOST "sudo rc-service motion stop"
Finally, a third script stanley-retrieve.sh moves the photos from the server. The first line copies the photos to a temporary directory on the client, and the second line deletes them from the server so you don't get duplicates the next time you run it.
sshpass -f $PW scp -rC $USR@$HOST:/var/lib/motion $HOME/tmp sshpass -f $PW ssh $USR@$HOST "sudo rm /var/lib/motion/*"It's convenient but perhaps less secure to configure the user on the server permission to run sudo without entering a password.