xl97
Posts: 47
Joined: Thu Jan 12, 2017 3:34 pm

PHP & Serial comm with RPi?

Wed Aug 08, 2018 9:20 pm

I have attempted serial comm via PHP to Arduono using a WAMP install on a PC...

serial comms seemed to work only one way.. and I was told that this was a PC issue, but on MAC/Linux, PHP serial comm works two ways without issue.

I have previously set up a RPi with Apache, MySQL and PHP...... (for some added support/features in an MQTT set-up/project)

I have never attempted to use PHP serial communication when things were on a Pi.

I will have an Arduino connected to the RPi.....
The RPi will have a webpage served up that I write.

What I would like to happen is upon clicking a link/hotspot in the hosted webpage it to send some serial data upon $_POST[] to the connected Arduino via PHP... have the webpage sit in an 'idle' state... and when the Arduino is done doing its task(s).. send out a serial message back to the RPi/PHP script.. and then change the display state/view of the hosted webpage.. (waiting for another 'user interaction' so to speak)


Re-cap:
Is 2-way serial communication possible with PHP when running on an RPi3?

Thanks

User avatar
DougieLawson
Posts: 33805
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: PHP & Serial comm with RPi?

Thu Aug 09, 2018 3:49 pm

If it runs under Apache2, Lighttpd or NGinx then you'll need to add user www-data to the dialout group.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

xl97
Posts: 47
Joined: Thu Jan 12, 2017 3:34 pm

Re: PHP & Serial comm with RPi?

Thu Aug 09, 2018 9:37 pm

Thank you for the reply.

