Created
October 16, 2011 09:06
-
-
Save ajfisher/1290685 to your computer and use it in GitHub Desktop.
Basic networked temperature sensor that displays a web page for arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Basic Networked temperature sensor. | |
Displays a web page with the temperature on it. | |
Author: Andrew Fisher | |
Date: 30 September 2011 | |
Version 0.1 | |
Adapted from generic web server example as art of IDE created by David Mellis and Tom Igoe. | |
Circuit: Ethernet shield or other ethenet enabled Arduino on pins 10,11,12,13 | |
+5v --------- 10K Thermistor ---------- 10K Resistor ------- GND | |
| | |
| | |
| | |
Arduino Analog 0 | |
This work is in the public domain, as was the original work by Mellis and Igoe above. | |
**/ | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#define TEMP_SENSOR 0 | |
// Enter a MAC address and IP address for your controller below. | |
// The IP address will be dependent on your local network: | |
byte mac[] = { 0x06, 0xB1, 0xF9, 0xC1, 0xE2, 0x38 }; | |
byte ip[] = { 10,16,0,33 }; | |
byte gateway[] = {10,16,0,1}; | |
byte subnet[] = {255,255,128,0}; | |
// initialise the web server | |
Server server(80); | |
float current_temp = 0; | |
void setup() { | |
Ethernet.begin(mac, ip, gateway, subnet); | |
server.begin(); | |
} | |
void loop() { | |
current_temp = get_temp(); | |
// listen for incoming clients | |
Client client = server.available(); | |
if (client) { | |
// an http request ends with a blank line | |
boolean currentLineIsBlank = true; | |
while (client.connected()) { | |
if (client.available()) { | |
char c = client.read(); | |
// if you've gotten to the end of the line (received a newline | |
// character) and the line is blank, the http request has ended, | |
// so you can send a reply | |
if (c == '\n' && currentLineIsBlank) { | |
// send a standard http response header | |
send_response(client); | |
break; | |
} | |
if (c == '\n') { | |
// you're starting a new line | |
currentLineIsBlank = true; | |
} | |
else if (c != '\r') { | |
// you've gotten a character on the current line | |
currentLineIsBlank = false; | |
} | |
} | |
} | |
// give the web browser time to receive the data | |
delay(1); | |
// close the connection: | |
client.stop(); | |
} | |
} | |
float get_temp() { | |
// get the sensor val and use Steinhart-Hart approximation | |
// to get the temperature | |
float THERMR = 10000; | |
float PAD = 10000; | |
int rawval = analogRead(TEMP_SENSOR); | |
long Resistance=((1024 * THERMR / rawval) - PAD); | |
float Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later | |
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp)); | |
Temp = Temp - 273.15; // Convert Kelvin to Celsius | |
return(Temp); | |
} | |
void send_response(Client client) { | |
// this function simply sends the response including relevant html as well | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-Type: text/html"); | |
client.println(); | |
client.println("<!doctype html>"); | |
client.println("<html><head><title>Arduino - Current Temperature</title>"); | |
client.println("<link href='http://fonts.googleapis.com/css?family=Rokkitt:400,700' rel='stylesheet' type='text/css'>"); | |
client.println("<link href='http://ajfisher.me/wds/temp.css' rel='stylesheet' type='text/css'>"); | |
client.println("</head><body>"); | |
client.println("<div class=\"temperature\">"); | |
client.println("<h1>Current Temperature:</h1>"); | |
client.print("<p><span id=\"temp\">"); | |
client.print(current_temp); | |
client.println("</span><sup id=\"deg\">°C</sup></p>"); | |
client.println("<h2>Design / CSS thanks to <a href=\"http://twitter.com/superhighfives\">superhighfives</a></2></p>"); | |
client.println("</div></body></html>"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment