Showing posts with label raspberry. Show all posts
Showing posts with label raspberry. Show all posts

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





 

Saturday, 31 December 2016

PIR Raspberry PI sensor

Using a HC-SR505(generic PIR) module to detect motion for your home automastion is pretty straigh forward. For my setup i connected pin 2 to 5v pin 12 to signal pin 14 to GND
check this for pinlayout : https://kjetiliot.blogspot.no/p/raspberry-pi.html

Install dependencies:

  1. sudo apt-get install python-pip
  2. pip install paho-mqtt
Code:

Make the script executable

sudo chmod a+x filename

Add script to crontab

To start the script to automaticly run at at startup run the command : sudo crontab -e and add the following line(edit the path to fit your filename and path) : @reboot sudo python /home/pi/pirsensor.py & You can now reboot and check everything is working.
You can test that the pir is publishing correctly by subscribint to the mqtt topic you used in the pir sensor python script mosquitto_sub -v -t 'home/office/presence'

Parts:


Sunday, 27 November 2016

Upgrade HASS RPAIO



sudo su -s /bin/bash hass (sudo)
source /srv/hass/hass_venv/bin/activate   (virtual enviroment)
pip3 install --upgrade homeassistant    (update)
exit ( return to user mode)
sudo systemctl restart home-assistant.service (restart HASS)

press ctrl + f5 to hard refresh your front end

ref: https://home-assistant.io/getting-started/installation-raspberry-pi-all-in-one/

sudo su -s /bin/bash homeassistant
source /srv/homeassistant/bin/activate
pip3 install --upgrade homeassistant
exit
sudo systemctl restart home-assistant@homeassistant