1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-11 22:24:13 +01:00
Arduino/libraries/Bridge/examples/TemperatureWebPanel/TemperatureWebPanel.ino

124 lines
3.2 KiB
Arduino
Raw Normal View History

2013-07-06 13:48:10 +02:00
/*
Temperature web interface
This example shows how to serve data from an analog input
2013-07-06 14:10:13 +02:00
via the Arduino Yún's built-in webserver using the Bridge library.
2013-07-06 13:48:10 +02:00
The circuit:
* TMP36 temperature sensor on analog pin A1
* SD card attached to SD card slot of the Arduino Yún
Prepare your SD card with an empty folder in the SD root
named "arduino" and a subfolder of that named "www".
This will ensure that the Yún will create a link
2013-07-06 13:48:10 +02:00
to the SD to the "/mnt/sd" path.
In this sketch folder is a basic webpage and a copy of zepto.js, a
2013-07-06 13:48:10 +02:00
minimized version of jQuery. When you upload your sketch, these files
will be placed in the /arduino/www/TemperatureWebPanel folder on your SD card.
2013-07-06 13:48:10 +02:00
You can then go to http://arduino.local/sd/TemperatureWebPanel
to see the output of this sketch.
You can remove the SD card while the Linux and the
2013-07-06 13:48:10 +02:00
sketch are running but be careful not to remove it while
the system is writing to it.
2013-07-06 13:48:10 +02:00
created 6 July 2013
by Tom Igoe
2013-07-06 13:48:10 +02:00
This example code is in the public domain.
2013-09-10 10:23:29 +02:00
http://arduino.cc/en/Tutorial/TemperatureWebPanel
2013-07-06 13:48:10 +02:00
*/
2013-09-09 20:56:15 +02:00
2013-07-06 13:48:10 +02:00
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
2013-07-06 13:48:10 +02:00
// Listen on default port 5555, the webserver on the Yun
// will forward there all the HTTP requests for us.
YunServer server;
2013-07-06 14:10:13 +02:00
String startString;
2013-07-06 14:20:02 +02:00
long hits = 0;
2013-07-06 13:48:10 +02:00
void setup() {
Serial.begin(9600);
// Bridge startup
pinMode(13, OUTPUT);
2013-07-06 13:48:10 +02:00
digitalWrite(13, LOW);
Bridge.begin();
digitalWrite(13, HIGH);
// using A0 and A2 as vcc and gnd for the TMP36 sensor:
pinMode(A0, OUTPUT);
pinMode(A2, OUTPUT);
digitalWrite(A0, HIGH);
digitalWrite(A2, LOW);
// Listen for incoming connection only from localhost
// (no one from the external network could connect)
server.listenOnLocalhost();
server.begin();
2013-07-06 14:10:13 +02:00
// get the time that this sketch started:
Process startTime;
startTime.runShellCommand("date");
while (startTime.available()) {
2013-07-06 14:10:13 +02:00
char c = startTime.read();
startString += c;
}
2013-07-06 13:48:10 +02:00
}
void loop() {
// 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") {
2013-07-06 14:10:13 +02:00
// get the time from the server:
Process time;
time.runShellCommand("date");
String timeString = "";
while (time.available()) {
2013-07-06 14:10:13 +02:00
char c = time.read();
timeString += c;
}
Serial.println(timeString);
2013-07-06 13:48:10 +02:00
int sensorValue = analogRead(A1);
// convert the reading to millivolts:
float voltage = sensorValue * (5000 / 1024);
2013-07-06 13:48:10 +02:00
// convert the millivolts to temperature celsius:
float temperature = (voltage - 500) / 10;
2013-07-06 13:48:10 +02:00
// print the temperature:
2013-07-06 14:10:13 +02:00
client.print("Current time on the Yún: ");
client.println(timeString);
client.print("<br>Current temperature: ");
2013-07-06 13:48:10 +02:00
client.print(temperature);
client.print(" degrees C");
2013-07-06 14:10:13 +02:00
client.print("<br>This sketch has been running since ");
client.print(startString);
2013-07-06 14:20:02 +02:00
client.print("<br>Hits so far: ");
client.print(hits);
2013-07-06 13:48:10 +02:00
}
// Close connection and free resources.
client.stop();
2013-07-06 14:20:02 +02:00
hits++;
2013-07-06 13:48:10 +02:00
}
delay(50); // Poll every 50ms
}
2013-07-06 14:10:13 +02:00