PHPUG CGN: Controlling Arduino With PHP

66
ARDUINO WITH PHP CONNECTIONS TO THE PHYSICAL WORLD Created by / Thomas Weinert @ThomasWeinert

Transcript of PHPUG CGN: Controlling Arduino With PHP

Page 1: PHPUG CGN: Controlling Arduino With PHP

ARDUINO WITH PHPCONNECTIONS TO THE PHYSICAL WORLD

Created by / Thomas Weinert @ThomasWeinert

Page 2: PHPUG CGN: Controlling Arduino With PHP

ARDUINOArduino is an open-source electronicsprototyping platform based on flexible, easy-to-use hardware and software. It's intended forartists, designers, hobbyists and anyoneinterested in creating interactive objects orenvironments.

Page 3: PHPUG CGN: Controlling Arduino With PHP

ARDUINO UNO

Page 4: PHPUG CGN: Controlling Arduino With PHP

ARDUINO MEGA

Page 5: PHPUG CGN: Controlling Arduino With PHP

SHIELDSHTTP://SHIELDLIST.ORG/

Page 6: PHPUG CGN: Controlling Arduino With PHP

NETWORK

Page 7: PHPUG CGN: Controlling Arduino With PHP

PROTOTYPE

Page 8: PHPUG CGN: Controlling Arduino With PHP

MOTOR

Page 9: PHPUG CGN: Controlling Arduino With PHP

ARDUINO NANO

Page 10: PHPUG CGN: Controlling Arduino With PHP

NANO ON BREADBOARD

Page 11: PHPUG CGN: Controlling Arduino With PHP

NANO ON BASE

Page 12: PHPUG CGN: Controlling Arduino With PHP

BREADBOARD

Page 13: PHPUG CGN: Controlling Arduino With PHP

BREADBOARD HALFSIZE

Page 14: PHPUG CGN: Controlling Arduino With PHP

BREADBOARD SCHEMA

Page 15: PHPUG CGN: Controlling Arduino With PHP

BREADBOARD IN USE

Page 16: PHPUG CGN: Controlling Arduino With PHP

BREADBOARD IN USE

Page 17: PHPUG CGN: Controlling Arduino With PHP

FRITZINGhttp://fritzing.org

11

55

10

10

15

15

20

20

25

25

30

30

AA

BB

CC

DD

EE

FF

GG

HH

II

JJ

Page 18: PHPUG CGN: Controlling Arduino With PHP

ARDUINO IDE

Page 19: PHPUG CGN: Controlling Arduino With PHP
Page 20: PHPUG CGN: Controlling Arduino With PHP

FIRMATA

Page 21: PHPUG CGN: Controlling Arduino With PHP

FIRMATA PROTOCOLMidi BasedPin StatusPin ManipulationExtended Commands

Page 22: PHPUG CGN: Controlling Arduino With PHP

FIRMATA TEST

Page 23: PHPUG CGN: Controlling Arduino With PHP

FIRMATA TEST PHP

Page 24: PHPUG CGN: Controlling Arduino With PHP
Page 25: PHPUG CGN: Controlling Arduino With PHP

PHP - SERIAL PORTS+ Just streams- Console commands for configuration

Page 26: PHPUG CGN: Controlling Arduino With PHP

WINDOWS- fread() blocks until it gets data.

Page 27: PHPUG CGN: Controlling Arduino With PHP

SERPROXY

Page 28: PHPUG CGN: Controlling Arduino With PHP

CARICA CHIP$led = new Carica\Chip\Led($board->pins[13]);$led->strobe()->on();

Page 29: PHPUG CGN: Controlling Arduino With PHP

CARICA CHIP$led = new Carica\Chip\Led($board->pins[13]);$led->pulse()->on();

Page 30: PHPUG CGN: Controlling Arduino With PHP

CARICA CHIP$led = new Carica\Chip\Led($board->pins[13]);$led->brightness(0.5)->pulse()->on();

Page 31: PHPUG CGN: Controlling Arduino With PHP

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

Page 32: PHPUG CGN: Controlling Arduino With PHP

