Advertisement
Guest User

PatenteChose

a guest
Sep 15th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. // Example testing sketch for various DHT humidity/temperature sensors
  2. // Written by ladyada, public domain
  3. #include "DHT.h"
  4. #include <UIPEthernet.h> // Used for Ethernet
  5.  
  6. // **** SENSOR SETTING ****
  7. #define DHTPIN 2 // what pin we're connected to
  8. // Uncomment whatever type you're using!
  9. #define DHTTYPE DHT11 // DHT 11
  10. //#define DHTTYPE DHT22 // DHT 22 (AM2302)
  11. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  12. // Initialize DHT sensor for normal 16mhz Arduino
  13. DHT dht(DHTPIN, DHTTYPE);
  14.  
  15. // **** ETHERNET SETTING ****
  16. byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
  17. IPAddress ip(192, 168, 1, 179);
  18. EthernetServer server(80);
  19.  
  20. void setup() {
  21. Serial.begin(9600);
  22. Serial.println("DHTxx test!");
  23. Serial.println("Ethernet WebServer Example");
  24. dht.begin();
  25. Ethernet.begin(mac, ip);
  26. server.begin();
  27. Serial.print("IP Address: ");
  28. Serial.println(Ethernet.localIP());
  29. }
  30.  
  31. void loop() {
  32. // Wait a few seconds between measurements.
  33. delay(2000);
  34.  
  35. // Reading temperature or humidity takes about 250 milliseconds!
  36. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  37. float h = dht.readHumidity();
  38. // Read temperature as Celsius
  39. float t = dht.readTemperature();
  40. // Read temperature as Fahrenheit
  41. float f = dht.readTemperature(true);
  42.  
  43. // Check if any reads failed and exit early (to try again).
  44. if (isnan(h) || isnan(t)) {
  45. Serial.println("Failed to read from DHT sensor!");
  46. return;
  47. }
  48.  
  49. Serial.print("Humidity: ");
  50. Serial.print(h);
  51. Serial.print(" %\t");
  52. Serial.print("Temperature: ");
  53. Serial.print(t);
  54. Serial.print(" *C\t");
  55. Serial.print("\n");
  56. // listen for incoming clients
  57. EthernetClient client = server.available();
  58.  
  59. if (client)
  60. {
  61. Serial.println("-> New Connection");
  62.  
  63. // an http request ends with a blank line
  64. boolean currentLineIsBlank = true;
  65.  
  66. while (client.connected())
  67. {
  68. if (client.available())
  69. {
  70. char c = client.read();
  71.  
  72. // if you've gotten to the end of the line (received a newline
  73. // character) and the line is blank, the http request has ended,
  74. // so you can send a reply
  75. if (c == '\n' && currentLineIsBlank)
  76. {
  77. client.println("<html><title>Hello World!</title><body><h3>Hello World!</h3></body>");
  78. break;
  79. }
  80.  
  81. if (c == '\n') {
  82. // you're starting a new line
  83. currentLineIsBlank = true;
  84. }
  85. else if (c != '\r')
  86. {
  87. // you've gotten a character on the current line
  88. currentLineIsBlank = false;
  89. }
  90. }
  91. }
  92.  
  93. // give the web browser time to receive the data
  94. delay(10);
  95.  
  96. // close the connection:
  97. client.stop();
  98. Serial.println(" Disconnected\n");
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement