03.12.2015 Views

laravel4cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Embedded Systems 296<br />

43 $this->socket<br />

44 )<br />

45 ),<br />

46 $port<br />

47 );<br />

48<br />

49 $this->info("Listening on port " . $port . ".");<br />

50 $server->run();<br />

51 }<br />

52<br />

53 protected function getOptions()<br />

54 {<br />

55 return [<br />

56 [<br />

57 "port",<br />

58 null,<br />

59 InputOption::VALUE_REQUIRED,<br />

60 "Port to listen on.",<br />

61 8081<br />

62 ]<br />

63 ];<br />

64 }<br />

65 }<br />

.<br />

This file should be saved as workbench/formativ/embedded/src/Formativ/Embedded/Command/Serve.php.<br />

These four files create a means with which we can start a socket server, listen for messages and<br />

respond to them. The shared.js file connects to this server and allows us to send and receive these<br />

messages. It’s simpler than last time; but sufficient for our needs!<br />

Connection To Arduino<br />

Real-time communication is typically done through a software layer called Firmata. There are<br />

Firmata libraries for many programming languages, though PHP is not one of them. While there is<br />

one library listed on the official Firmata website, I could not get it to work.<br />

So we’re going to create our own simple Firmata-like protocol. I managed to put one together that<br />

handles analog reads, analog writes and servo control. It does this by parsing strings received over<br />

serial connection. This is what it looks like:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!