HomeGenie Forum

General Category => General Discussion => Topic started by: petediscrete on January 06, 2016, 03:32:04 PM

Title: HG and IP camera motion detection email notification on RPI
Post by: petediscrete on January 06, 2016, 03:32:04 PM
Has anyone successfully managed to setup motion detection in HG using an IP camera as a sensor using the Raspberry PI platform.

I've got MotionPie running on an RPI and it is connected to a seperate RPI running HG via my local network. I've tried everything possible to setup motion detection between both of them without any success. I've looked through every previous post relating to this subject and I've noticed that the threads appear to tail off without a working solution. I've posted in the Troublehooting & Support section and have received valuable help from member Kevin1 but unfortunately his HG server is Windows 7 based.

Maybe some of the more experienced C# programmers on this forum could take up the challenge and write a program to automate this process. It has to be one of the most practical programs for HG as it would ensure that HG could provide a fully integrated embedded and very expandable security system at a fraction of the cost of the currently available propriety commercial systems
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: bkenobi on January 06, 2016, 04:34:55 PM
I have not used motion detection on cameras yet thought it's on my wish list.  There are several elements that must work together to get what you want.  First, you need a camera that provides a feed to some kind of processor.  The processor then reviews the frames to determine if whatever your motion criteria is exceeded and triggers a motion alert.  That alert needs to be fed to a module in HG that is watched by either the security APP or something else that will process it and send the email.

It sounds like you are using an IP camera for your video source.  Does the camera have built-in motion detection (some do, most don't AFAIK).  If it does not, then you will need to have some system set up to watch that feed.  If you use an RPi, be aware that the processing is fairly resource intensive.  I played with Blue Iris for Windows and it worked, but that was running on a full PC not a relatively low powered RPi.  If you want to use an RPi, you will most likely need to have a dedicated one for that processing (maybe one per feed, I don't know).  MotionPi is one of the software I read about but never looked into.  From reading, it looked like it would work quite well.

Whatever your processing solution is, you need to be able to send a signal to HG on motion start and stop.  If you use an RPi or Windows, you could install HG on that system and send a command from one to the other as one option.  Or, you could call the correct web page from your HG system using the API approach (see the other thread).

Once HG sees the motion, you need HG to do something with it.  The security APP can send an email alert on trigger.  If you don't like that, you could write your own small script that will do the same thing.  I believe the other thread included a snippet of code to send the email.  To make that work, you simply need a WhenModuleIsChanging command looking for your motion sensor module and send the email when the sensor turns on.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: petediscrete on January 06, 2016, 07:30:41 PM
Just to outline the setup I currently have in place

Server
---------
Raspberry Pi 2 B with CSI camera module running HomeGenie

X10 Equipment
---------------------
CM15Pro
5 MS13 motion sensors
Various light and appliance modules

Client
--------
Raspberry PI model B+ running MotionPie

I posted on the Troubleshooting section yesterday the problems  I was experiencing trying to setup an IP camera motion detection and notification system on HG. I was given some very informative information from member Kevin1 and tried what he suggested without success. Today, I did a configuration backup and a factory reset and restore. I sat down and analysed exactly what I was trying to achieve.

Firstly. as Kevin1 suggested, I needed to make a call from MotionPie to HG to advise HG of an alarm activation. I was able to do this by configuring a Web Hook in MotionPie. I then armed the alarm as he suggested. I triggered a motion detection in MotionPie and voila, an alarm activation in HG followed by an email alarm activation sent to me from HG. All I need to figure out now is how to get HG to send me the attached still images recorded by the MotionPie camera.

The main point here is that the MotionPie is now recognised by HG as a camera/motion sensor so I've nearly achieved what I set out to achieve. The fact that I have a camera working like this in HG means that HG is truly the answer to all my needs. I can even turn off and on lights as the IP Camera is now a fully blown sensor.

If anyone has any thoughts on how to attach images to the HG email notification I would really appreciate them.

