Use ASP.NET to control the GPIO pins on Raspberry Pi
By using the GPIO pins on Raspberry Pi with ASP.NET, it’s possible to measure temperature, humidity, turn LEDs on or off, control robots and much, much more. In this blog post I am going to start with just the basics, how to turn two LEDs on or off by using a simple web page and a REST API running on ASP.NET 5.
First of all, we need some hardware:
-
Raspberry Pi 2 Model B (should work with the older ones, but this is much faster)
-
Two LEDs (I use red ones, but you could use white, green, blue, purple, UV or whatever you have got with two legs (anode and cathode))
-
Two 220 Ohm resistors (red-red-brown)
-
Breadbord
-
Five wires (without a breakout board for your Raspberry Pi, I recommend using male-female wires. You could also connect the cathode directly to ground, which in case allows you to use just three wires)
-
A breakout board (not necessary, but makes it much easier! I am not using one here though)
First of all we need to set everything up. To install ASP.NET 5 on Raspbian, you can follow the steps in Scott Hanselmans blog post.
The next step is to connect the hardware. Turn you Raspberry Pi off when connecting the wires, by removing the micro USB-cable! We are going to use Gnd, Pin 23 and Pin 24 from the raspberry. If you are not using a breakout board with labels for the pins, you can find them here (physical pin 16 & 18):
http://pi.gadgetoid.com/pinout
You can also see the pins here:
When connecting the wires, resistors and LEDs, it should look like something like this:
Make sure you connect the anode and cathode correctly!
When everything is connected like this, we can make sure everything is working by manually turning one of the LEDs on. To do this we will use SSH to connect to the Raspberry Pi and start with this command:
cd /sys/class/gpio
If we then use “ls” to list all files we will get export, gpiochip0 and unexport. The first thing we need to do is to export one of the GPIO pins:
echo 23 > export
Running “ls” again should now list gpio23 as one of the “files“.
We have now activated that GPIO pin and are able to control it (set the value to HIGH or LOW, depending on if we want voltage to run through the pin or not. Default is LOW).
Since GPIO pins are Input/Output we first need to set the direction of the pin:
echo out > gpio23/direction
And now we can turn the LED on by sending 1 to the value:
echo 1 > gpio23/value
It should look like this now:
If your LED is not turned on, check your wiring again.
Control the LEDs using ASP.NET
Turning the LEDs on using the command line is probably just fine sometimes, but since we have installed ASP.NET on the Raspberry Pi, we can create a web site where the user can click on buttons to turn them on or off.
To get started the best way is to use one of the libraries available on the web. I have been using Raspberry.IO.GeneralPurpose, which makes it possible to just get started, without having to manually write and read from /sys/class/gpio. You can find the Nuget package here:
https://www.nuget.org/packages/Raspberry.IO.GeneralPurpose/2.2.0.1
To get the code I have used with this library, check the Github page:
https://github.com/MikaelSoderstrom/rpi-aspnet-leds
To clone the package on Raspberry Pi, run:
git clone https://github.com/MikaelSoderstrom/rpi-aspnet-leds.git
Since we need to be running as sudo to be able to modify the GPIO pins, you should run this command before starting the web site:
sudo –s
Then you can just run this inside of the project file, where the project.json is located:
k kestrel
And now we are done! Just navigate to http://ip.used.by.raspberry.pi:4000 and turn the LEDs on. :)
This is how it should look like: