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

Making Simple Things

Making Simple Things

Ben Foxall

March 09, 2017
Tweet

More Decks by Ben Foxall

Other Decks in Programming

Transcript

  1. 1. Many small (movable) devices - wires become an issue

    2. Batteries - constrains compute resource 3. Things need to be able to talk to each other
  2. Sending a post request to our LED strip? POST /led-strip

    HTTP/1.1 Host: benjaminbenben.com User-Agent: curl/7.51.0 content-type: application/json Accept: */* Cache-Control: no-cache Content-Length: 15 {"color":"red"}
  3. Handling HTTP —Opening ports —Responding to requests —Parsing request &

    headers —Understanding http codes & header fields —Chunked encoding —Persistent connections —IP address changes
  4. MQTT Broker —Could be a satellite —Could be a hub

    in a home —Could be a box sitting in a field
  5. Message structure +----------------------+ | fixed header | |----------------------| | variable

    header | |----------------------| | payload | +----------------------+
  6. Message Types CONNECT, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE,

    SUBACK, UNSUBSCRIBE, UNSUBACK, PINGREQ, PINGRESP & DISCONNECT
  7. CONNECT fixed header 7 6 5 4 3 2 1

    0 f i x e d h e a d e r MESSAGE TYPE 1 2 REMAINING LENGTH DUP RET QOS
  8. 'fourteen bytes' to connect —2 bytes - fixed header —12

    bytes - variable header —0-n bytes - flag values
  9. PUBLISH fixed header 7 6 5 4 3 2 1

    0 f i x e d h e a d e r MESSAGE TYPE 1 2 REMAINING LENGTH DUP RET QOS
  10. PUBLISH variable header 7 6 5 4 3 2 1

    0 TOPIC LENGTH / T O P I C 3 4 5 . . . . T
  11. ⨽⨽⨽⨽/button/push vs POST /button HTTP/1.1 Host: benjaminbenben.com User-Agent: curl/7.51.0 content-type:

    application/json Accept: */* Cache-Control: no-cache Content-Length: 15 {"action":"push"}
  12. SUBSCRIBE fixed header 7 6 5 4 3 2 1

    0 f i x e d h e a d e r MESSAGE TYPE 1 2 REMAINING LENGTH DUP RET QOS
  13. $socket = fsockopen('mqtt-host.com', 1883); $connect = constructConnectMessage(); fwrite($socket, $connect, strlen($connect));

    $publish = constructPublishMessage('/hello', 'world!'); fwrite($socket, $publish, strlen($connect));
  14. 7 6 5 4 3 2 1 0 f i

    x e d h e a d e r MESSAGE TYPE 1 2 REMAINING LENGTH DUP RET QOS
  15. QoS 1 At least once delivery Client Broker Client ----

    PUBLISH ----> [store] ---- PUBLISH ----> [delete] <------ PUBACK --- [delete]
  16. QoS 2 "Exactly once" Client Broker Client ----- PUBLISH ---->

    [store] ---- PUBLISH ----> <------ PUBACK ---- ------- PUBREL ---> [delete] <------ PUBCOMP --- [delete]
  17. 1/ Subscribe to MQTT topics Create an mqtt-wp bridge (A

    script that forwards on messages) github/benfoxall/mqtt-wp
  18. const WPAPI = require('wpapi') const wp = new WPAPI(config.wp) const

    update = (slug, content) => wp.pages().slug(slug) .update({content}) // update('my-thing', 'Sensor value: 4')
  19. 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()) update('my-sensor', content) })
  20. Option 1: Via WP-MQTT ➡ Nice and easy Option 2:

    Via Webhooks ➡ Better fit for a wp-bridge model
  21. ~

  22. More useful things with this —Displaying sensor content —weather sensors

    —door opening counters —whatever you can build
  23. ~

  24. ~

  25. Have a clear idea of what your thing is Build

    that thing as simply as you can
  26. MQTT over WebSockets Our device becomes another thing on the

    network const mqt = new MQT('test.mosquitto.org:8080') mqt.subscribe('/sensor/value' (value) => document.querySelector('#sensor').textContent = value ) mqt.publish('/phone/visit', document.location.pathname)
  27. Orientation & movement document.addEventListener('deviceorientation', ({alpha, beta, gamma}) => {} )

    document.addEventListener('devicemotion', ({acceleration}) => {} )
  28. Sounds var ctx = new AudioContext() var osc = ctx.createOscillator()

    osc.connect(ctx.destination) osc.frequency.value = 300 osc.start()