64
IOTDB.org Control all the Things with Node-JS

Control all the Things with Node-JS

Embed Size (px)

DESCRIPTION

My presentation at Full Stack Toronto on 2014-11-23 about how Node-IOTDB can be used to control and monitor many different types of devices, such as Philips Hues, Bluetooth Low Energy, TCP Connect, &c.. All code examples are available https://github.com/dpjanes/iotdb-examples/tree/master/demos/2014-11-fsto

Citation preview

Page 1: Control all the Things with Node-JS

IOTDB.org •

Control all the Things with Node-JS

Page 2: Control all the Things with Node-JS

David Janes@dpjanes

[email protected]://iotdb.org/social/imadeit/

November 2014

Page 3: Control all the Things with Node-JS

Introduction

Page 4: Control all the Things with Node-JS

N.B.

• Demo example code on github

• dpjanes/

• iotdb-examples/

• demos/

• 2014-11-fsto/

Page 5: Control all the Things with Node-JS

What is it?

{ "on" : true, "temperature" : 225 }

Page 6: Control all the Things with Node-JS

What does each field mean?

Page 7: Control all the Things with Node-JS

Control or Measurement?

• Does on mean:

• Turn it on? or

• Is it on?

• Does temperature mean:

• Set the temperature? or

• What is the temperature?

Page 8: Control all the Things with Node-JS

Units

• What does temperature refer to?

• degrees Celsius, Fahrenheit … or Kelvin?

Page 9: Control all the Things with Node-JS

What does the whole message represent?

Page 10: Control all the Things with Node-JS

What are we talking about … or with?

• An oven?

• A toaster?

• A sensor in the Large Hadron Collider?

Page 11: Control all the Things with Node-JS

Solve “the Basket Full of Remotes Apps

Problem”

Page 12: Control all the Things with Node-JS
Page 13: Control all the Things with Node-JS

Solve the N-standards• Open Interconnect

Consortium

• Thread Group

• AllSeen Alliance

• HyperCat Consortium

• Industrial Internet Consortium

• IoT-GSI) from ITU-T

• oneM2M

• Open Mobile Alliance

• Internet of Things (IEE)

• IETF

• IPSO Alliance

Page 14: Control all the Things with Node-JS

XKCD

Page 15: Control all the Things with Node-JS

IOTDB(.org)

Page 16: Control all the Things with Node-JS

Semantic Vocab

• Formal definitions

• https://iotdb.org/pub

• JSON-LD

Page 17: Control all the Things with Node-JS

Models

• https://iotdb.org/iotdb

• https://github.com/dpjanes/iotdb-models

Page 18: Control all the Things with Node-JS

Node-IOTDB

Page 19: Control all the Things with Node-JS

IOTDB Stack• Client Program

• Node-IOTDB

• Models

• Drivers

• Libraries

•simple_on.js

•require('iotdb')

•WeMoSwitch

•iot-driver:upnp

•upnplib

Page 20: Control all the Things with Node-JS

Client Programsimple_red

iot .connect() .set(':color', 'red');

Page 21: Control all the Things with Node-JS

require(‘iotdb’)

• locates Models, Drivers, Stores,…

• loads & maintains user configuration

• connects to iotdb.org (sometimes)

• manages Things (huge deal … too many things to go into in detail)

Page 22: Control all the Things with Node-JS

Model (I)

• Semantic description of Things

• (can be) written in JavaScript

• actually compiles to JSON-LD (with some restrained JavaScript)

• Node independent!

Page 23: Control all the Things with Node-JS

Model (II)

iotdb.make_model('WeMoSwitch') .facet(":device.switch") .product(“http://www.belkin.com/…”) .name("WeMo Switch") .description("Belkin WeMo Switch") .attribute( iotdb.make_boolean(":on") .name("on / off") .control() )

Page 24: Control all the Things with Node-JS

Model (III).driver_out(function(paramd) { if (paramd.thingd.on !== undefined) { paramd.driverd['urn:Belkin:service:basicevent:1'] = { 'SetBinaryState' : { 'BinaryState' : paramd.thingd.on ? 1 : 0 } } } })

Page 25: Control all the Things with Node-JS

Driversiot-driver:upnp

• Binds Models to the code that actually “does the work”

• discovery

• static configuration

• dynamic / environmental

Page 26: Control all the Things with Node-JS

Libraries

• What developers usually program against

• We’ve rewritten a number of libraries to make them more reliable

Page 27: Control all the Things with Node-JS

Demos

Page 28: Control all the Things with Node-JS

simple_on

things = iot.connect() things.set(':on', true);

Page 29: Control all the Things with Node-JS

simple_off

things = iot.connect() things.set(‘:on’, false);

Page 30: Control all the Things with Node-JS

simple_model

iot .connect(‘HueLight') .set(':on', true);

Page 31: Control all the Things with Node-JS

Metadata

Page 32: Control all the Things with Node-JS

Select Things

• select Things by (e.g.)

• Model

• name

• number

• place

• facet

Page 33: Control all the Things with Node-JS

meta_dump

var things = iot.connect();

iot.on_things(function() { console.log(things.metas()); });

Page 34: Control all the Things with Node-JS

meta_model

iot.connect() .with_model("TCPConnectedLight") .set(':on', false)

Page 35: Control all the Things with Node-JS

meta_name

iot .connect() .with_name("Hue Lamp 2") .set(':color', 'purple')

Page 36: Control all the Things with Node-JS

meta_number

iot .connect() .with_number(3) .set(':color', 'cyan')

Page 37: Control all the Things with Node-JS

meta_place

iot .connect() .with_room("David Bedroom") .with_floor("Second Floor") .set(':color', 'green')

Page 38: Control all the Things with Node-JS

meta_facet

iot .connect() .with_facet(":device.lighting") .set(':on', true);

Page 39: Control all the Things with Node-JS

where does Metadata come from?

• Metadata comes from several places

• can be altered in code

• can be persisted to locally / to disk

• can be retrieved from iotdb.org

• “inherent” in the Model / Driver

Page 40: Control all the Things with Node-JS

Events

Page 41: Control all the Things with Node-JS

two types of events

• thing.on(key, callback)

• thing.on_change(callback)

Page 42: Control all the Things with Node-JS

event_brightness

var lights = iot.connect() var input = iot.connect({ model: "FirmataInputUnit", pin: 0 }); input.on(":value", function(thing, attribute, value) { lights.set(':brightness', value); })

Page 43: Control all the Things with Node-JS

event_fob

var things = iot.connect(); iot .connect('TIKeyFob') .on('left', function() { things.set(':on', true); }) .on('right', function() { things.set(':on', false); })

Page 44: Control all the Things with Node-JS

Arduino / Firmata

Page 45: Control all the Things with Node-JS

firmata_cyclevar things = iot .connect() .connect({ model: "FirmataNeoPixel", pin: 6, n: 16 })

var colors = [ "red", "green", "blue", "white", "black" ]; var ci = 0;

setInterval(function() { things.set(":color", colors[ci++ % colors.length]); }, 2500)

Page 46: Control all the Things with Node-JS

firmata_neopixelvar n = 16; var leds = iot.connect({ model: "FirmataNeoPixel", pin: 6, n: n })

var c = new iotdb.libs.Color() var ci = 0; var cf = 0;

setInterval(function() { c.set_hsl(cf, 1, 0.5) cf += 0.015; if (cf > 1) cf = 0;

leds .with_number(ci++ % n) .set(":color", c.get_hex()) ;

}, 50)

Page 47: Control all the Things with Node-JS

Arduino Models• FirmataChainableLED

• FirmataDHT11

• FirmataGroveThermistor

• FirmataInputBoolean

• FirmataInputUnit

• FirmataLightDimmer

• FirmataLightSensor

• FirmataLightSimple

• FirmataMotionSensor

• FirmataNeoPixel

• FirmataOn

• FirmataOutputBoolean

Page 48: Control all the Things with Node-JS

Stores

Page 49: Control all the Things with Node-JS

Available Stores

• dweet

• http

• mqtt

• phant

• pubnub

• thingspeak

Page 50: Control all the Things with Node-JS

store_phant

var input = iot.connect('TIKeyFob')

var store = iot .store('phant') .track(input)

https://data.sparkfun.com/streams/aGNjQK9RwZHgEmx089Lg

Page 51: Control all the Things with Node-JS

store_mqtt

var input = iot.connect({ model: "FirmataInputUnit", pin: 0 });

var store = iot .store('mqtt') .track(input)

mqtt.iotdb.org u/dpjanes/#

Page 52: Control all the Things with Node-JS

Transmogrifiers

Page 53: Control all the Things with Node-JS
Page 54: Control all the Things with Node-JS

What if we have the wrong thing?

• Have Celsius but want Fahrenheit

• Want to set brightness but only have color

• What if brightness is 0-100 on one device and 0-1 on another

• Average data? Max data over time? &c…

Page 55: Control all the Things with Node-JS

trans_fahrenheit

var t_c = iot.connect("FirmataDHT11") t_c.on('temperature', function(thing, attribute, value) { console.log("+ temperature (C)", value) })

var t_f = t_c.transmogrify( iot.transmogrifier(":imperial/fahrenheit")) t_f.on('temperature', function(thing, attribute, value) { console.log("+ temperature (F)", value) })

Page 56: Control all the Things with Node-JS

warning

• transmogrifiers are very much a work in progress!

• not even pushed to npm yet

Page 57: Control all the Things with Node-JS

Takeaways

Page 58: Control all the Things with Node-JS

IOTDB

• Semantic description of how things work

Page 59: Control all the Things with Node-JS

Node-IOTDB

• Powerful language for controlling things

• Builds on IOTDB concepts

• Strong alpha

• Join! Help!

Page 60: Control all the Things with Node-JS

Modeling

{ "@context": "./meta-iri.jsonld", "on": true, "temperature": 225 }

Note: @context is just one way of doing this!

Page 61: Control all the Things with Node-JS

N.B.

• The data does not have to be JSON!

• You don’t need to have IOTDB!

• You don’t need to use IOTDB IRIs (but you should)

• https://iotdb.org/pub/

Page 62: Control all the Things with Node-JS

Additional Resources

• Many examples + House of Janeshttps://github.com/dpjanes/iotdb-examples

• Getting startedhttps://iotdb.org/docs/node/getting-started

• Conceptshttps://medium.com/@dpjanes

Page 63: Control all the Things with Node-JS

One last point

• Don’t be misled by the simplicity!

• I worked backward from “what do I want to do” to get the code

• Way more things than I demoed today

• I want to work with you!

Page 64: Control all the Things with Node-JS

Get in touch! David Janes

@[email protected]

http://iotdb.org/social/imadeit/