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

Bringing JavaScript to the IoT Edge - Node + JS...

Joe Karlsson
December 09, 2019

Bringing JavaScript to the IoT Edge - Node + JS Interactive 2019

The number of IoT devices is expected to be around 18 billion by 2022 and be a market of over $500 Billion. Edge computing is becoming an even bigger requirement. As a versatile language, JavaScript is in a great position to power many of these devices. This session will explore why and where JavaScript is being used in this industry segment and specifically the role that Node.JS plays in the Internet of Things.

Data collection in the IoT arena comes in fast and from a variety of sensors and devices. How to model that data is important for analysis downstream. We’ll also take a look at some different schema design patterns to think about when storing the data in a modern database, like MongoDB.

Bringing JavaScript to the IoT Edge - Node + JS Interactive 2019

Joe Karlsson

December 09, 2019
Tweet

More Decks by Joe Karlsson

Other Decks in Programming

Transcript

  1. @JoeKarlsson1 Bringing JS to the IoT Edge A Gentle Introduction

    to the Internet of Things @JoeKarlsson1
  2. { name: “Joe Karlsson”, company: “MongoDB”, title: [ “Developer Advocate”,

    “Software Engineer” ], twitter: “@JoeKarlsson1”, twitch: “joe_karlsson”, website: “joekarlsson.com”, }
  3. @JoeKarlsson1 Agenda • Intro to IoT • Why JS &

    IoT? • IoT data best practices • IoT Kitty Litter Box • The future of IoT @JoeKarlsson1
  4. @JoeKarlsson1 A company is an association or collection of individuals,

    whether natural persons, legal persons, or a mixture of both. Company members share a common purpose and unite in order to focus their various talents and organize their collectively available skills or resources to achieve specific, declared goals. Collect Devices and sensors collect data @JoeKarlsson1
  5. @JoeKarlsson1 A company is an association or collection of individuals,

    whether natural persons, legal persons, or a mixture of both. Company members share a common purpose and unite in order to focus their various talents and organize their collectively available skills or resources to achieve specific, declared goals. Communicate Data and events sent through the network Data Center The Cloud Home Router @JoeKarlsson1
  6. @JoeKarlsson1 A company is an association or collection of individuals,

    whether natural persons, legal persons, or a mixture of both. Company members share a common purpose and unite in order to focus their various talents and organize their collectively available skills or resources to achieve specific, declared goals. Analyze Computers analyzes the incoming data Run Reports Home Router Machine Learning View Reports @JoeKarlsson1
  7. @JoeKarlsson1 A company is an association or collection of individuals,

    whether natural persons, legal persons, or a mixture of both. Company members share a common purpose and unite in order to focus their various talents and organize their collectively available skills or resources to achieve specific, declared goals. Act Taking action based on the data Send a notification Communicate with another machine Stream @JoeKarlsson1
  8. @JoeKarlsson1 IoT and JS • Intro to IoT • Anything

    with a internet enabled chip in it
  9. @JoeKarlsson1 IoT and JS • Intro to IoT • Anything

    with a internet enabled chip in it • IoT is exploding
  10. @JoeKarlsson1 Agenda • Intro to IoT • Why JS &

    IoT? • IoT data best practices • IoT Kitty Litter Box • The future of IoT @JoeKarlsson1
  11. @JoeKarlsson1 Why Node? 58 percent of respondents that identified as

    IoT developers use Node Source: https://nodejs.org/en/blog/announcements/nodejs-foundation-survey/ @JoeKarlsson1
  12. @JoeKarlsson1 JS is a great choice for new devs Other

    low level programming languages can be difficult to develop @JoeKarlsson1
  13. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs
  14. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs • JS is great for new devs
  15. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs • JS is great for new devs • Easy to update over a network
  16. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs • JS is great for new devs • Easy to update over a network • The internet already speaks JS
  17. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs • JS is great for new devs • Easy to update over a network • The internet already speaks JS • Tons of great libraries
  18. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs • JS is great for new devs • Easy to update over a network • The internet already speaks JS • Tons of great libraries • JS is event driven
  19. @JoeKarlsson1 Agenda • Intro to IoT • Why JS &

    IoT? • IoT data best practices • IoT Kitty Litter Box • The future of IoT @JoeKarlsson1
  20. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices
  21. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • DB needs to ingest data quickly
  22. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • DB needs to ingest data quickly • DB should be event driven
  23. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • DB needs to ingest data quickly • DB should be event driven • Flexible schema
  24. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • DB needs to ingest data quickly • DB should be event driven • Flexible schema • Time series data works best
  25. @JoeKarlsson1 Agenda • Intro to IoT • Why JS &

    IoT? • IoT data best practices • IoT Kitty Litter Box • The future of IoT @JoeKarlsson1
  26. @JoeKarlsson1 General Purpose Input/Output Pins [GPIO] Can be used to

    interact in with the real world @JoeKarlsson1
  27. @JoeKarlsson1 Input Pins Switches that you can turn on or

    off from the outside world @JoeKarlsson1
  28. @JoeKarlsson1 Great Overview of the GPIO Pins on Wikipedia @JoeKarlsson1

    Source: https://en.wikipedia.org/wiki/Raspberry_Pi#General_purpose_input-output_(GPIO)_connector
  29. @JoeKarlsson1 var five = require("johnny-five"); var Raspi = require("raspi-io").RaspiIO; var

    board = new five.Board({ io: new Raspi() }); board.on("ready", function() { var led = new five.Led("P1-13"); led.blink(); });
  30. @JoeKarlsson1 const { RaspiIO } = require('raspi-io'); const five =

    require(‘johnny-five'); const board = new five.Board({ io: new RaspiIO(), }); let state = { isMaintenenceMode: false, }; board.on('ready', () => { const spdt = new five.Switch('GPIO16'); spdt.on('open', () => { state.isMaintenenceMode = true; }); spdt.on('close', () => { state.isMaintenenceMode = false; }); });
  31. @JoeKarlsson1 import time import sys import RPi.GPIO as GPIO from

    hx711 import HX711 while True: try: except (KeyboardInterrupt, SystemExit): cleanAndExit() hx = HX711(5, 6) hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(1) hx.reset() hx.tare() def cleanAndExit(): print("Cleaning...") GPIO.cleanup() print("Bye!") sys.exit() val = hx.get_weight(5) print(val) hx.power_down() hx.power_up() time.sleep(0.1) loadCell.py
  32. @JoeKarlsson1 const spawn = require('child_process').spawn; const process = spawn('python', ['./hx711py/scale.py'],

    { detached: true }); // Takes stdout data from script which executed // with arguments and send this data to res object process.stdout.on('data', data => { let currWeight = parseFloat(data); let avgWeight = updateAverageWeight(currWeight, recentsWeights); }); process.stderr.on('data', err => { console.error(err); }); process.on('close', (code) => { console.log(`child process exited with code ${code}`); });
  33. @JoeKarlsson1 Connect our IoT app with the Node MongoDB Driver

    @JoeKarlsson1 Want $200 in free credits? http://bit.ly/NodeJSInteractive
  34. const MongoClient = require(‘mongodb').MongoClient; const uri = ‘mongodb+srv://joe:[email protected]/iot const client

    = new MongoClient(uri, { useNewUrlParser: true }); client.connect(err => { const collection = client.db(‘IoT’).collection('toilets'); // perform actions on the collection object client.close(); }); @JoeKarlsson1
  35. { "_id": { "$oid": "dskfjlk2j92121293901233" }, "timestamp_day": { "$date": {

    "$numberLong": "1573854434214" } }, "type": "cat_in_box", "cat": { "name": "BMO", "weight": "200" }, "owner": "Joe Karlsson", "events": [ ] } IoT Time Series Schema { "timestamp_event": { "$date": { "$numberLong": "1573854435016" } }, "weight": { "$numberDouble": “15.593333333" } }, { "timestamp_event": { "$date": { "$numberLong": "1573854435824" } }, "weight": { "$numberDouble": "15.132222222" } }, { "timestamp_event": { "$date": { "$numberLong": "1573854436632" } }, “type”: “maintenance” } @JoeKarlsson1
  36. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box
  37. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • GPIO
  38. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • GPIO • IoT “Hello World” var five = require("johnny-five"); var Raspi = require("raspi-io").RaspiIO; var board = new five.Board({ io: new Raspi() }); board.on("ready", function() { var led = new five.Led("P1-13"); led.blink(); });
  39. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • GPIO • IoT “Hello World” • Switches and Load cells
  40. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • GPIO • IoT “Hello World” • Switches and Load cells • Sent IoT data to the cloud
  41. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • GPIO • IoT “Hello World” • Switches and Load cells • Sent IoT data to the cloud • Next steps…
  42. @JoeKarlsson1 Agenda • Intro to IoT • Why JS &

    IoT? • IoT data best practices • IoT Kitty Litter Box • The future of IoT @JoeKarlsson1
  43. @JoeKarlsson1 Smaller and more powerful devices \ Better Hardware support

    JS with a smaller footprint Batteries will become the bottleneck The Future of JS + IoT? @JoeKarlsson1
  44. @JoeKarlsson1 IoT and JS • Intro to IoT • Anything

    with a internet enabled chip in it
  45. @JoeKarlsson1 IoT and JS • Intro to IoT • Anything

    with a internet enabled chip in it • IoT is exploding
  46. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs
  47. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs • JS is great for new devs
  48. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs • JS is great for new devs • Easy to update over a network
  49. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs • JS is great for new devs • Easy to update over a network • The internet already speaks JS
  50. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs • JS is great for new devs • Easy to update over a network • The internet already speaks JS • Tons of great libraries
  51. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • Most IoT Devs are Node devs • JS is great for new devs • Easy to update over a network • The internet already speaks JS • Tons of great libraries • JS is event driven
  52. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices
  53. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • DB needs to ingest data quickly
  54. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • DB needs to ingest data quickly • DB should be event driven
  55. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • DB needs to ingest data quickly • DB should be event driven • Flexible schema
  56. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • DB needs to ingest data quickly • DB should be event driven • Flexible schema • Time series data works best
  57. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box
  58. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • GPIO
  59. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • GPIO • IoT “Hello World” var five = require("johnny-five"); var Raspi = require("raspi-io").RaspiIO; var board = new five.Board({ io: new Raspi() }); board.on("ready", function() { var led = new five.Led("P1-13"); led.blink(); });
  60. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • GPIO • IoT “Hello World” • Switches and Load cells
  61. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • GPIO • IoT “Hello World” • Switches and Load cells • Sent IoT data to the cloud
  62. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • GPIO • IoT “Hello World” • Switches and Load cells • Sent IoT data to the cloud • Next steps…
  63. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • The future of IoT
  64. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • The future of IoT • Smaller and more powerful
  65. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • The future of IoT • Smaller and more powerful • JS with a smaller footprint
  66. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • The future of IoT • Smaller and more powerful • JS with a smaller footprint • Better hardware support
  67. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • The future of IoT • Smaller and more powerful • JS with a smaller footprint • Better hardware support • Batteries are the bottleneck
  68. @JoeKarlsson1 IoT and JS • Intro to IoT • JS

    & IoT(?!) • IoT data best practices • IoT Kitty Litter Box • The future of IoT
  69. @JoeKarlsson1 All resources and links used in
 this talk can

    be found here: @JoeKarlsson1 bit.ly/IoTandJS
  70. { name: “Joe Karlsson”, company: “MongoDB”, title: [ “Developer Advocate”,

    “Software Engineer” ], twitter: “@JoeKarlsson1”, twitch: “joe_karlsson”, website: “joekarlsson.com”, } bit.ly/IotKittyBox
  71. @JoeKarlsson1 •Source Code - http://bit.ly/IotKittyBox • IoT Reference Architecture -

    https:// www.mongodb.com/collateral/iot-reference- architecture •Time Series Data and MongoDB: Best Practices Guide - https://www.mongodb.com/ collateral/time-series-best-practices Additional Resources