Advertisement
Guest User

kod2

a guest
Nov 11th, 2021
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <DHT.h>
  2.  
  3. #define DHTPIN A9     // what pin we're connected the DHT output
  4. #define DHTTYPE DHT11   // DHT 11
  5. DHT dht(DHTPIN, DHTTYPE);
  6.  
  7. #include <Wire.h>
  8.  
  9.  
  10. #include <SPI.h>
  11. #include <Ethernet.h>
  12.  
  13. // Enter a MAC address and IP address for your controller below.
  14. // The IP address will be dependent on your local network:
  15. byte mac[] = {
  16.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  17. };
  18. IPAddress ip(192, 168, 1, 54);
  19.  
  20. // Initialize the Ethernet server library
  21. // with the IP address and port you want to use
  22. // (port 80 is default for HTTP):
  23. EthernetServer server(80);
  24.  
  25. void setup() {
  26.   dht.begin();
  27.  
  28.   // Open serial communications and wait for port to open:
  29.   Serial.begin(9600);
  30.   while (!Serial) {
  31.     ; // wait for serial port to connect. Needed for Leonardo only
  32.   }
  33.  
  34.   // start the Ethernet connection and the server:
  35.   Ethernet.begin(mac, ip);
  36.   server.begin();
  37.   Serial.print("server is at ");
  38.   Serial.println(Ethernet.localIP());
  39. }
  40.  
  41.  
  42. void loop() {
  43.   int h = dht.readHumidity();
  44.   int t = dht.readTemperature();
  45.  
  46.   // listen for incoming clients
  47.   EthernetClient client = server.available();
  48.   if (client) {
  49.     Serial.println("new client");
  50.     // an http request ends with a blank line
  51.     boolean currentLineIsBlank = true;
  52.     while (client.connected()) {
  53.       if (client.available()) {
  54.         char c = client.read();
  55.         Serial.write(c);
  56.         // if you've gotten to the end of the line (received a newline
  57.         // character) and the line is blank, the http request has ended,
  58.         // so you can send a reply
  59.         if (c == '\n' && currentLineIsBlank) {
  60.           // send a standard http response header
  61.           client.println("HTTP/1.1 200 OK");
  62.           client.println("Content-Type: text/html");
  63.           client.println("Connnection: close");
  64.           client.println();
  65.           client.println("<!DOCTYPE HTML>");
  66.           client.println("<html>");
  67.           // add a meta refresh tag, so the browser pulls again every 5 seconds:
  68.           client.println("<head>");
  69.           client.println("<head>");
  70.           client.println("<meta http-equiv=\"refresh\" content=\"5\">");
  71.  
  72.  
  73.           // output the value of temperature and humuidity from DHT
  74.           client.print("<h4>Temperatura z czujnika dht11:<h4>");
  75.           client.println("<br />");
  76.  
  77.           client.print("<h4>temperatura=<h4>");
  78.           client.print(t);
  79.           client.print("<sup>0</sup>");
  80.           client.print("C");
  81.           client.println("<br />");
  82.           client.print("<h4>wilgotnosc=<h4>");
  83.           client.print(h);
  84.           client.print("%");
  85.           client.println("<br />");
  86.  
  87.           client.println("</html>");
  88.           break;
  89.         }
  90.         if (c == '\n') {
  91.           // you're starting a new line
  92.           currentLineIsBlank = true;
  93.         }
  94.         else if (c != '\r') {
  95.           // you've gotten a character on the current line
  96.           currentLineIsBlank = false;
  97.         }
  98.       }
  99.     }
  100.     // give the web browser time to receive the data
  101.     delay(1);
  102.     // close the connection:
  103.     client.stop();
  104.     Serial.println("client disonnected");
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement