Slide 1

Slide 1 text

@JoeKarlsson1 IoT and JS(?!) A Gentle Introduction to the Internet of Things @JoeKarlsson1

Slide 2

Slide 2 text

{ name: “Joe Karlsson”, company: “MongoDB”, title: [ “Developer Advocate”, “Software Engineer” ], twitter: “@JoeKarlsson1”, twitch: “joe_karlsson”, website: “joekarlsson.com”, }

Slide 3

Slide 3 text

@JoeKarlsson1 Before we get started… @JoeKarlsson1

Slide 4

Slide 4 text

@JoeKarlsson1 Thank you for coming! @JoeKarlsson1

Slide 5

Slide 5 text

@JoeKarlsson1 MagicMirror^2 @JoeKarlsson1

Slide 6

Slide 6 text

@JoeKarlsson1 Moodlite @JoeKarlsson1

Slide 7

Slide 7 text

@JoeKarlsson1 How is this going to work? @JoeKarlsson1

Slide 8

Slide 8 text

@JoeKarlsson1 A Quick Audience Poll @JoeKarlsson1

Slide 9

Slide 9 text

@JoeKarlsson1 IoT? @JoeKarlsson1

Slide 10

Slide 10 text

@JoeKarlsson1 JS? @JoeKarlsson1

Slide 11

Slide 11 text

@JoeKarlsson1 Agenda • Intro to IoT • IoT data cycle • Why JS and IoT? • IoT data best practices • IoT Demo • The future of IoT @JoeKarlsson1

Slide 12

Slide 12 text

@JoeKarlsson1 What the heck is IoT? @JoeKarlsson1

Slide 13

Slide 13 text

@JoeKarlsson1 A network of devices that have an IP address and can communicate with each other over the internet @JoeKarlsson1

Slide 14

Slide 14 text

@JoeKarlsson1 It’s just the internet, but with “things” on it @JoeKarlsson1

Slide 15

Slide 15 text

@JoeKarlsson1 …so what is a “thing?” @JoeKarlsson1

Slide 16

Slide 16 text

@JoeKarlsson1 @JoeKarlsson1

Slide 17

Slide 17 text

@JoeKarlsson1 Smart Home Home Security Smart Appliances Smart Light Bulbs Smart Outlets Nest Thermostat @JoeKarlsson1

Slide 18

Slide 18 text

@JoeKarlsson1 Automated Manufacturing Humidity Sensor Motion Sensor Remote Monitoring Smart Delivery Temperature Sensor @JoeKarlsson1

Slide 19

Slide 19 text

@JoeKarlsson1 Wearables Smart Water Bottle Fitbit Breathing Sensor Smart Running Shoes Lumo Run Shorts @JoeKarlsson1

Slide 20

Slide 20 text

@JoeKarlsson1 @JoeKarlsson1

Slide 21

Slide 21 text

@JoeKarlsson1

Slide 22

Slide 22 text

@JoeKarlsson1 IoT
 Lifecycle Collect Communicate Analyze Act @JoeKarlsson1

Slide 23

Slide 23 text

@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

Slide 24

Slide 24 text

@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

Slide 25

Slide 25 text

@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

Slide 26

Slide 26 text

@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

Slide 27

Slide 27 text

@JoeKarlsson1 Why does IoT Matter? Source: https://www.statista.com/statistics/471264/iot-number-of-connected-devices-worldwide/

Slide 28

Slide 28 text

@JoeKarlsson1 IoT + JS(?!) @JoeKarlsson1

Slide 29

Slide 29 text

@JoeKarlsson1 JS is a great choice for new devs Other low level programming languages can be difficult to develop @JoeKarlsson1

Slide 30

Slide 30 text

@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

Slide 31

Slide 31 text

@JoeKarlsson1 Easy to update over a network @JoeKarlsson1

Slide 32

Slide 32 text

@JoeKarlsson1 The internet already speaks JS @JoeKarlsson1

Slide 33

Slide 33 text

@JoeKarlsson1 Tons of existing libraries, plugins and APIs @JoeKarlsson1

Slide 34

Slide 34 text

@JoeKarlsson1 Cylon.js Johnny-Five https://cylonjs.com/ http://johnny-five.io/ @JoeKarlsson1

