PHPUG CGN: Controlling Arduino With PHP

1,202 views

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,202
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
221
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

PHPUG CGN: Controlling Arduino With PHP

  1. 1. ARDUINO WITH PHP CONNECTIONS TO THE PHYSICAL WORLD Createdby /ThomasWeinert @ThomasWeinert
  2. 2. ARDUINO Arduino is an open-source electronics prototyping platformbased 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. 3. ARDUINO UNO
  4. 4. ARDUINO MEGA
  5. 5. SHIELDS HTTP://SHIELDLIST.ORG/
  6. 6. NETWORK
  7. 7. PROTOTYPE
  8. 8. MOTOR
  9. 9. ARDUINO NANO
  10. 10. NANO ON BREADBOARD
  11. 11. NANO ON BASE
  12. 12. BREADBOARD
  13. 13. BREADBOARD HALFSIZE
  14. 14. BREADBOARD SCHEMA
  15. 15. BREADBOARD IN USE
  16. 16. BREADBOARD IN USE
  17. 17. FRITZING http://fritzing.org 11 55 1010 1515 2020 2525 3030 A A B B C C D D E E F F G G H H I I J J
  18. 18. ARDUINO IDE
  19. 19. FIRMATA
  20. 20. FIRMATA PROTOCOL MidiBased Pin Status Pin Manipulation Extended Commands
  21. 21. FIRMATA TEST
  22. 22. FIRMATA TEST PHP
  23. 23. PHP - SERIAL PORTS + Just streams - Consolecommands for configuration
  24. 24. WINDOWS - fread() blocks until it gets data.
  25. 25. SERPROXY
  26. 26. CARICA CHIP $led=newCaricaChipLed($board->pins[13]); $led->strobe()->on();
  27. 27. CARICA CHIP $led=newCaricaChipLed($board->pins[13]); $led->pulse()->on();
  28. 28. CARICA CHIP $led=newCaricaChipLed($board->pins[13]); $led->brightness(0.5)->pulse()->on();
  29. 29. CARICA FIRMATA $board ->activate() ->done( function()use($board){ $led=newCaricaChipLed($board->pins[13]); $led->strobe()->on(); } ); CaricaIoEventLoopFactory::run();
  30. 30. BASIC BLINK $board->pinMode(13,0x01); while(TRUE){ $board->digitalWrite(13,0xFF); sleep(1); $board->digitalWrite(13,0x00); sleep(1); }
  31. 31. BASIC BLINK OOP $board->pins[13]->mode=CaricaFirmataPin::MODE_OUTPUT; while(TRUE){ $board->pins[13]->digital=!$board->pins[13]->digital; sleep(1); }
  32. 32. BLINK NONBLOCKING $loop=IoEventLoopFactory::get(); $board ->activate() ->done( function()use($board,$loop){ $pin=$board->pins[13]; $pin->mode=FirmataPin::MODE_OUTPUT; $loop->setInterval( function()use($pin){ $pin->digital=!$pin->digital; }, 1000 ); } ); $loop->run();
  33. 33. CARICA IO EventLoop EventEmitter Promises
  34. 34. EVENT LOOP
  35. 35. JAVASCRIPT EXAMPLE vare=document.getElementById('output'); varcounter=0; varinterval=window.setInterval( function(){ e.textContent=e.textContent+counter.toString()+','; counter++; }, 1000 );
  36. 36. PHP EXAMPLE $loop=CaricaIoEventLoopFactory::get(); $loop->setInterval( function(){ static$i=0; echo$i++; }, 1000 ); $loop->run();
  37. 37. PROMISES They describe an objectthatacts as aproxy for a resultthatis initially unknown, usually because the computation of its value is yetincomplete.
  38. 38. JAVASCRIPT EXAMPLE jQuery .get('hello-world.xml') .done( function(xml){ alert('loaded.'); } ) );
  39. 39. PHP EXAMPLE $mysqlOne=newCaricaIoDeferredMySQL(newmysqli('localhost')); $mysqlTwo=newCaricaIoDeferredMySQL(newmysqli('localhost')); $time=microtime(TRUE); $debug=function($result)use($time){ var_dump(iterator_to_array($result)); var_dump(microtime(TRUE)-$time); }; $queries=CaricaIoDeferred::When( $mysqlOne("SELECT'Query1',SLEEP(5)") ->done($debug), $mysqlTwo("SELECT'Query2',SLEEP(1)") ->done($debug) ); CaricaIoEventLoopFactory::run($queries);
  40. 40. EVENT EMITTER $stream=newStreamFile('c:/tmp/sample.txt'); $stream->events()->on( 'read-data', function($data){ echo$data; } ); $stream->events()->on( 'error', function($error)use($loop){ echo$error; $loop->stop(); } );
  41. 41. INTERACTION
  42. 42. HTTPSERVER useCaricaIoNetworkHttp; $route=newHttpRoute(); $route->match( '/hello/{name}', function(HttpRequest$request,$parameters){ $response=$request->createResponse( newHttpResponseContentString( "Hello".$parameters['name']."!n" ) ); return$response; } );
  43. 43. FILES $route->match('/hello',newHttpRouteFile(__DIR__.'/files/hello.html')); $route->startsWith('/files',newHttpRouteDirectory(__DIR__)); $server=newCaricaIoNetworkHttpServer($route); $server->listen(8080);
  44. 44. INTERACTIVE LED
  45. 45. HTML FILE <!DOCTYPEhtml> <html> <head> <title>LedSwitch</title> </head> <body> <formaction="./switch/on"target="iframe"> <buttontype="submit">On</button> </form> <formaction="./switch/off"target="iframe"> <buttontype="submit">Off</button> </form> <iframestyle="width:200px;height:40px;border:none;"src="about:blank"name="ifram </body> </html>
  46. 46. PHP SERVER useCaricaIoNetworkHttp; $board ->activate() ->done( function()use($board){ $led=newCaricaChipLed($board->pins[20]); $route=newHttpRoute(); $route->match( '/switch/{state}', function(HttpRequest$request,array$parameters)use($led){ if($parameters['state']=='on'){ $led->on(); $message='ON'; }else{ $led->off(); $message='OFF'; } $response=$request->createResponse(); $response->content=newHttpResponseContentString( $message,'text/plain;charset=utf-8' ); return$response; } ); $route->match('/',newCaricaIoNetworkHttpRouteFile(__DIR__.'/index.html')); $server=newCaricaIoNetworkHttpServer($route);
  47. 47. COLOR CIRCLE
  48. 48. SERVER function()use($board){ $led=newCaricaChipRgbLed( $board->pins[3],$board->pins[5],$board->pins[6] ); $route=newCaricaIoNetworkHttpRoute(); $route->match( '/rgb', function(HttpRequest$request)use($led){ $color=isset($request->query['color']) ?$request->query['color']:'#000'; $led->color($color)->on(); $response=$request->createResponse(); $response->content=newHttpResponseContentString( 'Color:'.$color ); return$response; } ); $route->startsWith('/files',newHttpRouteDirectory(__DIR__)); $route->match('/',newHttpRouteFile(__DIR__.'/index.html')); $server=newCaricaIoNetworkHttpServer($route); $server->listen(8080); }
  49. 49. REACTPHP { "require":{ "react/react":"0.4.*" } }
  50. 50. RATCHET { "require":{ "cboden/ratchet":"0.3.*" } }
  51. 51. SENSOR PHALANX 1:07
  52. 52. SHIFTOUT()
  53. 53. SHIFTOUT() $loop->setInterval( function()use($board,$latchPin,$clockPin,$dataPin){ static$number=0; $latchPin->digital=FALSE; $board->shiftOut($dataPin->pin,$clockPin->pin,$number); $latchPin->digital=TRUE; if(++$number>255){ $number=0; } }, 1000 );
  54. 54. SHIFTOUT() 0:15
  55. 55. 7 SEGMENT DISPLAYS
  56. 56. 7 SEGMENT DISPLAYS $loop->setInterval( function()use( $board,$latchPin,$clockPin,$dataPin,$numbers,$segments ){ static$number=0; $digits=str_pad($number,$segments,0,STR_PAD_LEFT); $bytes=[]; for($i=strlen($digits)-1;$i>=0;$i--){ $bytes[]=0xFF^(int)$numbers[$digits[$i]]; } $latchPin->digital=FALSE; $board->shiftOut( $dataPin->pin,$clockPin->pin,$bytes ); $latchPin->digital=TRUE; if(++$number>(pow(10,$segments)-1)){ $number=0; } }, 100 );
  57. 57. 7SEG DISPLAYS 0:05
  58. 58. FIRST PROJECT
  59. 59. COMPOSER CREATE composercreate-projectcarica/chip-skeletonled--stability=dev
  60. 60. COMPOSER CREATE
  61. 61. CONFIGURE BOOTSTRAP /** *serial-serialconnection *tcp-tcpconnection(networkshieldorserproxy) */ define('CARICA_FIRMATA_MODE','serial'); define('CARICA_FIRMATA_SERIAL_DEVICE','/dev/tty0'); define('CARICA_FIRMATA_SERIAL_BAUD',57600); define('CARICA_FIRMATA_TCP_SERVER','127.0.0.1'); define('CARICA_FIRMATA_TCP_PORT',5339);
  62. 62. SKELETON $board=include(__DIR__.'/bootstrap.php'); useCaricaChipasChip; $board ->activate() ->done( function()use($board){ //Starthere! } ); CaricaIoEventLoopFactory::run();
  63. 63. LED OBJECT $board=include(__DIR__.'/bootstrap.php'); useCaricaChipasChip; $board ->activate() ->done( function()use($board){ $led=newChipLed($board->pins[13]); $led->strobe(2000)->on(); } ); CaricaIoEventLoopFactory::run();
  64. 64. THANKS

×