-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoteTempReader.ino
83 lines (75 loc) · 3.18 KB
/
remoteTempReader.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*--------------------------------------------------------------
author of this magical code: Hannes Breuer, https://github.com/habhabhab
server script author: W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/
#include <SPI.h>
#include <Ethernet.h>
const int sensorPin = A0;
const int sensorVal;
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 2, 254); // IP address needs to change depending on network
EthernetServer server(80); // server at port 80
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
//read value from sensor
int sensorVal = analogRead(sensorPin);
//calculate voltage from sensor value
float voltage = (sensorVal/1024.0) * 5.0;
//calculate temperature from voltage
float temperature = (voltage - .5) * 100;
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Raumtemperatur bei Hannes</title>");
client.println("</head>");
client.println("<body>");
client.println("<p>Sensor Value: ");
client.println(sensorVal);
client.println("</p>");
client.println("<p>Volts: ");
client.println(voltage);
client.println("</p>");
client.println("<p>Temp: ");
client.println(temperature);
client.println("</p>");
client.println("</body>");
client.println("</html>");
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}