Little WebServer on Raspberry PI

Sometime is better have a little WebServer on Raspberry PI than an application. Let’s see how to build and run it with Windows IoT Core and Visual Studio 2015.

I started to develop an application as UWP app on RPi for the remote control of device in my house and suddenly I’ve understand that this kind of application is better to have it on the phone. But there are a lot of kind of mobile phones and tablets and PC and so on. Plus I needed a kind of service on RPi to answer the questions from different apps. But isn’t better to develop a simple Webserver and deploy directly in html5/css3 webpages on browsers? I think is an idea and I followed this. Honestly I was inspired by the default local webserver on RPi and I said: “why not?”.

The first thing is to build a BackgroundService for RPi under Windows IoT Core. To do that is useful the new template pack for Windows IoT Core as free download here. This installer will add in your Visual Studio Templates a new one

WebServer 1

obviously choose a name and a folder for your project and click on OK button.

At this point Visual Studio will create for you a new draft for your background process.

WebServer 2

As you can see, there is a “Main” routine called directly from the system at startup. His name is “Run” Here we’ll write our task for instanciate our webserver to answer.

Now we need a class that will do the hard work: we can add a new class named MyWebserver.

We can use it in this way:

3

And now we can run our new empty webserver. After a while, our project will be in run mode. But immediately it will stop. Why? Simply! We didn’t say to the system that this is a task. We can do that with the “taskIterface” parameter in our “Run” method.

We have to store it in our local variable of type BackgroundTaskDeferral, call the interface method of the parameter “GetDeferral” and store the result in our variable.

4

The deferral task returned, will be only to never loose the status of run. If we want to close the app, we can simply call the method “myDeferralTask.Complete()” and the application will automatically close.

Then, now we have a task that runs our webserver, but this webserver need to take the connections and return the answers. How to do that? We have just create the method “MainResponseWebserver” in our Webserver class that is called from the Run method. In this method we have to create the listener to the web.

The listener will be this: var listener = new StreamSocketListener(); and create it as privare variable in the method MainResponseWebserver.

The StreamSocketListener has an event, ConnectionReceived that is the right callback where we can manage our responses. Each call to our webserver must have a port and this can be linked to our listener calling await listener.BindServiceNameAsync(“5432”);

and now we are able to receive the externall calls. Would we try? Let’s start the service! And after a while… everything will stops… damn! What’s happened?

It’s simple. We forgot to enable the capabilities of our app. Enable the “Internet (Client & Server)” to enable the capability of server.

Now it runs.

And now try to return a real HTML page of answer… but in the next article!

Stay tuned!

 

 

P.S. Follow the next post:

The WebServer response on Raspberry PI: the querystring

The WebServer response on Raspberry PI: html/css

The WebServer response on Raspberry PI: jquery/js