{"id":1855,"date":"2014-12-27T16:01:52","date_gmt":"2014-12-27T16:01:52","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=1855"},"modified":"2015-02-19T11:57:34","modified_gmt":"2015-02-19T11:57:34","slug":"uart-control-from-php","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-php-and-javascript\/uart\/uart-control-from-php","title":{"rendered":"UART control from PHP"},"content":{"rendered":"<h4>\nTurning off the UART functioning as a serial console<br \/>\n<\/h4>\n<p>\nSee the instructions <a href=\"https:\/\/raspberry-projects.com\/pi\/pi-operating-systems\/raspbian\/io-pins-raspbian\/uart-pins\">here<\/a>.\n<\/p>\n<h4>\nSetup Permission<br \/>\n<\/h4>\n<p>\nYou can determine the user PHP&nbsp;is running as with this command in a php page:\n<\/p>\n<pre>\r\n<code>\r\n&lt;?php\r\necho exec(&#39;whoami&#39;);\r\n?&gt;\r\n<\/code><\/pre>\n<p>\nIt will typcially&nbsp;be &#39;www-data&#39;. &nbsp;All serial connections,&nbsp;virtual or&nbsp;physical, are&nbsp;owned by the &#39;dialout&#39; group&nbsp;so if you&nbsp;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.\n<\/p>\n<pre>\r\n<code>\r\nusermod -a -G dialout www-data\r\n<\/code><\/pre>\n<p>\nYou can confirm this with:\n<\/p>\n<pre>\r\n<code>\r\ngroups www-data\r\n<\/code><\/pre>\n<p>\nYou now need to restart the RPi for the setting to take effect.\n<\/p>\n<h4>\nTransmit A String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n&lt;?php\r\n\techo system(&quot;echo \\&quot;hello\\&quot; &gt; \/dev\/ttyAMA0 &quot;);\r\n?&gt;\r\n<\/code><\/pre>\n<p>\nInclude this in a php page, load the page using a browser&nbsp;and you should see the data being sent out of the UART TX pin.\n<\/p>\n<p>\nIf you want to include variables:\n<\/p>\n<pre>\r\n<code>\r\n\t\t\/\/Set baud rate\r\n\t\tsystem(&quot;stty -F \/dev\/ttyAMA0 57600&quot;);\r\n\t\t\r\n\t\t\/\/Send string including variables\r\n\t\tsystem(&quot;sudo echo \\&quot;Hello the value is: &quot; . (string)$some_int_variable . &quot;and also: &quot; . escapeshellarg($some_string_variable) .\t&quot;\\&quot; &gt; \/dev\/ttyAMA0&quot;);\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Turning off the UART functioning as a serial console See the instructions here. Setup Permission You can determine the user PHP&nbsp;is running as with this command in a php page: &lt;?php echo exec(&#39;whoami&#39;); ?&gt; It will typcially&nbsp;be &#39;www-data&#39;. &nbsp;All serial connections,&nbsp;virtual or&nbsp;physical, are&nbsp;owned by the &#39;dialout&#39; group&nbsp;so if you&nbsp;add www-data to the dialout group your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94],"tags":[],"class_list":["post-1855","post","type-post","status-publish","format-standard","hentry","category-uart"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=1855"}],"version-history":[{"count":4,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1855\/revisions"}],"predecessor-version":[{"id":1877,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1855\/revisions\/1877"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=1855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=1855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=1855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}