venerdì 2 dicembre 2016

Progetto Temperatura e umidità su WEB

Foto progetto ultimato:



Link fonte:


Schema montaggio:


File programmazione

#include <Adafruit_Sensor.h>

#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 3, 4, 5, 6, 7, 8);
#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xCC, 0xEF, 0xFE, 0xAD
};
IPAddress ip(192, 168, 1, 129);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {

  lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Progetto Berri");
lcd.setCursor(0,1);
lcd.print("WEB-Te/umi");
delay(2000);


  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  lcd.begin(16,2);
  dht.begin();

  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();


lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("IP");
lcd.setCursor(0,1);
lcd.print(Ethernet.localIP());
delay(1000);
}


void loop() {
   // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  int h = dht.readHumidity();
  int t = dht.readTemperature();

    // set the cursor to (0,0):
  lcd.setCursor(0, 0);
  // print from 0 to 9:
  
   lcd.print("Temp: ");
   lcd.print(t);
   lcd.print("C");
  // set the cursor to (16,1):
  lcd.setCursor(0,1);
  lcd.print("Humidity: ");
  lcd.print(h);
  lcd.print("%");
    delay(200);

    
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // 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
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
            client.print("<center>");
            client.print("<font size=20>");
            client.print("Temperatura : ");
            client.print(t);
            client.print("C");
            client.println("<br />");
            client.print("Umidita : ");
            client.print(h);
            client.print("%");
            client.println("<br />");
          client.println("</html>");
          break;
        }
        
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}




Particolarità:

Scaricare libreria DHT22 da sito

Nessun commento:

Posta un commento