hey gang-
I am in need of some direction/advice here...
Long story short for project summary:
RPi all set-up (OS, MySQL, Apache, PHP, PhpMyAdmin)
RPi set-up to boot to default (locally hosted web site/page)
RPi set-up to boot to fullscreen (ie: KIOSK mode)
Database created, tables populated, webpage (drink menu) is dynamically created and displayed in browser.
Arduino (UNO) is connected to RPi via USB.
When webpage is submitted.. I take submitted/$_POST'd data... and send it via PHP to the connected Arduino on COM1
Arduino received serial data, parses it.. and does what it needs to do (move some steppers, move some servos...etc..etc. then returns back home)..
Everything up to this point is working as expected, and without issue!! (WOOT!)
However..... this is where I am stuck at:
When the Arduino is done doing its business.... it then fires off a confirmation that it is done/complete via SERIAL, BACK TO THE RPi!...
....and I am NOT receiving it.
When the webpage submits (to itself).. it shows a 'please wait' message... when the Arduino sends back its 'complete' confirmation to the RPi.. the webpage should re-direct back to the intitial page state (drink menu selection)...
My approach for this is:
HTML/CSS/PHP coded page.. that queries the DB.. and dynamically builds the drink menu based on active/available ingredients to display a front end GUI (images and order button)..
Upon submit ($_POST).. I take this drink recipe/data and send it out to the Arduino.
- (again) this is working without issue
I then have an AJAX call to an external PHP script that is used to query/listen to the SERIAL port.. with success/error calls back from this AJAX call.
- My understanding is that the AJAX callbacks will just wait/listen for the external PHP script response.. and execute/do whatever is in the callback function when a response it finally returned from said PHP script
But I am NEVER getting a callback response
(if I have webdev tool open and checking the console data.. when I hot the BACK button.. I -do- see the error callback message.. but never a success one, as in I am never receiving the serial data in the php script)
The response time is random/varies.. as there is no telling how long each specific drink will take to make/complete
At this point... I'm not sure if it how the SERIAL port may be configured?
Or how I am doing the AJAX/PHP serial read stuff? (I was under the impression that everything in LINUX is treated like a 'file'.. so I can treat the serial port the same way?... looking for the end of file? not really clear on how to best 'wait/listen'... and also know when the transmission is complete?)
Is there the PHP snippet I am using to:
1.) send the data to the connected Arduino..
2.) Output the AJAX (jQuery) call to the external PHP script
3.) The AJAX callback 'response handlers'
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"); //mode not available in Linux?
//saw this on several RPi posts? (but not sure of the difference? or why one would be used over the other?)
//this gets run command line on RPi, not via PHP Script (what are the parms doing even?)
//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 . '>');
?>
<div id="waitingContainer">
<p>Please wait, your brink is being made.</p>
</div>
<script type="text/JavaScript" language="JavaScript">
console.log("ajax routine hit");
$.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'){
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!");
//not sure what to do? (back to main menu anyways?)
}
},
//define error handler actions
error: function(response) {
console.log("PHP SERIAL READ FAIL: "+ 'Ready State: '+ response.readyState + ' Status: ' + response.status);
//do whatever here on failed complete response
}
});
</script>
<?
//close connection
//fclose($fp); //needed? //should this go in the external php script instead now?
}
}
Here are the contents of the serial_listener.php script:
//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, 1000);
}
echo $response;
fclose($fp);
What am I missing or doing wrong here?
All feedback is appreciated.
thanks!