简体   繁体   English

Arduino通讯停止工作

[英]Arduino communication stopped working

I am running a small WAMP server with Apache 2.2.22 and PHP 5.3.13. 我正在使用Apache 2.2.22和PHP 5.3.13运行小型WAMP服务器。 My problem is that my JavaScript/Ajax Code is no longer sending the $POST to my external PHP file which is supposed to then send the message via serial to my Arduino. 我的问题是我的JavaScript / Ajax代码不再将$POST发送到我的外部PHP文件,该文件随后应通过串行将消息发送到我的Arduino。

It was working perfectly fine, and then I simply changed the value of the input which is my 'Message' that I'm sending and suddenly it's not happy. 它工作得非常好,然后我只是更改了输入的值,这就是我要发送的“消息”,突然之间就很不高兴。 I have had this problem before so I just changed the value back to its original, but now even that's not working. 我以前遇到过这个问题,所以我只是将值改回了原来的值,但现在即使这样也无法正常工作。 I tried to clear all my Web history, re-started WAMP server, and I even re-booted my computer, but none of it worked. 我试图清除所有Web历史记录,重新启动WAMP服务器,甚至重新启动计算机,但都无济于事。

Anyway, here is my code: 无论如何,这是我的代码:

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

<?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();
    }
?>

And the code running on the Arduino Uno: 以及在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));
}

If you could point out any inconsistency or errors in my code that would be great. 如果您可以指出我的代码中的任何不一致或错误,那将是很好的。 Also, I am new to coding so if you have any suggestions for how I can improve my code to make it more efficient or alternative ways of doing things I would be very greatful. 另外,我是编码的新手,因此,如果您对如何改进代码以使其更有效或采取其他替代方式有任何建议,我将非常感激。

Turns out my .htaccess file was the problem. 原来我的.htaccess文件是问题所在。 I had some script to hide the extensions but for some reason the $POST Didn't like it. 我有一些脚本可以隐藏扩展名,但是由于某种原因, $POST不喜欢它。

Here is the code in the .htaccess file: 这是.htaccess文件中的代码:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
</IfModule>

If anyone knows a way around this i would love it if you shared it ;) 如果有人知道解决这个问题的方法,如果您分享的话,我会喜欢的;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM