1 | /*
|
---|
2 | Temperature web interface
|
---|
3 |
|
---|
4 | This example shows how to serve data from an analog input
|
---|
5 | via the Arduino Y炭n's built-in webserver using the Bridge library.
|
---|
6 |
|
---|
7 | The circuit:
|
---|
8 | * TMP36 temperature sensor on analog pin A1
|
---|
9 | * SD card attached to SD card slot of the Arduino Y炭n
|
---|
10 |
|
---|
11 | This sketch must be uploaded via wifi. REST API must be set to "open".
|
---|
12 |
|
---|
13 | Prepare your SD card with an empty folder in the SD root
|
---|
14 | named "arduino" and a subfolder of that named "www".
|
---|
15 | This will ensure that the Y炭n will create a link
|
---|
16 | to the SD to the "/mnt/sd" path.
|
---|
17 |
|
---|
18 | In this sketch folder is a basic webpage and a copy of zepto.js, a
|
---|
19 | minimized version of jQuery. When you upload your sketch, these files
|
---|
20 | will be placed in the /arduino/www/TemperatureWebPanel folder on your SD card.
|
---|
21 |
|
---|
22 | You can then go to http://arduino.local/sd/TemperatureWebPanel
|
---|
23 | to see the output of this sketch.
|
---|
24 |
|
---|
25 | You can remove the SD card while the Linux and the
|
---|
26 | sketch are running but be careful not to remove it while
|
---|
27 | the system is writing to it.
|
---|
28 |
|
---|
29 | created 6 July 2013
|
---|
30 | by Tom Igoe
|
---|
31 |
|
---|
32 | This example code is in the public domain.
|
---|
33 |
|
---|
34 | http://arduino.cc/en/Tutorial/TemperatureWebPanel
|
---|
35 |
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <Bridge.h>
|
---|
39 | #include <YunServer.h>
|
---|
40 | #include <YunClient.h>
|
---|
41 |
|
---|
42 | // Listen on default port 5555, the webserver on the Y炭n
|
---|
43 | // will forward there all the HTTP requests for us.
|
---|
44 | YunServer server;
|
---|
45 | String startString;
|
---|
46 | long hits = 0;
|
---|
47 |
|
---|
48 | void setup() {
|
---|
49 | Serial.begin(9600);
|
---|
50 |
|
---|
51 | // Bridge startup
|
---|
52 | pinMode(13, OUTPUT);
|
---|
53 | digitalWrite(13, LOW);
|
---|
54 | Bridge.begin();
|
---|
55 | digitalWrite(13, HIGH);
|
---|
56 |
|
---|
57 | // using A0 and A2 as vcc and gnd for the TMP36 sensor:
|
---|
58 | pinMode(A0, OUTPUT);
|
---|
59 | pinMode(A2, OUTPUT);
|
---|
60 | digitalWrite(A0, HIGH);
|
---|
61 | digitalWrite(A2, LOW);
|
---|
62 |
|
---|
63 | // Listen for incoming connection only from localhost
|
---|
64 | // (no one from the external network could connect)
|
---|
65 | server.listenOnLocalhost();
|
---|
66 | server.begin();
|
---|
67 |
|
---|
68 | // get the time that this sketch started:
|
---|
69 | Process startTime;
|
---|
70 | startTime.runShellCommand("date");
|
---|
71 | while (startTime.available()) {
|
---|
72 | char c = startTime.read();
|
---|
73 | startString += c;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | void loop() {
|
---|
78 | // Get clients coming from server
|
---|
79 | YunClient client = server.accept();
|
---|
80 |
|
---|
81 | // There is a new client?
|
---|
82 | if (client) {
|
---|
83 | // read the command
|
---|
84 | String command = client.readString();
|
---|
85 | command.trim(); //kill whitespace
|
---|
86 | Serial.println(command);
|
---|
87 | // is "temperature" command?
|
---|
88 | if (command == "temperature") {
|
---|
89 |
|
---|
90 | // get the time from the server:
|
---|
91 | Process time;
|
---|
92 | time.runShellCommand("date");
|
---|
93 | String timeString = "";
|
---|
94 | while (time.available()) {
|
---|
95 | char c = time.read();
|
---|
96 | timeString += c;
|
---|
97 | }
|
---|
98 | Serial.println(timeString);
|
---|
99 | int sensorValue = analogRead(A1);
|
---|
100 | // convert the reading to millivolts:
|
---|
101 | float voltage = sensorValue * (5000 / 1024);
|
---|
102 | // convert the millivolts to temperature celsius:
|
---|
103 | float temperature = (voltage - 500) / 10;
|
---|
104 | // print the temperature:
|
---|
105 | client.print("Current time on the Y炭n: ");
|
---|
106 | client.println(timeString);
|
---|
107 | client.print("<br>Current temperature: ");
|
---|
108 | client.print(temperature);
|
---|
109 | client.print(" degrees C");
|
---|
110 | client.print("<br>This sketch has been running since ");
|
---|
111 | client.print(startString);
|
---|
112 | client.print("<br>Hits so far: ");
|
---|
113 | client.print(hits);
|
---|
114 | }
|
---|
115 |
|
---|
116 | // Close connection and free resources.
|
---|
117 | client.stop();
|
---|
118 | hits++;
|
---|
119 | }
|
---|
120 |
|
---|
121 | delay(50); // Poll every 50ms
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 |
|
---|