0

我的设置:arduino leonardo + yun shield

我要完成的工作:通过 yun 提供的 html 上的单线显示温度。允许用户通过 yun html 页面上的滑块设置目标温度,并在函数“goaltemp”中设置变量的值。

这是我的html:

    <!DOCTYPE html>
<html>
    <head>
        <title>AngularJS / Arduino</title>
        <meta name="viewport" content="width=device-width">

        <script type="text/javascript" src="zepto.min.js"></script>
        <script type="text/javascript">
        function refresh() {
            $('#content').load('/arduino/temperature');
        }
        var value;
        function sendVal() {
            $.get('/arduino/goaltemp' + value, function(){
                    }
                );
            }
            function updateVal(val) {
            value = val;
            $("#val").text(value);
            sendVal();
            }
        </script>

    </head>
    <body onload="setInterval(refresh, 2000);">
      <span id="content">0</span>
      <p>Goal Temperature:
      <input type="range" min="0" max="230" onchange="updateVal(this.value);">
      <p id="val">0</p>
      </p>


    </body>
</html>

这是我的arduino草图:

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

#include <SevSeg.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//*****Temp Read
  //code from http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
  //Orange Stripe is PWR: 3-5V, 
  //White Stripe is GND
  //Blue Stripe is data.

  // Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
  // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  OneWire oneWire(ONE_WIRE_BUS);
  //Powerswitch Tail
  const int powerPin = A0;
  boolean powerOn = false;
  // Pass our oneWire reference to Dallas Temperature. 
  DallasTemperature sensors(&oneWire);

// Listen on default port 5555, the webserver on the Yún
// will forward there all the HTTP requests for us.
YunServer server;
String startString;
long hits = 0;

void setup() {
  //Serial.begin(9600);

  // Bridge startup
  Bridge.begin();

  // Listen for incoming connection only from localhost
  // no one from the external network could connect
  server.listenOnLocalhost();
  server.begin();

  // get the time that this sketch started:
  Process startTime;
  startTime.runShellCommand("date");
  while (startTime.available()) {
    char c = startTime.read();
    startString += c;
  }
}

void loop() {
  int temp = (sensors.getTempFByIndex(0));
  sensors.requestTemperatures(); // Send the command to get temperatures

  // Get clients coming from server
  YunClient client = server.accept();

  // There is a new client?
  if (client) {
    // read the command
    String command = client.readString();
    command.trim();        //kill whitespace
    Serial.println(command);
    // is "temperature" command?
    if (command == "temperature") {
      // get the time from the server:
      Process time;
      time.runShellCommand("date");
      String timeString = "";     
      while (time.available()) {
        char c = time.read();
        timeString += c;
      }
      Serial.println(timeString);
      int sensorValue = analogRead(A1);
      // convert the reading to millivolts:
      // convert the millivolts to temperature celsius:
      // print the temperature:
      //client.print("Current time on the Y&uacute;n: ");
      //client.println(timeString);
      client.print("<br>Current temperature: ");
      client.print(temp);
      client.print(" &deg;F");
      //client.print("<br>This sketch has been running since ");
      //client.print(startString);
      client.print("<br>Hits so far: ");
      client.print(hits);
    }
      if (command == "goaltemp") {
      int value = client.parseInt();
      client.print("<br>The Goal Temperature is ");
      client.print(value);
      client.print(" &deg;F");
      }

    // Close connection and free resources.
    client.stop();
    hits++;
  }

  delay(50); // Poll every 50ms
}

温度报告目前效果很好。但是,我无法弄清楚如何将我的 html 中滑块的值传递给 arduino。任何建议或帮助将不胜感激!

4

1 回答 1

0

您是否在此处查看了示例代码和文档:https ://www.arduino.cc/en/Guide/ArduinoYun#toc20 ?那里的示例代码涵盖了您尝试执行的操作。

具体来说,请注意代码作者如何使用不同的函数分解 URL 参数的处理,如下所示:

void process(YunClient client) {
  String command = client.readStringUntil('/');

  if (command == "digital") {
    digitalCommand(client);
  }
  if (command == "analog") {
    analogCommand(client);
  }
  if (command == "mode") {
    modeCommand(client);
  }
}

在每个处理函数中,您可以简单地执行一个 client.readStringUntil('/')向下解析树。最终,您将获得您的目标值。

提示,您可能希望将您的 URL 格式化为/arduino/goaltemp/20- 这将使解析更容易。

祝你好运!

于 2016-01-03T01:39:46.017 回答