def switchHeating( mySQLdbCursorObj, sw1, sw2, eventID, online_id, UID=TF_HEATSW_UID, writeLog=True ): ''' Switch Heating on or Off and report via mySQL ''' ipcon = getTFconn() try: dr = DualRelay(UID, ipcon) # Create device object isSw1, isSw2 = dr.get_state() except: Logger.exception("Unable to read switch status!") return now = datetime.datetime.now() if isSw1 == sw1 and isSw2 == sw2: Logger.debug("switchHeating: No change! " + str(sw1) +','+ str(sw2) ) return try: result = dr.set_state(sw1, sw2) Logger.info( "Changed switch to " + str(sw1) + ", " + str(sw2) + ". Result: " + str(result) ) except: Logger.exception("Unable to CHANGE switch status!") return # report into mySQL. Example: # INSERT INTO `heating_logbook` (`timestamp`, `eventID`, `eventStart`, `estimateOn`, `actualOn`, `actualOff`) VALUES (CURRENT_TIMESTAMP, '', '', '', '14:38:00', ''); sqlCmd = "INSERT INTO heating_logbook (`eventID`, `actualOn`, `actualOff`) VALUES ('" + str(eventID) + "', " actualOn = actualOff = '00:00' if sw1 == True: actualOn = now.strftime( "%H:%M" ) else: actualOff = now.strftime( "%H:%M" ) sqlCmd+= "'" + actualOn + "', '" + actualOff + "'); " # execute SQL remote and local executeSQL( mySQLdbCursorObj, sqlCmd, 'write heating switching time into' ) # local #if eventID==0: return # heating control is on manual, so no event id involved # and remote writeApiEventLog( online_id, actOn=actualOn, actOff=actualOff )
#!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "xyz" # Change to your UID import time from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_dual_relay import DualRelay if __name__ == "__main__": ipcon = IPConnection() # Create IP connection dr = DualRelay(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Turn relays alternating on/off for 10 times with 1 second delay for i in range(10): time.sleep(1) if i % 2: dr.set_state(True, False) else: dr.set_state(False, True) raw_input('Press key to exit\n') # Use input() in Python 3 ipcon.disconnect()