Slide 35

Slide 35 text

@JoeKarlsson1 JS is great at handling event-driven apps @JoeKarlsson1

Slide 36

Slide 36 text

@JoeKarlsson1 @JoeKarlsson1

Slide 37

Slide 37 text

@JoeKarlsson1 @JoeKarlsson1

Slide 38

Slide 38 text

@JoeKarlsson1 It got dark in here @JoeKarlsson1

Slide 39

Slide 39 text

@JoeKarlsson1 Can you turn yourself on? @JoeKarlsson1

Slide 40

Slide 40 text

@JoeKarlsson1 I turned myself on @JoeKarlsson1

Slide 41

Slide 41 text

@JoeKarlsson1 Events reduce the cost of listening for the device @JoeKarlsson1

Slide 42

Slide 42 text

@JoeKarlsson1 But what do we do with all that data? @JoeKarlsson1

Slide 43

Slide 43 text

@JoeKarlsson1 I’m totally biased, but… @JoeKarlsson1

Slide 44

Slide 44 text

@JoeKarlsson1 MongoDB is a great choice @JoeKarlsson1

Slide 45

Slide 45 text

@JoeKarlsson1 • Capable of high speed data ingest • An event driven DB • Makes it easy to update your schema • Great at handling time series data @JoeKarlsson1 MongoDB is:

Slide 46

Slide 46 text

@JoeKarlsson1 Let’s jump into something *practical @JoeKarlsson1

Slide 47

Slide 47 text

@JoeKarlsson1 @JoeKarlsson1

Slide 48

Slide 48 text

@JoeKarlsson1 OK, so it’s still a WIP, but let’s talk about it @JoeKarlsson1

Slide 49

Slide 49 text

@JoeKarlsson1 Want to watch me make it? https://www.twitch.tv/joe_karlsson @JoeKarlsson1

Slide 50

Slide 50 text

@JoeKarlsson1 @JoeKarlsson1

Slide 51

Slide 51 text

@JoeKarlsson1 1 x Raspberry Pi 3 Model B 1 x Breadboard 1 x HX711 Module [Load cell amplifier] What Materials Do We Need? 4 x 50 kg load cells [used to measure the weight] 1 x Magnetic door sensor 1 x Kitty Litter Box

Slide 52

Slide 52 text

@JoeKarlsson1 @JoeKarlsson1 @JoeKarlsson1

Slide 53

Slide 53 text

@JoeKarlsson1 @JoeKarlsson1

Slide 54

Slide 54 text

@JoeKarlsson1 Raspberry Pi @JoeKarlsson1

Slide 55

Slide 55 text

@JoeKarlsson1 Small and amazingly capable, credit card size $35 computer @JoeKarlsson1

Slide 56

Slide 56 text

@JoeKarlsson1 Install Node on your Pi Find tips here: https://www.losant.com/blog/how-to-install-nodejs-on-raspberry-pi @JoeKarlsson1

Slide 57

Slide 57 text

@JoeKarlsson1 General Purpose Input/Output Pins [GPIO] Can be used to interact in with the real world @JoeKarlsson1

Slide 58

Slide 58 text

@JoeKarlsson1 @JoeKarlsson1

Slide 59

Slide 59 text

@JoeKarlsson1 Output Pins Switches that the Raspberry Pi can turn on or off @JoeKarlsson1

Slide 60

Slide 60 text

@JoeKarlsson1 Input Pins Switches that you can turn on or off from the outside world @JoeKarlsson1

Slide 61

Slide 61 text

@JoeKarlsson1 Great Overview of the GPIO Pins on Wikipedia @JoeKarlsson1 Source: https://en.wikipedia.org/wiki/Raspberry_Pi#General_purpose_input-output_(GPIO)_connector

Slide 62

Slide 62 text

@JoeKarlsson1 @JoeKarlsson1

Slide 63

Slide 63 text

@JoeKarlsson1 Breadboard A breadboard is used for prototyping electronics @JoeKarlsson1

Slide 64

Slide 64 text

@JoeKarlsson1 @JoeKarlsson1

Slide 65

Slide 65 text

@JoeKarlsson1 @JoeKarlsson1

Slide 66