I have VERY LITTLE linux experience..... so once I get to that point of testing (in like 4 weeks I'm guessing).. I'll have to do some searching about this 'dial out group' stuff..

Clarification:

If sending OUT via PHP works normally under a default WAMP install..

using like so:

Code: Select all

<?PHP

     $pwmValue=$_POST["pwmValue"];

     exec("mode COM4 BAUD=9600 PARITY=N data=8 stop=1 xon=off");

     $fp = fopen("com4", "w");

     if (!$fp) {
        echo "Not open";
     }else {
        fwrite($fp, $pwmValue);
        fclose($fp);
     }

?>


and only the INCOMING data (from Arduino >> PHP) doesnt work...

is it still the 'dialout' group? (That allows for both incoming and outgoing data communication?


Its been a while since I installed MySQL/PHP...etc on a Raspberry Pi (and I've only done it tone time anyways).. I believe it was Apache.


Either way.. sounds like it wont be an issue (just need to 'do it')...

thanks!

User avatar
DougieLawson
Posts: 33805
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: PHP & Serial comm with RPi?

Fri Aug 10, 2018 6:51 am

Open an LXTerminal window and enter these two commands
sudo usermod -a -G dialout www-data
sudo reboot
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

xl97
Posts: 47
Joined: Thu Jan 12, 2017 3:34 pm

Re: PHP & Serial comm with RPi?

Thu Sep 20, 2018 9:35 pm

@DougieLawson

Thank you for the reply..

That seemed to work (no more port not open errors)..

Currently.. I have moved away from testing on my Windows PC/WAMP.. and have moved over to the RPI. Everything as far as set-up and installing apps/packages is working as expected. I have a DB with populated table(s).. and my web app is loading and displaying all dynamically generated content correctly as well.

My current testing phase is actual serial communication (2-way) from RPi/PHP >> Arduino ... and.. Arduino >> RPi/PHP

Since I can not do this in a Windows environment, I have my Arduino connected via USB to my RPI...

I load up the Chromium browser.. and my web page (drink menu) is displayed to me..

I click on the 'order' button.. (which saves the button data to a hidden field, upon $_POST.. that data is sent via PHP to the connected Arduino.)

And here is where I am currently..

* Since the Arduino is now connected via USB to the RPi.. I can not use the serial monitor to know when/what serial data is being sent BACK to the RPi...

* I believe that the Arduino is receiving the (some) serial data.. because when I click on the 'order' button and submit the webpage. I see the RX/TX lights on the Arduino start to blink.


Question:
I'm having trouble thinking of a way I can DEBUG this.. (without serial monitor or a way to see what is being sent BACK to the RPi)..

I was also not clear on how to go about reading (listening) to the serial port.. since the webpage will already be parsed after submission (to display a please wait type message)...

So I figured possibly an AJAX type of approach.. so the page can send initial serial data to connected Arduino.... display a 'please wait' message..... (All done server side via PHP after submit)

and then have a little AJAX snippet call an external PHP script that does the serial read? (also not sure where the best places to open/close the port is.. if using an external script to READ the serial port?) Can 1 php script open the port.. and another still access it?


Here is my current approach to attempt to read the SERIAL data back form the Arduino after it is finished (no telling when this data will come back from the Arduino)

Code: Select all


<?
if ($mode == 'submit') {
    
	//grab posted data (save to var)
    $drinkRecipe = $_POST['selectedDrink'];
    
    //set-up com port    
    exec("mode /dev/ttyACM0 BAUD=9600 PARITY=N data=8 stop=1 xon=off");
	//saw this on several RPi posts?  (but not sure of the difference? or why one would be used over the other?)
    //stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
    
    //open serial port
    $fp = fopen("/dev/ttyACM0", "w+"); //w = write w+ = read/write
        
    //check if open
    if (!$fp) {
        echo "Not open";
        //die();
    } else {
        //if open send data (via PHP) to connected Arduino on serial comm port 1 (ttyACM0)
        fwrite($fp, '<' . $drinkRecipe . '>');
		
		
		//arduino takes serial data, parsed it.. does it duty, 
		//and is supposed to reply back via serial to the awaiting (listening) 
		//PHP script executed via AJAX, since the front end needs to display 
		//a 'waiting' type message.. and the callback 'success' will 
		//re-direct back to menu/initial state
		?>
		<script type="text/JavaScript" language="JavaScript">
			$.ajax({
				//async: false,
				//type: "POST",
				url: "serial_listener.php",
				
				//define success handler actions
				success: function(response) {
					//alert("PHP RETURN CHECK: "+response);
					if($.trim(response) == 'complete'){
						alert("Drink making is complete... return to main menu");
						//do redirect here
					}else{
						alert("Some value other than 'complete' was returned... look into it!");
						//not sure what to do? (back to main menu anyways?)
					}
				},
				//define error handler actions
				error: function(response) {
					alert("PHP SERIAL READ FAIL: "+ 'Ready State: '+ response.readyState + ' Status: ' + response.status);
					
				}
			});	
		</script>		
		<?		        
        
        //close connection
        fclose($fp); //needed? //should this go in the external php script instead now?

    }    

}

Any suggestions to try and wrap my head around this is appreciated.

Thanks!

ghans
Posts: 7792
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: PHP & Serial comm with RPi?

Fri Sep 21, 2018 7:29 am

Linux follows the "everything is a file" motto of Unix. As soon you plug an Arduino into the Raspberry, a file should magically appear in /dev. This file represents a serial port and can be read and written to. The handling has not changed in the last 40 years so software like screen and minicom should directly work with it. Those are bidirectional serial terminals.

For Windows users PuTTY or GTKTerm might be better choices since they are graphical programs. All those programs can be installed via apt-get.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

xl97
Posts: 47
Joined: Thu Jan 12, 2017 3:34 pm

Re: PHP & Serial comm with RPi?

Fri Sep 21, 2018 5:19 pm

Thank you for the reply..

Are these suggestions for DEBUGGING?

I dont know what: "screen and minicom" are...

* So when trying to read/send data over serial via PHP.....I can treat things like a file..

So in the example posted above...

everything is correct?

And in my 'serial_listener.php' file.. I should.... (kinda lost on how to go about READING the serial port... because it wont be an 'instant' response...
The Arduino has to complete its task(s) before it will kick back the serial 'conformation' to the RPi/PHP script...

So what is the best way to 'wait' for this serial data to come?
Do I need to open the port again from a different script? (or is opening it in one file enough for other files to use it as well?)

Here is my PHP script/file.... (a portion of it at least).. that deals with the SUBMISSION... (ie: sending the serial data OUT to the connected Arduino)...

And then the AJAX snippet that calls another (external) .php script that does the SERIAL port listening and returns the data back to the AJAX on success callback..

Code: Select all

if ($mode == 'submit') {
    
	//grab posted data (save to var)
    $drinkRecipe = $_POST['selectedDrink'];
    
    //set-up com port    
    exec("mode /dev/ttyACM0 BAUD=9600 PARITY=N data=8 stop=1 xon=off");
	//saw this on several RPi posts?  (but not sure of the difference? or why one would be used over the other?)
    //stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
    
    //open serial port
    $fp = fopen("/dev/ttyACM0", "w+"); //w = write w+ = read/write
        
    //check if open
    if (!$fp) {
        echo "Not open";
        //die();
    } else {
        //if open send data (via PHP) to connected Arduino on serial comm port 1 (ttyACM0)
        fwrite($fp, '<' . $drinkRecipe . '>');
		
		
		//arduino takes serial data, parsed it.. does it duty, 
		//and is supposed to reply back via serial to the awaiting (listening) 
		//PHP script executed via AJAX, since the front end needs to display 
		//a 'waiting' type message.. and the callback 'success' will 
		//re-direct back to menu/initial state
		?>
		<script type="text/JavaScript" language="JavaScript">
			$.ajax({
				//async: false,
				//type: "POST",
				url: "serial_listener.php",
				
				//define success handler actions
				success: function(response) {
					//alert("PHP RETURN CHECK: "+response);
					if($.trim(response) == 'complete'){
						alert("Drink making is complete... return to main menu");
						//do redirect here
					}else{
						alert("Some value other than 'complete' was returned... look into it!");
						//not sure what to do? (back to main menu anyways?)
					}
				},
				//define error handler actions
				error: function(response) {
					alert("PHP SERIAL READ FAIL: "+ 'Ready State: '+ response.readyState + ' Status: ' + response.status);
					
				}
			});	
		</script>		
		<?		        
        
        //close connection
        fclose($fp); //needed? //should this go in the external php script instead now?

    }    

}

And the contents of the serial_listener.php script:

Code: Select all

//set-up com port    
exec("mode /dev/ttyACM0 BAUD=9600 PARITY=N data=8 stop=1 xon=off");

//open serial port
$fp = fopen("/dev/ttyACM0", "w+"); //w = write w+ = read/write

//check if open
if (!$fp) {
	echo "Not open";
	//die();
	
} else {

	while(!feof($fp)){
		$response = fread($fp, 10);
	}
	echo $response;
	fclose($fp);

}

ghans
Posts: 7792
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: PHP & Serial comm with RPi?

Sun Sep 23, 2018 7:31 am

1) All the programs i mentioned should aid you in debugging serial comms. Since you seem unfamiliar with Linux, GTKTerm or PuTTY might be the better choice instead of the other two options.
2) The "MODE" command does not exist on Linux.
3) You should put the endless loops into your JavaScript instead of the PHP files.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

xl97
Posts: 47
Joined: Thu Jan 12, 2017 3:34 pm

Re: PHP & Serial comm with RPi?

Sun Sep 23, 2018 3:03 pm

Thank you for the reply.

So I set everything up... Arduino , Pi, browser to load the hosted page....etc.

I click on an 'order' button...

* the page submits..
* the 'drink recipe' is sent via PHP to the serial comm
* connected Arduino gets the outgoing serial data
* Arduino parses serial data
* Arduino does what is needs to do (stepper, servo actions..returns to home)
* Arduino fires off 'complete' serial data BACK TO RPi...

And at this point.. I am NOT getting the response data.. the as the page never throws an alert/console.log..etc..

If I it the 'back' button in the browser.. I will actually see the AJAX error callback fired in the BEBUG (Web dev tools) window of FireFox..

Code: Select all

error: function(response) {
     alert("PHP SERIAL READ FAIL: "+ 'Ready State: '+ response.readyState + ' Status: ' + response.status);
}
So for some reason.. I am NOT getting an AJAX response/callback function fired.... (ever)..



To touch on your comments:

1.) MODE ok.. so this line is not valid your saying?

