I am very fortunate to have a great view from my home office window. When I am away, I want to see what I am missing and I am frequently away. I used to have my own website and a home weather station that would upload via ftp all of the weather data and a photo taken out the window, however, the website host provider made maintenance of the website onerous, so after many years, I dropped it. I intended to find a replacement method of looking out the window e.g. free websites, blogs, email... but in most cases automated ftp transfers to these potential solutions were blocked. An added complication is that I am frequently away for months at a time, so whatever solution that is settled upon must be reliable.
I was inspired by Instructables author Olivi3r and his Instructable for creating a security camera. After a day or so of hand wringing, I had it working. This Instructable adds a few key details.
Basically, the goal is to snap a photo with the webcam every morning and then send the photo to myself via gmail as an attachment. This will happen as follows:
- AC power will be switched on for 15 minutes using an 110VAC timer
- Computer will power up
- Webcam software will capture a photo
- A Windows batch file will execute that will:
- Execute a PowerShell script file that will send the email and attachment
- Batch file command will shut down the Computer
- AC power will turn off
Caveats: I am not a Windows programmer - don't ask me if it does not work. I got this approach to work by snorkeling through the Internet until I found the needed insight into debugging my files. Secondly, I wanted this to run on a Windows XP machine, I am sure that there are better solutions on a Windows 10 computer. In fact, there are probably better solutions on an XP machine. If you find any, stick them in the comments. Running this approach on a laptop could be dicey because the computer must turn on when the AC power is switched on. The Instructables code editor has a nasty habit of inserting
and other HTML tags into the code (including Olivi3r's code). I think I have edited all of these out, but beware.. Lastly, the Instructables "Full Preview" button would only give me a blank white page - so WYSIWYG!
We will do this in 4 steps:
- Prepare the computer
- Write and debug a PowerShell script
- Write and debug a batch file
- Wrap it all up and Bob's your uncle!
Step 1: Prepare the Computer
First, dig an old desktop out of the closet. If it is running on Windows XP (SP3) - it will work. This Instructable is designed for XP but the approach should be the same on Windows 10. Fire it up and invoke the BIOS (usually F1, del or F2 during the boot process). Find something like "Power Management" and change it to "AC Power Recovery - ON". This will make the computer boot up when the AC power is turned on. Test it.
You will need to disable all passwords and sign on as administrator. This is because the computer must complete the boot up process unattended. Go to Control Panel, User Accounts and make the appropriate changes if required.
Next, you need a webcam and a webcam application that will snap a picture and store it. I use Dorgem - simple and free. Set up Dorgem to take a picture and store it on the desktop. Don't change the name of the picture when taking a new picture, instead overwrite the last picture. In this example the picture is Pic.jpg. Also, note that I am storing the picture on the desktop. This is important because the directory path is easy. Drag and drop the Dorgem icon into the Windows STARTUP folder.
Now - a couple of tricky bits:
You must change your Chrome account to allow less secure apps. Goggle "chrome less secure apps" and select the first option - probably this one. Turn this feature on. You will get an email alert that informs you of the dangers of this setting. You may not have to do this if you use a different email server, e.g. Yahoo, AOL...
Next you must enable the execution of PowerShell scripts. If you are running Windows 10, this is easier - click on the Windows icon (lower left corner of screen), scroll down the list of programs to Windows PowerShell, expand, right click on PowerShell and select "Run as Administrator". This will open the PowerShell window. On XP the process is a bit more complicated - use File Explorer, find the PowerShell directory (something like C:\Windows\system32\WindowsPowerShell\v1.0), right click on the PowerShell icon and select "Pin to start menu". Now you have access to PowerShell by clicking on the Start icon (lower left) and clicking on PowerShell icon. Click on the PowerShell icon, in the window that pops up type the following command:
You will be asked to confirm by typing "y".
confirm that you have successfully changed the execution policy by typing:
Ok! Your computer is ready.
Step 2: Write and Debug a PowerShell File
Copy and paste this text into Notepad:
$EmailTo = "email@example.com" $EmailFrom = "firstname.lastname@example.org" $Subject = "View" $Body = "x" $SMTPServer = "smtp.gmail.com" $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body) $attachment = New-Object System.Net.Mail.Attachment("C:\Documents and Settings\Administrator\Desktop\pic.jpg") $SMTPMessage.Attachments.Add($attachment) $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $SMTPClient.EnableSsl = $True $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("userID", "password"); $SMTPClient.Send($SMTPMessage)
Save this file to the desktop as "SendPic.ps1".
Make the appropriate changes to; you, userID and password. Usually your userID is the same as your complete Gmail address.
If you are not using Gmail, then you must root around the Internet to find the port associated with your Smtp server and change the "587" to the appropriate port e.g. smtp.mail.yahoo.com and the port is 465.
Instead of using Notepad, the PowerShell editor is available.
Now for the big one - right click on the SendPic PowerShell file and select "Run with PowerShell". If it works you will receive and email in a few seconds. If it doesn't, then you need to start debugging.
Create a new PowerShell file on your desktop named test1 that sends an email without an attachment:
$EmailTo = "email@example.com" $EmailFrom = "firstname.lastname@example.org" $Subject = "test" $Body = "x" $SMTPServer = "smtp.gmail.com" $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body) $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $SMTPClient.EnableSsl = $True $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("userID", "password"); $SMTPClient.Send($SMTPMessage)
Open the file with the PowerShell editor by right clicking on file and selecting "Edit". By running from the editor, we can read the error messages that flash by when you double click on the file.
cd .\desktop .\test1.ps1
If this fails, it is probably due to a directory issue. Ensure that PowerShell is pointed to the desktop directory. The command prompt will look something like this:
If it doesn't, then you will have to remember all of your old DOS commands; cd, dir, .\, etc to get PowerShell pointed to the desktop. Try it again, if it does not work, read the error message to assist in discovering the problem
Step 3: Write and Debug a Batch File
Open Notepad and copy the following text:
PING localhost -n 180 >NUL powershell.exe .\SendPic.ps1 shutdown -s -t 100
The PING statement is a real hack that sends out a communication request every second 180 times. The delay should be long enough for the computer to boot up, establish Internet connectivity and snap a webcam photo. Newer versions of Windows support TIMEOUT - much cleaner.
The next statement executes the PowerShell file which sends the email with photo attached.
The final statement causes the computer to power off after a 100 second delay. This time is chosen as sufficient to send the email.
Save the file to your desktop as SendPic.bat (not .TXT as will happen if you don't add the extension to the filename. Drag and drop the file into the Windows STARTUP folder.
Double click on the batch file icon. You should receive the email. Whoa! Too easy. Yeah, well, a lot can go wrong.
The problem with debugging both this .bat file and the previous .ps1 file is that the errors flash by far too quickly to read. You can slow it down with:
PAUSE PING localhost -n 180 >NUL PAUSE powershell.exe .\SendPic.ps1 PAUSE shutdown -s -t 100 PAUSE
PAUSE will wait for you to press the ENTER key. Read the error message. Again, it is probably a directory issue. After you have fixed the problem, remove the PAUSE statements, otherwise the program will hang.
If you want to terminate the batch file while it is executing, click in the open cmd window and enter ^C (Ctrl C).
Step 4: Wrapping It Up
Old desktops running Windows are not renowned for their reliability. What is the first thing you do when your computer packs up? Turn off the power! So that is what we are going to do. Find an AC Timer Switch at Walmart or Amazon. The cheap ones are mechanical (less than $10), the more expensive ones are electronic (more than $20). Program the switch to turn on at, for example, 8 AM and turn off 15 minutes later. Plug your computer into the timer outlet.
When the timer switch sends power to your computer the following sequence of events is initiated:
- BIOS detects AC power, computer boots up
- Windows starts (without password signon)
- Webcam program starts and snaps picture
- Batch file execution starts
- Delay for completion of boot process, picture capture and Internet signon
- Execute PowerShell file to send email with attachment
- Delay for completion of email
- Shutdown computer
The timer switch will then remove power from the computer. This is key for reliable unattended operation. I have survived intermittent hard disk failures, program hangups and other computer stoppers but when the system fails, it comes back after a power cycle.
This approach is easily adaptable to security monitoring, for example, use iSpy to snag a short video clip when motion is detected and send an email with a video attachment. In fact, the concept could be extended to send an email alert upon the detection of any event and with the addition on an Arduino and the physical sensors available in that universe - the sky's the limit!