How to Program SmartThings

31
Programming Your SmartThings Janet Huang 2014.10.22 @ NTU CSIE [email protected]

description

How to program SmartApp and SmartDevice in SmartThings environment.

Transcript of How to Program SmartThings

Page 1: How to Program SmartThings

Programming Your SmartThingsJanet Huang

2014.10.22 @ NTU [email protected]

Page 2: How to Program SmartThings
Page 3: How to Program SmartThings

• Introduction

• Conceptual Architecture

• SmartThings Architecture

• How to Program SmartThings

• Build a SmartApp

• Build a Device Type

Outline

Page 4: How to Program SmartThings

Important Concepts

Image Capture Lock Contact

Sensor Switch Music Player Temperature Measurement

Page 5: How to Program SmartThings

Conceptual Architecture

Page 6: How to Program SmartThings

SmartThings Architecture

Page 7: How to Program SmartThings

SmartApps

SmartDevices

Customized Smart Device

SmartThings ShieldArduino-compatible board (Uno, Mega, Duemilanove, and Leonardo)

+

Sensor, Actuator

=

1

2

3 ZigBee or ZigWave Device

Page 8: How to Program SmartThings

SmartApp

Event Handler SmartApps

Solution Module SmartApps

Service Manager SmartApps

Subscribe events and call a handler method

Built-in apps in dashboard

Be used to connect to your external devices

Device Type

Hub Connected Devices

Cloud Connected Devices

LAN-Connected Devices

ZigBee Home Automation Devices Z-Wave Devices

Authenticate via a standard OAuth2 flow Communicate via HTTP-based APIs

Communicate via REST or with SOAP requests using UPnP (Universal Plug and Play)

Page 9: How to Program SmartThings

Device Type Handler

Page 10: How to Program SmartThings

1. Event Subscription

2. Scheduled Events

3. Endpoint Triggers

SmartApp Execution

call a method at a particular time, e.g. runIn()

attribute changes -> creates an event (triggers a subscription, calls a handler method)

create an endpoint accessible over the web that calls a method within your SmartApp

Page 11: How to Program SmartThings

Capabilities

https://graph.api.smartthings.com/ide/doc/capabilities

device state action

Page 12: How to Program SmartThings

Your First SmartApp

Page 13: How to Program SmartThings

SmartApp Empty Template

install setting

update setting

define input

Page 14: How to Program SmartThings

https://graph.api.smartthings.com

Page 15: How to Program SmartThings

Create an Empty SmartApp

Page 16: How to Program SmartThings

SmartThings IDE

Page 17: How to Program SmartThings

Choose SmartApp Examples

Page 18: How to Program SmartThings
Page 19: How to Program SmartThings

app setting

define action

define input

subscribe event

Example 1a: Lights Off, When Closed (Built-in example)

Page 20: How to Program SmartThings

https://gist.github.com/janetyc/22c0ecf586f1550669b5

Example 1b: Lights On/Off, When Opened/Closed

Page 21: How to Program SmartThings

check the state of the door

https://gist.github.com/janetyc/22c0ecf586f1550669b5

Page 22: How to Program SmartThings

1

2

Select location

Select testing devices

3 Testing on simulator

4 Testing on real devices

Page 23: How to Program SmartThings

Your First Device Type

Page 24: How to Program SmartThings

Create an Empty SmartDevice

Page 25: How to Program SmartThings

select capabilities

profile

Page 26: How to Program SmartThings

Device Type Empty Template

define UI

define capability, command

parse events

commands

Page 27: How to Program SmartThings

Tile Definitions

standardTile()

valueTile()

controlTile()

Tile Layouts

