Slide 1

Slide 1 text

Web Sites and Fairy Lights

Slide 2

Slide 2 text

Hi, I'm Ben Freelance web developer & other stuff Say hi on twi!er: @benjaminbenben

Slide 3

Slide 3 text

This is a talk about things:

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Some things that light up

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

A thing you can press down

Slide 8

Slide 8 text

These are: — wireless (bluetooth) — battery powered — puck.js

Slide 9

Slide 9 text

This is a talk of 4 parts

Slide 10

Slide 10 text

Part 1. Connecting this Bu!on to these Fairy Lights

Slide 11

Slide 11 text

Part 2. Connecting these Fairy Lights to a Web Server

Slide 12

Slide 12 text

Part 3. Connecting these Fairy Lights to a Web Browsers

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

PART ONE Connecting this Bu!on to these Fairy Lights

Slide 15

Slide 15 text

target outcome: When I press this thing, these things should turn red

Slide 16

Slide 16 text

Fairy ⇽!"☀$⭐⚡✨➤ Bu"on Lights

Slide 17

Slide 17 text

HTTP?

Slide 18

Slide 18 text

POST /colour HTTP/1.1 Host: fairy-lights.my-house.co.uk User-Agent: curl/7.51.0 content-type: application/json Accept: */* Cache-Control: no-cache Content-Length: 15 {"color":"red"}

Slide 19

Slide 19 text

Handling HTTP — Opening ports — Parsing request & headers — Responding to requests — Handling http codes & response headers — Persistent connections — IP address changes

Slide 20

Slide 20 text

(JSON is pre!y challenging too)

Slide 21

Slide 21 text

HTTP wasn't designed for IoT

Slide 22

Slide 22 text

HTTP

Slide 23

Slide 23 text

MQTT

Slide 24

Slide 24 text

MQTT Message Queue*

Slide 25

Slide 25 text

MQTT Telemetry Transport

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Broker Client

Slide 28

Slide 28 text

topics messages

Slide 29

Slide 29 text

Connection — Stateful — Message based — Binary

Slide 30

Slide 30 text

Publishing Messages > CONNECT < CONNACK > PUBLISH /fairy-lights/12 red

Slide 31

Slide 31 text

Subscribing to topics > CONNECT < CONNACK > SUBSCRIBE /fairy-lights/+ … … < PUBLISH /fairy-lights/12 red … … < PUBLISH /fairy-lights/15 blue …

Slide 32

Slide 32 text

Features (vs HTTP) — lightweight protocol — QoS

Slide 33

Slide 33 text

MQTT …that's about it

Slide 34

Slide 34 text

MQTT …that's about it (though also; security/encryption, client IDs, last will, persistence, ping/pong) -->

Slide 35

Slide 35 text

Demo Let's hook the bu!on and lights together

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

✨ We've connected a couple of things together

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

PART TWO Connecting Fairy Lights to a Web Server

Slide 40

Slide 40 text

target outcome: When I post to my blog, these things change colour

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

For WordPress to be a thing: 1/ Publish messages 2/ Subscribe to topics

Slide 43

Slide 43 text

1/ Publish messages

Slide 44

Slide 44 text

1/ Publish messages WP-MQTT

Slide 45

Slide 45 text

WP-MQTT se!ings

Slide 46

Slide 46 text

2/ Subscribe to topics

Slide 47

Slide 47 text

2/ Subscribe to topics MQTT-WP listen for messages then forward them on to the WP-REST API github/benfoxall/mq!-wp

Slide 48

Slide 48 text

const mqtt = require('mqtt') const client = mqtt.connect(config.mqtt_host) client.subscribe('my/sensor') client.on('message', (topic, buffer) => { const content = escape(buffer.toString()) updatePage('my-sensor', content) })

Slide 49

Slide 49 text

const WPAPI = require('wpapi') const wp = new WPAPI(config.wp) const updatePage = (slug, content) => wp.pages().slug(slug) .update({content})

Slide 50

Slide 50 text

Demo Let's get WordPress on our MQTT network

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

✨ We've made WordPress into a thing

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

PART THREE Connecting Fairy Lights to a Web Browser

Slide 55

Slide 55 text

target outcome: When I visit a web page, I can choose the colour of my lights

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

The easy way Post message to your web server…

Slide 58

Slide 58 text

These lights are right here! why control them from your web server?

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

- Advertising - Connecting - Services - Characteristics

Slide 62

Slide 62 text

Web Bluetooth navigator.bluetooth.requestDevice({ filters: [{namePrefix: 'Puck'}], optionalServices: [0xBCDE] })

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

navigator.bluetooth.requestDevice({ filters: [{namePrefix: 'Puck'}], optionalServices: [0xBCDE] }) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService(0xBCDE)) .then(service => service.getCharacteristic(0xABCD)) .then(fairyLights => { fairyLights.writeValue(Uint8ClampedArray.from([255,150,0]).buffer) })

Slide 65

Slide 65 text

Demo Change the colour of the lights with this webpage

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

We can do even be!er

Slide 68

Slide 68 text

Bonus point #1 Discovery

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

require("ble_eddystone") .advertise("goo.gl/IaXNnT")

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Bonus point #2 Offline Support

Slide 75

Slide 75 text

Service workers

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Bonus point #3 Making it feel less like a website

Slide 79

Slide 79 text

Slide 80

Slide 80 text

{ "short_name": "Lights", "name": "Fairy Lights", "icons": [ { "src": "icons/48.png", "type": "image/png", "sizes": "48x48" }, ... { "src": "icons/512.png", "type": "image/png", "sizes": "512x512" } ], "start_url": "https://benjaminbenben.com/lights/?homescreen", "background_color": "#333", "theme_color": "#000", "display": "fullscreen", "orientation": "portrait" }

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

✨ We've made a website into a thing

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Some advice to wrap up

Slide 87

Slide 87 text

What's best device to prototype all this edge web technology?

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

Focus on people then work back back from there

Slide 90

Slide 90 text

And build some things

Slide 91

Slide 91 text

Thank you for listening @benjaminbenben