Jump to content
View in the app

A better way to browse. Learn more.

Tinkerunity

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Geschrieben

I'm learning Python so I wrote some scripts for the Ambient Light and Linear Poti bricklets.  This may be useful for people who are new to programming.

This gives a basic idea on how to use the bricklets as a switch.

 

Ambient Light:

#!/usr/bin/env python
# -*- coding: utf-8 -*-  
import os

HOST = "localhost"
PORT = 4223
UID = "xxx" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_ambient_light import AmbientLight

# Callback function for illuminance callback (parameter has unit Lux/10)
def cb_illuminance(illuminance):

    less = '''Light.\n
    oooooooooooooooooooooooooo\n
    oooooooooooooooooooooooooo\n
    oooooooooooooooooooooooooo\n
    oooooooooooooooooooooooooo\n
    oooooooooooooooooooooooooo\n
    oooooooooooooooooooooooooo'''

    more = '''Dark.\n
    --------------------------\n
    --------------------------\n
    --------------------------\n
    --------------------------\n
    --------------------------\n
    --------------------------'''

    running = True
    while running:
    # Test with print('Illuminance: ' + str(illuminance/10.0) + ' Lux')
        lux = str(illuminance/10.0)
        if str(49) < lux:
            os.system('clear')
            print less
            break
        if str(51) > lux:
            os.system('clear')
            print more
            break

if __name__ == "__main__":
    ipcon = IPConnection(HOST, PORT) # Create IP connection to brickd

    al = AmbientLight(UID) # Create device object
    ipcon.add_device(al) # Add device to IP connection
    # Don't use device before it is added to a connection

    # Set Period for illuminance callback to 1s (1000ms)
    # Note: The illuminance callback is only called every second if the 
    #       illuminance has changed since the last call!
    al.set_illuminance_callback_period(1000)

    # Register illuminance callback to function cb_illuminance
    al.register_callback(al.CALLBACK_ILLUMINANCE, cb_illuminance)

    raw_input('Press key to exit\n') # Use input() in Python 3
    ipcon.destroy()

 

Linear Poti:

#!/usr/bin/env python
# -*- coding: utf-8 -*-  
import os

HOST = "localhost"
PORT = 4223
UID = "xxx" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_linear_poti import LinearPoti

# Callback function for position callback (parameter has range 0-100)
def cb_position(position):

    less = '''Switch Off.\n
    -------------\n
    -------------\n
    -------------\n
    0000000000000'''

    more = '''Switch On.\n
    0000000000000\n
    -------------\n
    -------------\n
    -------------'''

    running = True
    while running:
    # Test with print('Position: ' + str(position))
        slide = str(position)
        if str(49) < slide:
            os.system('clear')
            print less
            break
        if str(51) > slide:
            os.system('clear')
            print more
            break
        
if __name__ == "__main__":
    ipcon = IPConnection(HOST, PORT) # Create IP connection to brickd

    poti = LinearPoti(UID) # Create device object
    ipcon.add_device(poti) # Add device to IP connection
    # Don't use device before it is added to a connection

    # Set Period for position callback to 0.05s (50ms)
    # Note: The position position is only called every 50ms if the 
    #       position has changed since the last call!
    poti.set_position_callback_period(50)

    # Register position callback to function cb_position
    poti.register_callback(poti.CALLBACK_POSITION, cb_position)

    raw_input('Press key to exit\n') # Use input() in Python 3
    ipcon.destroy()

Geschrieben
  • Autor

Done. Although I'm new to the Wiki stuff so I'm sure someone can make it look better.  I'll add onto it as I play with Python more.

Geschrieben

I moved your code to its own page and added it to both categories (code kitchen and python) http://www.tinkerunity.org/wiki/index.php/Ambient_Light_Switch

Its easier to get an overview, if there are more code examples. You can see all Python code examples on the python page automatically http://www.tinkerunity.org/wiki/index.php/Category:Python

 

If you have some more code snippets, just create a new page in the wiki and add the following to the bottom of the wiki page: [[Category:Code-Sammlung]] [[Category:Python]] and your new page will appear on both category pages.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.