
I wanted to create a web api app that answered calls on my Raspberry Pi. The
first step I learned was how to install and configure .NET core 2.0 on the
Raspberry Pi.
Note – .NET Core only runs on the Raspberry Pi 2 or 3. It will not run on the
Raspberry Pi Zero or Zero W.
To get this working, I followed these great instructions from Jeremy Lindsay:
[Running a .NET Core 2 app on Raspbian Jessie, and deploying to the Pi with
Cake](https://jeremylindsayni.wordpress.com/2017/07/23/running-a-net-
core-2-app-on-raspbian-jessie-and-deploying-to-the-pi-with-cake/)
Once I had this template installed, I simply created a new .NET Core web api
app:
Note – Make sure you install the .NET core 2.0 SDK before attempting any of
this. .NET Downloads
dotnet new webapi -n WebApiApp
I added the following line of code to the Program.cs file. This will ensure
that the Pi answers to all host names (so I can access it from other computers
other than localhost).
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.UseUrls("<http://*:5000>") // add this line
.Build();
I then copied the following files and folders from the directory created in
the sample from the article above to my new WebApiApp folder:
- build.cake
- tools
- publish
I opened up build.cake and changed the correct app name to the name of the
project. Here are my configuration settings:
///////////////////////////////////////////////////////////////////////
// ARGUMENTS (WITH DEFAULT PARAMETERS FOR LINUX (Ubuntu 16.04, Raspbian Jessie, etc)
///////////////////////////////////////////////////////////////////////
var runtime = Argument("runtime", "linux-arm");
var destinationIp = Argument("destinationPi", "192.168.1.113");
var destinationDirectory = Argument("destinationDirectory", @"/home/pi/DotNetConsoleApps/WebApiApp");
var username = Argument("username", "pi");
var sessionname = Argument("sessionname", "Raspberry Pi");
var executableName = Argument("executableName", "WebApiApp");
I ensured that the /home/pi/DotNetConsoleApps/WebApiApp directory existed on
the Raspberry Pi and then ran the build script from the Powershell terminal:
../build
This used cake to clean, build, and deploy my web api app onto my Raspberry
Pi.
Once this was complete, I switched to my PuTTY terminal and typed in
./WebApiApp to start the Web Api app. It started listening on port 5000. I was
able to access the Web Api from a browser on my PC by using the IP address of
the Raspberry Pi:
http://192.168.1.113:5000/api/values
This displayed the default project api controller output of the template app
created in the earlier step.
I can’t believe how easy this was. .NET core is really improving the
development workflow immensely over previous versions of .NET. This was so
much better than what came before.
Update: I was able to get an LED wired up to the WebApi by using this great
article from Carlos Mendible:
Note – When you use the Gpio library, you will need to start your WbApiApp
using sudo ./WebApiApp due to the need to access IO pins
[Toggle Raspberry Pi GPIO Pins with ASP.NET Core
2.0](https://carlos.mendible.com/2017/09/01/toggle-raspberry-pi-gpio-pins-
with-asp-net-core-20/)
I wired the LED to physical pin 11 on the Pi. Here’s my controller (not much
different from his sample):
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Gpio;
namespace WebApi.Controllers
{
[Route("api/[controller]")]
public class GpioController : Controller
{
// GET api/values
[HttpGet]
public IEnumerable Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
[HttpGet("{value}")]
public string Get(bool value)
{
var pin = Pi.Gpio.Pin00;
pin.PinMode = GpioPinDriveMode.Output;
pin.Write(value);
return $"pin {pin.BcmPinNumber} set to {value}";
}
}
}
The
state of the LED after sending a true to the gpio api method.