Examples¶
All tinkerforge connections require a brick daemon connection. This ip connection can either be a brick daemon running on a computer or a one the Master Extensions available: Ethernet Master Extension, WIFI Master Extension, or WIFI Master Extension 2.0.
Below is a vers simple example, that reads a value from a Temperature Bricklet 2.0. In this case, the user needs to provide the ip and the unique id of the bricklet. If you are using the Brick Viewer software or the original Tinkerforge API, the uids are Base58 encoded and need to be decoded first.
Basic Example¶
#!/usr/bin/env python3
# pylint: disable=duplicate-code
"""
A simple example, that reads a value from a temperature bricklet.
"""
import asyncio
from tinkerforge_async import base58decode # pylint: disable=unused-import # uncomment below to use base58 encoded uids
from tinkerforge_async.bricklet_temperature_v2 import BrickletTemperatureV2
from tinkerforge_async.ip_connection import IPConnectionAsync
async def main() -> None:
"""Connect to the bricklet and query the temperature value"""
try:
uid = 123456 # alternatively use base58decode("XXX")
# Use the context manager of the ip connection. It will automatically do the cleanup.
async with IPConnectionAsync(host="127.0.0.1", port=4223) as connection:
bricklet = BrickletTemperatureV2(uid, connection)
# The temperature is in Kelvin. Convert to float and subtract 273.15 to get Celsius.
print(f"Bricklet: {bricklet}\nTemperature: {await bricklet.get_temperature()} K")
except ConnectionRefusedError:
print("Could not connect to server. Connection refused. Is the brick daemon up?")
# Start the main loop and run the async loop forever. Turn off the debug parameter for production code.
try:
asyncio.run(main(), debug=True)
except KeyboardInterrupt:
print("Shutting down gracefully.")
More Examples¶
More examples, that also use the autodiscovery capability of the ip connection can be found in the examples folder.