Łuk*_*zny 5 html php serial-port arduino
我有 PHP 脚本用于通过网站控制 Arduino 的二极管,但我遇到了问题。
我的Arduino代码是:
int green = 8;
int incomingbyte;
void setup()
{
Serial.begin(9600);
pinMode(green,OUTPUT);
}
void loop()
{
if(Serial.available() > 0)
{
incomingbyte = Serial.read();
}
if(incomingbyte == '0'){
digitalWrite(green,HIGH);
}
if(incomingbyte == '1'){
digitalWrite(green,LOW);
}
}
Run Code Online (Sandbox Code Playgroud)
我的 PHP 代码是:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
if (isset($_GET['action'])) {
require("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("COM3");
$serial->confBaudRate(9600);
$serial->deviceOpen();
if ($_GET['action'] == "green1") {
$serial->sendMessage("0\r");
} else if ($_GET['action'] == "green0") {
$serial->sendMessage("1\r");
}
$serial->deviceClose();
}
Run Code Online (Sandbox Code Playgroud)
我的 HTML 代码:
<!DOCTYPE html>
<html>
<head>
<title>ARDUINO</title>
</head>
<body>
<h1> ARDUINO AND PHP COMMUNICATION </h1>
<a href="led.php?action=green1">ON</a></br>
<a href="led.php?action=green0">OFF</a></br>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我有两个问题:
Arduino 仅收到传入字节 = 0,因此我可以打开二极管,但无法将其关闭。我修改了代码以设置传入字节 = 1 以打开二极管,但它也不起作用。所以我认为 Arduino 只得到传入字节 = 0。
我的网站在运行脚本后关闭。当我单击“打开”或“关闭”时,脚本正在运行,并且我得到白色(空白)站点。我应该怎样做才能始终停留在 HTML 网站上?
回复:2 在 php 表单处理程序下添加 html 代码 - 这样所有内容都由同一脚本提供,或者使用
header()
Run Code Online (Sandbox Code Playgroud)
重新定位回 html 页面 - 但随后您无法输出错误。
编辑以便以单个文件的方式执行此操作:
<?php
// led.php code in here
error_reporting(E_ALL);
ini_set("display_errors", 1);
if (isset($_GET['action'])) {
// and so on ...
?>
<!--// now show your html form regardless
of whether the form was submitted or not // -->
<!DOCTYPE html>
<html>
<head>
<title>ARDUINO</title>
</head>
<body>
<h1> ARDUINO AND PHP COMMUNICATION </h1>
<a href="?action=green1">ON</a></br>
<a href="?action=green0">OFF</a></br>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
编辑以尝试使解决方案更清晰一些。请注意,您不必将 led.php 添加到链接中,它们会提交回同一文件。
| 归档时间: |
|
| 查看次数: |
4367 次 |
| 最近记录: |