Fun With Motion Detection Software Part 2
If you haven’t read my first post about our new experiment with motion detection head o over and read part 1 – then come on back for part 2!
Everything is working great with with the software set up! It easily detects motion and takes pictures of the events. When we last posted about this I was in the process of adding notification to the software. I was able to add a couple of simple yet fun features to notify us when the software has detected motion.
I wanted a way to let us know when something was detected when we were at home so we could walk over to the computer and see what it caught. What better way to do that then to have the software send us a text message! Most carries have a way for you to send a text message by simply emailing your phone number using the carrier specific email address. After some brief searching I found that my carrier uses this format: yourphonenumber @ vtext.com sending an email to this address triggers their system to send a text message to your phone. NOTE: You will be charged for these if you don’t have an unlimited text plan be careful!
All I had to do now is add some code to send out the message after detection. Luckily .NET has some simple libraries to allow you to send out emails – so with just a few simple lines of code I was able to send an email that generates a text message whenever motion is detected. Here is the email code:
{
MailMessage msg = new MailMessage();
msg.To.Add("send to email");
msg.To.Add("send to email #2");
msg.From = new MailAddress("yourfromaddress");
msg.Subject = "New Motion Detection.";
string mMailServer = "yourmailserver";
msg.Body = "New Image Taken!";
SmtpClient mySmtpClient = new SmtpClient(mMailServer);
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("userName", "password");
mySmtpClient.Credentials = cred;
try
{
mySmtpClient.Send(msg);
}
catch (Exception e)
{
}
}
This works great I added a boolean to the code that is turned on and off by a menu choice allowing us to activate and de-activate the sending of messages when we want. It works great – my son gets really excited when he gets a text message that a new motion was detected!
That just wasn’t enough – there was something missing…
After my son and I discussed it we decided that we needed a way to see the images after they were captured but, without having to go to the computer to see them.
Since we already had the ability to send out email – we thought why not email the pictures to us so we can view them on our phones from anywhere!! Needless to say we changed the code once again and added the ability to send out an email with the images attached to it.
A couple of things to note are that attaching large amounts of photos to am email can make it very large depending on the size of the photos and some email providers won’t accept emails that large also, the processing time can be a hog and delay the rest of the program. To fix these we designed it to only attach the first five images that it collects and we also have it run on a separate thread so the program can go along it’s merry way as the email is processed and sent!
If your wondering about the attachments – it’s simple just add them to the
Message
object like this
msg.Attachment.Add(path to file)
.
As for the threading, once again, .NET makes it easy to create simple threading with the
BackgroundWorker
object. I will leave this for another day as this post is already way too long.
One last thing… After all the fun we’ve had with the motion detection we have decided to change things up so we can do time-lapse! We think it would be really neat to see how things happen over time. So stay tuned for some fun with time-lapse coming in some later posts!