Internet of Things
TinkerforgeCode in ActionAbout Me
  • Course Outline
  • 1 - Getting Started
    • Overview
    • Connect to the LED
    • Getting Started
      • Glitch
      • The Application Template
    • Concepts in Programming
      • What is Programming?
      • Variables
      • Functions and Commands
      • Control Structures
      • Loops
      • Objects and Libraries
    • Programming Simple Web Apps
    • Exercises
      • 1.1 Buttons and Inputs
  • 2 - Internet of Things
    • Overview
    • IoT in our Apps
      • Getting Started
        • Hardware Kit
        • Brick Viewer and Daemon
      • Connect to the Devices
        • The Tinkerforge Device Manager
      • Program the Devices
        • RGB LED
        • RGB LED Button
        • OLED Display
        • Sensors
          • Humidity Sensor
          • Ambient Light Sensor
    • Components and Use Cases
    • Exercises
      • 2.1 Lights and Buttons
      • 2.2 Sensors
      • 2.3 Display
  • 3 - Artificial Intelligence
    • Overview
    • AI in our Apps
      • Google's Teachable Machine
      • Face Recognition
      • Training a Custom Model
    • Rules vs. Learning
    • Learning from Data
    • Use Cases
      • Computer Vision
        • Image Classification
        • Handwriting Recognition
    • Machine Learning Algorithms
      • Artificial Neural Networks
      • Decision Trees
      • Logistic Regression
    • Exercises
      • 3.1 Rules vs. Learning
      • 3.2 Fruits and Vegetables
      • 3.3 Face Recognition
      • 3.4 A Classifier for Iris
  • 4 - Cloud & APIs
    • Overview
    • APIs in our Apps
    • Cloud and APIs
      • Weather API
      • NASA Open APIs
      • EDAMAM Nutrition and Recipes API
    • Push Notifications
    • Exercises
  • 5 - App Project
    • Overview
    • Summer 2021
    • Summer 2022
  • Appendix
    • Other Devices
      • Motorized Linear Poti
      • Sound Pressure Sensor
      • NFC Reader
      • Motion Detector
    • UI Features
      • Realtime Charts
      • Countdown Timer
    • Digital Computers
      • Overview
      • The Binary System
      • Code Systems
      • Logic Gates
      • Binary Addition
      • From Analog to Digital
    • Cheat Sheets
    • Projects
      • IoT @ Pickup-Boxes
Powered by GitBook
On this page
  • Set up the development environment
  • Install the latest Python version
  • Download and install Visual Studio Code
  • Install the Python extension for Visual Studio Code
  • Install Brick Daemon and Brick Viewer
  • Set up the Python project
  • Create a Python virtual environment
  • Activate the virtual environment
  • Install the Tinkerforge library
  • Example 1: Read the humidity sensor once
  • Example 2: Read the humidity sensor continuously

Was this helpful?

  1. Appendix
  2. Projects

IoT @ Pickup-Boxes

PreviousProjects

Last updated 3 years ago

Was this helpful?

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

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.

For this project, you don't need to configure the web sockets ports as decribed in the link above.

Set 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:

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. takes you to the extensions' website on the VS Code marketplace.

This link
Brick Viewer and Daemon
Download PythonPython.org
https://code.visualstudio.com/downloadcode.visualstudio.com
Doc | Tinkerforge
Logo
Logo
The output of the example program above.
The output of the example program above.