Multiple Devices on One Shield

@urman hinted to me that a solution to this is only weeks away, not months, but for those of you looking for an immediate solution, I think I have this worked out. As hinted by @stevesell, the solution is in a custom SmartApp.

I’ve built an Arduino-controlled 8-way relay board and like most of you managed to get it set up as a single device with multiple tiles for each relay. It exposes multiple custom capabilities. The problem is how to control each relay from regular smart apps. The next step is to add multiple ‘virtual’ switches. These do nothing but send an event when they are toggled on/off. The clever part is a small SmartApp which subscribes to events from these virtual switches and sends the appropriate custom commands to my Arduino/relay device.

Diagram

Code here: http://build.smartthings.com/projects/arduino8wayrelay

Here’s a snapshot of the virtual switch:

preferences {
    input("num", "number", title: "Switch number", description: "The switch (relay) number to connect to (1 to 8)", required: true)
}

// simulator metadata
simulator {
    status "on":  "command: 2003, payload: FF"
    status "off": "command: 2003, payload: 00"

    // reply messages
    reply "2001FF,delay 100,2502": "command: 2503, payload: FF"
    reply "200100,delay 100,2502": "command: 2503, payload: 00"
}

// tile definitions
tiles {
    standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
        state "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
        state "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
    }

    standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") {
        state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh"
    }

    main "switch"
    details(["switch","refresh"])
}

// handle commands
def on() {
	log.debug "On"
    sendEvent (name: "switch", value: "on")
}

def off() {
	log.debug "Off"
    sendEvent (name: "switch", value: "off")
}

As you can see, it is simply a ‘device.switch’ and sends two events via these lines:

sendEvent (name: "switch", value: "on")
sendEvent (name: "switch", value: "off")

The ‘parent’ SmartApp (excerpt):

preferences {
	section("Connect these virtual switches to the Arduino's relays") {
		input "switch1", title: "Switch for relay 1", "capability.switch"
        input "switch2", title: "Switch for relay 2", "capability.switch", required: false
        input "switch3", title: "Switch for relay 3", "capability.switch", required: false
        input "switch4", title: "Switch for relay 4", "capability.switch", required: false 
        input "switch5", title: "Switch for relay 5", "capability.switch", required: false
        input "switch6", title: "Switch for relay 6", "capability.switch", required: false
        input "switch7", title: "Switch for relay 7", "capability.switch", required: false
        input "switch8", title: "Switch for relay 8", "capability.switch", required: false
	}
    section("Which Arduino relay board to control?") {
		input "arduino", "device.arduinoRelayBoard"
    }    
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	subscribe()
}

def updated() {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
	subscribe()
}

def subscribe() {
    subscribe(switch1, "switch.on", switchOn1)
    subscribe(switch1, "switch.off", switchOff1)
    subscribe(switch2, "switch.on", switchOn2)
    subscribe(switch2, "switch.off", switchOff2)
    subscribe(switch3, "switch.on", switchOn3)
    subscribe(switch3, "switch.off", switchOff3)
    subscribe(switch4, "switch.on", switchOn4)
    subscribe(switch4, "switch.off", switchOff4)
    subscribe(switch5, "switch.on", switchOn5)
    subscribe(switch5, "switch.off", switchOff5)
    subscribe(switch6, "switch.on", switchOn6)
    subscribe(switch6, "switch.off", switchOff6)
    subscribe(switch7, "switch.on", switchOn7)
    subscribe(switch7, "switch.off", switchOff7)
    subscribe(switch8, "switch.on", switchOn8)
    subscribe(switch8, "switch.off", switchOff8)
}

def switchOn1(evt)
{
    log.debug "switchOn1($evt.name: $evt.value: $evt.deviceId)"
    arduino.RelayOn1()
}

def switchOff1(evt)
{
    log.debug "switchOff1($evt.name: $evt.value: $evt.deviceId)"
    arduino.RelayOff1()
}

<SNIP>

When installed, the SmartApp needs to be configured with the virtual switches it should listen to along with the Arduino Relay Board:

Settings

I hope that is useful to somebody!

Cheers, Jonathan

3 Likes