{"id":435,"date":"2012-04-30T06:57:45","date_gmt":"2012-04-30T04:57:45","guid":{"rendered":"http:\/\/blog.philippgoecke.de\/?p=435"},"modified":"2012-04-30T08:55:51","modified_gmt":"2012-04-30T06:55:51","slug":"arduino-temperaturehumidity-web-sensor","status":"publish","type":"post","link":"https:\/\/stls.eu\/blog\/2012\/04\/30\/arduino-temperaturehumidity-web-sensor\/","title":{"rendered":"Arduino Temperature\/Humidity Web Sensor"},"content":{"rendered":"<p>Used:<\/p>\n<ul>\n<li><a href=\"http:\/\/arduino.cc\/\" title=\"Arduino\">Arduino Duemilanove<\/a><\/li>\n<li>Arduino Ethernet Shield<\/li>\n<li>DHT22 Sensor<\/li>\n<li><a href=\"https:\/\/github.com\/adafruit\/DHT-sensor-library\" title=\"DHT-sensor-library\">DHT-sensor-library<\/a><\/li>\n<li><a href=\"http:\/\/www.ladyada.net\/learn\/sensors\/dht.html\" title=\"DHT Example\">DHT Example<\/a><\/li>\n<\/ul>\n<p>Connect Sensor-Pin1 to +5V<br \/>\nConnect Sensor-Pin2 to Digital In 2<br \/>\nConnect Sensor-Pin4 to GND<br \/>\nConnect Sensor-Pin1 with a 10K resistor to Sensor-Pin2<\/p>\n<p><a href=\"\/files\/DHTWEB.ino\">Programm Used<\/a> (Combination of WebServer and DHT Example):<\/p>\n<p><code>\/*<br \/>\n  Web Server<\/p>\n<p> A simple web server that shows the value of the analog input pins.<br \/>\n using an Arduino Wiznet Ethernet shield. <\/p>\n<p> Circuit:<br \/>\n * Ethernet shield attached to pins 10, 11, 12, 13<br \/>\n * Analog inputs attached to pins A0 through A5 (optional)<\/p>\n<p> created 18 Dec 2009<br \/>\n by David A. Mellis<br \/>\n modified 4 Sep 2010<br \/>\n by Tom Igoe<\/p>\n<p> *\/<\/p>\n<p>#include <SPI.h><br \/>\n#include <Ethernet.h><\/p>\n<p>\/\/ Example testing sketch for various DHT humidity\/temperature sensors<br \/>\n\/\/ Written by ladyada, public domain<\/p>\n<p>#include \"DHT.h\"<\/p>\n<p>#define DHTPIN 2     \/\/ what pin we're connected to<\/p>\n<p>\/\/ Uncomment whatever type you're using!<br \/>\n\/\/#define DHTTYPE DHT11   \/\/ DHT 11<br \/>\n#define DHTTYPE DHT22   \/\/ DHT 22  (AM2302)<br \/>\n\/\/#define DHTTYPE DHT21   \/\/ DHT 21 (AM2301)<\/p>\n<p>\/\/ Connect pin 1 (on the left) of the sensor to +5V<br \/>\n\/\/ Connect pin 2 of the sensor to whatever your DHTPIN is<br \/>\n\/\/ Connect pin 4 (on the right) of the sensor to GROUND<br \/>\n\/\/ Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor<\/p>\n<p>DHT dht(DHTPIN, DHTTYPE);<\/p>\n<p>\/\/ Enter a MAC address and IP address for your controller below.<br \/>\n\/\/ The IP address will be dependent on your local network:<br \/>\nbyte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };<br \/>\nIPAddress ip(192,168,1, 177);<\/p>\n<p>\/\/ Initialize the Ethernet server library<br \/>\n\/\/ with the IP address and port you want to use<br \/>\n\/\/ (port 80 is default for HTTP):<br \/>\nEthernetServer server(80);<\/p>\n<p>\/\/ Request<br \/>\nint Request;  \/\/ any function will see this variable<\/p>\n<p>void setup() {<br \/>\n  \/\/ start the Ethernet connection and the server:<br \/>\n  Ethernet.begin(mac, ip);<br \/>\n  server.begin();<\/p>\n<p>\/\/  Serial.begin(9600);<br \/>\n\/\/  Serial.println(\"DHTxx test!\");<\/p>\n<p>  \/\/ Request<br \/>\n  Request = 0;<\/p>\n<p>  \/\/ DHT<br \/>\n  dht.begin();<br \/>\n}<\/p>\n<p>void WEBloop()<br \/>\n{<br \/>\n  \/\/ listen for incoming clients<br \/>\n  EthernetClient client = server.available();<br \/>\n  if (client) {<br \/>\n    \/\/ an http request ends with a blank line<br \/>\n    boolean currentLineIsBlank = true;<br \/>\n    while (client.connected()) {<br \/>\n      if (client.available()) {<br \/>\n        char c = client.read();<br \/>\n        \/\/ if you've gotten to the end of the line (received a newline<br \/>\n        \/\/ character) and the line is blank, the http request has ended,<br \/>\n        \/\/ so you can send a reply<br \/>\n        if (c == '\\n' && currentLineIsBlank) {<br \/>\n          \/\/ send a standard http response header<br \/>\n          client.println(\"HTTP\/1.1 200 OK\");<br \/>\n          client.println(\"Content-Type: text\/html\");<br \/>\n          client.println();<\/p>\n<p>          \/\/ output the value of each analog input pin<br \/>\n          \/\/for (int analogChannel = 0; analogChannel < 6; analogChannel++) {\n            \/\/client.print(\"analog input \");\n            \/\/client.print(analogChannel);\n            \/\/client.print(\" is \");\n            \/\/client.print(analogRead(analogChannel));\n            \/\/client.println(\"<br \/>\");<br \/>\n          \/\/}<\/p>\n<p>          \/\/ Reading temperature or humidity takes about 250 milliseconds!<br \/>\n          \/\/ Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)<br \/>\n          float h = dht.readHumidity();<br \/>\n          float t = dht.readTemperature();<\/p>\n<p>          \/\/ check if returns are valid, if they are NaN (not a number) then something went wrong!<br \/>\n          if (isnan(t) || isnan(h)) {<br \/>\n\/\/            Serial.println(\"Failed to read from DHT\");<br \/>\n            \/\/<br \/>\n            client.print(\"Failed to read from DHT\");<br \/>\n            client.println(\"<br \/>\");<br \/>\n          } else {<br \/>\n\/\/            Serial.print(\"Humidity: \");<br \/>\n\/\/            Serial.print(h);<br \/>\n\/\/            Serial.print(\" %\\t\");<br \/>\n\/\/            Serial.print(\"Temperature: \");<br \/>\n\/\/            Serial.print(t);<br \/>\n\/\/            Serial.println(\" *C\");<br \/>\n            \/\/<br \/>\n            client.print(\"Request: \");<br \/>\n            \/\/client.println(random(1000));<br \/>\n            Request = Request + 1;<br \/>\n            client.println(Request);<br \/>\n            \/\/<br \/>\n            client.print(\"Humidity: \");<br \/>\n            client.print(h);<br \/>\n            client.print(\" %\\t\");<br \/>\n            client.print(\"Temperature: \");<br \/>\n            client.print(t);<br \/>\n            client.println(\" *C\");<br \/>\n            client.println(\"<br \/>\");<br \/>\n          } <\/p>\n<p>          break;<br \/>\n        }<br \/>\n        if (c == '\\n') {<br \/>\n          \/\/ you're starting a new line<br \/>\n          currentLineIsBlank = true;<br \/>\n        }<br \/>\n        else if (c != '\\r') {<br \/>\n          \/\/ you've gotten a character on the current line<br \/>\n          currentLineIsBlank = false;<br \/>\n        }<br \/>\n      }<br \/>\n    }<br \/>\n    \/\/ give the web browser time to receive the data<br \/>\n    delay(1);<br \/>\n    \/\/ close the connection:<br \/>\n    client.stop();<br \/>\n  }<br \/>\n}<\/p>\n<p>void loop() {<\/p>\n<p>  \/\/DHTloop();<br \/>\n  WEBloop();<\/p>\n<p>}<\/p>\n<p>void DHTloop() {<br \/>\n  \/\/ Reading temperature or humidity takes about 250 milliseconds!<br \/>\n  \/\/ Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)<br \/>\n  float h = dht.readHumidity();<br \/>\n  float t = dht.readTemperature();<\/p>\n<p>  \/\/ check if returns are valid, if they are NaN (not a number) then something went wrong!<br \/>\n  if (isnan(t) || isnan(h)) {<br \/>\n    Serial.println(\"Failed to read from DHT\");<br \/>\n  } else {<br \/>\n    Serial.print(\"Humidity: \");<br \/>\n    Serial.print(h);<br \/>\n    Serial.print(\" %\\t\");<br \/>\n    Serial.print(\"Temperature: \");<br \/>\n    Serial.print(t);<br \/>\n    Serial.println(\" *C\");<br \/>\n  }<br \/>\n}<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Used: Arduino Duemilanove Arduino Ethernet Shield DHT22 Sensor DHT-sensor-library DHT Example Connect Sensor-Pin1 to +5V Connect Sensor-Pin2 to Digital In 2 Connect Sensor-Pin4 to GND Connect Sensor-Pin1 with a 10K resistor to Sensor-Pin2 Programm Used (Combination of WebServer and DHT &hellip; <a href=\"https:\/\/stls.eu\/blog\/2012\/04\/30\/arduino-temperaturehumidity-web-sensor\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,15,32],"tags":[],"class_list":["post-435","post","type-post","status-publish","format-standard","hentry","category-arduino","category-networking","category-sensor"],"_links":{"self":[{"href":"https:\/\/stls.eu\/blog\/wp-json\/wp\/v2\/posts\/435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stls.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stls.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stls.eu\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/stls.eu\/blog\/wp-json\/wp\/v2\/comments?post=435"}],"version-history":[{"count":11,"href":"https:\/\/stls.eu\/blog\/wp-json\/wp\/v2\/posts\/435\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/stls.eu\/blog\/wp-json\/wp\/v2\/posts\/435\/revisions\/447"}],"wp:attachment":[{"href":"https:\/\/stls.eu\/blog\/wp-json\/wp\/v2\/media?parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stls.eu\/blog\/wp-json\/wp\/v2\/categories?post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stls.eu\/blog\/wp-json\/wp\/v2\/tags?post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}