36
FrOSCon 2013 Controlling Arduino With PHP

Controlling Arduino With PHP

Embed Size (px)

Citation preview

Page 1: Controlling Arduino With PHP

FrOSCon 2013

Controlling Arduino With PHP

Page 2: Controlling Arduino With PHP

About Me

● Thomas Weinert– @ThomasWeinert

● papaya Software GmbH– papaya CMS

● PHP, Javascript, XSLT

● XML Fanatic (JSON is BAD)

Page 3: Controlling Arduino With PHP

Arduino

Page 4: Controlling Arduino With PHP

Arduino Mega

Page 5: Controlling Arduino With PHP

Arduino Nano

Page 6: Controlling Arduino With PHP

Arduino Nano Breadboard

Page 7: Controlling Arduino With PHP

Arduino Nano Breakout

Page 8: Controlling Arduino With PHP

Arduino IDE

Page 9: Controlling Arduino With PHP

Firmata

● MIDI Based● Serial Port

– Forks for TCP

● http://firmata.org

Page 10: Controlling Arduino With PHP

Non-Blocking I/O

● Event Loop● Event Emitter● Promises

Page 11: Controlling Arduino With PHP

Event Loop

Listeners

Loop

External Process

External Process

File

Page 12: Controlling Arduino With PHP

Event Loop

● Timeouts● Intervals● Stream Listeners

● Implementations– StreamSelect– LibEvent

– MySQLi

Page 13: Controlling Arduino With PHP

Browser Example

var e = document.getElementById('output');var counter = 0;var interval = window.setInterval( function() { e.textContent = e.textContent + counter.toString() + ', '; counter++; }, 1000);

Page 14: Controlling Arduino With PHP

Event Emitter

Object

Event

Callback

Callback

Event

Callback

Event Event

● Attach● on(), once()

● Trigger● emit()

Page 15: Controlling Arduino With PHP

Event Emitter Example

$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(); });

Page 16: Controlling Arduino With PHP

Promises

● Asynchronous Condition● Attach Callbacks

– done()

– fail()– always()

● Change Status– reject()– resolve()

Page 17: Controlling Arduino With PHP

jQuery Example

jQuery( function () { jQuery .get('hello-world.xml') .done( function (xml) { $('#output').text( $('data', xml).text() ); } ); });

Page 18: Controlling Arduino With PHP

Carica Projects

● Carica I/O– https://bitbucket.org/ThomasWeinert/carica-io

● Carica Firmata– https://bitbucket.org/ThomasWeinert/carica-firmata

● Carica Chip– https://bitbucket.org/ThomasWeinert/carica-chip

Page 19: Controlling Arduino With PHP

Carica I/O

● Event Loop– Carica\Io\Event\Loop

● Event Emitter– Carica\Io\Event\Emitter

● Promises– Carica\Io\Deferred

– Carica\Io\Deferred\Promise

Page 20: Controlling Arduino With PHP

Timers

$loop = Loop\Factory::get();$i = 0;$loop->setInterval( function () use (&$i) { echo $i++; }, 1000);$loop->setTimeout( function () use ($loop) { $loop->stop(); }, 10000);$loop->run();

Page 21: Controlling Arduino With PHP

Asynchronous MySQL

$mysqlOne = new Io\Deferred\MySQL( new mysqli('localhost'));$mysqlTwo = new 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 = Io\Deferred::When( $mysqlOne("SELECT 'Query 1', SLEEP(5)") ->done($debug), $mysqlTwo("SELECT 'Query 2', SLEEP(1)") ->done($debug));Io\Event\Loop\Factory::run($queries);

Page 22: Controlling Arduino With PHP

Asynchronous MySQL

Page 23: Controlling Arduino With PHP

HTTP Server

Why?

Page 24: Controlling Arduino With PHP

HTTP Server

● PHP Daemons– Single process for all pequests

– Share variables

Page 25: Controlling Arduino With PHP

Carica HTTP Server

<?phpinclude(__DIR__.'/../../src/Carica/Io/Loader.php');Carica\Io\Loader::register();

use Carica\Io\Network\Http;

$route = new Carica\Io\Network\Http\Route();$route->startsWith( '/files', new Http\Route\File(__DIR__));

$server = new Carica\Io\Network\Http\Server($route);$server->listen(8080);

Carica\Io\Event\Loop\Factory::run();

Page 26: Controlling Arduino With PHP

Carica Firmata

Page 27: Controlling Arduino With PHP

Carica Firmata Board

<?php$board = new Firmata\Board( new Io\Stream\Serial( CARICA_FIRMATA_SERIAL_DEVICE, CARICA_FIRMATA_SERIAL_BAUD ));

$board ->activate() ->done( function () use ($board) { ... } );

Carica\Io\Event\Loop\Factory::run();

Page 28: Controlling Arduino With PHP

Carica Firmata Pins

function () use ($board) { $buttonPin = 2; $ledPin = 13;

$board->pins[$buttonPin]->mode = Firmata\PIN_STATE_INPUT; $board->pins[$ledPin]->mode = Firmata\PIN_STATE_OUTPUT;

$board->digitalRead( $buttonPin, function($value) use ($board, $ledPin) { $board->pins[$ledPin]->digital = $value == Firmata\DIGITAL_HIGH; } ); }

Page 29: Controlling Arduino With PHP

Example: Blink

function () use ($board, $loop) { $led = 9; $board->pinMode($led, Firmata\PIN_STATE_OUTPUT); $loop->setInterval( function () use ($board, $led) { static $ledOn = TRUE; echo 'LED: '.($ledOn ? 'on' : 'off')."\n"; $board->digitalWrite( $led, $ledOn ? Firmata\DIGITAL_HIGH : Firmata\DIGITAL_LOW ); $ledOn = !$ledOn; }, 1000 );}

Page 30: Controlling Arduino With PHP

RGB Wheel

Page 31: Controlling Arduino With PHP

Carica Chip

● Hardware Abstraction● Device Objects

Page 32: Controlling Arduino With PHP

LED

function () use ($board) { $led = new Carica\Chip\Led($board, 9); $led->blink();}

Page 33: Controlling Arduino With PHP

RGB LED

function () use ($board) { $colors = array('#F00', '#0F0', '#00F'); $led = new Carica\Chip\Led\Rgb($board, 10, 11, 9); $led->setColor('#000'); $index = 0; $next = function() use ($led, $colors, &$index, &$next) { if (isset($colors[$index])) { $color = $colors[$index]; $led->fadeTo($color)->done($next); } if (++$index >= count($colors)) { $index = 0; } }; $next();}

Page 34: Controlling Arduino With PHP

Servo

function () use ($board, $loop) { $positions = array( 0, 45, 90, 180 ); $servo = new Carica\Chip\Servo($board, 7, -180); $index = 0; $loop->setInterval( $next = function () use ($servo, $positions, &$index) { if (isset($positions[$index])) { $position = $positions[$index]; $servo->moveTo($position); echo $position, " Grad , ", $servo->getPosition(), " Grad\n"; } if (++$index >= count($positions)) { $index = 0; } }, 2000 ); $next();}

Page 35: Controlling Arduino With PHP

Analog Sensor

function () use ($board) { $sensor = new Carica\Chip\Sensor\Analog($board, 14); $sensor->onChange( function ($sensor) { echo $sensor, "\n"; } );}

Page 36: Controlling Arduino With PHP

Thank You

● Questions?

● Twitter: @ThomasWeinert● Blog: http://a-basketful-of-papayas.net