Turning off the UART functioning as a serial console
See the instructions here.
Setup Permission
You can determine the user PHP is running as with this command in a php page:
<?php
echo exec('whoami');
?>
It will typcially be 'www-data'. All serial connections, virtual or physical, are owned by the 'dialout' group so if you add www-data to the dialout group your PHP scripts will be able to open/read/write the UART (or other serial devices). The following command will add the group to www-data.
usermod -a -G dialout www-data
You can confirm this with:
groups www-data
You now need to restart the RPi for the setting to take effect.
Transmit A String
<?php
echo system("echo \"hello\" > /dev/ttyAMA0 ");
?>
Include this in a php page, load the page using a browser and you should see the data being sent out of the UART TX pin.
If you want to include variables:
//Set baud rate
system("stty -F /dev/ttyAMA0 57600");
//Send string including variables
system("sudo echo \"Hello the value is: " . (string)$some_int_variable . "and also: " . escapeshellarg($some_string_variable) . "\" > /dev/ttyAMA0");
2 years ago
Thanks for this tutorial, it’s very useful. Could you perhaps expand this to show how to read data from UART in PHP too?