PHPUG CGN: Arduino With PHP

PHPUG CGN: Arduino With PHP

Controlling Arduino from PHP using the Firmata protocol

3f2fb8bbcd44609346e1cc0c06d0a39b?s=128

Thomas Weinert

April 04, 2014
Tweet

Transcript

  1. ARDUINO WITH PHP CONNECTIONS TO THE PHYSICAL WORLD Created by

    / Thomas Weinert @ThomasWeinert
  2. ARDUINO Arduino is an open-source electronics prototyping platform based on

    flexible, easy-to- use hardware and software. It's intended for artists, designers, hobbyists and anyone interested in creating interactive objects or environments.
  3. ARDUINO UNO

  4. ARDUINO MEGA

  5. SHIELDS HTTP://SHIELDLIST.ORG/

  6. NETWORK

  7. PROTOTYPE

  8. MOTOR

  9. ARDUINO NANO

  10. NANO ON BREADBOARD

  11. NANO ON BASE

  12. BREADBOARD

  13. BREADBOARD HALFSIZE

  14. BREADBOARD SCHEMA

  15. BREADBOARD IN USE

  16. BREADBOARD IN USE

  17. FRITZING http://fritzing.org 1 1 5 5 10 10 15 15

    20 20 25 25 30 30 A A B B C C D D E E F F G G H H I I J J
  18. ARDUINO IDE

  19. None
  20. FIRMATA

  21. FIRMATA PROTOCOL Midi Based Pin Status Pin Manipulation Extended Commands

  22. FIRMATA TEST

  23. FIRMATA TEST PHP

  24. None
  25. PHP - SERIAL PORTS + Just streams - Console commands

    for configuration
  26. WINDOWS - fread() blocks until it gets data.

  27. SERPROXY

  28. CARICA CHIP $ l e d = n e w

    C a r i c a \ C h i p \ L e d ( $ b o a r d - > p i n s [ 1 3 ] ) ; $ l e d - > s t r o b e ( ) - > o n ( ) ;
  29. CARICA CHIP $ l e d = n e w

    C a r i c a \ C h i p \ L e d ( $ b o a r d - > p i n s [ 1 3 ] ) ; $ l e d - > p u l s e ( ) - > o n ( ) ;
  30. CARICA CHIP $ l e d = n e w

    C a r i c a \ C h i p \ L e d ( $ b o a r d - > p i n s [ 1 3 ] ) ; $ l e d - > b r i g h t n e s s ( 0 . 5 ) - > p u l s e ( ) - > o n ( ) ;
  31. CARICA FIRMATA $ b o a r d - >

    a c t i v a t e ( ) - > d o n e ( f u n c t i o n ( ) u s e ( $ b o a r d ) { $ l e d = n e w C a r i c a \ C h i p \ L e d ( $ b o a r d - > p i n s [ 1 3 ] ) ; $ l e d - > s t r o b e ( ) - > o n ( ) ; } ) ; C a r i c a \ I o \ E v e n t \ L o o p \ F a c t o r y : : r u n ( ) ;
  32. BASIC BLINK $ b o a r d - >

    p i n M o d e ( 1 3 , 0 x 0 1 ) ; w h i l e ( T R U E ) { $ b o a r d - > d i g i t a l W r i t e ( 1 3 , 0 x F F ) ; s l e e p ( 1 ) ; $ b o a r d - > d i g i t a l W r i t e ( 1 3 , 0 x 0 0 ) ; s l e e p ( 1 ) ; }
  33. BASIC BLINK OOP $ b o a r d -

    > p i n s [ 1 3 ] - > m o d e = C a r i c a \ F i r m a t a \ P i n : : M O D E _ O U T P U T ; w h i l e ( T R U E ) { $ b o a r d - > p i n s [ 1 3 ] - > d i g i t a l = ! $ b o a r d - > p i n s [ 1 3 ] - > d i g i t a l ; s l e e p ( 1 ) ; }
  34. BLINK NONBLOCKING $ l o o p = I o

    \ E v e n t \ L o o p \ F a c t o r y : : g e t ( ) ; $ b o a r d - > a c t i v a t e ( ) - > d o n e ( f u n c t i o n ( ) u s e ( $ b o a r d , $ l o o p ) { $ p i n = $ b o a r d - > p i n s [ 1 3 ] ; $ p i n - > m o d e = F i r m a t a \ P i n : : M O D E _ O U T P U T ; $ l o o p - > s e t I n t e r v a l ( f u n c t i o n ( ) u s e ( $ p i n ) { $ p i n - > d i g i t a l = ! $ p i n - > d i g i t a l ; } , 1 0 0 0 ) ; } ) ; $ l o o p - > r u n ( ) ;
  35. CARICA IO Event Loop Event Emitter Promises

  36. EVENT LOOP

  37. JAVASCRIPT EXAMPLE v a r e = d o c

    u m e n t . g e t E l e m e n t B y I d ( ' o u t p u t ' ) ; v a r c o u n t e r = 0 ; v a r i n t e r v a l = w i n d o w . s e t I n t e r v a l ( f u n c t i o n ( ) { e . t e x t C o n t e n t = e . t e x t C o n t e n t + c o u n t e r . t o S t r i n g ( ) + ' , ' ; c o u n t e r + + ; } , 1 0 0 0 ) ;
  38. PHP EXAMPLE $ l o o p = C a

    r i c a \ I o \ E v e n t \ L o o p \ F a c t o r y : : g e t ( ) ; $ l o o p - > s e t I n t e r v a l ( f u n c t i o n ( ) { s t a t i c $ i = 0 ; e c h o $ i + + ; } , 1 0 0 0 ) ; $ l o o p - > r u n ( ) ;
  39. PROMISES They describe an object that acts as a proxy

    for a result that is initially unknown, usually because the computation of its value is yet incomplete.
  40. JAVASCRIPT EXAMPLE j Q u e r y . g

    e t ( ' h e l l o - w o r l d . x m l ' ) . d o n e ( f u n c t i o n ( x m l ) { a l e r t ( ' l o a d e d . ' ) ; } ) ) ;
  41. PHP EXAMPLE $ m y s q l O n

    e = n e w C a r i c a \ I o \ D e f e r r e d \ M y S Q L ( n e w m y s q l i ( ' l o c a l h o s t ' ) ) ; $ m y s q l T w o = n e w C a r i c a \ I o \ D e f e r r e d \ M y S Q L ( n e w m y s q l i ( ' l o c a l h o s t ' ) ) ; $ t i m e = m i c r o t i m e ( T R U E ) ; $ d e b u g = f u n c t i o n ( $ r e s u l t ) u s e ( $ t i m e ) { v a r _ d u m p ( i t e r a t o r _ t o _ a r r a y ( $ r e s u l t ) ) ; v a r _ d u m p ( m i c r o t i m e ( T R U E ) - $ t i m e ) ; } ; $ q u e r i e s = C a r i c a \ I o \ D e f e r r e d : : W h e n ( $ m y s q l O n e ( " S E L E C T ' Q u e r y 1 ' , S L E E P ( 5 ) " ) - > d o n e ( $ d e b u g ) , $ m y s q l T w o ( " S E L E C T ' Q u e r y 2 ' , S L E E P ( 1 ) " ) - > d o n e ( $ d e b u g ) ) ; C a r i c a \ I o \ E v e n t \ L o o p \ F a c t o r y : : r u n ( $ q u e r i e s ) ;
  42. EVENT EMITTER $ s t r e a m =

    n e w S t r e a m \ F i l e ( ' c : / t m p / s a m p l e . t x t ' ) ; $ s t r e a m - > e v e n t s ( ) - > o n ( ' r e a d - d a t a ' , f u n c t i o n ( $ d a t a ) { e c h o $ d a t a ; } ) ; $ s t r e a m - > e v e n t s ( ) - > o n ( ' e r r o r ' , f u n c t i o n ( $ e r r o r ) u s e ( $ l o o p ) { e c h o $ e r r o r ; $ l o o p - > s t o p ( ) ; } ) ;
  43. INTERACTION

  44. HTTPSERVER u s e C a r i c a

    \ I o \ N e t w o r k \ H t t p ; $ r o u t e = n e w H t t p \ R o u t e ( ) ; $ r o u t e - > m a t c h ( ' / h e l l o / { n a m e } ' , f u n c t i o n ( H t t p \ R e q u e s t $ r e q u e s t , $ p a r a m e t e r s ) { $ r e s p o n s e = $ r e q u e s t - > c r e a t e R e s p o n s e ( n e w H t t p \ R e s p o n s e \ C o n t e n t \ S t r i n g ( " H e l l o " . $ p a r a m e t e r s [ ' n a m e ' ] . " ! \ n " ) ) ; r e t u r n $ r e s p o n s e ; } ) ;
  45. FILES $ r o u t e - > m

    a t c h ( ' / h e l l o ' , n e w H t t p \ R o u t e \ F i l e ( _ _ D I R _ _ . ' / f i l e s / h e l l o . h t m l ' ) ) ; $ r o u t e - > s t a r t s W i t h ( ' / f i l e s ' , n e w H t t p \ R o u t e \ D i r e c t o r y ( _ _ D I R _ _ ) ) ; $ s e r v e r = n e w C a r i c a \ I o \ N e t w o r k \ H t t p \ S e r v e r ( $ r o u t e ) ; $ s e r v e r - > l i s t e n ( 8 0 8 0 ) ;
  46. INTERACTIVE LED

  47. HTML FILE < ! D O C T Y P

    E h t m l > < h t m l > < h e a d > < t i t l e > L e d S w i t c h < / t i t l e > < / h e a d > < b o d y > < f o r m a c t i o n = " . / s w i t c h / o n " t a r g e t = " i f r a m e " > < b u t t o n t y p e = " s u b m i t " > O n < / b u t t o n > < / f o r m > < f o r m a c t i o n = " . / s w i t c h / o f f " t a r g e t = " i f r a m e " > < b u t t o n t y p e = " s u b m i t " > O f f < / b u t t o n > < / f o r m > < i f r a m e s t y l e = " w i d t h : 2 0 0 p x ; h e i g h t : 4 0 p x ; b o r d e r : n o n e ; " s r c = " a b o u t : b l a n k " n a m e = " i f r a m < / b o d y > < / h t m l >
  48. PHP SERVER u s e C a r i c

    a \ I o \ N e t w o r k \ H t t p ; $ b o a r d - > a c t i v a t e ( ) - > d o n e ( f u n c t i o n ( ) u s e ( $ b o a r d ) { $ l e d = n e w C a r i c a \ C h i p \ L e d ( $ b o a r d - > p i n s [ 2 0 ] ) ; $ r o u t e = n e w H t t p \ R o u t e ( ) ; $ r o u t e - > m a t c h ( ' / s w i t c h / { s t a t e } ' , f u n c t i o n ( H t t p \ R e q u e s t $ r e q u e s t , a r r a y $ p a r a m e t e r s ) u s e ( $ l e d ) { i f ( $ p a r a m e t e r s [ ' s t a t e ' ] = = ' o n ' ) { $ l e d - > o n ( ) ; $ m e s s a g e = ' O N ' ; } e l s e { $ l e d - > o f f ( ) ; $ m e s s a g e = ' O F F ' ; } $ r e s p o n s e = $ r e q u e s t - > c r e a t e R e s p o n s e ( ) ; $ r e s p o n s e - > c o n t e n t = n e w H t t p \ R e s p o n s e \ C o n t e n t \ S t r i n g ( $ m e s s a g e , ' t e x t / p l a i n ; c h a r s e t = u t f - 8 ' ) ; r e t u r n $ r e s p o n s e ; } ) ; $ r o u t e - > m a t c h ( ' / ' , n e w \ C a r i c a \ I o \ N e t w o r k \ H t t p \ R o u t e \ F i l e ( _ _ D I R _ _ . ' / i n d e x . h t m l ' ) ) ; $ s e r v e r = n e w C a r i c a \ I o \ N e t w o r k \ H t t p \ S e r v e r ( $ r o u t e ) ;
  49. COLOR CIRCLE

  50. SERVER f u n c t i o n (

    ) u s e ( $ b o a r d ) { $ l e d = n e w C a r i c a \ C h i p \ R g b \ L e d ( $ b o a r d - > p i n s [ 3 ] , $ b o a r d - > p i n s [ 5 ] , $ b o a r d - > p i n s [ 6 ] ) ; $ r o u t e = n e w C a r i c a \ I o \ N e t w o r k \ H t t p \ R o u t e ( ) ; $ r o u t e - > m a t c h ( ' / r g b ' , f u n c t i o n ( H t t p \ R e q u e s t $ r e q u e s t ) u s e ( $ l e d ) { $ c o l o r = i s s e t ( $ r e q u e s t - > q u e r y [ ' c o l o r ' ] ) ? $ r e q u e s t - > q u e r y [ ' c o l o r ' ] : ' # 0 0 0 ' ; $ l e d - > c o l o r ( $ c o l o r ) - > o n ( ) ; $ r e s p o n s e = $ r e q u e s t - > c r e a t e R e s p o n s e ( ) ; $ r e s p o n s e - > c o n t e n t = n e w H t t p \ R e s p o n s e \ C o n t e n t \ S t r i n g ( ' C o l o r : ' . $ c o l o r ) ; r e t u r n $ r e s p o n s e ; } ) ; $ r o u t e - > s t a r t s W i t h ( ' / f i l e s ' , n e w H t t p \ R o u t e \ D i r e c t o r y ( _ _ D I R _ _ ) ) ; $ r o u t e - > m a t c h ( ' / ' , n e w H t t p \ R o u t e \ F i l e ( _ _ D I R _ _ . ' / i n d e x . h t m l ' ) ) ; $ s e r v e r = n e w C a r i c a \ I o \ N e t w o r k \ H t t p \ S e r v e r ( $ r o u t e ) ; $ s e r v e r - > l i s t e n ( 8 0 8 0 ) ; }
  51. REACTPHP { " r e q u i r e

    " : { " r e a c t / r e a c t " : " 0 . 4 . * " } }
  52. RATCHET { " r e q u i r e

    " : { " c b o d e n / r a t c h e t " : " 0 . 3 . * " } }
  53. SENSOR PHALANX 1:07

  54. SHIFTOUT()

  55. SHIFTOUT() $ l o o p - > s e

    t I n t e r v a l ( f u n c t i o n ( ) u s e ( $ b o a r d , $ l a t c h P i n , $ c l o c k P i n , $ d a t a P i n ) { s t a t i c $ n u m b e r = 0 ; $ l a t c h P i n - > d i g i t a l = F A L S E ; $ b o a r d - > s h i f t O u t ( $ d a t a P i n - > p i n , $ c l o c k P i n - > p i n , $ n u m b e r ) ; $ l a t c h P i n - > d i g i t a l = T R U E ; i f ( + + $ n u m b e r > 2 5 5 ) { $ n u m b e r = 0 ; } } , 1 0 0 0 ) ;
  56. SHIFTOUT() 0:15

  57. 7 SEGMENT DISPLAYS

  58. 7 SEGMENT DISPLAYS $ l o o p - >

    s e t I n t e r v a l ( f u n c t i o n ( ) u s e ( $ b o a r d , $ l a t c h P i n , $ c l o c k P i n , $ d a t a P i n , $ n u m b e r s , $ s e g m e n t s ) { s t a t i c $ n u m b e r = 0 ; $ d i g i t s = s t r _ p a d ( $ n u m b e r , $ s e g m e n t s , 0 , S T R _ P A D _ L E F T ) ; $ b y t e s = [ ] ; f o r ( $ i = s t r l e n ( $ d i g i t s ) - 1 ; $ i > = 0 ; $ i - - ) { $ b y t e s [ ] = 0 x F F ^ ( i n t ) $ n u m b e r s [ $ d i g i t s [ $ i ] ] ; } $ l a t c h P i n - > d i g i t a l = F A L S E ; $ b o a r d - > s h i f t O u t ( $ d a t a P i n - > p i n , $ c l o c k P i n - > p i n , $ b y t e s ) ; $ l a t c h P i n - > d i g i t a l = T R U E ; i f ( + + $ n u m b e r > ( p o w ( 1 0 , $ s e g m e n t s ) - 1 ) ) { $ n u m b e r = 0 ; } } , 1 0 0 ) ;
  59. 7SEG DISPLAYS 0:05

  60. FIRST PROJECT

  61. COMPOSER CREATE c o m p o s e r

    c r e a t e - p r o j e c t c a r i c a / c h i p - s k e l e t o n l e d - - s t a b i l i t y = d e v
  62. COMPOSER CREATE

  63. CONFIGURE BOOTSTRAP / * * * s e r i

    a l - s e r i a l c o n n e c t i o n * t c p - t c p c o n n e c t i o n ( n e t w o r k s h i e l d o r s e r p r o x y ) * / d e f i n e ( ' C A R I C A _ F I R M A T A _ M O D E ' , ' s e r i a l ' ) ; d e f i n e ( ' C A R I C A _ F I R M A T A _ S E R I A L _ D E V I C E ' , ' / d e v / t t y 0 ' ) ; d e f i n e ( ' C A R I C A _ F I R M A T A _ S E R I A L _ B A U D ' , 5 7 6 0 0 ) ; d e f i n e ( ' C A R I C A _ F I R M A T A _ T C P _ S E R V E R ' , ' 1 2 7 . 0 . 0 . 1 ' ) ; d e f i n e ( ' C A R I C A _ F I R M A T A _ T C P _ P O R T ' , 5 3 3 9 ) ;
  64. SKELETON $ b o a r d = i n

    c l u d e ( _ _ D I R _ _ . ' / b o o t s t r a p . p h p ' ) ; u s e C a r i c a \ C h i p a s C h i p ; $ b o a r d - > a c t i v a t e ( ) - > d o n e ( f u n c t i o n ( ) u s e ( $ b o a r d ) { / / S t a r t h e r e ! } ) ; C a r i c a \ I o \ E v e n t \ L o o p \ F a c t o r y : : r u n ( ) ;
  65. LED OBJECT $ b o a r d = i

    n c l u d e ( _ _ D I R _ _ . ' / b o o t s t r a p . p h p ' ) ; u s e C a r i c a \ C h i p a s C h i p ; $ b o a r d - > a c t i v a t e ( ) - > d o n e ( f u n c t i o n ( ) u s e ( $ b o a r d ) { $ l e d = n e w C h i p \ L e d ( $ b o a r d - > p i n s [ 1 3 ] ) ; $ l e d - > s t r o b e ( 2 0 0 0 ) - > o n ( ) ; } ) ; C a r i c a \ I o \ E v e n t \ L o o p \ F a c t o r y : : r u n ( ) ;
  66. THANKS