Just as an aside, I stopped using MS Windows a long time ago.Fully converted to Linux in all its forms.Also I couldn't justify leaving a desktop PC running 24/7/365 acting as a HG server.Now I have 2 RPI's doing all of the work at a fraction of the energy costs. Still some work to do but I'm getting there.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: bkenobi on January 06, 2016, 07:45:30 PM
I think the key to getting the image emailed is determining where MotionPi saved the image when it detected motion.  If you have a path, you can then tell HG where the image is and attach it as Kevin1 suggested yesterday.  He used an image from a news station as an example, but you would just need to change the link to your RPi storage location or possibly something within the IP camera.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: Gene on January 06, 2016, 07:49:13 PM
http://www.homegenie.it/forum/index.php?topic=900.msg5444#msg5444 (http://www.homegenie.it/forum/index.php?topic=900.msg5444#msg5444)

hope this helps.
g.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: Gene on January 06, 2016, 07:57:18 PM
Further help.
When MotionPi detects motion, you have to make it call HG api.

If you are using the "Generic IP Camera" program, the api to call is:
Code: [Select]
http://<hg_address>/api/Media.IpCamera/<camera_number>/Camera.MotionDetected

then you could have another program that listen to the Sensor.MotionDetect event and sends the email with the code from the link in my previous message.

