Advertisement
Guest User

Untitled

a guest
May 29th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFi.h>
  2. #include "DHT.h"
  3.  
  4. // Uncomment one of the lines below for whatever DHT sensor type you're using!
  5. #define DHTTYPE DHT22
  6.  
  7. // Replace with your network credentials
  8. const char* ssid = "XXX";
  9. const char* password = "XXX";
  10.  
  11. WiFiServer server(80);
  12.  
  13. // DHT Sensor
  14. const int DHTPin = 16;
  15. // Initialize DHT sensor.
  16. DHT dht(DHTPin, DHTTYPE);
  17.  
  18. // Temporary variables
  19. static char celsiusTemp[7];
  20. static char fahrenheitTemp[7];
  21. static char humidityTemp[7];
  22.  
  23. // Client variables
  24. char linebuf[80];
  25. int charcount=0;
  26.  
  27. void setup() {
  28. // initialize the DHT sensor
  29. dht.begin();
  30.  
  31. //Initialize serial and wait for port to open:
  32. Serial.begin(115200);
  33. while(!Serial) {
  34. ; // wait for serial port to connect. Needed for native USB port only
  35. }
  36.  
  37. // We start by connecting to a WiFi network
  38. Serial.println();
  39. Serial.println();
  40. Serial.print("Connecting to ");
  41. Serial.println(ssid);
  42.  
  43. WiFi.begin(ssid, password);
  44.  
  45. // attempt to connect to Wifi network:
  46. while(WiFi.status() != WL_CONNECTED) {
  47. // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  48. delay(500);
  49. Serial.print(".");
  50. }
  51. Serial.println("");
  52. Serial.println("WiFi connected");
  53. Serial.println("IP address: ");
  54. Serial.println(WiFi.localIP());
  55.  
  56. server.begin();
  57. }
  58.  
  59. void loop()
  60. {
  61. // listen for incoming clients
  62. WiFiClient client = server.available();
  63. if (client)
  64. {
  65. Serial.println("New client");
  66. memset(linebuf,0,sizeof(linebuf));
  67. charcount=0;
  68. // an http request ends with a blank line
  69. boolean currentLineIsBlank = true;
  70. while (client.connected())
  71. {
  72. if (client.available())
  73. {
  74. char c = client.read();
  75. Serial.write(c);
  76. //read char by char HTTP request
  77. linebuf[charcount]=c;
  78. if (charcount<sizeof(linebuf)-1) charcount++;
  79.  
  80. if (c == '\n' && currentLineIsBlank)
  81. {
  82.  
  83. float h = dht.readHumidity();
  84. // Read temperature as Celsius (the default)
  85. float t = dht.readTemperature();
  86. // Read temperature as Fahrenheit (isFahrenheit = true)
  87. float f = dht.readTemperature(true);
  88. // Check if any reads failed and exit early (to try again).
  89. if (isnan(h) || isnan(t) || isnan(f))
  90. {
  91. Serial.println("Failed to read from DHT sensor!");
  92. }
  93. else{
  94.  
  95. float hic = dht.computeHeatIndex(t, h, false);
  96. dtostrf(hic, 6, 2, celsiusTemp);
  97. float hif = dht.computeHeatIndex(f, h);
  98. dtostrf(hif, 6, 2, fahrenheitTemp);
  99. dtostrf(h, 6, 2, humidityTemp);
  100. }
  101.  
  102. client.println("HTTP/1.1 200 OK");
  103. client.println("Content-Type: text/html");
  104. client.println("Connection: close"); // the connection will be closed after completion of the response
  105. client.println();
  106. client.println("<!DOCTYPE HTML><html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  107. client.println("<meta http-equiv=\"refresh\" content=\"2\"></head>");
  108. client.println("<body><h1><p>ESP32 DHT11 example</h1><p>");
  109.  
  110. client.println(celsiusTemp);
  111. client.println("*C</p><p>");
  112. client.println(fahrenheitTemp);
  113. client.println("*F</p><p>");
  114. client.println(humidityTemp);
  115. client.println("%</p>");
  116. client.println("</body></html>");
  117. break;
  118. }
  119. if (c == '\n')
  120. {
  121. // you're starting a new line
  122. currentLineIsBlank = true;
  123. memset(linebuf,0,sizeof(linebuf));
  124. charcount=0;
  125. }
  126. else if (c != '\r')
  127. {
  128. // you've gotten a character on the current line
  129. currentLineIsBlank = false;
  130. }
  131. }
  132. }
  133. // give the web browser time to receive the data
  134. delay(1);
  135. // close the connection:
  136. client.stop();
  137. Serial.println("client disconnected");
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement