gpt4 book ai didi

php - Raspberry PI/PHP + Arduino串行通信

转载 作者:行者123 更新时间:2023-12-01 06:09:50 24 4
gpt4 key购买 nike

我将尝试简要介绍该项目,然后介绍当前的代码方法。


我已经将RPi全部设置为最新的Stretch OS。
我已经安装了MySQL,PHP,APahce和PhpMyAdmin,并且可以正常工作。
我让它启动到默认(本地托管)网页,并以全屏(KIOSK)模式启动。
数据库已创建,表格已填充,查询就位..并且网页(饮料菜单)按预期正确显示。





我通过USB将Arduino UNO连接到RPI
网页上显示了一堆菜单选项..每个菜单选项都有自己的“顺序”按钮。
单击任何订单按钮时​​。我将此按钮数据保存到隐藏字段中,并使用jQuery提交/发布表单(自身)
在$ _POST上,我抓取此提交的数据,并通过PHP通过串行通信1发送出去。


这就是我目前所在的位置。


由于我的Arduino通过USB连接到RPi。我不能使用串行监视器调试东西。...我在这里还有其他选择吗?
当我提交网页时。我确实看到Arduino上的RX / TX指示灯闪烁(使我相信它正在接收串行数据)。我可以/将今晚通过连接步进器检查其是否正确再次转动电机,看ti是否移至正确的位置...


这是我被困/绊倒的地方..可以通过一些讨论使我走上正确的道路。

因此,在Arduino“发挥了魔力”之后..应该将确认消息发送回RPi ..说饮料已经完成..我可以回到主饮料菜单等待下一个订单

因为网页已经$ _POSTed ..并且串行数据已发送到连接的Arduino ....然后我让该页面显示“请稍候”消息...但是因为该页面已经在服务器端进行了解析,我现在需要如何通过PHP“监听”串行posrt。

我想..我可以使用一些AJAX来调用/加载一个外部php脚本..该脚本将等待/侦听串行端口..并将数据返回给AJAX'success'回调。

但是,因为我以前从未做过..如果这行得通..或者这甚至是正确的方法,我都会感到有点怀疑。

还有关于打开和关闭端口的最佳位置的随机问题。特别是如果有2个单独的脚本? (即:我可以在一个脚本中打开该端口..仍然在另一个脚本中访问它吗?还是访问该文件的文件..需要成为打开它的那个文件?)

这是我当前的代码片段,用于处理串行端口的发送和等待/监听:

[码]

<?
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?

}

}


甚至不知道应该输入什么内容:serial_listener.php脚本……只是一个while循环之类的东西?等待数据?或文件结尾或其他内容? (不确定在串行端口上使用fread()如何工作?)

任何建议,以尝试缠住我的头对此表示赞赏。

更新:我不确定我是否正确/清楚地解释了事情?

但是,当页面提交(到其自身)..就是将传出的串行数据发送到连接的(通过USB到RPi)Arduino...。

当页面“发布”时,它发送上述数据OUT ..,然后显示“请稍候”类型的消息。

在这一点上(据我所知)..服务器端脚本/解析现在已完成...,剩下的页面是“请稍候” ...

此时,没有更多的解析/服务器端。

这就是为什么我认为/不赞成对外部脚本使用AJAX调用,该脚本可以坐着并“等待”(监听)串行端口(不清楚这样做的最佳方式……while()循环)或者其他的东西?)...

然后当数据最终返回时...

*


(没有告诉您此“序列号”需要多长时间
来自Arduino的反馈”。因为每种饮料的摄入量各不相同
创造时间).........


*

它将使用AJAX“成功”回调函数来更新页面..最终只需将其再次重定向回饮料菜单主页面即可。

我不觉得在Arduino上使用timeout()或delay()不仅是个坏建议(即:如果有帮助,请不要使用delay())...但我什至看不到在哪里/为什么这样做感?



更新:

以及serial_listener.php脚本的内容:(脚本AJAX代码段调用)

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

}


最终更新:

我重新编写了一些东西,以便使用AJAX调用来发送我的数据..并且还等待响应。
AJAX调用执行的外部php脚本是端口现在打开的唯一位置(我没有关闭它)

这是具有AJAX调用的PHP表单的SUBMIT状态:

数据发送的工作时间为100%。.但是我看不到我的回复。

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>
<?
}


以下是AJAX调用执行的serial_listener.php脚本的内容:

//data sent from AJAX call (works)
$drinkData = $_POST['drinkData'];

//open serial port
$fp = fopen("/dev/ttyACM0", "w+"); //w = write w+ = read/write (works)
//$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';
}
}

最佳答案

无法发表评论(没有声望)
我对Ajax,jquery,Raspberry Pi或PHP不了解,但是...

如果您有串行转USB TTL设备(例如this),则可以使用SoftwareSerial库。在arduino上几个未使用的数字引脚上设置SoftwareSerial,并在那里发送调试信息。

不是答案,而是建议。

*********编辑***********

想一想,Raspberry Pi是否没有串行端口?
如果是这样,则不需要USB转换器。只需在Arduino上设置softwareSerial端口,然后使用该端口连接到Pi。您可以使用USB端口将调试com发送到计算机的方式。

关于php - Raspberry PI/PHP + Arduino串行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52433847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com