Use When.ModuleParameterChanged (https://genielabs.github.io/HomeGenie/api/ape/a00001.html#a7e82383574aeff32db8d09d4eb916718) to detect the event.

Code: [Select]
When.ModuleParameterChanged( (module, parameter) =>
{
    if (module.Is("My IP Camera") && parameter.Is("Sensor.MotionDetect"))
    {
        // TODO: fetch the image bytes from the remote camera and send email here
    }
    return true;
});

g.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: petediscrete on January 06, 2016, 10:59:03 PM
I think the key to getting the image emailed is determining where MotionPi saved the image when it detected motion.  If you have a path, you can then tell HG where the image is and attach it as Kevin1 suggested yesterday.  He used an image from a news station as an example, but you would just need to change the link to your RPi storage location or possibly something within the IP camera.

Bkenobi, I have the image stored on the MotionPie RPI. The url on my LAN is http:192.168.1.6. The path for the image file is /data/output/2016-01-06/15-55-35/. Using Gene's instruction could you tell me exactly what to put in the C# code to attach the image and send it. I've tried using Gene's code and I keep getting the error message "Argument cannot be null.Parameter name:buffer"
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: bkenobi on January 07, 2016, 01:04:27 AM
I haven't sent attachments before, but I think you would use Gene's code and then in the loop add Kevin1's snippet with the correct url location.  So, I would think it would be something like:

Code: [Select]
When.ModuleParameterChanged( (module, parameter) =>
{
    if (module.Is("My IP Camera") && parameter.Is("Sensor.MotionDetect"))
    {
        byte[] img = Net.WebService("192.168.1.6/data/output/2016-01-06/15-55-35/<image file name>").GetBytes();
        Net.AddAttachment( "", img )
           .SendMessage(  "<your email address>", "", "HG: Motion Detected" );
    }
    return true;
});

You still need to know the file name for your image, but I believe this code should be really close to your needs.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: petediscrete on January 07, 2016, 02:50:59 AM
Sorry Bkenobi. The image file name is actually 15-55-35.jpg. I left out the file extension on that URL I supplied in my previous post. I've tried what you have suggested but keep receiving that runtime error. The image file is definitely stored on the MotionPie SD card
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: kevin1 on January 07, 2016, 01:32:56 PM
Several comments...
-if the filename is constantly changing for the image file, you might have difficulty guessing it in the HG code.
-try adding http:// to the camera image path
-make sure you can get to your MotionPie "snapshot url" in browser and it shows the latest camera image, something like this: http://192.168.1.6/out.jpg? (http://192.168.1.6/out.jpg?) (I don't have MotionPie so I don't know the default path and filename).   Only after this is proven working in browser, then paste URL into HG.  Note that this won't be the exact motion image acquired by MotionPie, but it will be the latest image at the moment HG queries MotionPie
-if you are trying to do this outside your home network, might need to consider opening a port for camera; I don't think you need to do this.  But for example, to view my camera in HG webcam widget when outside home, the camera has to be on another open port.  HG widget does not copy the camera image inside then serve it outside, it references the image URL directly so http://192.168.1.6/out.jpg (http://192.168.1.6/out.jpg) doesn't work.

Rather than creating a new program, try to first change the security program code from this line:
        Net.SendMessage(recipients, subject, messagetext);
to this, to add camera image to existing HG email:
Code: [Select]
        byte[] img = Net.WebService("<snapshot_url").GetBytes();
        Net.AddAttachment( "", img )
        .SendMessage(recipients, subject, messagetext);


Working example from my code:
Code: [Select]
  byte[] img = Net.WebService("http://<my_external_ip>:8081/out.jpg").GetBytes();
    Net.AddAttachment( "", img )
       .SendMessage( "<phone_number>@mms.att.net", "", "Camera Image");

Title: Re: HG and IP camera motion detection email notification on RPI
Post by: petediscrete on January 07, 2016, 02:43:09 PM
OK Kevin1.I've confirmed that I can grab an image file from the web and send it as an attachment to HG.The Webcam link you supplied in an earlier link is dead. Now I need to grab an image from my Raspberry PI MotionPie. The path would look something like this

http://192.168.1.6/data/output/2016-01-06/22-10-28.jpg (http://192.168.1.6/data/output/2016-01-06/22-10-28.jpg)

I reckon I need to include a user name and password to access the files there. When I ssh into the RPI MotionPie I need to login as follows

Login as:root
Password:9c23762b               (This is the Pi serial number)

I need to know exactly what the syntax is to use in that http call to the Raspberry Pi
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: bkenobi on January 07, 2016, 04:06:18 PM
For testing, I'd recommend just saving a file to some known location so you can send a known accessible file.  Maybe just use the homegenie logo and save it at /home/pi/homegenie.jpg or something.

Once you know you can send an image, it just comes down to determining how to access the right one.  You can either use HG to link directly to it, MotionPie to copy/link the image, or some other script outside both to do one or the other.  I'm not sure what MotionPie's capabilities are, so I can't say if it can do the copy/link for you.  I'd personally consider not using the same file name each time since you would then lose your hard copy.  Seems to me that you could save the image in question to a motion detection image store and then send the one with the current date/time in the name of the file.

Code: [Select]
/home/pi/MotionDetection/image_20160107_0704.jpg
Then you wouldn't need to have any logic in HG.  You simply ask HG to grab the file based on the correct date/time that you have in your code.  Or, you could create this file and update a link to that file that is the current detection image and keep the hard copy too.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: petediscrete on January 07, 2016, 04:25:17 PM
Bkenobi,the image store is on the MotionPie. Its is stored on the SD card in the following format

 /output/data/2016/01/01/12-00-00.jpg The second folder is the day of the week and the third folder is the image time. MotionPie gives you the option to retain the files for a day or a week or a month.Depending on what you choose MotionPie will automatically delete the images you do not require.

I should be able to access the Raspberry Pi MotionPie camera from the Raspberry Pi HG with the http call, fetch the image and have HG email me with the image. It seems that the problem is the HG C# program cannot access or locate the image on the Raspberry Pi MotionPie camera.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: kevin1 on January 07, 2016, 04:42:31 PM
Try this in browser, until it works in browser no need to try in HG:

http://root:[email protected]/data/output/2016-01-06/22-10-28.jpg (http://root:[email protected]/data/output/2016-01-06/22-10-28.jpg)

I really think you need to find the snapshot URL, getting the date and time of exact MotionPie motion detect image seems prone to fail in my opinion.  Maybe you could create a shell script to copy the latest motion image to a known constant path/filename?
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: Gene on January 07, 2016, 04:55:58 PM
As stated by MotionPie documentation you need to use the SnapshotURL:

https://github.com/ccrisan/motionpie/wiki/FAQ (https://github.com/ccrisan/motionpie/wiki/FAQ)
Quote
What's the deal with Snapshot URL, Streaming URL and Embed URL?

Snapshot URL serves a single JPEG file when requested. The served JPEG image is always the most recently captured frame. You can use this URL as the src attribute of an <img> HTML tag but keep in mind that it won't update automatically. You shouldn't use this unless you know what you are doing.


You can provide access credentials for that url by using the WithCredentials (http://genielabs.github.io/HomeGenie/api/ape/a00008.html#a76a3d1cf9efd6035b0cbace66d94f0b5) method.

Code: [Select]
byte[] img = Net.WebService("<snapshot_url>").WithCredentials("user", "pass").GetBytes();
// ...

g.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: petediscrete on January 07, 2016, 07:08:35 PM
Tried what you suggested Gene but still do noy have this working

I'm way out of my depth here regarding C# programming.It would probably be a very easy task for someone who was familiar with C# programming.In fact the pros are probably smiling at this one ;)

I have managed to configure the MotionPie RPI camera in HG as a sensor.This means that it will trigger an alarm activation which in turn will email me to alert me of this fact without an image Seperately the MotionPie camera will email me the movement detection images.Belt and braces approach ;D

I really would have liked to have HG to forward me the image but I've tried everything without success.

A big thanks to Kevin1,Bkenobi and Gene for all their help.It was much appreciated.From the details i've supplied maybe someone will work out a working solution.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: bkenobi on January 07, 2016, 08:49:35 PM
I've been intrigued by security cameras for some time but have not invested any time or resources into actually using any beyond simple web cam + Blue Iris.  Can anyone recommend an inexpensive IP camera that might allow me to perform basic testing without costing too much.  I'd think something on the order of $25 USD or less might be available for an indoor model.  If I was going to spend more than that, I'd be close to that of some of the outdoor models I've considered.

My primary concern is being able to get these installed such that wire routing is possible.  I doubt wireless (WiFi) models would work well when used with more than 1-2 cameras due to bandwidth so I presume wired would be a better option.  If I need to wire the camera to a hub, it might be worth going with cheap non-IP cameras with a Unix based monitoring system (Lorex, Samsung, etc).

Either way, something cheap to play with would be a good place to start I think.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: kevin1 on January 08, 2016, 01:31:04 PM
I like cheap too :-)  I was using an old android phone with the ip webcam (https://play.google.com/store/apps/details?id=com.pas.webcam) app for a while until we had to call it back into service when another phone was broken.  Then I bought a $3 webcam and currently use that with yawcam  (http://www.yawcam.com/)software to make it an ipcamera for HG (I have a feature request to allow windows USB cameras in HG).  I just use these in the house when we go on trips for reassurance everything is okay at home.

I'm interested in a "real" outdoor system too but wiring will be difficult at my house. The Netgear Arlo (http://www.arlo.com/en-us/) cameras are interesting, but expensive, and from what I could gather (while standing in BestBuy debating an impulse buy), they aren't too "hacker friendly".  If not aware, they are weatherproof, HD, completely wireless... battery powered and some RF to a hub.  They only record when motion detection wakes them up.

I was thinking about a cheap trail camera mounted to tree aiming at house with an EyeFi card too :-)
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: petediscrete on January 08, 2016, 02:35:55 PM
You mention that bandwidth with a Wifi setup may be an issue. Surely you could install a seperate AP with a repeater to solve that issue. With broadband speeds up to 150mbs in a lot of areas surely this would more than cope with your requirements. I am trying desperately trying to escape from a wired system in all cases where possible. 
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: bkenobi on January 08, 2016, 04:32:17 PM
WiFi has a lot of channels, but only a few can be used in the same location (3 is the maximum to my knowledge).  WiFi spec suggests that there is plenty of bandwidth, but I don't know that I believe it as I've never seen anywhere close to spec speeds on either B or G systems.  N is probably a bit better since it seems to have the capability to send/receive using different antennas which should be a dramatic improvement, but I don't have an N router.

The other choice is to get an A router which would provide a whole new network since it operates at 5GHz rather than 2.4GHz like B/G/N.  Range is not as good but throughput should be better (spec claims anyway).  But, I don't know if any cameras use A so it may not be an option.

I've read about the Arlo cameras and they seem intriguing.  But, I haven't read anything beyond testing reports which could mean they theoretically work great but are garbage in reality.

One of my original thoughts was to utilize trail cameras that were WiFi or cellular.  I bought one to put in my field so I could see what animals hang out there but didn't think a non-connected camera helped in any way since it's only useful after an event for seeing what happened after the fact.  Security trail style cameras exist but are extremely expensive.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: kevin1 on January 08, 2016, 05:28:10 PM
That's where I thought an EyeFi card in the trail cam would be a way to monitor it for pictures if it was within wifi range.  I haven't used an EyeFi card so I don't know the capabilities or if there are better products from other brands.
Title: Re: HG and IP camera motion detection email notification on RPI
Post by: jshan on April 23, 2016, 07:49:16 AM
I've been meaning to write some code so that the basic alarm would send me some pictures from my cameras.  Then I found this thread.  So I uploaded the app I wrote:
http://www.homegenie.it/forum/index.php?topic=1530.0 (http://www.homegenie.it/forum/index.php?topic=1530.0)