Wednesday, March 30, 2016

Measuring water temperature in food-safe applications with Raspberry PI + PT100 sensor + Tinkerforge PTC Bricklet

I was looking for a temperature sensor (thermometer) that can be used with a Raspberry PI for measuring water temperature in food-safe (lebensmittelecht) applications.

The sensor should satisfy the following requirements:
  • food-safe
  • waterproof
  • heat resistant until at least 100°C
Since I couldn't find any out-of-the-box solution for the Raspberry PI, I decided to look for a separate temperature sensor and then trying to connect it with the Raspberry PI. Therefore, I found the PT100 temperature sensor, which pretty much satisfies all the given requirements.

The problem is, that a PT100 sensor cannot just be connected with a Raspberry PI, because you need an analog-digital converter. Lucky as I am, the company Tinkerforge offers some very nice modules to improve this situation. To connect a PT100 temperature sensor, you can use the Tinkerforge PTC Bricklet which gives you the current temperatur in Celsius as an integer number. Its getting even better: Tinkerforge offers an easy to use API for the "most popular" programming languages - including Python.

To connect the Tinkerforge PTC Bricklet with the Raspberry PI, you also need the Tinkerforge Master Brick, which is capable of connecting up to four Bricklets and has a mini USB connector to connect it with a computer.

The first thing to do, is to install brickd. When brickd is up and running, I recommend to change the following two lines in /etc/brickd.conf:
  •  listen.address = 127.0.0.1
  •  authentication.secret = topsecret
First, only allow local connections and second, specify a password for the authentication (I was having problems, when I disabled authentication). After brickd is configured properly, you need to find out the device identifiers of your Tinkerforge bricks, in order to use them through the API. Tinkerforge already provides a sample Python script that will list all connected devices. Because we previously configured authentication, the sample Python script needs to be modified, so that it will authenticate against brickd. In order to do so, add the following method call after the line where the call to the connect method takes place:

ipcon.authenticate('topsecret')

Replace 'topsecret' with your authentication password. Before you can make use of the Tinkerforge API, you have to install the bindings for the language that you want to use. In our case, the easiest way to do that, is to download the ZIP file with the Python bindings and copy the directory source/tinkerforge to your prefered location on your harddisc. To be able to import the bindings from a Python script, just copy the Python script to the same location where you copied the tinkerforge directory. After this is all done, you can run the script and it will output all connected devices and their corresponding identifiers.