我的设置: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ún: ");
//client.println(timeString);
client.print("<br>Current temperature: ");
client.print(temp);
client.print(" °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(" °F");
}
// Close connection and free resources.
client.stop();
hits++;
}
delay(50); // Poll every 50ms
}
温度报告目前效果很好。但是,我无法弄清楚如何将我的 html 中滑块的值传递给 arduino。任何建议或帮助将不胜感激!