details(["temperature",  “mode",  …])

main  "temperature"

valueTile("temperature",  "device.temperature",  width:  2,  height:  2)

Page 28: How to Program SmartThings

https://gist.github.com/janetyc/34c1642f8eaa3e4299da

metadata  {     definition  (name:  "On/Off  Shield  (Janet)",  namespace:  "janetyc",  author:  "Janet  Huang")  {       capability  "Switch"       capability  "Sensor"                     command  "hello"     }         simulator  {       //  TODO:  define  status  and  reply  messages  here                     status  "on":    "catchall:  0104  0000  01  01  0040  00  0A21  00  00  0000  0A  00  0A6F6E"                     status  "off":  "catchall:  0104  0000  01  01  0040  00  0A21  00  00  0000  0A  00  0A6F6666"                               //  reply  messages                     reply  "raw  0x0  {  00  00  0a  0a  6f  6e  }":  "catchall:  0104  0000  01  01  0040  00  0A21  00  00  0000  0A  00  0A6F6E"       reply  "raw  0x0  {  00  00  0a  0a  6f  66  66  }":  "catchall:  0104  0000  01  01  0040  00  0A21  00  00  0000  0A  00  0A6F6666"     }         tiles  {       //  TODO:  define  your  main  and  details  tiles  here       standardTile("switch",  "device.switch",  width:  2,  height:  2,  canChangeIcon:  true,  canChangeBackground:  true)  {                       state  "on",  label:  '${name}',  action:  "switch.off",  icon:  "st.shields.shields.arduino",  backgroundColor:  "#79b821"                             state  "off",  label:  '${name}',  action:  "switch.on",  icon:  "st.shields.shields.arduino",  backgroundColor:  "#ffffff"                     }                     standardTile("greeting",  "device.greeting",  width:  1,  height:  1,  canChangeIcon:  true,  canChangeBackground:  true)  {         state  "greeting",  label:  'hello',  action:  "hello",  icon:  "st.switches.switch.off",  backgroundColor:  "#ccccff"       }                     valueTile("message",  "device.message",  inactiveLabel:  false)  {         state  "message",  label:'${currentValue}',  unit:""       }                                valueTile("sensorValue",  "device.sensorValue",  ){                       state  "sensorValue",  label:'${currentValue}',  inactiveLabel:  false,                               backgroundColors:[                                       [value:  31,  color:  "#153591"],                                       [value:  44,  color:  "#1e9cbb"],                                       [value:  59,  color:  "#90d2a7"],                                       [value:  74,  color:  "#44b621"],                                       [value:  84,  color:  "#f1d801"],                                       [value:  120,  color:  "#d04e00"],                                       [value:  195,  color:  "#bc2323"]                               ]                     }                                       main  "switch"       details(["switch","greeting","message","sensorValue"])     }  }

Page 29: How to Program SmartThings

//  parse  events  into  attributes    def  parse(String  description)  {       log.debug  "raw:  ${description}”  !   def  value  =  zigbee.parse(description)?.text     def  linkText  =  getLinkText(device)     def  descriptionText  =  getDescriptionText(description,  linkText,  value)     def  handlerName  =  value     def  isStateChange  =  value  !=  "ping"     def  displayed  =  value  &&  isStateChange       def  name  =  "null"                        if  (value?.startsWith("lightValue:  "))  {       value  =  value  -­‐  "lightValue:  "                    name  =  "sensorValue"                    value  =  Integer.parseInt(value)            }else  if  (value?.startsWith("irValue:  "))  {       value  =  value  -­‐  "irValue:  "                    name  =  "irValue"                    value  =  Integer.parseInt(value)            }else  {               name  =  value  in  ["on","off"]  ?  "switch"  :  (value  &&  value  !=  "ping"  ?  "message"  :  "null")               value  =  value            }                 def  result  =  [       value:  value,                    name:  name,       handlerName:  handlerName,       linkText:  linkText,       descriptionText:  descriptionText,       isStateChange:  isStateChange,       displayed:  displayed     ]         result  }

debug logparse zigbee message

attribute name

https://gist.github.com/janetyc/34c1642f8eaa3e4299da

Page 30: How to Program SmartThings

//  handle  commands  def  on()  {     log.debug  "Executing  'on'"             zigbee.smartShield(text:  "on").format()  //send  'on'  to  device  }      def  off()  {             log.debug  "Executing  'off'"             zigbee.smartShield(text:  "off").format()  //send  'off'  to  device  }  //customized  commands    def  hello()  {     log.debug  "Executing  'hello'"     zigbee.smartShield(text:  "hello").format()  //send  'hello'  to  device  }

https://gist.github.com/janetyc/34c1642f8eaa3e4299da

Page 31: How to Program SmartThings

Build Your SmartApp & SmartDevice

It’s Your Turn!!