#!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your IO-4 Bricklet import time from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_io4_v2 import BrickletIO4V2 if __name__ == "__main__": ipcon = IPConnection() # Create IP connection io = BrickletIO4V2(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Configure all the channels as input no pull-up for i in range(0, 4): io.set_configuration(i, io.DIRECTION_IN, False) # Set edge count configuration for i in range(0, 4): io.set_edge_count_configuration(i, io.EDGE_TYPE_BOTH, 128) # Get edge count configuration for i in range(0, 4): print "[+] CH", i, "CONFIG =", io.get_edge_count_configuration(i) print ""
if "UID" in args: UID = args["UID"] if "CHANNELS" in args: CHANNELS = args["CHANNELS"] # Check arguments # print("JSON:H={},P={},U={},C={}".format(HOST,PORT,UID,CHANNELS)) else: status = 0 result = { "status" : "ERROR", "title" : "Wrong number of arguments." } if status == 1: try: # Create IP connection ipcon = IPConnection() # Create device object devobj = BrickletIO4V2(UID, ipcon) # Connect to brickd ipcon.connect(HOST, PORT) # Check if the bricklet is reachable - else error is triggered # In case the bricklet is not reachable, it takes 1-2 seconds befor a response is given devobj.read_uid() # SET one or more channels if CHANNELS != None: title = "" for item in CHANNELS: channel = int(item["channel"]) channelobj = devobj.get_configuration(channel) title = "channel:{} value:{}".format(channel,channelobj) result = { "status" : "OK", "title" : "{}".format(title) } # Disconnect ipcon.disconnect()
def onStart(self): Domoticz.Debug("onStart called") Domoticz.Debug("Debug Mode:" + Parameters["Mode6"]) if Parameters["Mode6"] == "Debug": self.debug = True Domoticz.Debugging(1) DumpConfigToLog() # Get the defaults for direction and value for each of the channels # The strings contain 4 entries separated by comma (,). ## Directions ChannelDirectionsParam = Parameters["Mode2"] Domoticz.Debug("ChannelDirections:" + ChannelDirectionsParam) ## Split the parameter string into a list of directions self.ChannelDirections = ChannelDirectionsParam.strip().split(',') # Check the list length against the constant CHANNELS if len(self.ChannelDirections) < CHANNELS: Domoticz.Error("[ERROR] Directions parameter not correct ("+str(len(self.ChannelDirections))+")! Number of channels should be " + str(CHANNELS) + ".") return ## Values - convert the values from string to int ChannelValuesParam = Parameters["Mode3"] Domoticz.Debug("ChannelValues:" + ChannelValuesParam) # self.ChannelValues = ChannelValuesParam.strip().split(',') # self.ChannelValues=[int(i.strip()) for i in ChannelValuesParam] self.ChannelValues = [int(x.strip()) for x in ChannelValuesParam.split(',')] if len(self.ChannelValues) < CHANNELS: Domoticz.Error("[ERROR] Values parameter not correct ("+str(len(self.ChannelValues))+")! Number of channels should be " + str(CHANNELS) + ".") return if (len(Devices) == 0): # Create new devices for the Hardware = each channel is a switch Domoticz.Debug("Creating new Devices") Domoticz.Device(Name="IO4 Channel 0", Unit=UNITCHANNEL1, TypeName="Switch", Used=1).Create() Domoticz.Debug("Device created: "+Devices[UNITCHANNEL1].Name) Domoticz.Device(Name="IO4 Channel 1", Unit=UNITCHANNEL2, TypeName="Switch", Used=1).Create() Domoticz.Debug("Device created: "+Devices[UNITCHANNEL2].Name) Domoticz.Device(Name="IO4 Channel 2", Unit=UNITCHANNEL3, TypeName="Switch", Used=1).Create() Domoticz.Debug("Device created: "+Devices[UNITCHANNEL3].Name) Domoticz.Device(Name="IO4 Channel 3", Unit=UNITCHANNEL4, TypeName="Switch", Used=1).Create() Domoticz.Debug("Device created: "+Devices[UNITCHANNEL4].Name) Domoticz.Device(Name="IO4 Status", Unit=UNITSTATUS, TypeName="Alert", Used=1).Create() Domoticz.Debug("Device created: "+Devices[UNITSTATUS].Name) updateStatus(STATUSLEVELOK, STATUSTEXTOK) # Get the UID of the IO4 self.UID = Parameters["Mode1"] if len(self.UID) == 0: updateStatus(STATUSLEVELERROR, "[ERROR] Device UID not set. Get the UID using the Brick Viewer.") return # Flag to check if connected to the master brick self.ipConnected = 0 # Create IP connection self.ipConn = IPConnection() # Create device object self.ioDev = BrickletIO4V2(self.UID, self.ipConn) # Connect to brickd using Host and Port try: self.ipConn.connect(Parameters["Address"], int(Parameters["Port"])) self.ipConnected = 1 Domoticz.Debug("IP Connection - OK") except: updateStatus(STATUSLEVELERROR, "[ERROR] IP Connection failed. Check the parameter.") return # Don't use device before ipcon is connected if self.ipConnected == 0: updateStatus(STATUSLEVELERROR, "[ERROR] Can not connect to Master Brick. Check settings, correct and restart Domoticz." ) return Domoticz.Debug("Connected to the Master Brick." ) Domoticz.Debug("Configure channels." ) # Configure the channels 0-3 - which is unit 1-4; i=input;o=output for channel, direction in enumerate(self.ChannelDirections): channelvalue = self.ChannelValues[channel] if channelvalue == 0: value = False else: value = True # Set the configuation of the io4 bricklet Show error in case ricklet not reachable. try: # Set the direction and initial value for the chhannel self.ioDev.set_configuration(channel, direction, value) Domoticz.Debug("Channel=%s, Direction=%s, Value=%s" % (channel, direction, value)) # Register input value callback to function cb_input_value if direction == "i": self.ioDev.register_callback(self.ioDev.CALLBACK_INPUT_VALUE, onInputCallback) # Set period for input value (channel 1) callback to 0.5s (500ms) self.ioDev.set_input_value_callback_configuration(channel, CALLBACKPERIOD, False) Domoticz.Debug("Channel=%s = Callback registered" % (channel)) except: updateStatus(STATUSLEVELERROR, "[ERROR] Set configuration - IO4V2 Bricklet not reachable.") # Set the domoticz device value if value == True: Unit = channel + 1 Devices[Unit].Update(nValue=1,sValue="")
def connect_tinkerforge(config): result = False # Define the globals for he tinkerforge connection and io4 bricklet global tfHost, tfPort global io4Uid, io4CallbackPeriod, io4StatusLED, io4Dev # Get the config parameter if DEBUG == True: print("Config=:{}".format(config)) # Parse the config string args = json.loads(config) # Assign to the vars tfHost = args["HOST"] if "HOST" in args else TF_HOST tfPort = int(args["PORT"]) if "PORT" in args else TF_PORT io4Uid = args["UID"] if "UID" in args else IO4_UID io4StatusLED = int( args["STATUSLED"]) if "STATUSLED" in args else IO4_STATUS_LED io4CallbackPeriod = IO4_CALLBACK_PERIOD io4Channels = args["CHANNELS"] if "CHANNELS" in args else "" # Check arguments if DEBUG == True: print( "connect tinkerforge: host={},port={},uid={},callbackperiod={},channels={}" .format(tfHost, tfPort, io4Uid, io4CallbackPeriod, io4Channels)) try: # Create IP connection ipCon = IPConnection() # Create device object io4Dev = BrickletIO4V2(io4Uid, ipCon) # Connect to brickd ipCon.connect(tfHost, tfPort) # Check if the bricklet is reachable - else error is triggered # In case the bricklet is not reachable, it takes 1-2 seconds befor a response is given io4Dev.read_uid() # Set the status led io4Dev.set_status_led_config(io4StatusLED) # Set one or more channels if io4Channels != None: title = "" for item in io4Channels: channel = int(item["channel"]) value = int(item["value"]) direction = item["direction"] # set configuration is used to enable the of getting the value using get_configuration io4Dev.set_configuration(channel, direction, value) # Register input value callback to function cb_input_value if direction == "i": io4Dev.register_callback(io4Dev.CALLBACK_INPUT_VALUE, on_io4_callback) # Set period for input value for the channel with callback io4Dev.set_input_value_callback_configuration( channel, io4CallbackPeriod, False) title = "channel:{} value:{} direction:{}".format( channel, value, direction) result = True # result = { "status" : "OK", "title" : "{}".format(title) } # print("Tinkerforge Connected: host={},port={},uid={}".format( tfHost, tfPort, io4Uid)) # Disconnect # ipCon.disconnect() except: result = False # result = { "status" : "ERROR", "title" : "io4 failed. Check master & bricklet." } # Print the result which is used by the dzVents Lua script if DEBUG == True: print(json.dumps(result)) return result