Slide 66 text

@JoeKarlsson1 @JoeKarlsson1

Slide 67

Slide 67 text

@JoeKarlsson1 Building a circuit

Slide 68

Slide 68 text

@JoeKarlsson1 @JoeKarlsson1 Code: https://github.com/JoeKarlsson/iot-kitty-litter-box

Slide 69

Slide 69 text

@JoeKarlsson1 @JoeKarlsson1 The first step of any IoT project is to make an LED blink

Slide 70

Slide 70 text

@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(); });

Slide 71

Slide 71 text

@JoeKarlsson1 Alright, let’s make a toilet @JoeKarlsson1

Slide 72

Slide 72 text

@JoeKarlsson1 • When box is opened, go into maintenance mode.
 • When the box is closed, recalibrate weight.
 • When significant weight is sensed, take weight measurement of cat and send data to MongoDB Atlas. @JoeKarlsson1

Slide 73

Slide 73 text

@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; }); });

Slide 74

Slide 74 text

@JoeKarlsson1 The load cells… @JoeKarlsson1

Slide 75

Slide 75 text

@JoeKarlsson1 Couldn’t find support for my load cells with Johnny-Five, so I had to get creative. @JoeKarlsson1

Slide 76

Slide 76 text

@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

Slide 77

Slide 77 text

