Advertisement
ossipee

Untitled

Apr 28th, 2016
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.25 KB | None | 0 0
  1. #include <DHT.h>
  2.  
  3. #define DHTPIN 2     // what pin we're connected to
  4.  
  5. // Uncomment whatever type you're using!
  6. //#define DHTTYPE DHT11   // DHT 11
  7. #define DHTTYPE DHT22   // DHT 22  (AM2302)
  8. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
  9.  
  10. // Connect pin 1 (on the left) of the sensor to +5V
  11. // Connect pin 2 of the sensor to whatever your DHTPIN is
  12. // Connect pin 4 (on the right) of the sensor to GROUND
  13. // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
  14.  
  15. DHT dht(DHTPIN, DHTTYPE);
  16.  
  17. #include <SPI.h>
  18. #include <Ethernet.h>
  19.  
  20. // Enter a MAC address and IP address for your controller below.
  21. // The IP address will be dependent on your local network:
  22. byte mac[] = {
  23.   mac here}; //MAC address found on the back of your ethernet shield.
  24. IPAddress ip(here); // IP address dependent upon your network addresses.
  25.  
  26. // Initialize the Ethernet server library
  27. // with the IP address and port you want to use
  28. // (port 80 is default for HTTP):
  29. EthernetServer server(80);
  30.  
  31. void setup() {
  32. // Open serial communications and wait for port to open:
  33.   Serial.begin(9600);
  34.    while (!Serial) {
  35.     ; // wait for serial port to connect. Needed for Leonardo only
  36.   }
  37.  
  38.   dht.begin();
  39.  
  40.   // start the Ethernet connection and the server:
  41.   Ethernet.begin(mac, ip);
  42.   server.begin();
  43.   Serial.print("server is at ");
  44.   Serial.println(Ethernet.localIP());
  45. }
  46.  
  47.  
  48. void loop() {
  49.  
  50.   // Reading temperature or humidity takes about 250 milliseconds!
  51.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  52.   float h = dht.readHumidity();
  53.   float t = dht.readTemperature();
  54.  
  55.   // check if returns are valid, if they are NaN (not a number) then something went wrong!
  56.   if (isnan(t) || isnan(h)) {
  57.     Serial.println("Failed to read from DHT");
  58.   } else {
  59.     Serial.print("Porter Maine:");
  60.     Serial.print("Humidity: ");
  61.     Serial.print(h);
  62.     Serial.print(" %\t");
  63.     Serial.print("Temperature: ");
  64.     Serial.print(t);
  65.     Serial.println(" *C");
  66.   }
  67.  
  68.   // listen for incoming clients
  69.   EthernetClient client = server.available();
  70.   if (client) {
  71.     Serial.println("new client");
  72.     // an http request ends with a blank line
  73.     boolean currentLineIsBlank = true;
  74.     while (client.connected()) {
  75.       if (client.available()) {
  76.         char c = client.read();
  77.         Serial.write(c);
  78.         // if you've gotten to the end of the line (received a newline
  79.         // character) and the line is blank, the http request has ended,
  80.         // so you can send a reply
  81.         if (c == '\n' && currentLineIsBlank) {
  82.           // send a standard http response header
  83.           client.println("HTTP/1.1 200 OK");
  84.           client.println("Content-Type: text/html");
  85.           client.println("Connection: close");  // the connection will be closed after completion of the response
  86.    client.println("Refresh: 5");  // refresh the page automatically every 5 sec
  87.           client.println();
  88.           client.println("<!DOCTYPE HTML>");
  89.           client.println("<html>");
  90.        
  91.           // output the value of the DHT-11
  92.          client.print("Ossipee's Kitchen:");
  93.          client.println("<H2>");
  94.             client.print("Humidity: ");
  95.             client.println("</H2>");
  96.             client.println("<p />");
  97.             client.println("<H1>");
  98.             client.print(h);
  99.             client.print(" %\t");
  100.             client.println("</H1>");
  101.             client.println("<p />");
  102.             client.println("<H2>");
  103.             client.print("Temperature: ");
  104.             client.println("</H2>");
  105.             client.println("<H1>");
  106.             client.print(t*1.8+32);
  107.             client.println(" &#176;");
  108.             client.println("F");
  109.             client.println("</H1>");
  110.            
  111.                
  112.          
  113.           client.println("</html>");
  114.           break;
  115.         }
  116.         if (c == '\n') {
  117.           // you're starting a new line
  118.           currentLineIsBlank = true;
  119.         }
  120.         else if (c != '\r') {
  121.           // you've gotten a character on the current line
  122.           currentLineIsBlank = false;
  123.         }
  124.       }
  125.     }
  126.     // give the web browser time to receive the data
  127.     delay(1);
  128.     // close the connection:
  129.     client.stop();
  130.     Serial.println("client disonnected");
  131.   }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement