从arduino uno收到的网站中不断更新传感器数据的问题

时间:2018-10-20 08:13:06

标签: javascript php html5 arduino-uno esp8266

我是Arduino Uno的新手。我的任务是将Ir传感器数据发送到Web应用程序。我使用AT+CIPSEND命令将数据发送到Web应用程序。我正在使用xampp控制面板在我的PC上创建服务器,但问题是我从php获取的值抛出了错误的未识别索引值。从我的角度来看,可能存在延迟问题。我尝试使用setInterval()每1秒拨打一次Ajax,但问题仍然相同。

Arduino代码

    #include <SoftwareSerial.h>
    #define RX 10
    #define TX 11
    String AP = "Tenda_2704A8";       // CHANGE ME
    String PASS = "8108805837"; // CHANGE ME
    String HOST = "192.168.0.104";
    String PORT = "80";
    String Data;
    int countTrueCommand;
    int countTimeCommand;
    boolean found = false;

    int LED = 13; // Use the onboard Uno LED
    int isObstaclePin = 7;  // This is our input pin
    int isObstacle = HIGH;  // HIGH MEANS NO OBSTACLE


    SoftwareSerial esp8266(RX, TX);


    void setup() {
      pinMode(LED, OUTPUT);
      pinMode(isObstaclePin, INPUT);
      Serial.begin(9600);
      esp8266.begin(115200);
      sendCommand("AT", 5, "OK");
      sendCommand("AT+CWMODE=1", 5, "OK");
      sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
      sendCommand("AT+CIPMUX=1",5,"OK");  


    }
    void loop() {

      String output;

      isObstacle = digitalRead(isObstaclePin);
      if (isObstacle == LOW)
      { 
        output = "obstacle";
        Serial.println("OBSTACLE!!, OBSTACLE!!");
        digitalWrite(LED, HIGH);
      }
      else
      { 
        output = "clear";
        Serial.println("clear");
        digitalWrite(LED, LOW);
      }



      Data = "GET project/ajax/arduino.php?value=2";
      sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,5,"OK");
      sendCommand("AT+CIPSEND=0," +String(Data.length()+4),4,">");
      Serial.println(Data);delay(100);countTrueCommand++;
      sendCommand("AT+CIPCLOSE=0",5,"OK");



    }


    void sendCommand(String command, int maxTime, char readReplay[]) {
      Serial.print(countTrueCommand);
      Serial.print(". at command => ");
      Serial.print(command);
      Serial.print(" ");
      while (countTimeCommand < (maxTime * 1))
      {
        esp8266.println(command);//at+cipsend
        if (esp8266.find(readReplay)) //ok
        {
          found = true;
          break;
        }

        countTimeCommand++;
      }

      if (found == true)
      {
        Serial.println("Yes");
        countTrueCommand++;
        countTimeCommand = 0;
      }

      if (found == false)
      {
        Serial.println("Fail");
        countTrueCommand = 0;
        countTimeCommand = 0;
      }

      found = false;
    }

Arduino.html

<!DOCTYPE html>
<html>
<head>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <title></title>
    <script>
        $(document).ready(function(){
            function fetchdata(){
                $.ajax({
                url:"arduino.php",
                success:function(response){
                    $("#value").text(response);
                }

                });
            }
            setInterval(fetchdata,100);

        });
    </script>
</head>
<body>
    <h1 id = "value"></h1>

</body>
</html>

arduino.php

<?php
/*$conn=mysqli_connect("localhost","root","","arduino");
if(!$conn){
    die("connectionfailed".mysqli_error());
}*/

$val = $_GET['value'];

echo $val;

//$sql = "INSERT INTO `arduino` (value) VALUES ('".$_GET['value']."')";

//mysqli_query($conn,$sql);


?>

1 个答案:

答案 0 :(得分:0)

您的代码有问题。您的代码仅打印从arduino接收的值。您将需要存储从arduino接收的数据,以便在需要时将其返回。你应该做的像

if(isset($_GET["value"]))
{
//save the data somewhere
}
else{
//fetch saved data and echo it
}