Control your Arduino Pro Mini by Apache2 and PHP5 on BananaPi M1
The PHP5 code:
<?php
$verz="1.0";
$comPort = "/dev/ttyUSB0"; /*change to correct com port */
$led = $_POST["led"];
$fp =fopen($comPort, "w");
sleep(2);
fwrite($fp, $led); /* this is the number that it will write */
fclose($fp);
?>
<html>
<body>
<center><h1>Arduino from PHP Example</h1><b>Version <?php echo $verz; ?></b></c$
<form method="post" action="<?php echo $PHP_SELF;?>">
    
<input type="text" name="led">
<input type="submit" value="OK">
<br />
</form>
</body>
</html>
Arduino code :
int motor1Pin = 9; // the first motor's port number
int usbnumber = 0; //this variable holds what we are currently reading from serial
void setup() { //call this once at the beginning
pinMode(motor1Pin, OUTPUT);
Serial.begin(9600); //start up serial port
}
void loop() { //main loop
if (Serial.available() > 0) { //if there is anything on the serial port, read it
usbnumber = Serial.read(); //store it in the usbnumber variable
}
if(usbnumber == '1')
digitalWrite(motor1Pin, HIGH);
else
digitalWrite(motor1Pin, LOW);
}
Please don't forget grant your PHP5 is able to access your serial port first, by the following commands:
pi@bananapi:~$ sudo adduser www-data dialout
Adding user `www-data' to group `dialout' ...
Adding user www-data to group dialout
Done.
pi@bananapi:~$ sudo /etc/init.d/apache2 restart
[ ok ] Restarting apache2 (via systemctl): apache2.service.
The demostration is on YouTube now :
翻譯年糕