RUNNING .NET APPLICATIONS ON A RASPBERRY PI THAT COMMUNICATES WITH AZURE

  • This post exists to help with an MSDN Magazine article that I am authoring
  • It provides some of the low-level details for the article
    • How to install Mono and root certificates on a raspberry pi
    • How to create an Azure mobile service
    • How to create a Custom API inside Azure mobile services that the raspberry pi can call into
    • How to create an Azure storage account
SmartDoor Client on Github - Mono-based C# Raspberry PI Client https://github.com/sedouard/SmartDoorDemo
Raspberry Pi - Basic hardware setup, Plugging in camera, Connecting GPIO breakout, Closing up case https://stevenedouard.com/wiring-raspberry-pi-send-photos-cloud/
Running code on your Raspberry Pi to Send Photos the Cloud https://stevenedouard.com/running-code-raspberry-pi-send-photos-cloud/
How to upload Node Packages to Azure Mobile Services https://blogs.msdn.com/b/brunoterkaly/archive/2014/06/18/how-to-upload-node-packages-to-azure-mobile-services.aspx#
How To Provision A Shared Access Signature That Allows Clients To Upload Files To To Azure Storage Using Node.js Inside Of Azure Mobile Services https://blogs.msdn.com/b/brunoterkaly/archive/2014/06/13/how-to-provision-a-shared-access-signatures-that-allows-clients-to-upload-files-to-to-azure-storage-using-node-js-inside-of-azure-mobile-services.aspx#
Running .net Applications On A Raspberry Pi That Communicates With Azure https://blogs.msdn.com/b/brunoterkaly/archive/2014/06/11/mono-how-to-install-on-a-raspberry-pi.aspx#
Using Fiddler and Advanced Rest Client to test Azure Storage and Azure Mobile Services https://blogs.msdn.com/b/brunoterkaly/archive/2014/06/18/using-fiddler-and-advanced-rest-client-to-test-azure-storage-and-azure-mobile-services.aspx

MONO - HOW TO INSTALL ON A RASPBERRY PI

  • Why Mono?
  • How to install Mono on a raspberry pi
  • Installing trusted root certificates on to the raspberry pi
  • https://www.mono-project.com/Main_Page
  • An open source, cross-platform, implementation of C# and the CLR that is binary compatible with Microsoft.NET
  • Mono is a free and open source project led by Xamarin (formerly by Novell) that provides a .NET Framework-compatible set of tools including, among others, a C# compiler and a Common Language Runtime

WHY MONO?

  • Because it lets us write .net code compiled on Windows
  • We can simply copy the binary files from Windows to Linux and run it as is
  • From a raspberry pi device, it is possible to use a .net application to take a photo and upload it to Windows Azure storage

HOW TO INSTALL ON A RASPBERRY PI RUNNING LINUX

You will issue the following commands:

pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get install mono-complete

The first command makes sure all the local package index are up to date with the changes made in repositories. Second command installs the complete Mono tooling and runtime.

MAKING SURE THAT YOUR MONO APPLICATIONS CAN MAKE A HTTPS REST-BASED CALLS

This command downloads the trusted root certificates from the Mozilla LXR web site into the Mono certificate store. Once complete, the Raspberry PI will be capable of making web requests using HTTPS requests within Mono.

pi@raspberrypi ~ $ mozroots --import --ask-remove --machine

CREATING A NEW AZURE MOBILE SERVICES ACCOUNT

  • The mobile services account is needed to host a Node.js application that provides shared access signatures to raspberry pi devices
  • The shared access signature is needed by the raspberry pi, so that it can directly and securely upload photos to Azure storage

STEPS TO CREATE AN AZURE MOBILE SERVICE

  • The steps below will create an Azure mobile service
    • The service will be used to host a Node.js application interacting with a raspberry pi devices
    • We will provision a SQL database, although it will not be used initially