Code: Select all

exec("mode /dev/ttyACM0 BAUD=9600 PARITY=N data=8 stop=1 xon=off");
So what do I use?

this:

Code: Select all

stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
I dont even understand that line.. NOR all the parameters?

but perhaps this is WHY I am not receiving response data? the comm port isnt configured correctly?


2.) Why put the loop in the AJAX call? (not in the external php script that is actually doing the serial port listening?)
Wouldnt (doesnt) the AJAX call just 'wait' until the PHP response is complete in one fashion or another? and then it will execute one of the callback functions once the data arrives? (or fails) I was under the impression that is how the AJAX stuff worked?

let the php stuff does its thing... and the ajax call will handle the response when its ready/there is one.

User avatar
topguy
Posts: 4857
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: PHP & Serial comm with RPi?

Sun Sep 23, 2018 11:28 pm

Yes you use the "stty" command, you dont need to understand the parameters, but if you want : http://man7.org/linux/man-pages/man1/stty.1.html
Linux treats serialports as "terrminals" and might do certain things with some special characters, you are just turning all that OFF.

I would consider the arduino send messages back more often, not only when everything it done.
- "recipe recevied"
- "recipe ok" ( or "not ok" )
- "step 1" ( "step 2", etc )
- and then at the end "complete"

xl97
Posts: 47
Joined: Thu Jan 12, 2017 3:34 pm

Re: PHP & Serial comm with RPi?

Mon Sep 24, 2018 1:44 am

Thank you.. and I did run that line anyways.

I would like to send confirmations back more often form the Arduino..... but for now.. I need to start with '1' LOL

For the life of me could NOT get any response form the Arduino back to the RPi..

then all of a sudden 'some combination' of things started working..

with a trimmed down attempt like this:

Code: Select all

$responseValue = "";
	do{
		$incomingChar = fread($fp, 1);
		$responseValue .= $incomingChar;
	}while(strlen($responseValue) < 1);
	echo $responseValue;
However.. for some reason now (and for the last few hours).. I have not been able to get communication back.

Some combination of rebooting,, or capacitor on Arduino to Stop reset.... or 'something' happened.. and I have not been able to connect to port again.

Writing to Arduino has worked flawlessly every time. :(



If I just send back something static to check/ensure the AJAX callback are working.. and will wait until a response comes (whenever that is)..

like:

sleep(5);
echo 'complete';

the 'web page' gets the response without error every time.

So its not an AJAX/callback response thing..

its 100% the READING aspect/approach.


Random thoughts:

* opening the port in the MAIN php file (where the data gets SENT to Arduino from RPi).
-- does it needs to be closed in the file as well?
-- do I need to be opening it in the external serial_listener.php script? (answer: yes..or it throws error.. perhaps trying to pass an instance of that port via the AJAX $_POST?)

* How did I 'magically' get things working before? but can no longer connect to port reading? or get any data returned?

xl97
Posts: 47
Joined: Thu Jan 12, 2017 3:34 pm

Re: PHP & Serial comm with RPi?

Fri Sep 28, 2018 12:50 pm

Bummer! I got notification there was a reply! (was hoping someone was going to help me out here!) Must have been spam?

To-recap where I am to see if anyone has some suggestions..

Using USB CABLE from Arduino to RPi...

* Add 'www-data' user to dial-out group
# sudo usermod -a -G dialout www-data
# sudo reboot


* Give read/write permissions to the port for all users to the Raspberry Pi (potentially unsafe):

# sudo chmod 777 /dev/ttyACM0


I can NOT understand why I can SEND data out to Arduino.. but can NOT READ any data..

I open the PORT, (I know this is working because I dont get the NOT OPEN error in my PHP script.. and the SENDING of data TO Arduino works, but the subsequent READ, when the same port is STILL OPEN.. does not return anything)


Here is the MAIN (PHP) page.. that has the content. and submits the data to another PHP script (that does the port opening, and send/receiving).. and returns the data back to the MAIN PHP script.. that has an AJAX call.. and waiting callbacks to handle this response)..

If I hard code something as a response.. it works fine.. even if I add a 10 second delay.. and then hard code a response.. the AJAX callbacks handle it FINE... its the READ of the serial port that is failing.

Main script that has the AJAX handler in it (this not only SENDS data to the Arduino.. which works fine every time,,, but also should handle the RESPOSNE from the same php snippet)

