Guest User

hackathon2019Weather

a guest
Nov 4th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1.  
  2. #include <WiFi.h>
  3. #include "DHT.h"
  4.  
  5. #define DHTTYPE DHT11 // DHT 11
  6.  
  7.  
  8. const char* ssid = "Motoz2";
  9. const char* password = "test1234";
  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. ;
  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. delay(500);
  48. Serial.print(".");
  49. }
  50. Serial.println("");
  51. Serial.println("WiFi connected");
  52. Serial.println("IP address: ");
  53. Serial.println(WiFi.localIP());
  54.  
  55. server.begin();
  56. }
  57.  
  58. void loop() {
  59. // listen for incoming clients
  60. WiFiClient client = server.available();
  61. if (client) {
  62. Serial.println("New client");
  63. memset(linebuf,0,sizeof(linebuf));
  64. charcount=0;
  65. // an http request ends with a blank line
  66. boolean currentLineIsBlank = true;
  67. while (client.connected()) {
  68. if (client.available()) {
  69. char c = client.read();
  70. Serial.write(c);
  71. //read char by char HTTP request
  72. linebuf[charcount]=c;
  73. if (charcount<sizeof(linebuf)-1) charcount++;
  74. // if you've gotten to the end of the line (received a newline
  75. // character) and the line is blank, the http request has ended,
  76. // so you can send a reply
  77. if (c == '\n' && currentLineIsBlank) {
  78. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  79. float h = dht.readHumidity();
  80. // Read temperature as Celsius (the default)
  81. float t = dht.readTemperature();
  82. // Read temperature as Fahrenheit (isFahrenheit = true)
  83. float f = dht.readTemperature(true);
  84. // Check if any reads failed and exit early (to try again).
  85. if (isnan(h) || isnan(t) || isnan(f)) {
  86. Serial.println("Failed to read from DHT sensor!");
  87. strcpy(celsiusTemp,"Failed");
  88. strcpy(fahrenheitTemp, "Failed");
  89. strcpy(humidityTemp, "Failed");
  90. }
  91. else{
  92. // Computes temperature values in Celsius + Fahrenheit and Humidity
  93. float hic = dht.computeHeatIndex(t, h, false);
  94. dtostrf(hic, 6, 2, celsiusTemp);
  95. float hif = dht.computeHeatIndex(f, h);
  96. dtostrf(hif, 6, 2, fahrenheitTemp);
  97. dtostrf(h, 6, 2, humidityTemp);
  98.  
  99. }
  100. client.println("HTTP/1.1 200 OK");
  101. client.println("Content-type:text/html");
  102. client.println("Connection: close");
  103. client.println();
  104.  
  105. // Display the HTML web page
  106. client.println("<!DOCTYPE html><html>");
  107. client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  108. client.println("<link rel=\"icon\" href=\"data:,\">");
  109. // CSS to style the table
  110. client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}");
  111. client.println("table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }");
  112. client.println("th { padding: 12px; background-color: #0043af; color: white; }");
  113. client.println("tr { border: 1px solid #ddd; padding: 12px; }");
  114. client.println("tr:hover { background-color: #bcbcbc; }");
  115. client.println("td { border: none; padding: 12px; }");
  116. client.println(".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }");
  117.  
  118. // Web Page Heading
  119. client.println("</style></head><body><h1>Local Weather Tracker</h1>");
  120. client.println("<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>");
  121. client.println("<tr><td>Temp. Celsius</td><td><span class=\"sensor\">");
  122. client.println(celsiusTemp);
  123. client.println(" *C</span></td></tr>");
  124. client.println("<tr><td>Temp. Fahrenheit</td><td><span class=\"sensor\">");
  125. client.println(fahrenheitTemp);
  126. client.println(" *F</span></td></tr>");
  127.  
  128. client.println("<tr><td>Humidity</td><td><span class=\"sensor\">");
  129. client.println(humidityTemp);
  130. client.println(" %</span></td></tr>");
  131. client.println("</body></html>");
  132. client.println("<p>MLH 2019- TEAM Cyber X Master</p>");
  133. client.println("<p>Deep Web Challenge</p>");
  134. // The HTTP response ends with another blank line
  135. client.println();
  136. // Break out of the while loop
  137. break;
  138.  
  139. }
  140. if (c == '\n') {
  141. // you're starting a new line
  142. currentLineIsBlank = true;
  143. memset(linebuf,0,sizeof(linebuf));
  144. charcount=0;
  145. } else if (c != '\r') {
  146. // you've gotten a character on the current line
  147. currentLineIsBlank = false;
  148. }
  149. }
  150. }
  151. // give the web browser time to receive the data
  152. delay(1);
  153.  
  154. // close the connection:
  155. client.stop();
  156. Serial.println("client disconnected");
  157. }
  158. }
Add Comment
Please, Sign In to add comment