Spark Core for IoT device development in SmartThings

In this example I will show you how to turn on and off the Spark Core blue, on board, LED connected to D7 from your SmartThings App.

First you will have to flash this code to your Spark Core

/* A Spark function to parse the commands */
int ledControl(String command);

// We name pin D7 as led
int led = D7; 

// This routine runs only once upon reset
void setup() 
{
  //Register Spark function
  Spark.function("ledstate", ledControl);    
  // Initialize D0 pin as an output
  pinMode(led, OUTPUT);
}

// This routine loops forever 
void loop()
{
  // Nothing to do here
}

int ledControl(String command)
{
  if (command == "1") {   
    digitalWrite(led, HIGH);   // Turn ON the LED
    return 1;
  } else {               
    digitalWrite(led, LOW);    // Turn OFF the LED
    return 0;
  }
}

Then, on SmartThings IDE:

  • Log in to SmartThings IDE
  • Go to “My Device Type” and click on “+New SmartDevice”
  • Create the Device type with the following values:
  • Name: Spark Core
  • Author: juano23@gmail.com
  • Click Create
  • Paste the following code
/**
 *  Spark Core
 *
 *  Author: juano23@gmail.com
 *  Date: 2014-01-23
 */
 
 preferences {
    input("deviceId", "text", title: "Device ID")
    input("token", "text", title: "Access Token")
}
 
 // for the UI
metadata {
    // Automatically generated. Make future change here.
	definition (name: "Spark Core", author: "juano23@gmail.com") {
    	capability "Switch"
	}

    // 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"
		}

		main "switch"
		details "switch"
	}
}

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

def on() {
	put '1'
}

def off() {
	put '0'
}

private put(led) {
    //Spark Core API Call
	httpPost(
		uri: "https://api.spark.io/v1/devices/${deviceId}/ledstate",
        body: [access_token: token, command: led],  
	) {response -> log.debug (response.data)}
}

  • Go to “My Devices” and click on “+New Device”
  • Create the Device type with the following values
  • Name: Spark Core
  • Device Network Id: 2323
  • Type: Spark Core
  • Click Create
  • Restart the App and you should be able to see the new device
  • In the device Preferences you will be able enter your Device ID and your Access Token
  • All set :)

 

18 thoughts on “Spark Core for IoT device development in SmartThings

    1. juano2310

      No, I finished this afternoon and now I’m working in a Neo-pixel led strip using the Spark Core and SmartThings.

      Reply
    1. juano2310

      Hi @xchriss001:disqus ! I did some more work on it but I haven’t finish it yet. I will post it on the blog as soon as I’m done. Make sure to subscribe to my twitter account to get all the updates 🙂

      Reply
  1. Sean

    Great work! Thanks.
    Dumb question perhaps but: This requires a SmartThings hub, correct?
    Or can it work without the hub but just the SmartThings phone app, since my Spark Core can already talk to the outside world via its wifi and the Spark server/cloud?

    Reply
    1. juano2310

      What’s the error that you get? Also, were you able to enter your Device ID and your Access Token in the SmartApp?

      Reply
      1. mich4

        Hi juano2310,
        first there is no “My Device Type” in SmartThings so I don’t know where to paste the following code.
        Edit: clicking on Developer gets me to My Device Type… i will try again

        Reply
  2. TerryGauchat

    Dumb question: But can this be tweaked to not required the Spark / Particle cloud? Isn’t there sufficient logic in the Core for a LAN only connection?

    I’m about to start playing with my Core’s, but I figured I had to ask this particular question somewhere! 🙂

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *