Upgrade to Pro — share decks privately, control downloads, hide ads and more …

IoT made easy @ SITCON x HK

Jeremy Yen
October 22, 2016

IoT made easy @ SITCON x HK

IoT made easy: RPi, Sensortag, Websocket with Node.js

Jeremy Yen

October 22, 2016
Tweet

More Decks by Jeremy Yen

Other Decks in Technology

Transcript

  1. QBUSJDLTUXTMJEFT 0VUMJOF ˙ $PNNVOJDBUJPOXJUI#-&EFWJDF ˙ 8SBQQJOHVQXJUI/PEFKT ˙ 'FFEJUUP8FCTPDLFU ˙ $POOFDUJOHUP.VMUJQMF%FWJDF

    ˙ 5SPVCMFTIPPUJOH ˙ "EEJUJPOBM*OTUBMMJOHPO.BD049 ˙ "EEJUJPOBM3FBEJOH23$PEFXJUI[CBS 3
  2. 7

  3. QBUSJDLTUXTMJEFT 5*4FOTPSUBH $$ ˙ *35FNQFSBUVSF ˙ "DDFMFSPNFUFS ˙ )VNJEJUZ ˙

    4JNQMF,FZ ˙ .BHOFUPNFUFS ˙ #BSPNFUSJD1SFTTVSF ˙ (ZSPTDPQF ˙ -VYPNFUFS ˙ *0 $$ ˙ *35FNQFSBUVSF ˙ "DDFMFSPNFUFS ˙ )VNJEJUZ ˙ 4JNQMF,FZ ˙ .BHOFUPNFUFS ˙ #BSPNFUSJD1SFTTVSF ˙ (ZSPTDPQF 10
  4. QBUSJDLTUXTMJEFT *OTUBMM#MVF[%SJWFS ˙ %FQFOEFODZ ˙ TVEPBQUHFUJOTUBMMMJCVTCEFWMJCECVTEFWMJCHMJC EFWMJCVEFWEFWMJCJDBMEFWMJCSFBEMJOFEFWMJCCMVFUPPUI EFW ˙ *OTUBMM

    ˙ XHFUXXXLFSOFMPSHQVCMJOVYCMVFUPPUI CMVF[UBSY[ ˙ DPOHVSFEJTBCMFTZTUFNE ˙ NBLF ˙ TVEPNBLFJOTUBMM 13
  5. QBUSJDLTUXTMJEFT ("551SPUPDPM # Enable Humidity Sensor Service [78:A5:04:19:59:B9][LE]> char-write-cmd 0x3f

    01 # Enable Notification [78:A5:04:19:59:B9][LE]> char-write-cmd 0x3c 01:00 Notification handle = 0x003b value: 38 6c be 84 Notification handle = 0x003b value: 38 6c be 84 ... http://processors.wiki.ti.com/images/a/a8/BLE_SensorTag_GATT_Server.pdf 18
  6. QBUSJDLTUXTMJEFT 1SPDFTTJOH7BMVF var buffer = new Buffer([0x38, 0x6c, 0xbe, 0x84]);

    var temperature = -46.85 + 175.72 / 65536.0 * buffer.readUInt16LE(0); var humidity = -6.0 + 125.0 / 65536.0 * (buffer.readUInt16LE(2) & ~0x0003); 19
  7. QBUSJDLTUXTMJEFT *OTUBMM1BDLBHF sudo apt-get install nodejs npm sudo npm -g

    install npm node-gyp npm install sensortag IUUQTHJUIVCDPNTBOEFFQNJTUSZOPEFTFOTPSUBH 22
  8. QBUSJDLTUXTMJEFT XTGVODUJPO router.ws('/humidity/:uuid', function(ws, req) { _tag[uuid].on('humidityChange', function (temperature, humidity)

    { var obj = {}; obj[uuid] = { temperature: temperature, humidity: humidity }; ws.send(JSON.stringify(obj));; 29
  9. QBUSJDLTUXTMJEFT OPUJGZEFWJDFTUBUVT function tagDiscovery(tag) { tag.on('disconnect', function() { // Remove

    Property delete device_info[tag.uuid]; // Emit Disconnected Event events.emit('device_disconnect'); 30
  10. QBUSJDLTUXTMJEFT OPUJGZEFWJDFTUBUVT var EventEmitter = require('events').EventEmitter, events = new EventEmitter();

    router.ws('/connected', function(ws, req) { events.on('device_disconnect', function() { ws.send(JSON.stringify(device_info)); 31
  11. QBUSJDLTUXTMJEFT OPCMFXBSOJOHEVSJOHDPOOFDUJPO QSPDFTT ˙ )BQQFOT SBOEPNMZ CFGPSF DPOOFDU"OE4FU6Q.F  ˙

    8PSLBSPVOE ˙ DBUDIDPOTPMFXBSOGVODUJPO NTH JGOPCMF XBSOJOHBQQFBSTUIFOSFTUBSUOPEFKTQSPDFTT 41
  12. QBUSJDLTUXTMJEFT $BOOPUFOBCMFEFWJDFTFSWJDF ˙ 4PNFUJNFT "GUFSDPOOFDU"OE4FU6Q.F UIF EFWJDFTFSWJDFXPOUFOBCMF ˙ 8PSLBSPVOE ˙

    4FUVQBTFU5JNFPVU XBUDIEPHBOEXBJU TFDPOET*GTFSWJDFOPUFOBCMFECZUIBUUJNF  UBHEJTDPOOFDU 42
  13. 48