MK Dynamics

Exploring PHP


Space

Using PHP to control the Arduino

Arduino Code
The following code is uploaded to an Arduino UNO R3. It listens to the serial port and
activates a digital I/O pin based on the number it received from the serial port. If it received
a "1", then it illuminates the first LED, if it receives a "2", then it illuminated the second LED,
if it receives a "3", it illuminates the third LED. The code is shown below:

PHP Code
The following PHP is run within an HTML page. It opens the serial port and sends data to the Arduino.

"; $comPort = "/dev/ttyACM0"; /*change to correct com port */ $fp =fopen("/dev/ttyACM0", "w") or die("Can't open port!!"); $count = 1; while ($count <=5) { echo "Now outputting a 1 onto the serial port...
"; fwrite($fp,1); echo "Now outputting a 2 onto the serial port...
"; fwrite($fp,2); echo "Now outputting a 3 onto the serial port...
"; fwrite($fp,3); ++$count; } /*echo fgetc($fp);*/ fclose($fp); ?>