class sound_activated(modes.standard.abstractMode): _ipcon = None _si = None def __init__(self): ''' Constructor ''' modes.standard.abstractMode.__init__(self) config = ConfigParser.ConfigParser() config.read("config.ini") if config.has_section("Tinkerforge") and config.has_option('Tinkerforge', 'HOST') and config.has_option('Tinkerforge', 'PORT') and config.has_option('Tinkerforge', 'UID'): HOST = config.get('Tinkerforge', 'HOST') PORT = config.getint('Tinkerforge', 'PORT') UID = config.get('Tinkerforge', 'UID') else: print "Can't load Tinkerforge Settings from config.ini" self._ipcon = IPConnection() # Create IP connection self._si = SoundIntensity(UID, self._ipcon) # Create device object self._ipcon.connect(HOST, PORT) # Connect to brickd def __del__(self): self._ipcon.disconnect() def myround(self, x, base=5): return int(base * round(float(x) / base)) def start(self): high = 0.0 count = 0 while True: intensity = self._si.get_intensity() # reset high_level after a Song if count > 100: high = intensity count = 0 if intensity > high: high = intensity else: count += 1 if high > 0: level = self.myround((100 / float(high)) * float(intensity)) else: level = 0 RED = BLUE = GREEN = 0 if level <= 33: BLUE = 100 elif level <= 66: GREEN = 100 else: RED = 100 self.setRGB([RED, GREEN, BLUE]) time.sleep(self._DELAY) def getName(self): return "Sound Activated"
def __init__(self): ''' Constructor ''' modes.standard.abstractMode.__init__(self) config = ConfigParser.ConfigParser() config.read("config.ini") if config.has_section("Tinkerforge") and config.has_option('Tinkerforge', 'HOST') and config.has_option('Tinkerforge', 'PORT') and config.has_option('Tinkerforge', 'UID'): HOST = config.get('Tinkerforge', 'HOST') PORT = config.getint('Tinkerforge', 'PORT') UID = config.get('Tinkerforge', 'UID') else: print "Can't load Tinkerforge Settings from config.ini" self._ipcon = IPConnection() # Create IP connection self._si = SoundIntensity(UID, self._ipcon) # Create device object self._ipcon.connect(HOST, PORT) # Connect to brickd
from tinkerforge.bricklet_sound_intensity import SoundIntensity from tinkerforge.bricklet_dust_detector import DustDetector from tinkerforge.bricklet_temperature import Temperature from tinkerforge.bricklet_ambient_light import AmbientLight from tinkerforge.bricklet_uv_light import BrickletUVLight from tinkerforge.bricklet_motion_detector import BrickletMotionDetector from tinkerforge.bricklet_humidity_v2 import HumidityV2 import time from datetime import datetime if __name__ == "__main__": ipcon = IPConnection() # Create IP connection co2 = BrickletCO2(CO2_UID, ipcon) # Create device object humidity = HumidityV2(HUMIDITY_UID, ipcon) sound_intensity = SoundIntensity(SOUND_INTENSITY_UID, ipcon) dust_density = DustDetector(DUST_UID, ipcon) temperature = Temperature(TEMPERATURE_UID, ipcon) ambientlight = AmbientLight(AMBIENTLIGHT_UID, ipcon) uvlight = BrickletUVLight(UVLIGHT_UID, ipcon) motiondetect = BrickletMotionDetector(MOTIONDETECTOR_UID, ipcon) ipcon.connect(HOST, PORT) # Connect to brickd while (True): curtime = datetime.now() print("Time: " + curtime.strftime('%Y/%m/%d %H:%M:%S')) motion = motiondetect.get_motion_detected() print("Motion: " + str(motion))
# -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change to your UID from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_sound_intensity import SoundIntensity # Callback for intensity greater than 2000 def cb_reached(intensity): print('Intensity: ' + str(intensity)) if __name__ == "__main__": ipcon = IPConnection() # Create IP connection si = SoundIntensity(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Get threshold callbacks with a debounce time of 1 seconds (1000ms) si.set_debounce_period(1000) # Register threshold reached callback to function cb_reached si.register_callback(si.CALLBACK_INTENSITY_REACHED, cb_reached) # Configure threshold for "greater than 2000" si.set_intensity_callback_threshold('>', 2000, 0) raw_input('Press key to exit\n') # Use input() in Python 3 ipcon.disconnect()
def cb_enumerate(self, uid, connected_uid, position, hardware_version, firmware_version, device_identifier, enumeration_type): """Recherche des brickets et configuration.""" if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \ enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE: if device_identifier == Temperature.DEVICE_IDENTIFIER: try: self.temp = Temperature(uid, self.ipcon) log.info( time.strftime("%Y-%m-%d %H:%M:%S") + ' Temperature initialized') except Error as err: log.error( time.strftime("%Y-%m-%d %H:%M:%S") + ' Temperature init failed: ' + str(err.description)) self.temp = None elif device_identifier == SoundIntensity.DEVICE_IDENTIFIER: try: self.sound = SoundIntensity(uid, self.ipcon) log.info( time.strftime("%Y-%m-%d %H:%M:%S") + ' Sound intensity initialized') except Error as err: log.error( time.strftime("%Y-%m-%d %H:%M:%S") + ' Sound intensity init failed: ' + str(err.description)) self.sound = None elif device_identifier == Accelerometer.DEVICE_IDENTIFIER: try: self.accel = Accelerometer(uid, self.ipcon) log.info( time.strftime("%Y-%m-%d %H:%M:%S") + ' Accelerometer initialized') except Error as err: log.error( time.strftime("%Y-%m-%d %H:%M:%S") + ' Accelerometer init failed: ' + str(err.description)) self.accel = None elif device_identifier == IndustrialAnalogOut.DEVICE_IDENTIFIER: try: self.aout = IndustrialAnalogOut(uid, self.ipcon) self.aout.set_configuration( self.aout.VOLTAGE_RANGE_0_TO_5V, self.aout.CURRENT_RANGE_0_TO_20MA) self.aout.enable() self.aout_connected = True log.info( time.strftime("%Y-%m-%d %H:%M:%S") + ' IndustrialAnalogOut initialized') except Error as err: log.error( time.strftime("%Y-%m-%d %H:%M:%S") + ' IndustrialAnalogOut init failed: ' + str(err.description)) self.aout = None elif device_identifier == IndustrialDualAnalogIn.DEVICE_IDENTIFIER: try: self.ain = IndustrialDualAnalogIn(uid, self.ipcon) self.ain.set_sample_rate(6) self.ain_connected = True log.info( time.strftime("%Y-%m-%d %H:%M:%S") + ' IndustrialDualAnalogIn initialized') except Error as err: log.error( time.strftime("%Y-%m-%d %H:%M:%S") + ' IndustrialDualAnalogIn init failed: ' + str(err.description)) self.ain = None elif device_identifier == IndustrialDigitalIn4.DEVICE_IDENTIFIER: try: self.din = IndustrialDigitalIn4(uid, self.ipcon) self.din.set_interrupt(8) self.din.set_debounce_period(0) self.din.register_callback(self.din.CALLBACK_INTERRUPT, self.cb_compteur_turbine) self.din_connected = True log.info( time.strftime("%Y-%m-%d %H:%M:%S") + ' IndustrialDigitalIn4 initialized') except Error as err: log.error( time.strftime("%Y-%m-%d %H:%M:%S") + ' IndustrialDigitalIn4 init failed: ' + str(err.description)) self.din = None