I am attempting to get my challenge back on track after a few hiccups. My car is now fixed but insurance has yet to replace my laptop so my progress will still be slow. I'm also yet to receive the EnOcean Pi and sensor kit, so can't make any headway on that front.
This means my tasks have to shuffle around a bit, and I will work on some aspects of my project that were "time-permitting" rather than "mission-critical".
I currently have OpenHAB running on my RaspberryPi B+ but it was just running the demo project, ie. nothing useful.
Since I have a Logitech USB webcam to hand, I thought I'd look at getting basic motion detection going. I'm learning this as I go, and hopefully the info below is also useful to someone else.
Installing motion
Motion is a free software package designed to monitor video cameras and detect when the image is changing. Perfect for my needs.
Installing it on the RaspberryPi is simple:
sudo apt-get install motion
After installation, I made sure motion would run on start-up, by changing /etc/default/motion to read:
start_motion_daemon=yes
You may want to experiment with the resolution and framerate (also defined in motion.conf). Higher values will place a load on the CPU.
By default, motion will run at 320x240 @ 2fps - this was using about 3% of the CPU normally. I tried it at 640x480 @ 30fps, and motion was using ~45% of the CPU. For now, I'm running with 640x480 @ 2fps (~13% load), but may have to adjust this later.
You can check what formats your camera natively supports with the command:
v4l2-ctl --list-formats-ext
So after all that, I rebooted and checked that motion was indeed running.
Viewing the webcam stream from openHAB
This was easy thanks to a previous post from fvan. Details shamelessly copied from [CaTS] ForgetMeNot - Week 2: Elro CoCo and Pi Cam with OpenHAB. Just add the following to your sitemap:
Frame label="Camera" { Text item=Camera { Frame { Video url="http://localhost:8081" encoding="mjpeg" } } }
Better integration with openHAB
The next step was to figure out how to get openHAB to read the motion status - ie. is there movement or not
Motion provides a number of events which can trigger actions. The two that I am interested in today are on_event_start, and on_event_end. These commands are specified in motion.conf and occur at the start and end of motion detection.
First I added a new "motionDetected" item in openHAB:
Switch motionDetectState "Motion Detected"
and added that switch to my sitemap.
Then I modified my motion.conf as follows:
on_event_start curl --max-time 2 --connect-timeout 2 --header "Content-Type: text/plain" --request PUT --data "ON" http://localhost:8080/rest/items/motionDetectState/state on_event_end curl --max-time 2 --connect-timeout 2 --header "Content-Type: text/plain" --request PUT --data "OFF" http://localhost:8080/rest/items/motionDetectState/state
Essentially this gets motion to tell openHAB to set the motionDetectState when motion is detected, and then clear it when motion is no longer detected.
Restart motion:
sudo /etc/init.d/motion restart
And run openHAB. Now when motion is detected, my switch is set to ON:
and I can see in my logfile:
23:32:25.595 INFO runtime.busevents[:26] - motionDetectState state updated to ON
After a minute of no activity (the default gap setting is 60 seconds), I see the following:
23:33:28.528 INFO runtime.busevents[:26] - motionDetectState state updated to OFF
and the switch returns to the off state: