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

WDCNZ 2015 - Automating the Real World

WDCNZ 2015 - Automating the Real World

Whether it's a complex sensor network or an app that turns on your coffee machine, devices connected to the Internet are becoming more and more diverse. With simple connected hardware using an increasing percentage of web traffic, where does that leave the web developer? Are we still as relevant in an industry full of hardware without screens? In this talk James will demonstrate how a world full of connected hardware can be controlled using the same tools we use every day. James will argue that web developers already have the skills and know-how to create hardware both for ourselves and for customers. As we go from writing the code to deploying to a server, everything should seem pleasantly familiar...

James Macfie

July 23, 2015
Tweet

More Decks by James Macfie

Other Decks in Technology

Transcript

  1. The web isn’t just sites and apps #wdcnz 50 billion

    connected devices by 2020 There’s currently ~18 billion Token exponential graph for when talking about IoT
  2. #wdcnz If you can build something useful for the web,

    you can build something useful for the real world.
  3. Let’s fix a real world problem #wdcnz Turning on my

    laundry lights is a real pain in the ass Also, my house is weird and ‘just moving the lightswitch’ would be super expensive
  4. How we’re going to do this #wdcnz Simple three part

    architecture: - an app that acts as a message broker - a IR motion detector - a relay + the lamp
  5. THE MESSAGE BROKER #wdcnz It’s written in Node, but doesn’t

    have to be Uses Websockets to transfer messages Will route all messages sent from one client to all other clients app
  6. The broker $ n p m i n i t

    - y $ n p m i n s t a l l n o d e j s - w e b s o c k e t — s a v e
  7. The broker v a r w s = r e

    q u i r e ( ' n o d e j s - w e b s o c k e t ' ) ; 
 
 w s . c r e a t e S e r v e r ( f u n c t i o n ( c o n n e c t i o n ) { 
 c o n s o l e . l o g ( ' S w e e t , w e h a v e a c o n n e c t i o n . ' , c o n n e c t i o n ) ; 
 
 c o n n e c t i o n . o n ( ' t e x t ' , f u n c t i o n ( s t r ) { } ) ; 
 } ) . l i s t e n ( 3 0 0 0 ) ; 
 
 c o n s o l e . l o g ( ' L i s t e n i n g . . . ' ) ;
  8. The broker v a r w s = r e

    q u i r e ( ' n o d e j s - w e b s o c k e t ' ) ; 
 v a r c o n n e c t i o n s = [ ] ; 
 
 w s . c r e a t e S e r v e r ( f u n c t i o n ( c o n n e c t i o n ) { 
 c o n s o l e . l o g ( ' S w e e t , w e h a v e a c o n n e c t i o n . ' ) ; 
 
 c o n n e c t i o n s . p u s h ( c o n n e c t i o n ) ; 
 
 c o n n e c t i o n . o n ( ' t e x t ' , f u n c t i o n ( s t r ) { 
 c o n s o l e . l o g ( ' R e c e i v e d ' , s t r ) ; 
 
 c o n n e c t i o n s . f o r E a c h ( f u n c t i o n ( c ) { 
 i f ( c o n n e c t i o n ! = = c ) { 
 c . s e n d T e x t ( s t r ) ; 
 } e l s e { 
 c o n n e c t i o n . s e n d T e x t ( ' T h a n k s ' ) ; 
 } 
 } ) ; 
 } ) ; 
 } ) . l i s t e n ( 3 0 0 0 ) ; 
 
 c o n s o l e . l o g ( ‘ L i s t e n i n g . . . ' ) ;
  9. TURNing on the lamp #wdcnz We’ll be using a Tessel

    - runs Node, Python or Rust - untethered - wifi - modular hardware components
  10. TURNing on the lamp #wdcnz Turning on/of appliances requires a

    relay The Tessel has a module that makes this easy for us We’re dealing with mains power. Be careful!
  11. Turning on the lamp $ n p m i n

    s t a l l t e s s e l - g $ n p m i n i t - y 
 $ n p m i n s t a l l r e l a y - m o n o — s a v e $ n p m i n s t a l l n o d e j s - w e b s o c k e t — s a v e
  12. Turning on the lamp v a r t e s

    s e l = r e q u i r e ( ' t e s s e l ' ) ; 
 v a r r e l a y l i b = r e q u i r e ( ' r e l a y - m o n o ' ) ; 
 v a r r e l a y = r e l a y l i b . u s e ( t e s s e l . p o r t [ ' A ' ] ) ; 
 
 r e l a y . o n ( ' r e a d y ' , f u n c t i o n ( ) { 
 s e t I n t e r v a l ( f u n c t i o n ( ) { 
 r e l a y . t o g g l e ( 2 ) ; 
 } , 2 0 0 0 ) ; 
 } ) ;
  13. Turning on the lamp v a r t e s

    s e l = r e q u i r e ( ' t e s s e l ' ) ; v a r r e l a y l i b = r e q u i r e ( ' r e l a y - m o n o ' ) ; v a r r e l a y = r e l a y l i b . u s e ( t e s s e l . p o r t [ ' A ' ] ) ; v a r w s = r e q u i r e ( ' n o d e j s - w e b s o c k e t ' ) ; v a r c l i e n t = w s . c o n n e c t ( ' w s : / / 1 9 2 . 1 6 8 . 4 3 . 2 3 6 : 3 0 0 0 ' ) ; r e l a y . o n ( ' r e a d y ' , f u n c t i o n ( ) { c l i e n t . o n ( ' t e x t ' , f u n c t i o n ( m e s s a g e ) { i f ( m e s s a g e = = = ' o n ' ) { r e l a y . t u r n O n ( 2 ) ; } e l s e i f ( m e s s a g e = = = ' o f f ' ) { r e l a y . t u r n O f f ( 2 ) ; } } ) ; } ) ;
  14. Using motion detection #wdcnz Using a tethered Arduino and the

    Johnny-5 library I would have used another Tessel but I only had one on hand The setup is a little more involved. I won’t go through it; there’s plenty of tutorials on the internet
  15. Using motion detection $ n p m i n i

    t - y 
 $ n p m i n s t a l l j o h n n y - f i v e - - s a v e 
 $ n p m i n s t a l l n o d e j s - w e b s o c k e t - - s a v e
  16. Using motion detection v a r f i v e

    = r e q u i r e ( ' j o h n n y - f i v e ' ) ; 
 v a r w s = r e q u i r e ( ' n o d e j s - w e b s o c k e t ' ) ; 
 
 v a r b o a r d = n e w f i v e . B o a r d ( ) ; 
 
 b o a r d . o n ( ' r e a d y ' , f u n c t i o n ( ) { 
 v a r m o t i o n = n e w f i v e . M o t i o n ( 7 ) ; 
 v a r c l i e n t = w s . c o n n e c t ( ' w s : / / 1 9 2 . 1 6 8 . 4 3 . 2 3 6 : 3 0 0 0 ' ) ; 
 
 c l i e n t . o n ( ' c o n n e c t ' , f u n c t i o n ( ) { 
 m o t i o n . o n ( ' m o t i o n s t a r t ' , f u n c t i o n ( ) { 
 c l i e n t . s e n d T e x t ( ' o n ' ) ; 
 } ) ; 
 
 m o t i o n . o n ( ' m o t i o n e n d ' , f u n c t i o n ( ) { 
 c l i e n t . s e n d T e x t ( ' o f f ' ) ; 
 } ) ; 
 } ) ; 
 } ) ;
  17. #wdcnz we don’t have to just build apps for devices.

    we can build the devices themselves