FrOSCon 2013
Controlling Arduino With PHP
About Me
● Thomas Weinert
– @ThomasWeinert
● papaya Software GmbH
– papaya CMS
● PHP, Javascript, XSLT
● XML Fanatic (JSON...
Arduino
Arduino Mega
Arduino Nano
Arduino Nano Breadboard
Arduino Nano Breakout
Arduino IDE
Firmata
● MIDI Based
● Serial Port
– Forks for TCP
● http://firmata.org
Non-Blocking I/O
● Event Loop
● Event Emitter
● Promises
Event Loop
Listeners
Loop
External
Process
External
Process
File
Event Loop
● Timeouts
● Intervals
● Stream Listeners
● Implementations
– StreamSelect
– LibEvent
– MySQLi
Browser Example
var e = document.getElementById('output');
var counter = 0;
var interval = window.setInterval(
function() ...
Event Emitter
Object
Event
Callback
Callback
Event
Callback
Event Event
● Attach
● on(), once()
● Trigger
● emit()
Event Emitter Example
$stream = new StreamFile('c:/tmp/sample.txt');
$stream->events()->on(
'read-data',
function($data) {...
Promises
● Asynchronous Condition
● Attach Callbacks
– done()
– fail()
– always()
● Change Status
– reject()
– resolve()
jQuery Example
jQuery(
function () {
jQuery
.get('hello-world.xml')
.done(
function (xml) {
$('#output').text(
$('data', x...
Carica Projects
● Carica I/O
– https://bitbucket.org/ThomasWeinert/carica-io
● Carica Firmata
– https://bitbucket.org/Thom...
Carica I/O
● Event Loop
– CaricaIoEventLoop
● Event Emitter
– CaricaIoEventEmitter
● Promises
– CaricaIoDeferred
– CaricaI...
Timers
$loop = LoopFactory::get();
$i = 0;
$loop->setInterval(
function () use (&$i) {
echo $i++;
},
1000
);
$loop->setTim...
Asynchronous MySQL
$mysqlOne = new IoDeferredMySQL(
new mysqli('localhost')
);
$mysqlTwo = new IoDeferredMySQL(
new mysqli...
Asynchronous MySQL
HTTP Server
Why?
HTTP Server
● PHP Daemons
– Single process for all pequests
– Share variables
Carica HTTP Server
<?php
include(__DIR__.'/../../src/Carica/Io/Loader.php');
CaricaIoLoader::register();
use CaricaIoNetwo...
Carica Firmata
Carica Firmata Board
<?php
$board = new FirmataBoard(
new IoStreamSerial(
CARICA_FIRMATA_SERIAL_DEVICE,
CARICA_FIRMATA_SER...
Carica Firmata Pins
function () use ($board) {
$buttonPin = 2;
$ledPin = 13;
$board->pins[$buttonPin]->mode = FirmataPIN_S...
Example: Blink
function () use ($board, $loop) {
$led = 9;
$board->pinMode($led, FirmataPIN_STATE_OUTPUT);
$loop->setInter...
RGB Wheel
Carica Chip
● Hardware Abstraction
● Device Objects
LED
function () use ($board) {
$led = new CaricaChipLed($board, 9);
$led->blink();
}
RGB LED
function () use ($board) {
$colors = array('#F00', '#0F0', '#00F');
$led = new CaricaChipLedRgb($board, 10, 11, 9)...
Servo
function () use ($board, $loop) {
$positions = array(
0, 45, 90, 180
);
$servo = new CaricaChipServo($board, 7, -180...
Analog Sensor
function () use ($board) {
$sensor = new CaricaChipSensorAnalog($board, 14);
$sensor->onChange(
function ($s...
Thank You
● Questions?
● Twitter: @ThomasWeinert
● Blog: http://a-basketful-of-papayas.net
Upcoming SlideShare
Loading in …5
×

Controlling Arduino With PHP

9,855 views

Published on

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

No Downloads
Views
Total views
9,855
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
80
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Controlling Arduino With PHP

  1. 1. FrOSCon 2013 Controlling Arduino With PHP
  2. 2. About Me ● Thomas Weinert – @ThomasWeinert ● papaya Software GmbH – papaya CMS ● PHP, Javascript, XSLT ● XML Fanatic (JSON is BAD)
  3. 3. Arduino
  4. 4. Arduino Mega
  5. 5. Arduino Nano
  6. 6. Arduino Nano Breadboard
  7. 7. Arduino Nano Breakout
  8. 8. Arduino IDE
  9. 9. Firmata ● MIDI Based ● Serial Port – Forks for TCP ● http://firmata.org
  10. 10. Non-Blocking I/O ● Event Loop ● Event Emitter ● Promises
  11. 11. Event Loop Listeners Loop External Process External Process File
  12. 12. Event Loop ● Timeouts ● Intervals ● Stream Listeners ● Implementations – StreamSelect – LibEvent – MySQLi
  13. 13. Browser Example var e = document.getElementById('output'); var counter = 0; var interval = window.setInterval( function() { e.textContent = e.textContent + counter.toString() + ', '; counter++; }, 1000 );
  14. 14. Event Emitter Object Event Callback Callback Event Callback Event Event ● Attach ● on(), once() ● Trigger ● emit()
  15. 15. Event Emitter Example $stream = new StreamFile('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(); } );
  16. 16. Promises ● Asynchronous Condition ● Attach Callbacks – done() – fail() – always() ● Change Status – reject() – resolve()
  17. 17. jQuery Example jQuery( function () { jQuery .get('hello-world.xml') .done( function (xml) { $('#output').text( $('data', xml).text() ); } ); } );
  18. 18. 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
  19. 19. Carica I/O ● Event Loop – CaricaIoEventLoop ● Event Emitter – CaricaIoEventEmitter ● Promises – CaricaIoDeferred – CaricaIoDeferredPromise
  20. 20. Timers $loop = LoopFactory::get(); $i = 0; $loop->setInterval( function () use (&$i) { echo $i++; }, 1000 ); $loop->setTimeout( function () use ($loop) { $loop->stop(); }, 10000 ); $loop->run();
  21. 21. Asynchronous MySQL $mysqlOne = new IoDeferredMySQL( new mysqli('localhost') ); $mysqlTwo = new IoDeferredMySQL( new mysqli('localhost') ); $time = microtime(TRUE); $debug = function($result) use ($time) { var_dump(iterator_to_array($result)); var_dump(microtime(TRUE) - $time); }; $queries = IoDeferred::When( $mysqlOne("SELECT 'Query 1', SLEEP(5)") ->done($debug), $mysqlTwo("SELECT 'Query 2', SLEEP(1)") ->done($debug) ); IoEventLoopFactory::run($queries);
  22. 22. Asynchronous MySQL
  23. 23. HTTP Server Why?
  24. 24. HTTP Server ● PHP Daemons – Single process for all pequests – Share variables
  25. 25. Carica HTTP Server <?php include(__DIR__.'/../../src/Carica/Io/Loader.php'); CaricaIoLoader::register(); use CaricaIoNetworkHttp; $route = new CaricaIoNetworkHttpRoute(); $route->startsWith( '/files', new HttpRouteFile(__DIR__) ); $server = new CaricaIoNetworkHttpServer($route); $server->listen(8080); CaricaIoEventLoopFactory::run();
  26. 26. Carica Firmata
  27. 27. Carica Firmata Board <?php $board = new FirmataBoard( new IoStreamSerial( CARICA_FIRMATA_SERIAL_DEVICE, CARICA_FIRMATA_SERIAL_BAUD ) ); $board ->activate() ->done( function () use ($board) { ... } ); CaricaIoEventLoopFactory::run();
  28. 28. Carica Firmata Pins function () use ($board) { $buttonPin = 2; $ledPin = 13; $board->pins[$buttonPin]->mode = FirmataPIN_STATE_INPUT; $board->pins[$ledPin]->mode = FirmataPIN_STATE_OUTPUT; $board->digitalRead( $buttonPin, function($value) use ($board, $ledPin) { $board->pins[$ledPin]->digital = $value == FirmataDIGITAL_HIGH; } ); }
  29. 29. Example: Blink function () use ($board, $loop) { $led = 9; $board->pinMode($led, FirmataPIN_STATE_OUTPUT); $loop->setInterval( function () use ($board, $led) { static $ledOn = TRUE; echo 'LED: '.($ledOn ? 'on' : 'off')."n"; $board->digitalWrite( $led, $ledOn ? FirmataDIGITAL_HIGH : FirmataDIGITAL_LOW ); $ledOn = !$ledOn; }, 1000 ); }
  30. 30. RGB Wheel
  31. 31. Carica Chip ● Hardware Abstraction ● Device Objects
  32. 32. LED function () use ($board) { $led = new CaricaChipLed($board, 9); $led->blink(); }
  33. 33. RGB LED function () use ($board) { $colors = array('#F00', '#0F0', '#00F'); $led = new CaricaChipLedRgb($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(); }
  34. 34. Servo function () use ($board, $loop) { $positions = array( 0, 45, 90, 180 ); $servo = new CaricaChipServo($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(), " Gradn"; } if (++$index >= count($positions)) { $index = 0; } }, 2000 ); $next(); }
  35. 35. Analog Sensor function () use ($board) { $sensor = new CaricaChipSensorAnalog($board, 14); $sensor->onChange( function ($sensor) { echo $sensor, "n"; } ); }
  36. 36. Thank You ● Questions? ● Twitter: @ThomasWeinert ● Blog: http://a-basketful-of-papayas.net

×