Geo-fence Garage Door Using Raspberry Pi, Azure, C#, and Windows Phone
Part Four

We can control the garage door from our Raspberry Pi, via relay module and a modified garage door opener.
In this part, we will create a Mobile Service and azure table, where we will keep information about the garage door. You will need Microsoft Azure subscription, you can try Azure for one month free Trial.

Go to your Azure Portal and create new Mobile Service. Open the Mobile Service you created, select the windows tab and expand the Connect an Existing Windows section.
Mobile Service Configuration

Scroll down to the code sample and copy the first section.
Mobile Service Connection

Select the Data tab and click Add Table.
Mobile Service Data

Type GarageDoor for table name and click OK.
Mobile Service Table

Go to Visual Studio and open the GaragePi.Net project that was created in part two, right click on the project and click Manage NuGet. Search for WindowsAzure.MobileServices, select it and click install.
Mobile Service SDK NuGet

Close NuGet manager after installation completes. Open the Program class and add the using statement below.
using Microsoft.WindowsAzure.MobileServices;

Paste the code you copied from Azure inside the Program class.
Mobile Service Connection in VS

You will now create a class that will represent the garage door. Right click on the project, click Add Class, and name the new class GarageDoor (the class name and the Azure table name must match). Make the class public and add the below properties. You may add other properties, if you wish.
public string Id { get; set; }
public string Name { get; set; }
public bool StateChanged { get; set; }

Garage Door Class

Add the method below to the Program class. This method will first try to get garage door from Azure Table. If garage door doesn’t exit, it will create new one, and insert it into the Azure Table.
 static async Task GetGarageDoor()
var garageDoor = await MobileService.GetTable().Where(g => g.Name == "Home Garage").ToListAsync();
if (garageDoor == null || garageDoor.Count < 1)
//Create Garage Door
GarageDoor newGarageDoor = new GarageDoor() {
Name = "Home Garage",
StateChanged = true

await MobileService.GetTable().InsertAsync(newGarageDoor);
garageDoor = new List();
return garageDoor.First();

Add another method that will update the Azure table, after opening or closing the garage door.
public async static Task UpdateGarageDoor(GarageDoor door)
door.StateChanged = false;
await MobileService.GetTable().UpdateAsync(door);

Remove or comment out all the code inside the Main method.
Setup Main Method

In the main method add the code below. This will call the GetGrageDoor method and wait for the result.
GarageDoor garageDoor;

garageDoor = GetGarageDoor().Result;

Go to the GetGarageDoor method and put a breakpoint at the if statement. Start debugging.
Breakpoint One

We will get no records from Azure. Place another breakpoint on the return line and click Continue.
At Breakpoint One

A new record will be created and returned. Click Continue.
At Breakpoint Two

The program will end. Go to your Azure portal, open your Mobile Service, select the data tab, and open the GarageDoor table. There will be one record in your table.
New Azure Record

Go back to Visual Studio. Replace all the code in the Main method, with the code below. This will call Azure every three second, which is bad idea. Service Bus or a web server on the Pi will be much better solution.
GarageDoor garageDoor;

//Setup the GPIO pin direction
LibGpio.Gpio.SetupChannel(BroadcomPinNumber.TwentyThree, Direction.Output);

while (true)
//Get garage door from Azure.
Console.WriteLine("Getting garage door status from Azure.");
garageDoor = GetGarageDoor().Result;

//Check if the status of the garage door changed.
if (garageDoor.StateChanged)
Console.WriteLine("Opening/Closing the garage door.");

//Set GPIO pin 23 to high.
LibGpio.Gpio.OutputValue(BroadcomPinNumber.TwentyThree, true);
//wait for half a second
//Set GPIO pin 23 back to low.
LibGpio.Gpio.OutputValue(BroadcomPinNumber.TwentyThree, false);

Console.WriteLine("Updating the garage door status");
//Update the garage door record

Console.WriteLine("Updated the garage door status");

//wait for 3 seconds before you check again.

Open the App.config file, locate the runtime section, and in the assemblyBinding .. section add the xml fragment below. We are doing this because right now, the newest version of System.Net.Http can’t be loaded on the Raspberry Pi.
  <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="" newVersion="" />

App Config. File
Build the project and make sure it builds successfully.
Power up the Raspberry Pi and wait a minute for the Pi to get started. Start PuTTY and WinSCP and login to both of them. On the WinSCP, locate the debug file of your project and drag it to the pi folder of the Raspberry Pi.
Copy Program

Warning! Your garage door is about to open or close. Go to PuTTY, run the GragePi.NET.exe program and go to where you can see your garage door, or keep your eyes on the relay module.
Run Program

The program will Open/Close the garage door and the red LED of the relay will turn on then off.
Running Program
The garage door can now be controlled from outside of your home network.
Go to Part Five