需要帮助从yun上的html滑块获取值到arduino值

时间:2015-06-07 21:46:25

标签: arduino arduino-yun

我的设置: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。任何建议或帮助将不胜感激!

1 个答案:

答案 0 :(得分: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('/')来解析树。最终,你将获得你的goaltemp价值。

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

祝你好运!