BASIC BLINK$board->pinMode(13, 0x01);while (TRUE) { $board->digitalWrite(13, 0xFF); sleep(1); $board->digitalWrite(13, 0x00); sleep(1);}

Page 33: PHPUG CGN: Controlling Arduino With PHP

BASIC BLINK OOP$board->pins[13]->mode = Carica\Firmata\Pin::MODE_OUTPUT;while (TRUE) { $board->pins[13]->digital = !$board->pins[13]->digital; sleep(1);}

Page 34: PHPUG CGN: Controlling Arduino With PHP

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

Page 35: PHPUG CGN: Controlling Arduino With PHP

CARICA IOEvent LoopEvent EmitterPromises

Page 36: PHPUG CGN: Controlling Arduino With PHP

EVENT LOOP

Page 37: PHPUG CGN: Controlling Arduino With PHP

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

Page 38: PHPUG CGN: Controlling Arduino With PHP

PHP EXAMPLE$loop = Carica\Io\Event\Loop\Factory::get();$loop->setInterval( function () { static $i = 0; echo $i++; }, 1000);$loop->run();

Page 39: PHPUG CGN: Controlling Arduino With PHP

PROMISESThey describe an object that acts as a proxy for aresult that is initially unknown, usually becausethe computation of its value is yet incomplete.

Page 40: PHPUG CGN: Controlling Arduino With PHP

JAVASCRIPT EXAMPLEjQuery .get('hello-world.xml') .done( function (xml) { alert('loaded.'); } ));

Page 41: PHPUG CGN: Controlling Arduino With PHP

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);

Page 42: PHPUG CGN: Controlling Arduino With PHP

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

Page 43: PHPUG CGN: Controlling Arduino With PHP

INTERACTION

Page 44: PHPUG CGN: Controlling Arduino With PHP

HTTPSERVERuse 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; });

Page 45: PHPUG CGN: Controlling Arduino With PHP

FILES$route->match('/hello', new Http\Route\File(__DIR__.'/files/hello.html'));$route->startsWith('/files', new Http\Route\Directory(__DIR__));

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

Page 46: PHPUG CGN: Controlling Arduino With PHP

INTERACTIVE LED

Page 47: PHPUG CGN: Controlling Arduino With PHP

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" </body></html>

Page 48: PHPUG CGN: Controlling Arduino With PHP

PHP SERVERuse 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);

Page 49: PHPUG CGN: Controlling Arduino With PHP

COLOR CIRCLE

Page 50: PHPUG CGN: Controlling Arduino With PHP

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

Page 51: PHPUG CGN: Controlling Arduino With PHP

REACTPHP{ "require": { "react/react": "0.4.*" }}

Page 52: PHPUG CGN: Controlling Arduino With PHP

RATCHET{ "require": { "cboden/ratchet": "0.3.*" }}

Page 53: PHPUG CGN: Controlling Arduino With PHP

SENSOR PHALANX

1:07

Page 54: PHPUG CGN: Controlling Arduino With PHP

SHIFTOUT()

Page 55: PHPUG CGN: Controlling Arduino With PHP

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);

Page 56: PHPUG CGN: Controlling Arduino With PHP

SHIFTOUT()

0:15

Page 57: PHPUG CGN: Controlling Arduino With PHP

7 SEGMENT DISPLAYS

Page 58: PHPUG CGN: Controlling Arduino With PHP

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);

Page 59: PHPUG CGN: Controlling Arduino With PHP

7SEG DISPLAYS

0:05

Page 60: PHPUG CGN: Controlling Arduino With PHP

FIRST PROJECT

Page 61: PHPUG CGN: Controlling Arduino With PHP

COMPOSER CREATEcomposer create-project carica/chip-skeleton led --stability=dev

Page 62: PHPUG CGN: Controlling Arduino With PHP

COMPOSER CREATE

Page 63: PHPUG CGN: Controlling Arduino With PHP

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);

Page 64: PHPUG CGN: Controlling Arduino With PHP

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

Page 65: PHPUG CGN: Controlling Arduino With PHP

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

Page 66: PHPUG CGN: Controlling Arduino With PHP

THANKS