Advertisement
ossipee

online weather

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