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

Controlling Arduino With PHP

Controlling Arduino With PHP

PHPSummit - September 2014

Thomas Weinert

October 07, 2014
Tweet

More Decks by Thomas Weinert

Other Decks in Programming

Transcript

  1. 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.
  2. 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
  3. CONFIGURE BOOTSTRAP /** * serial - serial connection * tcp

    - tcp connection (network shield or serproxy) */ 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);
  4. SKELETON $board = include(__DIR__.'/bootstrap.php'); use Carica\Chip as Chip; $board ->activate()

    ->done( function () use ($board) { // Start here! } ); Carica\Io\Event\Loop\Factory::run();
  5. LED OBJECT $board = include(__DIR__.'/bootstrap.php'); use Carica\Chip as Chip; $board

    ->activate() ->done( function () use ($board) { $led = new Chip\Led($board->pins[13]); $led->strobe(2000)->on(); } ); Carica\Io\Event\Loop\Factory::run();
  6. Io Event Loop Server Firmata Protocol Chip Devices 010001000100 111000111000

    011111110001 100100100111 100100101100 001100110110
  7. CARICA FIRMATA $board ->activate() ->done( function () use ($board) {

    $led = new Carica\Chip\Led($board->pins[13]); $led->strobe()->on(); } ); Carica\Io\Event\Loop\Factory::run();
  8. BLINK NONBLOCKING $loop = Io\Event\Loop\Factory::get(); $board ->activate() ->done( function ()

    use ($board, $loop) { $pin = $board->pins[13]; $pin->mode = Firmata\Pin::MODE_OUTPUT; $loop->setInterval( function () use ($pin) { $pin->digital = !$pin->digital; }, 1000 ); } ); $loop->run();
  9. JAVASCRIPT EXAMPLE var e = document.getElementById('output'); var counter = 0;

    var interval = window.setInterval( function() { e.textContent = e.textContent + counter.toString() + ', '; counter++; }, 1000 );
  10. 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.
  11. PHP EXAMPLE $mysqlOne = new Carica\Io\Deferred\MySQL(new mysqli('localhost')); $mysqlTwo = new

    Carica\Io\Deferred\MySQL(new mysqli('localhost')); $time = microtime(TRUE); $debug = function($result) use ($time) { var_dump(iterator_to_array($result)); var_dump(microtime(TRUE) - $time); }; $queries = Carica\Io\Deferred::When( $mysqlOne("SELECT 'Query 1', SLEEP(5)") ->done($debug), $mysqlTwo("SELECT 'Query 2', SLEEP(1)") ->done($debug) ); Carica\Io\Event\Loop\Factory::run($queries);
  12. EVENT EMITTER $stream = new Stream\File('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(); } );
  13. HTTPSERVER use Carica\Io\Network\Http; $route = new Http\Route(); $route->match( '/hello/{name}', function

    (Http\Request $request, $parameters) { $response = $request->createResponse( new Http\Response\Content\String( "Hello ".$parameters['name']."!\n" ) ); return $response; } );
  14. HTML FILE <!DOCTYPE html> <html> <head> <title>Led Switch</title> </head> <body>

    <form action="./switch/on" target="iframe"> <button type="submit">On</button> </form> <form action="./switch/off" target="iframe"> <button type="submit">Off</button> </form> <iframe style="width: 200px; height: 40px; border: none;" src="about:blank" name="iframe"></ifra </body> </html>
  15. PHP SERVER use Carica\Io\Network\Http; $board ->activate() ->done( function () use

    ($board) { $led = new Carica\Chip\Led($board->pins[20]); $route = new Http\Route(); $route->match( '/switch/{state}', function (Http\Request $request, array $parameters) use ($led) { if ($parameters['state'] == 'on') { $led->on(); $message = 'ON'; } else { $led->off(); $message = 'OFF'; } $response = $request->createResponse(); $response->content = new Http\Response\Content\String( $message, 'text/plain; charset=utf-8' ); return $response; } ); $route->match('/', new \Carica\Io\Network\Http\Route\File(__DIR__.'/index.html')); $server = new Carica\Io\Network\Http\Server($route);
  16. SERVER function () use ($board) { $led = new Carica\Chip\Rgb\Led(

    $board->pins[3], $board->pins[5], $board->pins[6] ); $route = new Carica\Io\Network\Http\Route(); $route->match( '/rgb', function (Http\Request $request) use ($led) { $color = isset($request->query['color']) ? $request->query['color'] : '#000'; $led->color($color)->on(); $response = $request->createResponse(); $response->content = new Http\Response\Content\String( 'Color: '.$color ); return $response; } ); $route->startsWith('/files', new Http\Route\Directory(__DIR__)); $route->match('/', new Http\Route\File(__DIR__.'/index.html')); $server = new Carica\Io\Network\Http\Server($route); $server->listen(8080); }
  17. 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 );
  18. 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 );
  19. $max = new Carica\Chip\Max7219\Matrix( $board, 11, 12, 8 ); $displayCount

    = 4; for ($i = 0; $i < $displayCount; $i++) { $max->addDisplay($i * 8, 0); } $image = imagecreatefrompng(__DIR__.'/elephpant-animation.png'); $max->scrollX($image, -17, 150)->brightness(0.5)->on();