test.ino 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*********
  2. Rui Santos
  3. Complete project details at http://randomnerdtutorials.com
  4. *********/
  5. // Including the ESP8266 WiFi library
  6. #include <ESP8266WiFi.h>
  7. #include "DHT.h"
  8. // Uncomment one of the lines below for whatever DHT sensor type you're using!
  9. //#define DHTTYPE DHT11 // DHT 11
  10. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  11. #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
  12. // Replace with your network details
  13. const char* ssid = "KS61T5SH";
  14. const char* password = "Gurken651salat";
  15. // Web Server on port 80
  16. WiFiServer server(80);
  17. // DHT Sensor
  18. const int DHTPin = 13;
  19. // Initialize DHT sensor.
  20. DHT dht(DHTPin, DHTTYPE);
  21. // Temporary variables
  22. static char celsiusTemp[7];
  23. static char fahrenheitTemp[7];
  24. static char humidityTemp[7];
  25. // only runs once on boot
  26. void setup() {
  27. // Initializing serial port for debugging purposes
  28. Serial.begin(115200);
  29. delay(10);
  30. dht.begin();
  31. // Connecting to WiFi network
  32. Serial.println();
  33. Serial.print("Connecting to ");
  34. Serial.println(ssid);
  35. WiFi.begin(ssid, password);
  36. while (WiFi.status() != WL_CONNECTED) {
  37. delay(500);
  38. Serial.print(".");
  39. }
  40. Serial.println("");
  41. Serial.println("WiFi connected");
  42. // Starting the web server
  43. server.begin();
  44. Serial.println("Web server running. Waiting for the ESP IP...");
  45. delay(10000);
  46. // Printing the ESP IP address
  47. Serial.println(WiFi.localIP());
  48. }
  49. // runs over and over again
  50. void loop() {
  51. // Listenning for new clients
  52. WiFiClient client = server.available();
  53. if (client) {
  54. Serial.println("New client");
  55. // bolean to locate when the http request ends
  56. boolean blank_line = true;
  57. while (client.connected()) {
  58. if (client.available()) {
  59. char c = client.read();
  60. if (c == '\n' && blank_line) {
  61. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  62. float h = dht.readHumidity();
  63. // Read temperature as Celsius (the default)
  64. float t = dht.readTemperature();
  65. // Read temperature as Fahrenheit (isFahrenheit = true)
  66. float f = dht.readTemperature(true);
  67. // Check if any reads failed and exit early (to try again).
  68. if (isnan(h) || isnan(t) || isnan(f)) {
  69. Serial.println("Failed to read from DHT sensor!");
  70. strcpy(celsiusTemp,"Failed");
  71. strcpy(fahrenheitTemp, "Failed");
  72. strcpy(humidityTemp, "Failed");
  73. }
  74. else{
  75. // Computes temperature values in Celsius + Fahrenheit and Humidity
  76. float hic = dht.computeHeatIndex(t, h, false);
  77. dtostrf(hic, 6, 2, celsiusTemp);
  78. float hif = dht.computeHeatIndex(f, h);
  79. dtostrf(hif, 6, 2, fahrenheitTemp);
  80. dtostrf(h, 6, 2, humidityTemp);
  81. // You can delete the following Serial.print's, it's just for debugging purposes
  82. Serial.print("Humidity: ");
  83. Serial.print(h);
  84. Serial.print(" %\t Temperature: ");
  85. Serial.print(t);
  86. Serial.print(" *C ");
  87. Serial.print(f);
  88. Serial.print(" *F\t Heat index: ");
  89. Serial.print(hic);
  90. Serial.print(" *C ");
  91. Serial.print(hif);
  92. Serial.print(" *F");
  93. Serial.print("Humidity: ");
  94. Serial.print(h);
  95. Serial.print(" %\t Temperature: ");
  96. Serial.print(t);
  97. Serial.print(" *C ");
  98. Serial.print(f);
  99. Serial.print(" *F\t Heat index: ");
  100. Serial.print(hic);
  101. Serial.print(" *C ");
  102. Serial.print(hif);
  103. Serial.println(" *F");
  104. }
  105. client.println("HTTP/1.1 200 OK");
  106. client.println("Content-Type: text/html");
  107. client.println("Connection: close");
  108. client.println();
  109. // your actual web page that displays temperature and humidity
  110. client.println("<!DOCTYPE HTML>");
  111. client.println("<html>");
  112. client.println("<head></head><body><h1>ESP8266 - Temperature and Humidity</h1><h3>Temperature in Celsius: ");
  113. client.println(celsiusTemp);
  114. client.println("*C</h3><h3>Temperature in Fahrenheit: ");
  115. client.println(fahrenheitTemp);
  116. client.println("*F</h3><h3>Humidity: ");
  117. client.println(humidityTemp);
  118. client.println("%</h3><h3>");
  119. client.println("</body></html>");
  120. break;
  121. }
  122. if (c == '\n') {
  123. // when starts reading a new line
  124. blank_line = true;
  125. }
  126. else if (c != '\r') {
  127. // when finds a character on the current line
  128. blank_line = false;
  129. }
  130. }
  131. }
  132. // closing the client connection
  133. delay(1);
  134. client.stop();
  135. Serial.println("Client disconnected.");
  136. }
  137. }