Hey Gang-
I know many here like to tinker with Arduino/RPi stuff...
I have a project that I am working on.. and have been STUCK for over a week now.... and finally need to reach out and ask for some help/advice/suggestions. :)
* Disclaimer: I am using PHP, please no mention of Python, I dont know any Python, I -want- to use PHP :)
Long story short:
Arduino connected to RPi via straight USB cable
RPi has Apache, MySQL, PHP, PhpMyAdmin installed on it
Locally hosted web page (RPi) coded and working as expected...
My 'web page' is basically just a 'menu' GUI/front end.. (scroll.. find something you like.. hit order button)
Page submits to itself.. where I grab the $_POST string value.
When the page submits.. I am using an AJAX call to load/execute an external .php script...
in this external .php script.... is where I am doing:
* the port opening...
* writing (sending the $_POST data from above) to the serial port (connected Arduino)..
* Arduino 'does its thing' (move some steppers/servos around..etc)
* Arduino send back a serial confirmation stating it is done/complete
^ At this point.. everything works fine.. stable & reliable.
I do NOT get port not open error...
Arduino always gets and parses its incoming serial data (sent from the RPi/PHP)
Arduino -always- sends back its completed serial confirmation..
--------------
Here is where the problem comes into play... after I open the port.. send [ fwrite() ] my string data to the Arduino....
I then put the php script into a 'loop' checking the serial port over and over [ fread() ] until I get a response (character).... (there is no set time on when the Arduino response will come back)
However, I never can actually get a read/response*
I am just stuck in the while loop() checking the serial port/reading it over and over...
if I comment out the loop and add a static echo 'complete'; statement, it works fine! (EACH AND EVERY TIME!)...
I can even put a sleep(10); action before it.. and the awaiting AJAX callback will still receive it.. (10 seconds later!)...
I have read, and read,.... and read some more.. everything I have tried (except one test) did not work. I can NEVER get a READ from the serial port.
I am not well versed with using RPi's for projects... and am a complete beginner with Linux and CLI stuff... (so I sorta read, and blindly execute some of the commands)
* I stumbled upon others talking about the same issues..(stack overflow).. where someone mentioned using 'minicom' (not sure what minicom even is.. my best understanding is some sort of terminal app?)
I was (am) desperate for a solution.. so I installed this minicom.... and started it up manually (using Putty on my PC and connecting to the RPi)...
When minicom was 'running'.... and I then loaded the page up, submitted the form... data got sent to the Arduino and it did its tihng (as normal).. but then the SERIAL READ WORKED!!..
I got a response/read from the serial port......and it was echo'd back to the awaiting AJAX (success) callback!.. Which in turn refreshes the webpage and brings it back to the 'menu' front end.. awaiting for another submission!!
This works exactly 2 times!.. on the 3rd time.. I saw my response show up/display in this 'minicom' screen? the 4th submission... nothing.. on either end (webpage or minicom).. and the minicom menu/screen... seemed to be frozen.
So my questions are:
1.) How can I configure the PORT correctly on the RPi so I can RELIABLY read it using PHP each and every time?
2.) What is this minicom doing that cant be set permanently on the RPi port settings itself? (and why did it steal my data on the 3rd attempt.. and then freeze?)
Here is the 'submit' routine ofthe MAIN .php page (the HTML form)
if ($mode == 'submit') {
//grab posted data (save to var)
$drinkRecipe = $_POST['selectedDrink'];
//output some jQuery/AJAX call to an external PHP script that does the port LISTENING, and respond back once data is received
?>
<script type="text/JavaScript" language="JavaScript">
var submittedDrink = <?=$drinkRecipe?>;
$.ajax({
//async: false,
type: "POST",
url: "serial_listener.php",
datatype: "text",
data:{
"submittedDrink": submittedDrink
},
//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!");
alert("Response Received: " + response);
//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>
<?
}
And an example of the code/routine in the serial_listener.php script the AJAX snippet executes...
$drinkData = $_POST['submittedDrink'];
//open serial port
$fp = fopen("/dev/ttyACM0", "w+"); //w = write w+ = read/write
//check if open
if (!$fp) {
echo "Not open";
} else {
//if open send data (via PHP) to connected Arduino on serial comm port 1 (ttyACM0)
fwrite($fp, '<' . $drinkData . '>');
sleep(2);
//check and wait for incoming serial data form Arduino response
$responseValue = "";
while(strlen($responseValue) < 1){
$incomingChar = fread($fp, 1);
if($incomingChar != ''){
$responseValue .= $incomingChar;
}
}
echo $responseValue;
}
What am I missing? or doing wrong? I -feel- this is a port configuration issue.... since I have NOT changed any code (Arduino or PHP side).. and installing that 'minicom' app allowed communication (briefly)...
Thanks for any advice/feedback here... its been a LONG week+ just on this serial stuff!
Thanks!
-J