how to program smartthings

Post on 28-Nov-2014

130 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

How to program SmartApp and SmartDevice in SmartThings environment.

TRANSCRIPT

Programming Your SmartThingsJanet Huang

2014.10.22 @ NTU CSIEjanetyc@gmail.com

• Introduction

• Conceptual Architecture

• SmartThings Architecture

• How to Program SmartThings

• Build a SmartApp

• Build a Device Type

Outline

Important Concepts

Image Capture Lock Contact

Sensor Switch Music Player Temperature Measurement

Conceptual Architecture

SmartThings Architecture

SmartApps

SmartDevices

Customized Smart Device

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

+

Sensor, Actuator

=

1

2

3 ZigBee or ZigWave Device

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)

Device Type Handler

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

Capabilities

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

device state action

Your First SmartApp

SmartApp Empty Template

install setting

update setting

define input

https://graph.api.smartthings.com

Create an Empty SmartApp

SmartThings IDE

Choose SmartApp Examples

app setting

define action

define input

subscribe event

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

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

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

check the state of the door

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

1

2

Select location

Select testing devices

3 Testing on simulator

4 Testing on real devices

Your First Device Type

Create an Empty SmartDevice

select capabilities

profile

Device Type Empty Template

define UI

define capability, command

parse events

commands

Tile Definitions

standardTile()

valueTile()

controlTile()

Tile Layouts

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

main  "temperature"

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

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"])     }  }

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

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

Build Your SmartApp & SmartDevice

It’s Your Turn!!

top related