我正在运行一个带有 Apache 2.2.22 和 PHP 5.3.13的小型WAMP服务器。我的问题是我的 JavaScript/Ajax 代码不再发送$POST
到我的外部 PHP 文件,该文件应该通过串行将消息发送到我的 Arduino。
它工作得很好,然后我只是更改了输入的值,即我正在发送的“消息”,突然它不开心了。我以前遇到过这个问题,所以我只是将值改回原来的值,但现在即使这样也行不通。我试图清除我所有的 Web 历史记录,重新启动 WAMP 服务器,甚至重新启动我的计算机,但都没有成功。
无论如何,这是我的代码:
HTML
<div id="Room1Controls">
<div id="ControlButton" class="Control1"><a>B
A
C
K</a></div>
<div class="Room1SwitchContainer">
<div id="LightsSwitchBox" class="SwitchBox">
<h1>Lights</h1>
<input type="checkbox" class="SwitchButton1" value="L11"><a class="SButton1">On</a></input>
<input type="checkbox" class="SwitchButton2" value="L10"><a class="SButton2">Off</a></input>
</div>
<div id="DoorsSwitchBox" class="SwitchBox">
<h1>Doors</h1>
<input type="checkbox" class="SwitchButton3" value="D10"><a class="SButton3">Open</a></input>
<input type="checkbox" class="SwitchButton4" value="D11"><a class="SButton4">Lock</a></input>
</div>
<div id="BlindsSwitchBox" class="SwitchBox">
<h1>Blinds</h1>
<input type="checkbox" class="SwitchButton5" value="B11"><a class="SButton5">Open</a></input>
<input type="checkbox" class="SwitchButton6" value="B10"><a class="SButton6">Close</a></input>
</div>
</div>
</div>
JavaScript/Ajax
$(document).ready(function(){
var value;
$(".SButton1").click(function(){
$(".SwitchButton1").trigger('click');
});
$(".SButton2").click(function(){
$(".SwitchButton2").trigger('click');
});
$(".SButton3").click(function(){
$(".SwitchButton3").trigger('click');
});
$(".SButton4").click(function(){
$(".SwitchButton4").trigger('click');
});
$(".SButton5").click(function(){
$(".SwitchButton5").trigger('click');
});
$(".SButton6").click(function(){
$(".SwitchButton6").trigger('click');
});
$("#Room1Controls input").click(function(){
value=$(this).val();
$.post('Inc/Parts/arduino.php',{message:value});
});
});
PHP
<?php
$message=$_POST['message'];
if (isset($_POST['message'])) {
require("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("COM3");
exec('mode COM3 BAUD=96 PARITY=n DATA=8 STOP=1 xon=off octs=off rts=on');
$serial->deviceOpen();
if ($_POST['message'] == "L11") {
$serial->sendMessage("L");
} else if ($_POST['message'] == "L10") {
$serial->sendMessage("l");
} else if ($_POST['message'] == "D10") {
$serial->sendMessage("D");
} else if ($_POST['message'] == "D11") {
$serial->sendMessage("d");
} else if ($_POST['message'] == "B11") {
$serial->sendMessage("B");
} else if ($_POST['message'] == "B10") {
$serial->sendMessage("b");
}
$serial->deviceClose();
}
?>
以及在 Arduino Uno 上运行的代码:
int ldr = 0;
int door = 13;
int blind = 12;
int led = 11;
int in = 0;
void setup() {
pinMode(door, OUTPUT);
pinMode(blind, OUTPUT);
pinMode(led, OUTPUT);
pinMode(ldr, INPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
in = Serial.read();
}
if (in == 'L')
{
int val = (analogRead(ldr));
val = constrain(val, 150, 500);
int LightLvl = map(val, 150, 500, 255, 0);
analogWrite(led, LightLvl);
}
else
if (in == 'l') (analogWrite(led, LOW));
if (in == 'D')
{
digitalWrite(door, LOW);
}
else
if (in == 'd') (digitalWrite(door, HIGH));
if (in == 'B')
{
digitalWrite(blind, HIGH);
}
else
if (in == 'b') (digitalWrite(blind, LOW));
}
如果您能指出我的代码中的任何不一致或错误,那就太好了。另外,我是编码新手,所以如果您对如何改进我的代码以使其更高效或替代做事方式有任何建议,我将非常感激。