The document introduces the IoT Toolkit and Smart Object API, which provide an open-source framework for building Internet of Things applications and connecting smart objects. The toolkit includes a reference implementation of the Smart Object API that defines a REST API and event model for IoT. It allows for discovery of connected things and semantic modeling of sensor and device data to enable interoperability between IoT applications and platforms.
IoT Toolkit Tutorial: Build Connected Things with Smart Object API
1. IoT
Toolkit
and
Smart
Object
API
Tutorial
Introduc7on
Open
Source
Internet
of
Things
2. IoT
2.0
–
Interoperability
M2M
CoAP
M2M
MQ
SOA
M2M
HTTP
Smart
Object
API
IoT
Toolkit
Discovery
Applica7ons
Thing
Models
Resource
Models
• Object
Model
• REST
+
Event
Model
• M2M
Abstrac<ons
IoT
Toolkit
Connected
Things,
Sensors,
Actuators,
Data
Sources
Seman5c
Repository
• Easy
to
deploy
new
things
and
applica7ons
using
data
models
• Write
once
run
anywhere,
any
app
to
any
thing
via
any
M2M
• Network
effect
enabled
• Allows
for
appropriate
M2M
choice
3. Interoperability
Architecture
SO
SO
SO
Gateway
Server
Cloud
Endpoints
• Sensors
• Devices
Applica7on
Components
And
Resources
Databases
Instances
Classes
SRT
• Discovery
• Persistence
• Replica7on
• Resource
Access
• Data
Models
• Sensor
Models
• Machine
Models
• Templates
4. IoT
Toolkit
• IoT
Toolkit
is
a
reference
implementa7on
of
the
Smart
Object
API
and
related
tools
• REST
API
with
real-‐7me
event
model
for
the
Internet
of
Things
• Lightweight
server
that
can
run
on
small
computers
e.g.
Raspberry
Pi
• Work
in
progress
prototype
(limited
excep7on
handling
and
simple
interfaces)
5. Smart
Object
API
• Object
Model
–
Web
Object
Encapsula7on
• Data
Models
–
Linked
Data
descrip7ons
• Event
Model
–
Asynchronous
M2M
and
applica7on
soUware
event
handlers
• REST
API
using
JSON,
XML,
RDF
representa7ons
• Web
standards
and
best
prac7ces
with
new
extensions
6. Object
Model
Web
Object
Encapsula7on
Smart
Object
Web
protocol
interfaces,
also
M2M
e.g.
MQTT
Event
Model
Links
data
with
ac7ons
Pub-‐Sub
and
event
handlers
Encapsulates
local
soUware
components
and
handlers
Self-‐describing
data
model
For
Resource
Discovery
and
Linkage
Sensor
or
other
data
JSON,
XML,
data
feeds
8. REST
API
• Python
API
–
Local
SoUware,
Agents
and
Handlers
– Resources
(objects)
are
hierarchical
proper7es
of
other
resources
– self.indoorTempValue = sensors.weather.indoor_temp.get()!
• Web
API
–
HYp
REST
Client
access
– Resources
are
organized
in
hierarchical
path
– GET http:my_server:8000/sensors/weather/indoor_temp!
• Default
Property
– Returns
a
resource
associated
with
a
higher
level
resource
– GET
of
Smart
Object
returns
it’s
Descrip7on;
GET
of
Observable
Property
returns
it’s
Observa7on
Value
9. API
–
Descrip7on
(Data
Model)
• Contains
graphs
describing
the
resource
data
model
• RDF
in
many
popular
representa7ons
– create
(POST)
loads
a
graph
into
the
descrip7on
delete
(DELETE)
removes
a
graph
or
subgraph
from
the
descrip7on
– set
(PUT)
updates
a
graph
or
subgraph
– get
(GET)
returns
a
graph
or
subgraph
10. API
–
Observable
Property
• Represents
sensor
data
or
a
data
feed
• Can
be
JSON,
xml,
text,
or
other
content
type
– create
(POST):
make
new
observable
property
– delete
(DELETE)
remove
observable
property
– set
(PUT)
update
Observa7on
Value
or
data
feed
– get
(GET)
returns
the
current
Observa7on
Value
11. API
–
Observers
• Contains
graphs
which
define
endpoints
of
event
driven
processing
• Publish,
Subscribe,
and
Event
Handler
paYerns
• Reference
Implementa7on
– create
(POST)
makes
a
new
Observer
• Publisher,
Subscriber,
or
Handler
Class
– set
(PUT)
updates
an
Observer’s
seangs
– get
(GET)
returns
an
Observer’s
seangs
– delete
(DELETE)
removes
an
observer
graph
12. API
–
Agent
• Container
for
Event
Handlers,
soUware
components
invoked
within
the
local
context
of
the
Smart
Object
• Observer
class
invokes
Event
Handler
code
• Reference
Implementa7on
– create
(POST)
makes
a
new
code
object
instance
• type
handler
or
daemon
– delete
(DELETE)
removes
code
instance
– set
(PUT)
updates
seangs
of
a
code
instance
– get
(GET)
returns
seangs
of
a
code
instance
13. Weather
Sensor
Example
• Weather
Sensor
hardware
from
Sparkfun
• Arduino
connects
hardware
sensors
to
gateway
via
ethernet
• Raspberry
Pi
gateway
runs
Smart
Object
API
• Arduino
uses
hYp
POST
of
JSON
value
to
update
Smart
Object
Observable
Proper7es
• Smart
Object
built
using
python
code
14. Weather
sensor
example
Sensor
(Arduino)
Gateway
(Rpi)
Sensor
Hardware
• Wind
Speed
• Wind
Direc7on
• Rainfall
• Temperature
• Humidity
• Barometer
Reads
sensor
elements
and
creates
sensor
output
values
to
update
Smart
Object
in
the
Gateway
using
a
simple
hYp
client
Gateway
runs
Smart
Object
API
and
exposes
HTTP
interface,
adds
descrip7onand
other
resources,
Observers
send
updates
to
cloud
server
Local
Ethernet
Cloud
Server
acts
as
Gateway-‐as-‐a-‐Service
for
Xively
Receives
updates
from
the
gateway,
Observers
Send
periodic
updates
to
Xively
feed
Cloud
Server
Internet
Client
(Xively)
Internet
Xively
acts
as
client
applica7on
and
receives
updates
from
the
cloud
service
ac7ng
as
GaaS
15. Arduino
Sketch
PString jsonObject(objectBuffer, sizeof(objectBuffer));!
!
if(nextPushPoint==0) {!
nextPushPoint=1;!
/* use Pstring.print to format JSON string from integer */!
jsonObject.println(daily_rain,1); !
/* PUT update to Smart Object server property using JSON */!
client.print("PUT ");!
client.print(serverPath);!
client.print(”daily_rain");!
client.println(" HTTP/1.1");!
client.println("Content-Type: application/json");!
client.print("Content-Length: ");!
client.println(jsonObject.length()-2);!
client.println("Connection: close");!
client.println();!
client.println(jsonObject);!
}!
16. Demo
Code
for
Weather
Sensor
–
Smart
Object
and
Service
Crea7on
baseObject
=
SmartObject()
#
create
a
Smart
Object
to
serve
as
the
base
container
for
other
Smart
Objects
and
resources
server
=
SmartObjectService(baseObject)
#
make
an
instance
of
the
service,
baseObject
is
the
object
root
server.start(8000)
#
forks
a
server
thread
to
listen
on
port
8000
print
'hDpd
started
at',
baseObject.Proper<es.get('hDpService’)
#
top
level
object
container
for
sensors,
class
is
SmartObject
sensors
=
baseObject.create({'resourceName':
'sensors',
'resourceClass':
'SmartObject'})
#weather
sensor
Smart
Object
under
sensors
for
the
weather
sensor
weather
=
sensors.create({'resourceName':
'rhvWeather-‐01',
'resourceClass':
'SmartObject’}!
17. Data
Model
–
Descrip7on
for
object
discovery
and
linkage
#
make
a
reference
to
the
weather
sensor
object
Descrip7on
and
build
an
example
graph
(could
use
the
built-‐in
reference
as
well)
weather.descrip7on
=
weather.Resources.get('Descrip<on')
weather.descrip7on.set((URIRef('sensors/rhvWeather-‐01/outdoor_temperature'),
RDFS.Resource,
Literal('sensor')))
weather.descrip7on.set((URIRef('sensors/rhvWeather-‐01/outdoor_temperature'),
RDF.type,
Literal('temperature')))
weather.descrip7on.set((URIRef('sensors/rhvWeather-‐01/outdoor_humidity'),
RDFS.Resource,
Literal('sensor')))
weather.descrip7on.set((URIRef('sensors/rhvWeather-‐01/outdoor_humidity'),
RDF.type,
Literal('humidity')))
weather.descrip7on.set((URIRef('sensors/rhvWeather-‐01/sealevel_pressure'),
RDFS.Resource,
Literal('sensor')))
weather.descrip7on.set((URIRef('sensors/rhvWeather-‐01/sealevel_pressure'),
RDF.type,
Literal('pressure')))!
18. Encapsula7on:
Observable
Property,
Observer,
and
Subscriber
sealevel_pressure
=
weather.create({'resourceName':
'sealevel_pressure',
'resourceClass':
'ObservableProperty'})
sealevel_pressure.Observers.create({'resourceName':'demoServiceObserver',
'resourceClass':
'hDpPublisher',
'targetURI':
'hDp://smartobjectservice.com:
8000/sensors/rhvWeather-‐01/sealevel_pressure'})
#
make
a
named
subscriber
resource
outdoor_humidity.Observers.create({'resourceName':
'humiditySubscriber',
'resourceClass':
'hDpSubscriber',
'observerURI':
'hDp://localhost:8000/sensors/rhvWeather-‐01/
indoor_humidity',
'observerName':
'humiditySubObserver'
})
20. Event
Model
–
Handler
and
Callback
No7fier
#addHandler
class
adds
2
proper7es
values
together
and
stores
in
a
third
weather.Agent.create({'resourceName':
'addHandler',
'resourceClass':
'addHandler',
'addendLink1':'sensors/rhvWeather-‐01/indoor_temperature',
'addendLink2':
'sensors/rhvWeather-‐01/indoor_temperature',
'sumOutLink':
'sensors/rhvWeather-‐01/outdoor_humidity'})
#
now
create
a
callback
observer
endpoint
indoor_temperature.Observers.create({'resourceName':
'callbackTempObserver',
'resourceClass':
'callbackNo<fier',
'handlerURI':
'callback:///sensors/rhvWeather-‐01/Agent/
addHandler'})
21. IoT
Toolkit
Project
• Smart
Object
API
is
nominally
complete
and
stable
• Development
priori7es
– Web
UI
for
debug
and
demo,
simple
applica7ons
• Navigator,
Dashboard,
Analy7cs,
Graphs
and
Charts
– M2M
Endpoints
for
CoAP,
MQTT,
and
others
– Test
code
for
API
– Internal
features
development
• Build
SmartObject
Instances
from
Resource
Models
• Object
Persistence
• Data
persistence
and
storage
22. MQTT
Observer
• Publish,
Subscribe,
or
Pub+Sub
using
the
mqYObserver
resource
class
• Prototype
opens
a
connec7on
to
a
specified
broker
for
each
endpoint
Observers.create({'resourceName': 'mqttTestObserver',!
! ! ! ! ! 'resourceClass': 'mqttObserver',!
'connection': 'smartobjectservice.com',!
'pubTopic': ’sealevel_pressure',!
'subTopic': None,!
'QoS': 0,!
'keepAlive': 60 })!
23. MQTT
Observer
MQTT
Broker
REST
Endpoint
ObservableProperty
mqYObserver
PUT
GET
Publish
from
data
producer
Publish
From
REST
API
Publish
to
Other
Subscribers
SUB
Publish
to
REST
API
Connects
REST
Resource
to
MQTT
Topic
Publish
and
Subscribe
24. MQTT
Observer
MQTT
Broker
REST
Endpoint
ObservableProperty
mqYObserver
PUT
GET
Publish
from
data
producer
Publish
From
REST
API
Publish
to
Other
Subscribers
SUB
Publish
to
REST
API
Publisher
Publishes
REST
Resource
updates
to
the
broker
25. MQTT
Observer
MQTT
Broker
REST
Endpoint
ObservableProperty
mqYObserver
PUT
GET
Publish
from
data
producer
Publish
From
REST
API
Publish
to
Other
Subscribers
SUB
Publish
to
REST
API
Subscriber
Makes
last
published
data
available
at
the
REST
endpoint
26. MQTT
Observer
MQTT
Broker
REST
Endpoint
ObservableProperty
mqYObserver
PUT
GET
Publish
from
data
producer
Publish
From
REST
API
Publish
to
Other
Subscribers
SUB
Publish
to
REST
API
Pub+Sub
Repeats
data
updates
in
both
direc7ons
27. MQTT
Bridge
to
mul7ple
REST
endpoints
MQTT
Broker
REST
Endpoint
ObservableProperty
mqYObserver
PUT
GET
Publish
from
data
producer
Publish
to
Other
Subscribers
REST
Endpoint
ObservableProperty
mqYObserver
PUT
GET