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");

 

 

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

  1. Mark

    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?

Comments

Your email address will not be published. Required fields are marked *