This tutorial is demonstrating one solution to serve data read from TinkerForge sensors via a REST web service. It is implemented utilizing Python on a Fedora Unix (for ARM) running on a Cubietruck. It is not handling the basics of connecting to TinkerForge hardware, as this is covered quite well on their homepage.
REST is an acronym for “Representational State Transfer” and stands for a programmers paradigm mainly used in web applications. The main idea behind REST states, that one URL should present exactly one type of site content (independent where this content was generated from; e.g. a search on database). This behavior for static content (such as permalinks) is written down in the HTTP Internet standard. REST is based on five principles:
- Client – Server separation; server and client may be implemented independently, even in varying programming languages, when the interface between them is well-matched.
- Cacheable – All responses must be flagged as cacheable or not; when using cache headers in the responses with cache expired times this helps improving performance and scalability, as the client can respect this for further connections to the service.
- Stateless Transfer – this means that each individual request from a client to the service holds all necessary information to establish the connection (e.g. user data for authentication)
- Layered System – for a client it is not clear whether it is already connected to the end server or everywhere in-between (for instance: a load balancing system). This is also connected with the REST caching.
- Uniform interface – this means a decoupling of the architecture. An individual resource is identified in a request for instance using URIs. And the resources are conceptual separated from the representation that is send to the client. This means, a REST service may connect to a database based on a query that is invoked by the client connection, but will return not the database query back, but rather XML or JSON. For the client it is not visible how exactly the service is “working” in the background.
It is important to note, that REST itself is not a standard, like SOAP-based web services are. REST is meant to be an architectural style, whereas SOAP is a protocol. But REST is resting upon common Internet standards, like URI, HTTP or XML.
REST HTTP methods
|URL (or resource)||GET||PUT||POST||DELETE|
|List all URIs and maybe more details of collection members||Replace the collection by another one||Create a new collection member. The service will return the URI of the new member.||Delete the full collection|
|Element URI, such as
|Retrieve a representation of this member including the data connected with the member in a format like JSON or XML.||Create a new member or when it exists already, replace it.||Most likely not used / implemented.||Delete the specific member in the collection|
After some theory, let´s get started with the implementation. I assume, that you already installed python and „pip”, as well as the whole Tinkerforge SDK on your system.
If not already, install them:
sudo yum install python-setuptools python-setuptools-devel python
To install the Tinkerforge SDK, please visit the webpage about their SDK for Python installation.
Let´s get started
There are multiple frameworks, like Django REST framework etc. available for Python, but I used Flask with mimerender as this combination seemed most flexible in their set-up for me. As just basic functionality regarding GET requests is needed for the implementation I decided against a complex framework like Django is.
To install Flask and mimerender, just call the following command from the command line:
sudo pip install Flask sudo pip install mimerender
After installation, create a Python file that will include the source code for setting up a.) a rest web service b.) the connection to the Tinkerforge hardware.