Slide 1

Slide 1 text

ARDUINO WITH PHP CONNECTIONS TO THE PHYSICAL WORLD @ThomasWeinert

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

ARDUINO UNO

Slide 4

Slide 4 text

ARDUINO MEGA

Slide 5

Slide 5 text

SHIELDS HTTP://SHIELDLIST.ORG/

Slide 6

Slide 6 text

NETWORK

Slide 7

Slide 7 text

PROTOTYPE

Slide 8

Slide 8 text

MOTOR

Slide 9

Slide 9 text

ARDUINO NANO

Slide 10

Slide 10 text

NANO ON BREADBOARD

Slide 11

Slide 11 text

NANO ON BASE

Slide 12

Slide 12 text

BREADBOARD

Slide 13

Slide 13 text

BREADBOARD HALFSIZE

Slide 14

Slide 14 text

BREADBOARD SCHEMA

Slide 15

Slide 15 text

BREADBOARD IN USE

Slide 16

Slide 16 text

BREADBOARD IN USE

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

ARDUINO IDE

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

FIRMATA

Slide 21

Slide 21 text

FIRMATA PROTOCOL Midi Based Pin Status Pin Manipulation Extended Commands

Slide 22

Slide 22 text

FIRMATA TEST

Slide 23

Slide 23 text

FIRMATA TEST PHP

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

WINDOWS - fread() blocks until it gets data.

Slide 27

Slide 27 text

SERPROXY

Slide 28

Slide 28 text

GORILLA PHPMAKE\SERIALPORT https://github.com/oasynnoum/phpmake_serialport

Slide 29

Slide 29 text

FIRST PROJECT

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Io Event Loop Server Firmata Protocol Chip Devices 010001000100 111000111000 011111110001 100100100111 100100101100 001100110110

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

CARICA IO Event Loop Event Emitter Promises

Slide 44

Slide 44 text

EVENT LOOP

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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.

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

INTERACTION

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

INTERACTIVE LED

Slide 55

Slide 55 text

HTML FILE Led Switch On Off </ifra </body> </html>

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

COLOR CIRCLE

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

SENSOR PHALANX 1:07

Slide 62

Slide 62 text

SHIFTOUT()

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

SHIFTOUT() 0:15

Slide 65

Slide 65 text

7 SEGMENT DISPLAYS

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

7SEG DISPLAYS 0:05

Slide 68

Slide 68 text

MAX7219 8 Digit - 7Segment Display 8x8 Matrix Displays

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

0:00

Slide 71

Slide 71 text

THANKS