The Programmator

Want to Connect your IoT device with no intermediate party costs? Here’s how!

Having devices connected to the internet is a really cool thing. But most of the time, this comes with a cost for an intermediate party that queries and delivers device status information. I was about to write an article for controlling a Windows 10 IoT installed Raspberry Pi when I got the notification that my Azure subscription reached its limit, and was therefor suspended. So I believe the time has come to avoid using huge intermediate parties, and get/push device state directly to the device. As usual, the code for this sample is available on GitHub.

How to access a Raspberry Pi over the internet

If we want to directly access our Raspberry Pi over the internet, it will have to listen to incoming messages and be able to send responses back to whoever is calling. For this, we’ll be using the very promising Restup library by Tom Kuijsten. It’s currently still in beta, so you might find that some error handling is not yet in there, but overall, it does the job very nicely.

Thanks to the library, setting up a REST API on a Windows Universal Application is peanuts. The code in the following gist shows just that.

What we see in the code here, is a Web API controller-like class structure defining the type of controller (Singleton or instance Per Call), and one method which will return all available GPIO pins on our Raspberry Pi. Note that your pins may vary based on which Pi version you’re working with. Next, we initialize a HttpServer class listening on port 8800, and register the Controller.

And that’s it! We debug/deploy the application code to our Raspberry Pi and voila, the response is returned!

Reponse of a REST call on a windows universal app

Reponse of a REST call on a windows universal app

Now that we have this very basic sample running, a numerous amount of exciting possibilities comes to mind! Controlling GPIO pins is one thing, but we could also return device state, have specific calls perform actions over an I²C channel to an Arduino, and so forth.

Change GPIO pin state over the internet

Let’s use the above REST API to toggle an LED connected to a GPIO pin. I’ve extended the GpioController above to include calls to get and update (HTTP Put verb) GPIO pin state (HIGH / LOW).

Two simple methods that will update or retrieve pin state. When calling http://[PiAddress]:8800/api/gpio/[LedPin]/value/true, we see our LED is now powered on!

Access Raspberry Pi over internet using REST API

Where do we go from here?

Having this simple yet effective way of communicating with our Raspberry Pi opens up a whole range of exciting possibilities. Yet, there are still a few problems we will need to solve when trying to access the API from outside the home-network. We’ll try to solve that in a next post! Meanwhile, if you have any ideas on how to access the Pi from outside the home-network, or you have any suggestions regarding this sample, feel free to join in on the conversation below!

Like what you read? Maybe your friends do too! Share this article:

Leave a Reply

Tags: , ,