In this project we will be building a solution using Microsoft Windows 10 IoT running on a Raspberry Pi 2 B with a USB Webcam attached that will capture an image every 15 minutes and then upload the image to a Microsoft Azure account.
The solution will be built using Microsoft Visual Studio and written in Microsoft Visual C#.
This project will require the following:
Raspberry Pi 2 B
Network cable (Or Raspberry Pi USB Wi-Fi dongle.)
Microsoft Azure account
The Raspberry Pi 2 B will require an installation of Microsoft’s Windows 10 IOT Core.
The USB Webcam will need to be connected to a USB port on the Raspberry Pi.
The Raspberry Pi will also require an internet connection for this demo.
The internet connection can be configured by using either a network cable connected from the network jack on the Raspberry Pi to your internet router OR by using the Raspberry Pi USB Wi-Fi dongle.
A Microsoft Azure account is required for this demo.
You can create a Microsoft Azure account here: http://azure.microsoft.com
You should be able to find a link for a Free Trial on the main page.
Once you set up your Microsoft Azure account you will need to create a Storage Account.
At this point you will want to make note of your Storage Account’s name.
Once your Azure Storage Account has been created, you will need to create a Container in your Storage Account.
At this point you will want to make note of your Storage Account’s Container’s name.
While you are in your Azure Storage Account you will want to click on the link to Manage Access Keys.
On the Manage Access Keys pop up, make a note of the Storage Accounts Primary Access Key.
On this screen you can also confirm the Storage Account Name that you noted earlier during this process.
Finally we get to the CODE
Using Microsoft Visual Studio create a new Project.
In the Microsoft Visual Studio main menu navigate to the following: File > New > Project
In the New Project window we want to select the following: Templates > Visual C# > Windows > Universal > Blank App
This will load a blank Windows Universal application project for Visual C#.
Once the project loads the next thing we are going to do is add in support for using Microsoft Azure Storage.
In Microsoft Visual Studio, on the main menu, we are going to navigate to the following: Tools > NuGet Package Manager > Manage NuGet Packages for Solution…
In the search box, search for Azure
Find WindowsAzure.Storage and select the option to install the latest stable build.
After a few minutes WindowsAzure.Storage support will be added to the Solution. During this process you may be prompted to acknowledge a Licensing Agreement.
Once the NUGET package is installed we are ready to code.
In Microsoft Visual Studio view the Solution Explorer.
Expand MainPage.xaml and double click on MainPage.xaml.cs
All of the code will be in the MainPage.xaml code behind file or Visual c# file.
First thing we will do is add a couple using statements to help make the code a little friendlier. Add the following using statements:
Next, inside the MainPage class we are going to add the following fields:
private Timer imageCaptureTimer;
private TimeSpan imageCaptureTimerInterval = new TimeSpan(0, 15, 0); // 15 Minutes.
private string azureStorageAccount = "iotusbcameraazurestorageaccount";
private string azureStoragePrimaryKey = "KXlM0LsABAjsi9aqQcxN9nOYRTbphlrZ4DtUDbf/dGPdiz3ghpMslR9CZOS6MPwX5zEXE9uenBzfsnf/fkL69w==";
private string azureStorageContainer = "iotusbcamerastoragecontainer";
private MediaCapture mediaCapture;
The imageCaptureTimer and the imageCaptureTimerInterval fields will be used for our Timer object.
The three Azure related fields will be used for the Azure account. The values listed here are the values you noted earlier when setting up your Azure account with Storage services.
Next we are going to create an initialize method. We will use this method to initialize or setup our objects.
private async void initialize()
// Create a new instance of a MediaCapture object and Initialize it Asynchronously.
this.mediaCapture = new MediaCapture();
// Set the CaptureElement's Source.
this.previewElement.Source = this.mediaCapture;
// Call the StartPreviewAsync method of the MediaCapture.
In this method we Create a new instance of the MediaCapture object and Initialize it.
We set the Source of the CaptureElement object.
We then start the preview process.
Next we are going to create a function to build a DateTime Stamp in exactly the format that I want.
private string buildDateTimeStamp()
// Create a StringBuilder to hold the time stamp value.
StringBuilder sb = new StringBuilder();
// Store the current DateTime.
DateTime currentDate = DateTime.Now;
// Add the Year to the StringBuilder.
// Add the Month to the StringBuilder.
if (currentDate.Month.ToString().Length == 1)
sb.Append("0" + currentDate.Month.ToString());
// Add the Day to the StringBuilder.
if (currentDate.Day.ToString().Length == 1)
sb.Append("0" + currentDate.Day.ToString());
// Add the Hour to the StringBuilder.
if (currentDate.Hour.ToString().Length == 1)
sb.Append("0" + currentDate.Hour.ToString());
// Add the Minute to the StringBuilder.
if (currentDate.Minute.ToString().Length == 1)
sb.Append("0" + currentDate.Minute.ToString());
// Add the Second to the StringBuilder.
if (currentDate.Second.ToString().Length == 1)
sb.Append("0" + currentDate.Second.ToString());
// Return the value.
This function should be pretty self-explanatory. We are creating a StringBuilder object and then building it out using the DateTime object.
Next we are going to create the method that will be executed every time the Timer is executed.
private async void imageCaptureTimerExecuted(object sender)
// Photo StorageFile.
// Photo file name.
string photoFileName = this.buildDateTimeStamp() + ".jpg";
// Create the PhotoFile in the PicturesLibrary System folder.
photoFile = await KnownFolders.PicturesLibrary.CreateFileAsync(photoFileName, CreationCollisionOption.ReplaceExisting);
// Create an ImageEncodingProperties object for the photo file.
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
// Using the MediaCapture, capture the photo and save it to the PicturesLibrary System folder.
await this.mediaCapture.CapturePhotoToStorageFileAsync(imageProperties, photoFile);
// Create an Azure CloudStorageAccount object.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=" + this.azureStorageAccount + ";AccountKey=" + this.azureStoragePrimaryKey);
// Create a CloudBlobClient object using the CloudStorageAccount.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Create a CloudBlobContain object using the CloudBlobClient.
CloudBlobContainer container = blobClient.GetContainerReference(this.azureStorageContainer);
// Create a CloudBlockBlob using the CloudBlobContainer.
CloudBlockBlob blockBlob = container.GetBlockBlobReference(photoFileName);
// Using the CloudBlockBlob, Upload the file to Azure.
This method is really where all the magic happens.
We are going to create a StorageFile and save it to the local PictureLibrary.
Using the MediaCapture object we are going to call the CapturePhotoToStorageFileAsync method. This captures and saves the photo.
Finally, we are then going to use the Azure elements from the NuGet package we installed in to our project at the beginning to grab the photo file and upload it to Microsoft Azure.
Finally, we are going to edit the Constructor for the MainPage.
// Call the initialize method.
// Configure and Start the Timer.
this.imageCaptureTimer = new Timer(this.imageCaptureTimerExecuted, new object(), this.imageCaptureTimerInterval, this.imageCaptureTimerInterval);
In the constructor for the MainPage we are going to call the initialize method to setup our image capturing and we are also going to setup and start our Timer object.
That is about it for the code.
You should be able to successfully build the project and deploy the code to your Raspberry Pi 2 Model B.
If you have any questions, feel free to contact me. Always happy to help!