FOLLOW THESE STEPS TO CREATE THE MOBILE SERVICE

  • Login into the Azure Portal
  • Select MOBILE SERVICES from the left menu pane at the Azure Portal.
  • In the lower left corner select "+NEW" to create a new Azure Mobile Service.
  • Make sure you've selected, "COMPUTE / MOBILE SERVICE / CREATE."
  • You will now enter a url. We will call this service raspberrymobileservice.
  • For the DATABASE, we will choose "Create a new SQL database instance."
  • The REGION we chose is "West US."
  • The BACKEND is "JavaScript."
  • Click the "->" arrow to proceed to the next screen.
  • In this screen you will "Specify database settings."
  • The NAME of your database will based on the URL you entered previously.
  • In this case, the database is called "raspberrymobileservice_db."
  • You will need to choose a SERVER. We will choose "New SQL database server" from the drop-down list.
  • You will need to provide a SERVER LOGIN NAME and a SERVER LOGIN PASSWORD.
  • Take note of the login you provided as it will be needed later

CREATING A CUSTOM API

  • Azure mobile services allows you to create a custom API written in JavaScript that can be called from a raspberry pi device using REST
  • This custom API is really just a Node.js application running in the server

CREATING THE API TO RESPOND TO THE DEVICE TRYING TO UPLOAD PHOTOS

  • Now that the service is established, we will turn our attention to creating an API that the device can call into to upload a photo.

  • Login into the Azure Portal

  • Your mobile service will take a few minutes to complete, and you should see the "Ready" flag as the "Status" for your service.

  • Once it is ready you can drill into your service to customize its behavior. Just to the right of the service name, click the right arrow key "->" to drill into the service details.

    • The top menu bar will offer many options, but we are interested in the one titled "API."
    • The API allows you to create a series of node.JS API calls that a device can call into using rest-based approaches.
  • Click on "API." from there, select "CREATE A CUSTOM API." You will be asked to provide an API name. Type in "photos" for the API name.

  • Below you will see a series of drop-down combo boxes that relate to permission.

  • We will keep the default value of "Anybody with the application key."

  • Click the checkmark to complete the process.

  • The name of the AP you just created, "Photos," should be visible on the portal interface.

  • To drill into the photos API click on the right arrow key "->". The right arrow key will be just to the right of the name of the API "Photos".

  • At this point you should see a basic script that has been provided by default. We will overwrite this default script with our own script as described in the MSDN Magazine article.

CREATING A STORAGE ACCOUNT TO STORE THE PHOTOS

  • Navigate to the portal and create a storage account
  • Create a container for the photos
  • Obtain the:
    • Storage Account Name (you will provide a name)
    • Storage Account Access key (generated for you)
    • Container Name (you will create)

CREATING A STORAGE ACCOUNT

  • We will need a storage account so that we can upload photos to it.

  • The steps are well documented here: https://azure.microsoft.com/en-us/documentation/articles/storage-create-storage-account/

  • In our case we call the storage account raspberrystorage.

  • As you complete these steps make sure that you choose the storage account location to be the same location as was used for your mobile services account.

  • This avoids any unnecessary latency or bandwidth costs between data centers.

  • Once the storage account is created, we will need to create a container within it.

  • Photos or any blob for that matter, are always stored within a container. To create a container drill into your newly created storage account and select CONTAINERS from the top menu. From there, select CREATE A CONTAINER. The new container dialog box will ask for a name for your container. Take note of the name you provide. We are calling our container ?photocontainer.?

image001

  • When the raspberry pi device uploads photos to the storage account, it will target a specific container, such as the one we just created. You will next be asked to indicate ACCESS rights. To keep things simple we will select access rights of Public Blob.

ENTERING APP SETTINGS

  • Rather than hard-code storage account information inside your JavaScript/Node.js applications, you should consider using apps settings inside of the Azure mobile services portal
  • This post also discusses it well:
  • Navigate to the Azure Mobile Services section of the portal. Drill into the specific service by hitting the arrow below

image002

  • Select from the Configure Menu at the top

image003

  • Scroll down to the very bottom to see app settings

image004

  • Note that we need to enter: - We need to get this from Azure Storage - PhotoContainerName - AccountName - AccountKey
    • We get this information from the Azure Storage Section of the Portal. Note that you need to have provisioned a Storage Account to have this information.
    • How to get the AccountKey with Azure Storage Services

image005

  • Now you can get the access keys

image006

HOW NODE.JS WILL ACCESS THE APP SETTINGS

  • You will create a Node.js application inside of Azure Mobile Services

    • See previous steps

THE NODE.JS APPLICATION READING APP SETTINGS

  • You will starting by going back to Azure Mobile Services and drill down into your newly minted service
    • We called ours raspberrymobileservice

image007

  • Once you click API, you should see:

    image008

  • Notice the app settings are being read on lines 12 to 14.