IoT @ Pickup-Boxes
Set up the development environment
Install the latest Python version
First, download and install the latest version of Python for your operating system (currently 3.10.0).
Download and install Visual Studio Code
Next, download and install Visual Studio Code, an open-source integrated development environment, for your operating system.
Install the Python extension for Visual Studio Code
Visual Studio Code allows you to extend the functionality with so-called extensions. For Python development, I recommend the Python extension. You can install the extension directly from VS Code. Click on the extensions tab on the left menu and type “Python” into the search field. This link takes you to the extensions' website on the VS Code marketplace.
Install Brick Daemon and Brick Viewer
To work with the Tinkerforge devices on your local computer, you need the Brick Daemon to connect to the devices on your USB port. Additionally, to play around with the devices, you can install the Brick Viewer.
Brick Viewer and DaemonSet up the Python project
Create a Python virtual environment
When developing in multiple projects with Python, it is a good practice to create a virtual environment for each project. This way you can ensure that you don't have conflicting dependencies between different projects. To create a virtual environment for your project, start a terminal in VS Code and change to the directory where you want the project to be. I put all my code into a directory called c:\code
on my main drive:
C:\code\> python -m venv c:\code\tinkerforge-with-python
Activate the virtual environment
Once created, you need to activate the virtual environment as follows:
cd tinkerforge-with-python
C:\code\tinkerforge-with-python> env\Scripts\activate
After this command, the terminal should display this:
(env) C:\code\tinkerforge-with-python>
Install the Tinkerforge library
To work with Tinkerforge devices from our Python code, we need to install their official library:
(env) C:\code\tinkerforge-with-python> pip install tinkerforge
Example 1: Read the humidity sensor once
You can copy the program code below into a new Python file (e.g., run.py
). Make sure you replace the UID
in line 3 with the correct value for your particular sensor. Run the program by typing python run.py
on the terminal.
HOST = "localhost"
PORT = 4223
UID = "Dk8" # Change this value to the UID of your sensor
from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_humidity_v2 import BrickletHumidityV2
ipcon = IPConnection() # Create IP connection
h = BrickletHumidityV2(UID, ipcon) # Create device object
ipcon.connect(HOST, PORT) # Connect to brickd
# Don't use device before ipcon is connected
# Get current humidity
humidity = h.get_humidity()
print("Humidity: " + str(humidity/100.0) + " %RH")
input("Press key to exit\n") # Use raw_input() in Python 2
ipcon.disconnect()
If everything works correctly, the terminal should display the current humidity:

Example 2: Read the humidity sensor continuously
For most use cases, it makes sense to read the values continuously. Either in a certain time interval (e.g, every second), or whenever the value changes. The following example uses a so-called callback function callback_humidity
that is registered with the sensor (line 20) and called every 1000 milliseconds (line 23).
HOST = "localhost"
PORT = 4223
UID = "Dk8" # Change this value to the UID of your sensor
from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_humidity_v2 import BrickletHumidityV2
# Callback function for humidity callback
def callback_humidity(humidity):
print("Humidity: " + str(humidity/100.0) + " %RH")
ipcon = IPConnection() # Create IP connection
h = BrickletHumidityV2(UID, ipcon) # Create device object
ipcon.connect(HOST, PORT) # Connect to brickd
# Don't use device before ipcon is connected
# Register humidity callback to function cb_humidity
h.register_callback(h.CALLBACK_HUMIDITY, callback_humidity)
# Set period for humidity callback to 1s (1000ms) without a threshold
h.set_humidity_callback_configuration(1000, False, "x", 0, 0)
input("Press key to exit\n") # Use raw_input() in Python 2
ipcon.disconnect()
The result is a continuous output of the humidity value (every 1 second) until the user interrupts the program with ⌨ Ctrl + X.

You can find this example and the API documentation for all devices on the official Tinkerforge website:
Last updated
Was this helpful?