Updated: Motion Detection

Over 3 years ago I augmented my home security system with a Passive Infrared Motion (PIR) sensor and placed it at the front door. I quickly put together a Python script that runs on a Raspberry Pi (RPi) computer.  Basically it sends and email and triggers a IFTTT event whenever there is movement (detected by the RPi GPIO library) by the front door.

Side note: If you haven’t checked out IFTTT I highly recommend you check it out, there are a LOT of really cool things you can do with it.

There has been an issue with the sensor sending multiple text messages per “event”. This is due to how I wrote the code to handle the event detection with a callback. This would allow for multiple events / threads to be fired per movement in rapid succession. There are many ways I could handle this issue.

  • I could do some low level locks around the global variable
    • pro: control the date variable that’s being used by each thread
    • pro: very granular level of control
    • cons: more difficult to code
    • cons: more prone to errors
  • I could add all events into a global in-memory and loop on the main thread over the queue
    • assumption: there is a thread safe in-memory queuing library in Python
    • pro: easy to code
    • pro: could add extra functionality easy per event (e.g. logging messages)
    • con: need to find a thread safe queuing system
    • con: might be overkill
  • I could listen to different events that don’t run on a seperate threads
    • pro: after looking over the RPi GPIO (General Purpose Input Output) documentation again. It seems there are other events that might be more suited to the task.
    • cons: the events I’m looking out would likely miss the extra events that are triggered, but who cares! 🙂

I’m leaning towards the last option. I’ll start with that one tomorrow and see how it goes, and update the blog soon.

 

Update:

Okay, so updated the code. Haven’t checked it into GitHub yet and it is working well. I’m not getting the multiple notifications within the same second. However, I’m still get false positives on occasion. So since this is hardware, I thought maybe something is up with the voltage. Especially, since my RPi is about 40+ feet from the PIR sensor. I got the multimeter out and it was reading a constant 3.2V, so I think that’s fine. What I did notice was the connections were pretty loose, so I tightened those up a bit and we’ll have to wait and see if I still get those false positives.

https://photos.app.goo.gl/Kt5yd05WI6UQSVlE3

 

Leave a Reply

Your email address will not be published. Required fields are marked *