@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}`); });

Slide 78

Slide 78 text

@JoeKarlsson1 process.stdout.on('data', data => { let currWeight = parseFloat(data); let avgWeight = updateAverageWeight(currWeight, recentsWeights); }); const checkIfCatIsPresent = (avgWeight, baseBoxWeight) => { if (avgWeight - bufferWeight > baseBoxWeight + config.cat.weight) { return true; } return false; }; const updateAverageWeight = (currWeight, recentsWeights) => { if (currWeight) { const maxArrLength = 20; if (recentsWeights.length >= maxArrLength) { recentsWeights.shift(); } recentsWeights.push(currWeight); return getAvg(recentsWeights); } }; const getAvg = arr => { if (arr.length) { const sum = arr.reduce(function(a, b) { return a + b; }, 0); const avg = sum / arr.length; return avg; } return arr[0]; }; const catPresent = checkIfCatIsPresent(avgWeight, baseBoxWeight); if (catPresent) { // Send data to MongoDB }

Slide 79

Slide 79 text

@JoeKarlsson1 Connecting to the MongoDB Atlas Cluster @JoeKarlsson1 Want $200 in free credits? http://bit.ly/grizzhacks

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

@JoeKarlsson1 Connect our IoT app with the Node MongoDB Driver @JoeKarlsson1 Want $200 in free credits? http://bit.ly/grizzhacks

Slide 83

Slide 83 text

const MongoClient = require(‘mongodb').MongoClient; const uri = ‘mongodb+srv://kay:[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(); });

Slide 84

Slide 84 text

@JoeKarlsson1 • Smooth out calibration • Hook up Stitch functions to send out weekly email report • Make web app that visualizes my cats bathroom habits/weight with MongoDB Charts @JoeKarlsson1

Slide 85

Slide 85 text

@JoeKarlsson1 @JoeKarlsson1

Slide 86

Slide 86 text

@JoeKarlsson1 1 x Raspberry Pi 3 Model B 1 x Breadboard 1 x 68 Ohms Resistor What Materials Do We Need? 1 x LED 2 x Female to Male Wires

Slide 87

Slide 87 text

@JoeKarlsson1 Raspberry Pi @JoeKarlsson1

Slide 88

Slide 88 text

@JoeKarlsson1 Small and amazingly capable, credit card size $35 computer @JoeKarlsson1

Slide 89

Slide 89 text

@JoeKarlsson1 Install Node on your Pi Find tips here: https://www.losant.com/blog/how-to-install-nodejs-on-raspberry-pi @JoeKarlsson1

Slide 90

Slide 90 text

@JoeKarlsson1 @JoeKarlsson1

Slide 91

Slide 91 text

@JoeKarlsson1 General Purpose Input/Output Pins [GPIO] Can be used to interact in with the real world @JoeKarlsson1

Slide 92

Slide 92 text

@JoeKarlsson1 @JoeKarlsson1

Slide 93

Slide 93 text

@JoeKarlsson1 Output Pins Switches that the Raspberry Pi can turn on or off @JoeKarlsson1

Slide 94

Slide 94 text

@JoeKarlsson1 Input Pins Switches that you can turn on or off from the outside world @JoeKarlsson1

Slide 95

Slide 95 text

@JoeKarlsson1 Great Overview of the GPIO Pins on Wikipedia @JoeKarlsson1 Source: https://en.wikipedia.org/wiki/Raspberry_Pi#General_purpose_input-output_(GPIO)_connector

Slide 96

Slide 96 text

@JoeKarlsson1 @JoeKarlsson1

Slide 97

Slide 97 text

@JoeKarlsson1 onoff npm package for interacting with GPIO @JoeKarlsson1

Slide 98

Slide 98 text

@JoeKarlsson1 Breadboard A breadboard is used for prototyping electronics @JoeKarlsson1

Slide 99

Slide 99 text

@JoeKarlsson1 @JoeKarlsson1

Slide 100

Slide 100 text

@JoeKarlsson1 @JoeKarlsson1

Slide 101

Slide 101 text

@JoeKarlsson1 @JoeKarlsson1 Code: https://github.com/JoeKarlsson/IoT-Morse-Code-Demo

Slide 102

Slide 102 text

@JoeKarlsson1 @JoeKarlsson1

Slide 103

Slide 103 text

@JoeKarlsson1 LED Resistors Resistors are used to reduce current, adjust signal levels, etc. A diode that emits light when a voltage is applied to it. @JoeKarlsson1

Slide 104

Slide 104 text

@JoeKarlsson1 @JoeKarlsson1

Slide 105

Slide 105 text

@JoeKarlsson1 @JoeKarlsson1

Slide 106

Slide 106 text

@JoeKarlsson1 Building a circuit @JoeKarlsson1

Slide 107

Slide 107 text

@JoeKarlsson1 @JoeKarlsson1 Code: https://github.com/JoeKarlsson/IoT-Morse-Code-Demo

Slide 108

Slide 108 text

@JoeKarlsson1 @JoeKarlsson1

Slide 109

Slide 109 text

@JoeKarlsson1 @JoeKarlsson1

Slide 110

Slide 110 text

@JoeKarlsson1 @JoeKarlsson1

Slide 111

Slide 111 text

@JoeKarlsson1 @JoeKarlsson1

Slide 112

Slide 112 text

@JoeKarlsson1 IoT Morse Code Machine

Slide 113

Slide 113 text

@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

Slide 114

Slide 114 text

@JoeKarlsson1 Do you want to be an IoT and JS master? @JoeKarlsson1

Slide 115

Slide 115 text

@JoeKarlsson1 Just do it! @JoeKarlsson1

Slide 116

Slide 116 text

@JoeKarlsson1 IoT is exploding Raspberry Pi’s use GPIO to interact with the real world JS is a great choice for IoT Easy to update apps Internet already speaks JS Great supporting libraries JS excels at event-driven apps Start with a small project and jump in and learn as you go! MongoDB is a great choice for IoT projects

Slide 117

Slide 117 text

@JoeKarlsson1 Questions? @JoeKarlsson1

Slide 118

Slide 118 text

@JoeKarlsson1 Thank You! @JoeKarlsson1

Slide 119

Slide 119 text

{ name: “Joe Karlsson”, company: “MongoDB”, title: [ “Developer Advocate”, “Software Engineer” ], twitter: “@JoeKarlsson1”, twitch: “joe_karlsson”, website: “joekarlsson.com”, }

Slide 120

Slide 120 text

MongoDB Community • MongoDB University: • university.mongodb.com • MongoDB Community Slack Channel: • https://launchpass.com/mongo-db @JoeKarlsson1

Slide 121

Slide 121 text

@JoeKarlsson1 •$200 in free Atlas credits: http://bit.ly/nyccodecamp2019 •Code - https://github.com/JoeKarlsson/IoT-Morse- Code-Demo • 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
 •A language for the Internet: Why JavaScript and Node.js is right for Internet Applications - Tom Hughes-Croucher Additional Resources

Slide 122

Slide 122 text

@JoeKarlsson1