Friday 17 November 2017

Motion detection with raspi and MQTT

I built a simple surveillance system for my hallway to monitor my cat during holiday. Used a raspberry 2 with a cheap china camera, probably the cheapest way to get a good camera with good possibilities.
I followed the guide to install mjpg-streamer at https://github.com/jacksonliam/mjpg-streamer
and installed motion with : apt-get install motion , verify that motion is working with browsing to IP:8080 and 8081


Mqtt Scripts


I made two bash scripts, one for mqtt switch on and one for off. This could probably be done smarter but it works.


/home/user/motion/mqtt/MotionOff.sh
#!/bin/sh
mosquitto_pub -t /dev/hallwaymotion -m 0

/home/user/motion/mqtt/MotionOn.sh
#!/bin/sh
mosquitto_pub -t /dev/hallwaymotion -m 1

In my case I run the mqtt server on the same server as motion. You might change the mosquitto command a bit.

 

Motion config file


The configfile for motion is located at /etc/motion/motion.conf

In my case I changed the added the following lines:

daemon on
netcam_url http://192.168.1.114:8080/?action=stream
netcam_keepalive on
webcontrol_localhost off
stream_localhost off
on_event_start /home/user/motion/mqttMotionOn.sh
on_event_end /home/user/motion/mqttMotionOff.sh



remember to remove any videodevice lines

Homeassistant 


sensor:
  platform: mqtt
    state_topic: "/dev/hallwaymotion"
    name: "kitchenpresence"
camera:
  - platform: generic
    name: kjokken
    still_image_url: http://192.168.1.114:8080/?action=snapshot

Note: I use the remote camera instead on the local motion stream because i want a better resolution in HomeAssistant than the one motion created