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 299<br />

85 else<br />

86 {<br />

87 buffer += in;<br />

88 }<br />

89<br />

90 if (in == '.')<br />

91 {<br />

92 index = 0;<br />

93 buffer = "";<br />

94 handle();<br />

95 }<br />

96 }<br />

97 }<br />

.<br />

This code needs to be uploaded to the Arduino you’re working with for any communication<br />

between our server and the Arduino to take place.<br />

The Arduino sketch is only one side of the puzzle. We also need to modify our socket server to<br />

communicate with the Arduino:<br />

1 protected $device;<br />

2<br />

3 public function __construct(SocketInterface $socket)<br />

4 {<br />

5 parent::__construct();<br />

6<br />

7 $this->socket = $socket;<br />

8<br />

9 $socket->getEmitter()->on("message", function($message)<br />

10 {<br />

11 fwrite($this->device, $message);<br />

12<br />

13 $data = trim(stream_get_contents($this->device));<br />

14 $this->info($data);<br />

15<br />

16 $this->socket->send($data);<br />

17 });<br />

18<br />

19 $socket->getEmitter()->on("error", function($e)<br />

20 {

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

Saved successfully!

Ooh no, something went wrong!