SmartThings control of Spark Core-controlled relay board

Hello! I’m trying to write a Device type that allows for the control of my Spark Core which is attached to a 16-relay board. (The sole outlet of the GE duplex Z-wave outlets isn’t enough for my workshop :smile: ). Things are going well, but I’m noticing that sometimes the switches are left in limbo in the mobile app. So, let’s say I hit a relay to turn it off, it does indeed click off, but the status stays as “turning off”. I didn’t build the device type with polling and/or refreshing built in. Where would I insert this in the SmartThings device type? Would this resolve the problem? Should I use the response code sent by the Spark Core to accomplish this?


  • Spark Core Relay Control
  • Thanks to Jonathan Wilson and
  • Author: CMS
  • Date: 2014-12-20

preferences {
input(“deviceId”, “text”, title: “Device ID”)
input(“token”, “text”, title: “Access Token”)
input(“outletNumber”, “text”, title: “Outlet Number (0-15)”)

// for the UI
metadata {
// Automatically generated. Make future change here.
definition (name: “Spark Core Outlets”, author: "") {
capability “Switch”

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

	main "switch"
	details "switch"


def parse(String description) {
log.error "This device does not support incoming events"
return null

def on() {
put outletNumber + ‘,HIGH’

def off() {
put outletNumber + ‘,LOW’

private put(relayCommand) {
//Spark Core API Call
uri: “${deviceId}/relay”,
body: [access_token: token, command: relayCommand],
) {response -> log.debug (}

And for the Spark Core code:

int relayControl(String command);
int relay1 = D0;
int relay2 = D1;
int relay3 = D2;
int relay4 = D3;
int relay5 = D4;
int relay6 = D5;
int relay7 = D6;
int relay8 = D7;
int relay9 = A0;
int relay10 = A1;
int relay11 = A2;
int relay12 = A3;
int relay13 = A4;
int relay14 = A5;
int relay15 = A6;
int relay16 = A7;

int * relayArray[16] = { &relay1, &relay2, &relay3, &relay4, &relay5, &relay6, &relay7, &relay8, &relay9, &relay10, &relay11, &relay12, &relay13, &relay14, &relay15, &relay16 };

void setup()
//Initialize the relay control pins as output
Spark.function(“relay”, relayControl);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(relay5, OUTPUT);
pinMode(relay6, OUTPUT);
pinMode(relay7, OUTPUT);
pinMode(relay8, OUTPUT);
pinMode(relay9, OUTPUT);
pinMode(relay10, OUTPUT);
pinMode(relay11, OUTPUT);
pinMode(relay12, OUTPUT);
pinMode(relay13, OUTPUT);
pinMode(relay14, OUTPUT);
pinMode(relay15, OUTPUT);
pinMode(relay16, OUTPUT);
//// Initialize all relays to an OFF state
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
digitalWrite(relay5, LOW);
digitalWrite(relay6, LOW);
digitalWrite(relay7, LOW);
digitalWrite(relay8, LOW);
digitalWrite(relay9, LOW);
digitalWrite(relay10, LOW);
digitalWrite(relay11, LOW);
digitalWrite(relay12, LOW);
digitalWrite(relay13, LOW);
digitalWrite(relay14, LOW);
digitalWrite(relay15, LOW);
digitalWrite(relay16, LOW);

//register the Spark function


void loop()
// This loops for ever

int relayControl(String command) { // syntax pinnum,ON
Serial.print("unparsed command rec’d: ");
int relayNum = -1;
int relayState = 0;

char * params = new char[command.length() + 1];

strcpy(params, command.c_str());
char * param1 = strtok(params, ",");
char * param2 = strtok(NULL, ",");
//see what the parse job looked like
Serial.print("Param 1: ");
Serial.print("Param 2: ");

if (!strcmp(param2, "HIGH")) {
	relayState = 1;

else if (!strcmp(param2, "LOW")) {
	relayState = 0;

relayNum = atoi(param1);
if (relayNum >= 1 || relayNum <= 16) {

	//write to the relay
	Serial.print("relay "); Serial.print(relayNum); Serial.print(" "); Serial.println(relayState);
	digitalWrite((uint8_t)*relayArray[relayNum], relayState);

	if (relayState == 1) 
	    return 1;
	} else if (relayState == 0)
	    return 0


1 Like

All I needed to do was add sendEvent statements after each put statement (i.e. sendEvent(name: ‘switch’, value:‘off’).

Seems to be working dandy.

Did you get all the relays to work probably with feedback status as well? This would be awesome to use and monitor all the old door and window alarm sensors.

Well, I can say that now SmartThings is keeping account of the state of the switch. What was happening before was me essentialy shortcutting the process and speaking only to the Spark Core and leaving ST out of it.

As for feedback statuses, I don’t know. My setup for these relays is having them turn on outlets. If you’re asking whether they’re just like every other switch, so far it seems so. The only thing it doesn’t provide is polling. What specifically were you thinking with your question?

Hi I am sory because bad english .I’ve tried a lot of ways, but I could not get it to work
Could you show me a video step by step please

@scordinskyc, I realize this is an old thread, but can you post any update you might have made to this code?

I’m looking for a good starting point for a Particle Photon relay and your’s looks pretty good! Did you figure out the polling?