Code: Select all

if ($mode == 'submit') {
    
	//grab posted data (save to var)
    $drinkRecipe = $_POST['selectedDrink'];
	
	?>	
	<div id="waitingContainer">
		<p>Please wait, your brink is being made.</p>
	</div>
	
	<script type="text/JavaScript" language="JavaScript">
		console.log("ajax routine hit");
		
		//var drinkRecipe = "<?php echo $drinkRecipe ?>";
		var drinkRecipe = "<?=$drinkRecipe?>";
		
		var xhr = $.ajax({
			//async: false,
			type: "POST",
			url: "serial_listener.php",
			//datatype: "html",
			datatype: "text",
			data:({"drinkData":drinkRecipe}),
			//define success handler actions
			success:function(response) {
				//alert("PHP RETURN CHECK: "+response);
				if($.trim(response) == 'complete'){
					console.log("Drink making is complete... return to main menu");
					//do redirect here
				}else{
					console.log("Some value other than 'complete' was returned... look into it!");
					console.log("RESPONSE SENT WAS: " + response);
					//not sure what to do? (back to main menu anyways?)
				}
				//kill the request
				xhr.abort();
			},
			//define error handler actions
			error: function(response) {
				console.log("PHP SERIAL READ FAIL: "+ 'Ready State: '+ response.readyState + ' Status: ' + response.status);
				//kill the request
				xhr.abort();
			}
		});		
	</script>
	<?		       
}
Here is the contents of the serial_listener.php script that the above AJAX call loads/executes..

** Again.. sending the data OUT to ARDUINO works every time.. but I can NOT get a READ:

Code: Select all

$drinkData = $_POST['drinkData'];

//open serial port
$fp = fopen("/dev/ttyACM0", "w+"); //w = write w+ = read/write
//$fp = fopen("/dev/ttyUSB0", "w+"); //w = write w+ = read/write  //tried with USB-TTL cable too.. couldnt even send data)

//check if open
if (!$fp) {
	echo "Not open";
	//die();	
} else {

	if($drinkData != ''){	
		//send drink data
		fwrite($fp, '<' . $drinkData . '>');
		
		//wait for response
		$chars = "";
		do{
			$char = fread($fp, 1);
			$chars .= $char;
		}while(strlen($char) < 1);
		echo $char;
	}else{
		echo 'drink recipe is empty';
	}	
}

What can I do here? I'd rather use the USB cable.. and NOT a USB-TTL adapter type of approach.

Is this possible? I believe others have successfully used a USB cable to enable 2-way serial communication from RPI >> Arduino?\

I believe the port has the correct permissions...

I have tried use a capacitor on the Arduino RESET/GND pins to stop the reset when open/closing the ports... (seems to stop things for working properly though)

This is the LAST step in this project (for the front end).. and I have literally been throwing a few hours at this almost every day for a week..

changing the front end code... using AJAX... doing READ/WRITE in one AJAX call... trying USB-TTL cables..etc...

Please, any suggestions are appreciated! :)

ghans
Posts: 7792
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: PHP & Serial comm with RPi?

Sat Sep 29, 2018 1:55 pm

Make the Arduino output much more debug messages (say 5-7 per second) and filter all those debug messages with JavaScript.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

xl97
Posts: 47
Joined: Thu Jan 12, 2017 3:34 pm

Re: PHP & Serial comm with RPi?

Sat Sep 29, 2018 5:31 pm

I'm sorry.. I guess I'm not following or understanding your suggestion here?

I only need the Arduino to output ONE response.. (for now) telling me the actions are complete.... so I can update the state of the webpage.

The php while() function is already waiting/listening for ANY response on the PORT... before its being sent.

The while() function executes.. and I'm stuck in it, because it NEVER gets anything..

I updated the Arduino sketch to output the same 'complete' string.. wait for 1/4 of second.. send it again.. wait another 1/4 second.. send another.. and did like 4-5 times... so we'll see if it that helps getting the data back over to the RPi I guess...

and nothing.

Its got to be the configuration of the port perhaps?

Return to “Other programming languages”

Who is online

Users browsing this forum: No registered users and 1 guest