more G-Labs products

Author Topic: HG and IP camera motion detection email notification on RPI  (Read 4532 times)

January 06, 2016, 03:32:04 PM
Read 4532 times

petediscrete

  • ****
  • Information
  • Sr. Member
  • Posts: 220
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

January 06, 2016, 04:34:55 PM
Reply #1

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.

January 06, 2016, 07:30:41 PM
Reply #2

petediscrete

  • ****
  • Information
  • Sr. Member
  • Posts: 220
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.

January 06, 2016, 07:45:30 PM
Reply #3

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.

January 06, 2016, 07:49:13 PM
Reply #4

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer

January 06, 2016, 07:57:18 PM
Reply #5

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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 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.
« Last Edit: January 06, 2016, 08:00:08 PM by Gene »

January 06, 2016, 10:59:03 PM
Reply #6

petediscrete

  • ****
  • Information
  • Sr. Member
  • Posts: 220
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"

January 07, 2016, 01:04:27 AM
Reply #7

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.

January 07, 2016, 02:50:59 AM
Reply #8

petediscrete

  • ****
  • Information
  • Sr. Member
  • Posts: 220
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

January 07, 2016, 01:32:56 PM
Reply #9

kevin1

  • *****
  • Information
  • Hero Member
  • Posts: 330
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? (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 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");


January 07, 2016, 02:43:09 PM
Reply #10

petediscrete

  • ****
  • Information
  • Sr. Member
  • Posts: 220
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

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

January 07, 2016, 04:06:18 PM
Reply #11

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.

January 07, 2016, 04:25:17 PM
Reply #12

petediscrete

  • ****
  • Information
  • Sr. Member
  • Posts: 220
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.

January 07, 2016, 04:42:31 PM
Reply #13

kevin1

  • *****
  • Information
  • Hero Member
  • Posts: 330
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

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?

January 07, 2016, 04:55:58 PM
Reply #14

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
As stated by MotionPie documentation you need to use the SnapshotURL:

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 method.

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

g.