def start(main_controller_connection): signal.signal(signal.SIGTERM, signal_handler) logger.info("Starting gpio_controller") while True: # Check GPIO if not GPIO.input(4): command = None presstime = 0 for _ in range(50): if not GPIO.input(4): presstime += 1 else: command = State.reboot_system break if presstime > 20: command = State.shutdown_system break time.sleep(0.1) ipcon.connect('localhost', 4223) time.sleep(1.0) master_brick = BrickMaster('6QHvJ1', ipcon) master_brick.reset() if command is State.reboot_system: subprocess.call(["sudo", "reboot"]) elif command is State.shutdown_system: subprocess.call(["sudo", "shutdown", "-h", "now"]) time.sleep(0.3)
def __init__(self, _): self.ipcon = IPConnection() # Create IP connection self.master = BrickMaster(UID, self.ipcon) # Create device object self.ipcon.connect(HOST, PORT) self.timeout = 1 self.read_buffer = [] self.baudrate = None time.sleep(1)
#!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XXYYZZ" # Change XXYYZZ to the UID of your Master Brick from tinkerforge.ip_connection import IPConnection from tinkerforge.brick_master import BrickMaster if __name__ == "__main__": ipcon = IPConnection() # Create IP connection master = BrickMaster(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Get current stack voltage (unit is mV) stack_voltage = master.get_stack_voltage() print("Stack Voltage: " + str(stack_voltage/1000.0) + " V") # Get current stack current (unit is mA) stack_current = master.get_stack_current() print("Stack Current: " + str(stack_current/1000.0) + " A") raw_input("Press key to exit\n") # Use input() in Python 3 ipcon.disconnect()
class TFSerial: def __init__(self, _): self.ipcon = IPConnection() # Create IP connection self.master = BrickMaster(UID, self.ipcon) # Create device object self.ipcon.connect(HOST, PORT) self.timeout = 1 self.read_buffer = [] self.baudrate = None time.sleep(1) # Implement Serial functions that we don't need def setDTR(self, _): pass def flushInput(self): pass def flushOutput(self): pass # We misuse the setRTS call to start the bootloader mode (flush everyting etc) def setRTS(self, value): if value: self.master.start_wifi2_bootloader() def write(self, data): try: data = map(ord, data) while data != []: data_chunk = data[:60] length = len(data_chunk) data_chunk.extend([0]*(60-length)) ret = self.master.write_wifi2_flash(data_chunk, length) data = data[60:] except: traceback.print_exc() print "write", length, data def read(self, length): if len(self.read_buffer) >= length: ret = self.read_buffer[:length] self.read_buffer = self.read_buffer[length:] return ''.join(map(chr, ret)) try: length_read = 0 t = time.time() while len(self.read_buffer) < length: data, l = self.master.read_wifi2_flash(60) data = data[:l] self.read_buffer.extend(data) if len(self.read_buffer) < length: time.sleep(0.1) if time.time() - t > self.timeout: break print "read", l, "of", length, data ret = ''.join(map(chr, data[:l])) except: traceback.print_exc() ret = self.read_buffer[:length] self.read_buffer = self.read_buffer[length:] return ''.join(map(chr, ret)) def done(self): self.master.reset()
# -*- coding: utf-8 -*- HOST = "192.168.1.134" PORT = 4223 UID = "BvH" # Change to your UID MASTER_UID = "68bR6c" VALUE_A_ON = (1 << 0) | (1 << 2) # Pin 0 and 2 high VALUE_A_OFF = (1 << 0) | (1 << 3) # Pin 0 and 3 high VALUE_B_ON = (1 << 1) | (1 << 2) # Pin 1 and 2 high VALUE_B_OFF = (1 << 1) | (1 << 3) # Pin 1 and 3 high from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_industrial_quad_relay import IndustrialQuadRelay from tinkerforge.brick_master import BrickMaster if __name__ == "__main__": ipcon = IPConnection() # Create IP connection master = BrickMaster(UID, ipcon) # Create device object iqr = IndustrialQuadRelay(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected #master.disable_wifi2_status_led() iqr.set_monoflop(VALUE_A_OFF, 15, 1500) # Set pins to high for 1.5 seconds ipcon.disconnect()
def cb_enumerate(self, uid, connected_uid, position, hardware_version, firmware_version, device_identifier, enumeration_type): #global self.led found = False if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \ enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE: # Enumeration for LED if device_identifier == LEDStrip.DEVICE_IDENTIFIER: self.LEDs.append(LEDStrip(uid, self.ipcon)) temp_uid = str(self.LEDs[-1].get_identity()[1]) + "." + str( self.LEDs[-1].get_identity()[0]) self.LEDList.addLED(self.LEDs[-1], temp_uid) self.LEDs[-1].set_frame_duration(200) if tifo_config.LEDs.get(temp_uid) <> None: self.LEDs[-1].set_chip_type( tifo_config.LEDs.get(temp_uid)[0]) self.LEDs[-1].set_frame_duration( tifo_config.LEDs.get(temp_uid)[1]) found = True #self.led.register_callback(self.led.CALLBACK_FRAME_RENDERED, # lambda x: __cb_frame_rendered__(self.led, x)) #self.led.set_rgb_values(0, self.NUM_LEDS, self.r, self.g, self.b) #self.led.set_rgb_values(15, self.NUM_LEDS, self.r, self.g, self.b) #self.led.set_rgb_values(30, self.NUM_LEDS, self.r, self.g, self.b) if device_identifier == IO16.DEVICE_IDENTIFIER: self.io.append(IO16(uid, self.ipcon)) temp_uid = str(self.io[-1].get_identity()[1]) + "." + str( self.io[-1].get_identity()[0]) self.io16list.addIO(self.io[-1], temp_uid, 16) self.io[-1].set_debounce_period(100) if tifo_config.IO16.get(temp_uid) <> None: self.io[-1].set_port_interrupt( 'a', tifo_config.IO16.get(temp_uid)[0]) self.io[-1].set_port_interrupt( 'b', tifo_config.IO16.get(temp_uid)[1]) self.io[-1].set_port_configuration( 'a', tifo_config.IO16.get(temp_uid)[0], 'i', True) self.io[-1].set_port_configuration( 'b', tifo_config.IO16.get(temp_uid)[1], 'i', True) self.io[-1].set_port_configuration( 'a', tifo_config.IO16.get(temp_uid)[2], 'o', False) self.io[-1].set_port_configuration( 'b', tifo_config.IO16.get(temp_uid)[3], 'o', False) #self.io[-1].set_port_monoflop('a', tifo_config.IO16.get(temp_uid)[4],0,tifo_config.IO16.get(temp_uid)[6]) #self.io[-1].set_port_monoflop('b', tifo_config.IO16.get(temp_uid)[5],0,tifo_config.IO16.get(temp_uid)[6]) self.io[-1].register_callback( self.io[-1].CALLBACK_INTERRUPT, partial(self.cb_interrupt, device=self.io[-1], uid=temp_uid)) found = True if device_identifier == AmbientLight.DEVICE_IDENTIFIER: self.al.append(AmbientLight(uid, self.ipcon)) self.al[-1].set_illuminance_callback_threshold('o', 0, 0) self.al[-1].set_debounce_period(10) #self.al.set_illuminance_callback_threshold('<', 30, 30) #self.al.set_analog_value_callback_period(10000) #self.al.set_illuminance_callback_period(10000) #self.al.register_callback(self.al.CALLBACK_ILLUMINANCE, self.cb_ambLight) #self.al.register_callback(self.al.CALLBACK_ILLUMINANCE_REACHED, self.cb_ambLight) args = self.al[-1] #self.al[-1].register_callback(self.al[-1].CALLBACK_ILLUMINANCE_REACHED, lambda event1, event2, event3, args=args: self.cb_ambLight(event1, event2, event3, args)) self.al[-1].register_callback( self.al[-1].CALLBACK_ILLUMINANCE_REACHED, partial(self.cb_ambLight, device=args)) temp_uid = str(self.al[-1].get_identity()[1]) + "." + str( self.al[-1].get_identity()[0]) found = True thread_cb_amb = Timer(60, self.thread_ambLight, [self.al[-1]]) thread_cb_amb.start() if device_identifier == BrickletCO2.DEVICE_IDENTIFIER: self.co2.append(BrickletCO2(uid, self.ipcon)) temp_uid = str(self.co2[-1].get_identity()[1]) + "." + str( self.co2[-1].get_identity()[0]) found = True thread_co2_ = Timer(5, self.thread_CO2, [self.co2[-1]]) thread_co2_.start() self.threadliste.append(thread_co2_) if device_identifier == BrickletDualRelay.DEVICE_IDENTIFIER: self.drb.append(BrickletDualRelay(uid, self.ipcon)) # # if device_identifier == Moisture.DEVICE_IDENTIFIER: # self.moist = Moisture(uid, self.ipcon) # self.moist.set_moisture_callback_period(10000) # self.moist.register_callback(self.moist.CALLBACK_MOISTURE, self.cb_moisture) if device_identifier == BrickletMotionDetector.DEVICE_IDENTIFIER: self.md.append(BrickletMotionDetector(uid, self.ipcon)) temp_uid = str(self.md[-1].get_identity()[1]) + "." + str( self.md[-1].get_identity()[0]) self.md[-1].register_callback( self.md[-1].CALLBACK_MOTION_DETECTED, partial(self.cb_md, device=self.md[-1], uid=temp_uid)) self.md[-1].register_callback( self.md[-1].CALLBACK_DETECTION_CYCLE_ENDED, partial(self.cb_md_end, device=self.md[-1], uid=temp_uid)) found = True if device_identifier == BrickletSoundIntensity.DEVICE_IDENTIFIER: self.si.append(BrickletSoundIntensity(uid, self.ipcon)) temp_uid = str(self.si[-1].get_identity()[1]) + "." + str( self.si[-1].get_identity()[0]) # TODO: remove all ifs found = True self.si[-1].set_debounce_period(1000) self.si[-1].register_callback( self.si[-1].CALLBACK_INTENSITY_REACHED, partial(self.cb_si, device=self.si[-1], uid=temp_uid)) self.si[-1].set_intensity_callback_threshold('>', 200, 0) if device_identifier == BrickletPTC.DEVICE_IDENTIFIER: self.ptc.append(BrickletPTC(uid, self.ipcon)) temp_uid = str(self.ptc[-1].get_identity()[1]) + "." + str( self.ptc[-1].get_identity()[0]) found = True thread_pt_ = Timer(5, self.thread_pt, [self.ptc[-1]]) thread_pt_.start() self.threadliste.append(thread_pt_) if device_identifier == BrickMaster.DEVICE_IDENTIFIER: self.master.append(BrickMaster(uid, self.ipcon)) thread_rs_error = Timer(60, self.thread_RSerror, []) #thread_rs_error.start() if tifo_config.inputs.get(uid) <> None: found = True if not found: print connected_uid, uid, device_identifier
#!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "6e6MVj" # Change to your UID from tinkerforge.ip_connection import IPConnection from tinkerforge.brick_master import BrickMaster if __name__ == "__main__": ipcon = IPConnection() # Create IP connection master = BrickMaster(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected master.start_wifi2_bootloader() raw_input('Press key to exit\n') # Use input() in Python 3 ipcon.disconnect()