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

Domotica with Node.js

Kevin
March 04, 2014

Domotica with Node.js

Using the node-rfxcom JavaScript library with an RFXtrx433 transceiver to monitor and control your home.

Kevin

March 04, 2014
Tweet

More Decks by Kevin

Other Decks in Technology

Transcript

  1. NODE.JS Uses the v8 JavaScript engine from Chrome. Just like

    in the browser, fully asynchronous. Used by tools like Grunt.js, Bower etc.
  2. RFXTRX433 USB RADIO TRANSCEIVER Receives and transmits in the 433.92Mhz

    range. Translates the received transmissions into bytes and sends them down the USB wire.
  3. SOFTWARE SUPPORT Supported by a wide range of home-automation software

    - not all Open Source projects Domoticz, DomotiGa, Domogik, Ed-win, EventGhost, FHEM, HomeAutom8, Homeseer HS2 and HS3, HomiDom, HouseAgent, Indigo, MeteoHub, OpenHAB, Open Source Automation, Beyond Measure, Digital Home Server, Heyu, Mydombox.com, VERA
  4. HARDWARE SUPPORT Supports a huge range of "popular" hardware Power

    consumption - OWL devices PIR Motion Detector - Marmitek MS90 Lighting control - LightwaveRF as seen in B&Q includes a switchable relay Curtain control Weather sensing - including Oregon Scientific Power socket control - LightwaveRF and others
  5. PROJECT Started in September 2012 to scratch an itch Current

    release is 0.7.8 Around 2500 lines of JavaScript including tests Runs on Raspberry Pi amongst other things Used in https://github.com/bigkevmcd/node-rfxcom The Thing System
  6. LET'S SEE SOME JAVASCRIPT v a r r f x

    c o m = r e q u i r e ( ' r f x c o m ' ) ; v a r r f x t r x = n e w r f x c o m . R f x C o m ( ' / d e v / t t y U S B 0 ' ) , l i g h t w a v e r f = n e w r f x c o m . L i g h t i n g 5 ( r f x t r x , r f x c o m . l i g h t i n g 5 . L I G H T W A V E R F ) ; r f x t r x . o n ( ' s e c u r i t y 1 ' , f u n c t i o n ( e v t ) { i f ( e v t . d e v i c e S t a t u s = = = r f x c o m . s e c u r i t y . M O T I O N ) { l i g h t w a v e r f . s w i t c h O n ( ' 0 x F 0 9 A C 8 / 1 ' ) ; } e l s e i f ( e v t . d e v i c e S t a t u s = = = r f x c o m . s e c u r i t y . N O M O T I O N ) { l i g h t w a v e r f . s w i t c h O f f ( ' 0 x F 0 9 A C 8 / 1 ' ) ; } } ) ; r f x t r x . i n i t i a l i s e ( f u n c t i o n ( ) { c o n s o l e . l o g ( ' D e v i c e i n i t i a l i s e d ' ) ; } ) ;
  7. PROTOCOL S e n d a 1 4 b y

    t e I n t e r f a c e c o m m a n d – R e s e t p a c k e t ( h e x 0 D 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ) T h e R F X C O M w i l l n o w s t o p t h e R F r e c e i v e f o r 1 0 s e c o n d s . T h i s p e r i o d i s t e r m i n a t e d b y s e n d i n g a S t a t u s R e q u e s t . W a i t a t l e a s t 5 0 m i l l i s e c o n d s ( m a x 9 s e c o n d s ) t h e n c l e a r t h e C O M p o r t r e c e i v e b u f f e r s . S e n d a 1 4 b y t e I n t e r f a c e c o m m a n d – G e t S t a t u s p a c k e t ( h e x 0 D 0 0 0 0 0 1 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ) T h e R F X C O M w i l l r e s p o n d w i t h t h e s t a t u s a n d t h e 1 0 s e c o n d s r e s e t t i m e o u t i s t e r m i n a t e d . I f n e c e s s a r y s e n d a s e l e c t f r e q u e n c y s e l e c t i o n c o m m a n d . T h e 4 3 3 . 9 2 M H z t r a n s c e i v e r d o e s n o t h a v e a f r e q u e n c y s e l e c t a n d o p e r a t e s a l w a y s o n 4 3 3 . 9 2 M H z . T h e R F X t r x i s n o w r e a d y t o r e c e i v e R F d a t a a n d t o r e c e i v e c o m m a n d s f r o m t h e a p p l i c a t i o n f o r t h e t r a n s m i t t e r .
  8. / / T h i s i s a b

    u f f e r i n g p a r s e r w h i c h a c c u m u l a t e s b y t e s u n t i l i t r e c e i v e s t h e / / n u m b e r o f b y t e s s p e c i f i e d i n t h e f i r s t b y t e o f t h e m e s s a g e . / / I t r e l i e s o n a f l u s h e d b u f f e r , t o e n s u r e t h e f i r s t b y t e c o r r e s p o n d s t o t h e / / s i z e o f t h e f i r s t m e s s a g e . / / T h e ' d a t a ' m e s s a g e e m i t t e d h a s a l l t h e b y t e s f r o m t h e m e s s a g e . s e l f . r f x t r x P a r s e r = f u n c t i o n ( ) { v a r d a t a = [ ] , r e q u i r e d B y t e s = 0 ; r e t u r n f u n c t i o n ( e m i t t e r , b u f f e r ) { / / C o l l e c t d a t a d a t a . p u s h . a p p l y ( d a t a , b u f f e r ) ; i f ( r e q u i r e d B y t e s = = = 0 ) { r e q u i r e d B y t e s = d a t a [ 0 ] + 1 ; } i f ( d a t a . l e n g t h > = r e q u i r e d B y t e s ) { e m i t t e r . e m i t ( " d a t a " , d a t a . s l i c e ( 0 , r e q u i r e d B y t e s ) ) ; d a t a = d a t a . s l i c e ( r e q u i r e d B y t e s ) ; r e q u i r e d B y t e s = 0 ; } } ; } ;
  9. R f x C o m . p r o

    t o t y p e . s e c u r i t y 1 H a n d l e r = f u n c t i o n ( d a t a ) { v a r s e l f = t h i s , s u b t y p e = d a t a [ 0 ] , s e q n b r = d a t a [ 1 ] , i d = " 0 x " + s e l f . d u m p H e x ( d a t a . s l i c e ( 2 , 5 ) , f a l s e ) . j o i n ( " " ) , d e v i c e S t a t u s = d a t a [ 5 ] & ~ 0 x 8 0 , b a t t e r y S i g n a l L e v e l = d a t a [ 6 ] , e v t = { s u b t y p e : s u b t y p e , i d : i d , d e v i c e S t a t u s : d e v i c e S t a t u s , b a t t e r y L e v e l : b a t t e r y S i g n a l L e v e l & 0 x 0 f , r s s i : b a t t e r y S i g n a l L e v e l > > 4 , t a m p e r e d : d a t a [ 5 ] & 0 x 8 0 } ; s e l f . e m i t ( " s e c u r i t y 1 " , e v t ) ; } ;
  10. TEMPERATURE r f x t r x . o n

    ( ' t h 3 ' , f u n c t i o n ( e v t ) { / / O r e g o n S c i e n t i f i c T e m p e r a t u r e a n d H u m i d i t y s u b t y p e : 0 x 0 3 , / / D i f f e r e n t m o d e l s i n d i c a t e d . i d : ' 0 x F E 0 1 ' , / / U n i q u e p e r d e v i c e s e q n b r : 0 x 0 9 , / / m e s s a g e i d g e n e r a t e d b y d e v i c e ( o r s e n t t o d e v i c e ) m o d 2 5 6 t e m p e r a t u r e : 0 x 1 4 , / / D e g r e e s C e l s i u s h u m i d i t y : 0 x 3 2 , / / R e l a t i v e H u m i d i t y % h u m i d i t y S t a t u s : 0 x 0 3 , / / N o r m a l , C o m f o r t , D r y , W e t b a t t e r y L e v e l : 9 , / / 0 - 9 s t r e n g t h o f b a t t e r y r s s i : 4 , / / S i g n a l s t r e n g t h } ) ;
  11. POWER r f x t r x . o n

    ( ' e l e c 2 ' , f u n c t i o n ( e v t ) { / / O W L E l e c t r i c i t y M o n i t o r i n g h a r d w a r e s u b t y p e : 0 x 0 1 , / / C M 1 1 9 / C M 1 6 0 c o m m o n l y a v a i l a b l e i n t h e U K i d : ' 0 x F E 0 2 ' , / / U n i q u e p e r d e v i c e s e q n b r : 0 x 0 A , / / m e s s a g e i d g e n e r a t e d b y d e v i c e ( o r s e n t t o d e v i c e ) m o d 2 5 6 c u r r e n t W a t t s : 3 7 0 , / / C u r r e n t l y d e t e c t e d e n e r g y c o n s u m p t i o n t o t a l W a t t s : 3 0 2 2 5 . 8 2 , / / T o t a l m e a s u r e d b y d e v i c e b a t t e r y L e v e l : 9 , / / 0 - 9 s t r e n g t h o f b a t t e r y r s s i : 4 , / / S i g n a l s t r e n g t h } ) ;
  12. SECURITY r f x t r x . o n

    ( ' s e c u r i t y 1 ' , f u n c t i o n ( e v t ) { / / X 1 0 , K D 1 0 1 , V i s o n i c , M e i a n t e c h , S A 3 0 P I R s a n d W i n d o w S e n s o r s s u b t y p e : 0 x 0 1 , / / X 1 0 S e c u r i t y h a r d w a r e i d : ' 0 x F E 0 2 0 4 ' , / / U n i q u e p e r d e v i c e s e q n b r : 0 x 0 B , / / m e s s a g e i d g e n e r a t e d b y d e v i c e ( o r s e n t t o d e v i c e ) m o d 2 5 6 d e v i c e S t a t u s : 0 x 0 4 , / / X 1 0 s e c u r i t y s e n s o r m o t i o n d e t e c t e d t a m p e r e d : 0 , / / T a m p e r d e t e c t i o n o n d e v i c e s b a t t e r y L e v e l : 9 , / / 0 - 9 s t r e n g t h o f b a t t e r y r s s i : 4 , / / S i g n a l s t r e n g t h } ) ;
  13. LIGHTS PLEASE! v a r r f x t r

    x = n e w r f x c o m . R f x C o m ( ' / d e v / t t y U S B 0 ' ) ; v a r l i g h t i n g 5 = n e w r f x c o m . L i g h t i n g 5 ( r f x t r x , r f x c o m . l i g h t i n g 5 . L I G H T W A V E R F ) ; l i g h t i n g 5 . s w i t c h O n ( ' 0 x F 0 9 A C A / 0 1 ' ) ; r f x t r x . d e l a y ( 3 0 0 0 ) ; / / S l e e p f o r 3 s e c o n d s l i g h t i n g 5 . s w i t c h O f f ( ' 0 x F 0 9 A C A / 0 1 ' ) ; r f x t r x . d e l a y ( 3 0 0 0 ) ; / / S l e e p f o r 3 s e c o n d s l i g h t i n g 5 . s w i t c h O n ( ' 0 x F 0 9 A C A / 0 1 ' , { l e v e l : 6 } ) ;
  14. CURTAIN CONTROL v a r r f x t r

    x = n e w r f x c o m . R f x C o m ( ' / d e v / t t y U S B 0 ' ) ; v a r c u r t a i n 1 = n e w r f x c o m . C u r t a i n 1 ( r f x t r x ) ; c u r t a i n 1 . o p e n ( ' 0 x 4 1 / 0 1 ' ) ; r f x t r x . d e l a y ( 3 0 0 0 ) ; / / S l e e p f o r 3 s e c o n d s c u r t a i n 1 . s t o p ( ' 0 x 4 1 / 0 1 ' ) ; r f x t r x . d e l a y ( 3 0 0 0 ) ; / / S l e e p f o r 3 s e c o n d s c u r t a i n 1 . c l o s e ( ' 0 x 4 1 / 0 1 ' ) ;
  15. DOORBELL v a r r f x t r x

    = n e w r f x c o m . R f x C o m ( ' / d e v / t t y U S B 0 ' ) ; v a r l i g h t i n g 1 = n e w r f x c o m . L i g h t i n g 1 ( r f x t r x , r f x c o m . l i g h t i n g 1 . A R C ) ; l i g h t i n g 1 . c h i m e ( ' C 1 4 ' ) ;
  16. INTEGRATION WITH NODE-CRON v a r S u n C

    a l c = r e q u i r e ( ' s u n c a l c ' ) , m o m e n t = r e q u i r e ( ' m o m e n t ' ) ; v a r t u r n L i g h t s F i f t y P e r c e n t = f u n c t i o n ( ) { v a r t i m e s = S u n C a l c . g e t T i m e s ( m o m e n t ( ) , 5 5 . 8 5 8 , - 4 . 2 5 9 ) ; i f ( t i m e s . s u n r i s e > n e w D a t e ( ) ) { l i g h t w a v e . s w i t c h O n ( ' 0 x F A C 2 7 1 / 1 ' , { l e v e l : 0 x 1 0 } ) ; } } ; v a r j o b = n e w c r o n J o b ( ' 0 0 5 0 0 6 * * 1 - 5 ' , t u r n L i g h t s F i f t y P e r c e n t , n u l l , t r u e ) ; v a r j o b = n e w c r o n J o b ( ' 0 0 5 5 0 6 * * 1 - 5 ' , t u r n L i g h t s F u l l O n , n u l l , t r u e ) ; r f x t r x . i n i t i a l i s e ( f u n c t i o n ( e r r o r ) { i f ( e r r o r ) { t h r o w n e w E r r o r ( ' U n a b l e t o i n i t i a l i s e t h e r v a r t u r n L i g h t s F u l l O n = f u n c t i o n ( ) { v a r t i m e s = S u n C a l c . g e t T i m e s ( m o m e n t ( ) , 5 5 . 8 5 8 , - 4 . 2 5 9 ) ; i f ( t i m e s . s u n r i s e > m o m e n t ( ) . s u b t r a c t ( ' m i n u t e s ' , 5 ) ) { l i g h t w a v e . s w i t c h O n ( ' 0 x F A C 2 7 1 / 1 ' , { l e v e l : 0 x 1 F } ) ; } } ;
  17. POWER MONITORING v a r r f x c o

    m = r e q u i r e ( ' r f x c o m ' ) , p g = r e q u i r e ( ' p g ' ) . n a t i v e , c o n S t r i n g = ' p g : / / u s e r : p a s s w o r d @ l o c a l h o s t / u s e r ' , c l i e n t = n e w p g . C l i e n t ( c o n S t r i n g ) ; v a r r f x t r x = n e w r f x c o m . R f x C o m ( ' / d e v / t t y U S B 0 ' ) ; r f x t r x . o n ( ' e l e c 2 ' , f u n c t i o n ( e v t ) { / / R e q u i r e s a P o s t g r e S Q L t a b l e / / C R E A T E T A B L E e n e r g y ( r e c o r d e d _ t i m e t i m e s t a m p D E F A U L T N O W ( ) , / / d e v i c e _ i d V A R C H A R , c u r r e n t _ w a t t s F L O A T ) c l i e n t . q u e r y ( ' I N S E R T I N T O e n e r g y ( d e v i c e _ i d , c u r r e n t _ w a t t s ) v a l u e s ( $ 1 , $ 2 ) ' , [ e v t . i d , e v t . c u r r e n t W a t t s ] ) ; } ) ; r f x t r x . i n i t i a l i s e ( f u n c t i o n ( ) { c o n s o l e . l o g ( ' D e v i c e i n i t i a l i s e d ' ) ; } ) ;
  18. kw/h 0 0 k kw w//h h 0 kw/h 2

    2,,0 00 00 0 2,000 4 4,,0 00 00 0 4,000 1 10 0 A AM M 10 AM 1 11 1 A AM M 11 AM 1 12 2 P PM M 12 PM 0 01 1 P PM M 01 PM 0 02 2 P PM M 02 PM 0 03 3 P PM M 03 PM 0 04 4 P PM M 04 PM Avg: 499 kw/h Min: 241 kw/h Max: 3380 kw/h Mar 2, 09:46 AM ­ Mar 2, 04:24 PM U
  19. kw/h 0 0 k kw w//h h 0 kw/h 2

    2,,0 00 00 0 2,000 4 4,,0 00 00 0 4,000 1 10 0 P PM M 10 PM 1 11 1 P PM M 11 PM T Tu ue e 0 04 4 Tue 04 0 01 1 A AM M 01 AM 0 02 2 A AM M 02 AM 0 03 3 A AM M 03 AM 0 04 4 A AM M 04 AM 0 05 5 A AM M 05 AM 0 06 6 A AM M 06 AM 0 07 7 A AM M 07 AM Avg: 443 kw/h Min: 273 kw/h Max: 3492 kw/h Mar 3, 09:46 PM ­ Mar 4, 07:34 AM U
  20. REST API FOR LIGHTSWITCH s e r v e r

    . g e t ( ' / l i g h t / : d e v i c e / : c o d e / : o p t i o n / ' , f u n c t i o n ( r e q , r e s , n e x t ) { v a r d e v i c e I d = r e q . p a r a m s . d e v i c e + ' / ' + r e q . p a r a m s . c o d e , s t a t u s C o d e = 2 0 0 ; s w i t c h ( r e q . p a r a m s . o p t i o n ) { c a s e ' o n ' : l i g h t w a v e . s w i t c h O n ( d e v i c e I d ) ; b r e a k ; c a s e ' o f f ' : l i g h t w a v e . s w i t c h O f f ( d e v i c e I d ) ; b r e a k ; d e f a u l t : c o n s o l e . l o g ( ' U n k n o w n o p t i o n ' , r e q . p a r a m s . o p t i o n ) ; s t a t u s C o d e = 4 0 0 ; b r e a k ; } r e s . s e n d ( s t a t u s C o d e ) ; r e t u r n n e x t ( ) ; } ) ;
  21. LIGHTING X10, ARC, ELRO, Waveman, EMW200, IMPULS, RisingSun, Philips (control

    coming soon) AC, HomeEasy EU, ANSLUT LightwaveRF, Siemens, EMW100, BBSB, MDREMOTE, RSL2, Livolo, RGB
  22. OTHER SENSORS OWL CM119/160/180 electricity monitors X10, KD101, Visonic (433Mhz

    only), Meiantech, SA30 security sensors Oregon Scientific temperature and humidity sensors Oregon Scientific digital scales (BWR101/102 and GR102)
  23. d e s c r i b e ( '

    . s w i t c h O n ' , f u n c t i o n ( ) { b e f o r e E a c h ( f u n c t i o n ( ) { l i g h t i n g 5 = n e w r f x c o m . L i g h t i n g 5 ( d e v i c e , r f x c o m . l i g h t i n g 5 . L I G H T W A V E R F ) ; } ) ; i t ( ' s h o u l d s e n d t h e c o r r e c t b y t e s t o t h e s e r i a l p o r t ' , f u n c t i o n ( d o n e ) { v a r s e n t C o m m a n d I d ; l i g h t i n g 5 . s w i t c h O n ( ' 0 x F 0 9 A C 8 / 1 ' , f u n c t i o n ( e r r , r e s p o n s e , c m d I d ) { s e n t C o m m a n d I d = c m d I d ; d o n e ( ) ; } ) ; e x p e c t ( f a k e S e r i a l P o r t ) . t o H a v e S e n t ( [ 0 x 0 A , 0 x 1 4 , 0 x 0 0 , 0 x 0 0 , 0 x F 0 , 0 x 9 A , 0 x C 8 , 0 x 0 1 , 0 x 0 1 e x p e c t ( s e n t C o m m a n d I d ) . t o E q u a l ( 0 ) ; } ) ; } ) ;
  24. MQTT.js allows subscription to "topics" which can be used notify

    of events e.g. subscribe /home/temperature/conservatory
  25. Conservatory Temperature 0 0 C C 0 C 5 5

    5 1 10 0 10 1 10 0 A AM M 10 AM 1 11 1 A AM M 11 AM 1 12 2 P PM M 12 PM 0 01 1 P PM M 01 PM 0 02 2 P PM M 02 PM 0 03 3 P PM M 03 PM 0 04 4 P PM M 04 PM Avg: 8 C Min: 6 C Max: 10 C Feb 3, 09:46 AM ­ Feb 3, 04:22 PM U
  26. Conservatory Humidity 0 0 % % 0 % 5 50

    0 50 1 10 00 0 100 1 10 0 A AM M 10 AM 1 11 1 A AM M 11 AM 1 12 2 P PM M 12 PM 0 01 1 P PM M 01 PM 0 02 2 P PM M 02 PM 0 03 3 P PM M 03 PM 0 04 4 P PM M 04 PM Avg: 88 % Min: 86 % Max: 93 % Feb 3, 09:46 AM ­ Feb 3, 04:22 PM U