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
  • Test the connection with the Brick Viewer
  • Establish the connection
  • Getting the device's UID
  • Connect from Python
  • What does each line do?
  • Exercises

Was this helpful?

  1. 1 - Getting Started

Connect to the LED

We'll begin by learning how to connect to the colored LED from our Python program and use some of its functions.

PreviousOverviewNextGetting Started

Last updated 2 years ago

Was this helpful?

Test the connection with the Brick Viewer

The is a great companion to work the Tinkerforge devices. The software allows us to test each device's functionality using a simple graphical user interface (GUI) and check whether we can connect to the master brick at all. It is a good idea to first test the connection in the Brick Viewer to make sure everything is set up properly before we jump into the code.

Establish the connection

Using Brick Viewer to test Tinkerforge devices is simple. When you have the installed and running, which you only need to do once, and you connected the to your computer via the USB cable, you can open the Brick Viewer and click the "Connect" button.

Getting the device's UID

When using the Brick Viewer software, we can find the Unique Identifier (UID) of a device. This UID is necessary to directly connect to the device from Python. Later on, we will show another way to connect to devices without needing the UID. In this method, the devices will actively send their UID (and other information) through a provided function, allowing us to connect to them.

While we are at it, feel free to play around with RGB LED (and other devices that you have) and set its color from the Brick Viewer software. You can use sliders to set the value for each of the three RGB colors (red, green, blue), or you can use the three slider at the bottom for setting the color via HSL. We will soon learn how to do both from our Python program with a few lines of code.

Connect from Python

We can see from the screenshot above that the UID of the LED that is currently connected to my computer via a USB cable is AU1. Unless you have the same device instance, your UID will be different when you try this. So copy your UID and replace it in the code below (line 3):

rgb_led.py
HOST = "localhost"
PORT = 4223
UID = "AU1" # Change this to the UID of your RGB LED Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_rgb_led import BrickletRGBLED

ipcon = IPConnection() # Create IP connection
ipcon.connect(HOST, PORT) # Connect to brickd

# Create device object
led = BrickletRGBLED(UID, ipcon)

# Set to full green color
led.set_rgb_value(0, 255, 0)

input("Press key to exit\n")

ipcon.disconnect() 

What does each line do?

When you run the above program from your laptop using python rgb_led.py, it should turn your LED to green and then wait for you to press any key to exit the program. Let's take a closer look at what each line of code does.

Defining some constants

The first three lines of code define a set of so-called constants. A constant in programming is similar to a variable with one important difference: while variables' values can be changed at any point in the program, constants constantly stay the same. Hence, the name "constants". In Python, we indicate that we define a constant by writing its name in all-capital-letters. This is only conventional, and we technically could change the values later on. Note that this is different in other programming languages like Java, where we can define real constants, whose values can't be changed after initialization.

HOST = "localhost"
PORT = 4223
UID = "AU1"

We use the constants to define the values for the connection parameters HOST, PORT, and UID. It makes sense to define them as constants, as we do not expect their values to change during our program. They are only used once at the beginning of the program to establish the connection.

Import external code (libraries)

In lines 5 and 6 we use the from and import commands to indicate that we make use of external code libraries in our program. Specifically, we import the objects IPConnection and BrickletRGBLED from different namespaces of the tinkerforge library. This library was developed and made available by Tinkerforge and facilitates working with their hardware devices. While we use Python in this module, we could use the Tinkerforge devices with many other programming languages as well, as Tinkerforge offers support for almost any popular programming language in the form of a specific library.

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_rgb_led import BrickletRGBLED

Importing the two objects from the tinkerforge library makes sure we can later use these two objects to create instances of them.

Create the IP connection

The Tinkerforge devices communicate using a protocol called the Internet Protocol (IP). This protocol is used to exchange data between devices and is commonly used on the World Wide Web. To create a connection, we create a new variable called ipcon and assign it to a new instance of the IPConnection object class from the tinkerforge library. This creates a new IP-connection object which we can access using the ipcon variable. We can then use the connect() function, which expects two parameters: the hostname and port, to establish the IP-connection. These values have been defined as constants earlier in the code.

ipcon = IPConnection()
ipcon.connect(HOST, PORT)

Exercises

  1. What happens if you run the code but comment out line 9, so that the IP-connection does not get connected?

  2. From your Python program, how can you find out whether an IP-connection is connected? Enhance your code to include a check for that, and only then continue with everything else!

Brick Viewer software
Brick Daemon
Master Brick
With the "Connect" button, we can establish a connection to the local Tinkerforge devices.
You can find the device's UID in the Brick Viewer in the upper-left corner of each tab.