Reputation: 63
I want to control the GPIO pins of my raspberry pi over a web interface. The usual solution is to run Linux with apache and start some python scripts over a web interface.
But I want to do this with a c# written solution running windows 10 iot. I found a little webserver (http://www.codeproject.com/Articles/1505/Create-your-own-Web-Server-using-C) but I have no idea how to fill a website with the current states of the GPIO pins or how to handle button clicks to toggle the GPIO pins
does anyone know in what kind it is solvable?
PS: I want to control some led strips and other lights over a relayboard.
Upvotes: 1
Views: 1845
Reputation: 226
The way Windows 10 IoT Core and Raspberry Pi works is that you have something in the middle that connects both parts. Like a bridge. Azure (Microsoft’s Cloud Platform) is perfectly suited for this.
In Azure you have a service called “Storage Queues” which is essentially a FIFO (First In First Out). So you put something on the queue from your website which is connected to Azure, and then your Raspberry Pi reads from the queue. If there is something on the queue; the Raspberry Pi could do something (like flash a LED through the GPIO ports by setting the voltage). This could basically trigger anything that goes on electricity, and not only a LED.
Start by creating a simple ASP.NET MVC Application from Visual Studio through File => New Project => Web.
Then go into your index.cshtml inside the Home-folder (which is the front-end and main landing page when you fire up your webpage).
Make a simple form with a button:
<form method="POST" action="/Home/ToggleLight">
<button>Toggle</button>
</form>
Then right-click on “References” in your solution and choose “Manage NuGet Packages”.
Search for “Azure Storage” and press install on “WindowsAzure.Storage”.
Log into Azure (portal.azure.com) and follow this guide on how to setup a Storage Account and create a queue through the Storage Explorer. https://github.com/msdevno/LED-to-Azure-Starter-Pack
Go into your HomeController.cs in your “Controllers” folder and create a CloudQueue function like this:
private static CloudQueue InitializeQueue()
{
var connectionstring = "YOUR OWN CONNECTION STRING GOES HERE==";
var cloudStorageAccount = CloudStorageAccount.Parse(connectionstring);
var queueClient = cloudStorageAccount.CreateCloudQueueClient();
var queue = queueClient.GetQueueReference("rpi2queue"); //Name of queue
try
{
queue.CreateIfNotExistsAsync();
}
catch (Exception ex)
{
Console.WriteLine("it didn't work! Here is the error: " + ex.Message);
}
return queue;
}
Also don’t forget to add the following using statements in the HomeController.cs-file.
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
Create a ToggleLight method like the following in the HomeController.cs-file:
[HttpPost]
public ActionResult ToggleLight()
{
var queue = InitializeQueue();
var message = new CloudQueueMessage("On,1000");
queue.AddMessage(message);
return RedirectToAction("Index");
}
Your website is now complete and can add messages in the queue on Azure on button-click.
What you need to do now is create the UWP App for the Raspberry Pi 2 IoT Core Device. The "QueueReader_RPI2"-folder contains a fully functional UWP-app which contains code on how to trigger the GPIO-ports by reading from the queue in Azure that you just created.
https://github.com/msdevno/LED-to-Azure-Starter-Pack/tree/master/Source/LED2Azure
And that is how you create a website connected to Azure which triggers the Raspberry Pi to make a LED light up.
EDIT: You could also take a look at the video for the GitHub-repo (In Norwegian, but you will catch the drift and just follow the code walk through if you struggle): https://channel9.msdn.com/Series/MSDEVNO/LED-to-Azure-Starter-Pack If you want to see how the Raspberry Pi 2 is connected, you could take a look at another video that goes through what parts you need and how it is connected (also in Norwegian): https://channel9.msdn.com/Series/MSDEVNO/IoT-og-Raspberry-Pi-2-Kickstart-Guide
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
Upvotes: 3