My Experience with MQTT and Motion Detection

Hello everyone, I wanted to share my experience setting up a motion detection system using MQTT and a Raspberry Pi. It’s been a fun and educational project, and I hope my insights can help others who are starting out with similar setups.

I recently purchased a PIR sensor and connected it to my Raspberry Pi. The goal was to detect motion in my garage and send notifications to my Home Assistant setup. Initially, I set up the sensor without authentication, and it worked perfectly. However, I soon realized the importance of securing my MQTT broker, and that’s where I ran into a bit of trouble.

After some research, I found that incorporating authentication into my Python script was the way to go. I stumbled upon a helpful script that others had shared, and with a few modifications, I was able to get everything working smoothly. The script now includes username and password authentication, which makes my setup much more secure.

Here’s a snippet of the modified script for those who might be interested:

python
import RPi.GPIO as GPIO
import time
import paho.mqtt.publish as publish

GPIO.setmode(GPIO.BCM)
GPIO_PIR = 4
print(“PIR Module Test (CTRL-C to exit)”)
GPIO.setup(GPIO_PIR, GPIO.IN)

Current_State = 0
Previous_State = 0

try:
print(“Waiting for PIR to settle…”)
while GPIO.input(GPIO_PIR) == 1:
Current_State = 0
print(“Ready”)

while True:
    Current_State = GPIO.input(GPIO_PIR)
    if Current_State == 1 and Previous_State == 0:
        print("Motion detected!")
        publish.single("motion/motion", "Motion Detected", retain=True, hostname="172.28.8.110", username="your_username", password="your_password")
        Previous_State = 1
    elif Current_State == 0 and Previous_State == 1:
        print("Ready")
        publish.single("motion/motion", "No Motion", retain=True, hostname="172.28.8.110", username="your_username", password="your_password")
        Previous_State = 0
    time.sleep(0.01)

except KeyboardInterrupt:
print(“Quit”)
GPIO.cleanup()

This script now includes authentication details, which I found were crucial for maintaining the security of my MQTT broker. I also learned the importance of properly configuring the GPIO pins and ensuring that the sensor has enough time to settle before detecting motion.

One thing I’m still curious about is how to handle multiple sensors simultaneously. I’m planning to expand this setup to cover more areas of my home, and I’d love to hear how others have managed similar projects. Have you incorporated multiple PIR sensors into your MQTT setup? How do you handle the data flow and ensure that everything runs smoothly?

I’m also thinking about integrating this motion detection system with my existing smart home devices. For example, detecting motion in the garage could trigger the lights to turn on or send a notification to my phone. The possibilities are endless, and I’m excited to explore more as I continue to learn.

If anyone has tips or suggestions for improving this setup, I’d be more than happy to hear them. Happy tinkering everyone! :rocket: