|
| 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 | + Prepare your SD card with an empty folder in the SD root |
| 12 | + named "arduino" and a subfolder of that named "www". |
| 13 | + This will ensure that the Yún will create a link |
| 14 | + to the SD to the "/mnt/sd" path. |
| 15 | + |
| 16 | + In this sketch folder is a basic webpage and a copy of zepto.js, a |
| 17 | + minimized version of jQuery. When you upload your sketch, these files |
| 18 | + will be placed in the /arduino/www/TemperatureWebPanel folder on your SD card. |
| 19 | + |
| 20 | + You can then go to http://arduino.local/sd/TemperatureWebPanel |
| 21 | + to see the output of this sketch. |
| 22 | + |
| 23 | + You can remove the SD card while the Linux and the |
| 24 | + sketch are running but be careful not to remove it while |
| 25 | + the system is writing to it. |
| 26 | + |
| 27 | + created 6 July 2013 |
| 28 | + by Tom Igoe |
| 29 | +
|
| 30 | + |
| 31 | + This example code is in the public domain. |
| 32 | + |
| 33 | + */ |
| 34 | +#include <Bridge.h> |
| 35 | +#include <YunServer.h> |
| 36 | + |
| 37 | +// Listen on default port 5555, the webserver on the Yun |
| 38 | +// will forward there all the HTTP requests for us. |
| 39 | +YunServer server; |
| 40 | + |
| 41 | +void setup() { |
| 42 | + Serial.begin(9600); |
| 43 | + |
| 44 | + // Bridge startup |
| 45 | + pinMode(13,OUTPUT); |
| 46 | + digitalWrite(13, LOW); |
| 47 | + Bridge.begin(); |
| 48 | + digitalWrite(13, HIGH); |
| 49 | + |
| 50 | + // using A0 and A2 as vcc and gnd for the TMP36 sensor: |
| 51 | + pinMode(A0, OUTPUT); |
| 52 | + pinMode(A2, OUTPUT); |
| 53 | + digitalWrite(A0, HIGH); |
| 54 | + digitalWrite(A2, LOW); |
| 55 | + |
| 56 | + // Listen for incoming connection only from localhost |
| 57 | + // (no one from the external network could connect) |
| 58 | + server.listenOnLocalhost(); |
| 59 | + server.begin(); |
| 60 | +} |
| 61 | + |
| 62 | +void loop() { |
| 63 | + // Get clients coming from server |
| 64 | + YunClient client = server.accept(); |
| 65 | + |
| 66 | + // There is a new client? |
| 67 | + if (client) { |
| 68 | + // read the command |
| 69 | + String command = client.readString(); |
| 70 | + command.trim(); //kill whitespace |
| 71 | + Serial.println(command); |
| 72 | + // is "temperature" command? |
| 73 | + if (command == "temperature") { |
| 74 | + int sensorValue = analogRead(A1); |
| 75 | + // convert the reading to millivolts: |
| 76 | + float voltage = sensorValue * (5000/ 1024); |
| 77 | + // convert the millivolts to temperature celsius: |
| 78 | + float temperature = (voltage - 500)/10; |
| 79 | + // print the temperature: |
| 80 | + client.print("Current temperature: "); |
| 81 | + client.print(temperature); |
| 82 | + client.print(" degrees C"); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + // Close connection and free resources. |
| 87 | + client.stop(); |
| 88 | + } |
| 89 | + |
| 90 | + delay(50); // Poll every 50ms |
| 91 | +} |
| 92 | + |
0 commit comments