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

In part one we setup our Raspberry Pi, installed Mono, and ran our first C# program.
In part two, we will connect a Relay to the Pi and turn it on/off using .NET. Listed below are what you will need to complete part two, in addition to the hardware/software you used in part one.

Hardware :Software :
  • PiSharp Raspberry Pi GPIO Library for .NET

First, we will make sure the Pi is turned off and the power cord is disconnected. You can shut down the Pi by running the command below.
sudo shutdown now

Wait for the green LED of the Pi to stop blinking (the red LED will stay on) and then unplug the power.
We are now ready to connect jumper wires to the Raspberry Pi. Identify the GPIO pins we will use for this experiment, locate the 5V pin, the ground (you can select any ground pin, I am using the one closest to the 5V pin), and pin 23 (I am using pin 23 for signal, you are free to select another pin).
Raspberry PinOut

Connect the jumper wires to the Raspberry Pi GPIO pins as shown on the image below. Don’t worry about the color of the wires.
Jump wires to Pi

Go ahead and connect the jumper wires from the Raspberry Pi to the breadboard. The 5V wire will go to the red row (+), the ground wire will go to blue row (-), and the pin 23 wire can go to any row except the red or the blue rows. I am using row f.
Pi to Breadboard

Get the Relay and identify the pins.
Relay Pins

Connect another 3 jumper wires to the Relay.
Jumper Wires to Relay

Connect the Relay jumper wires to the breadboard. The wire from the ground pin will go to the (-) pin of the Relay, the wire from the 5V pin will go to the (+) pin of the Relay, and the wire from pin 23 will go to the Signal pin of the Relay.
All Connected

You will now download Pi#, PiSharp is a Raspberry Pi GPIO Library for .NET, download it from here
After you download Pi# unzip it, open the Visual Studio solution located in the src folder, and build the solution. After build succeeds right click on the PiSharp.LibGpio project, open Folder in File Explorer. Browse to the bin/Debug folder, locate the PiSharp.LibGpio.dll file, and copy it. You will add this .dll to your project.
Start new instance of Visual Studio and create new Console project. I am naming my project GaragePi.NET.
Visual Studio Project

Create new folder, name it PiSharpRef, and paste the PiSharp.LibGpio.dll you copied from the PiSharp solution.
Pi Sharp DLL

Right click on References and add the PiSharp.LibGpio.dll file. Be sure to use the file located in the PiSharpRef folder.
Add Reference

Add the below using statements to the Program class.
using PiSharp.LibGpio;
using PiSharp.LibGpio.Entities;

Add the below code to the main method and build the project.
The first line will set the pin 23 to Output. Inside the while loop, we will set pin 23 to high (on) wait for 2 seconds, set pin 23 to low (off), and wait another two seconds. This will be repeated until we exit the program.
LibGpio.Gpio.SetupChannel(BroadcomPinNumber.TwentyThree, Direction.Output);
while (true)
LibGpio.Gpio.OutputValue(BroadcomPinNumber.TwentyThree, true);


LibGpio.Gpio.OutputValue(BroadcomPinNumber.TwentyThree, false);


We will now go to the Pi and test our experiment. Connect the power cord to the Pi and wait for the Pi to start. After the Pi is up and running, start PuTTY and login to your Pi.
Login to Pi

Start WinSCP and login to the Pi. From your local machine locate the debug folder of the GaragePi.NET project and drag it to the pi folder of the Raspberry Pi.
Copy Files

Go back to the Pi and run the GaragePi.NET.exe program.
sudo mono Debug/GaragePi.NET.exe

Running Program

The Relay will now make clicking sound and the red LED will turn on/off. Congratulations, you can now turn appliances on/off using Raspberry Pi and your .NET skills.
To quit the program press Ctrl + C.

Go to Part Three