Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. // DHT Temperature & Humidity Sensor
  2. // Unified Sensor Library Example
  3. // Written by Tony DiCola for Adafruit Industries
  4. // Released under an MIT license.
  5.  
  6. // Depends on the following Arduino libraries:
  7. // - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
  8. // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
  9.  
  10. #include <Adafruit_Sensor.h>
  11. #include <DHT.h>
  12. #include <DHT_U.h>
  13.  
  14. #define DHTPIN D2 // Pin which is connected to the DHT sensor.
  15.  
  16. // Uncomment the type of sensor in use:
  17. //#define DHTTYPE DHT11 // DHT 11
  18. #define DHTTYPE DHT22 // DHT 22 (AM2302)
  19. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  20.  
  21. // See guide for details on sensor wiring and usage:
  22. // https://learn.adafruit.com/dht/overview
  23.  
  24. DHT_Unified dht(DHTPIN, DHTTYPE);
  25.  
  26. uint32_t delayMS;
  27.  
  28.  
  29. #include <ESP8266WiFi.h>
  30.  
  31. const char* ssid = "TLU";
  32. const char* password = "";
  33.  
  34. const char* host = "web.zone.ee";
  35. const char* streamId = "....................";
  36. const char* privateKey = "....................";
  37.  
  38.  
  39. int value = 0;
  40. //float temp;
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. void setup() {
  49.  
  50. Serial.begin(115200);
  51. delay(10);
  52.  
  53.  
  54. // We start by connecting to a WiFi network
  55.  
  56. // Serial.println();
  57. // Serial.println();
  58. Serial.print("Connecting to ");
  59. // Serial.println(ssid);
  60.  
  61. WiFi.begin(ssid, password);
  62.  
  63. while (WiFi.status() != WL_CONNECTED) {
  64. delay(500);
  65. Serial.print(".");
  66. }
  67.  
  68. // Serial.println("");
  69. // Serial.println("WiFi connected");
  70. // Serial.println("IP address: ");
  71. Serial.println(WiFi.localIP());
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. Serial.begin(9600);
  86. // Initialize device.
  87. dht.begin();
  88. Serial.println("DHTxx Unified Sensor Example");
  89. // Print temperature sensor details.
  90. sensor_t sensor;
  91. dht.temperature().getSensor(&sensor);
  92. Serial.println("------------------------------------");
  93. Serial.println("Temperature");
  94. Serial.print ("Sensor: "); Serial.println(sensor.name);
  95. Serial.print ("Driver Ver: "); Serial.println(sensor.version);
  96. Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
  97. Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
  98. Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
  99. Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
  100. Serial.println("------------------------------------");
  101. // Print humidity sensor details.
  102. dht.humidity().getSensor(&sensor);
  103. Serial.println("------------------------------------");
  104. Serial.println("Humidity");
  105. Serial.print ("Sensor: "); Serial.println(sensor.name);
  106. Serial.print ("Driver Ver: "); Serial.println(sensor.version);
  107. Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
  108. Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
  109. Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
  110. Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
  111. Serial.println("------------------------------------");
  112. // Set delay between sensor readings based on sensor details.
  113. delayMS = sensor.min_delay / 1000;
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120. void loop() {
  121. String tempToString;
  122. String humToString;
  123.  
  124. // Delay between measurements.
  125. delay(delayMS);
  126. // Get temperature event and print its value.
  127. sensors_event_t event;
  128. dht.temperature().getEvent(&event);
  129. if (isnan(event.temperature)) {
  130. Serial.println("Error reading temperature!");
  131. }
  132. else {
  133. Serial.print("Temperature: ");
  134. tempToString = String(event.temperature, 2);
  135. Serial.print(event.temperature);
  136. Serial.println(" *C");
  137. }
  138. // Get humidity event and print its value.
  139. dht.humidity().getEvent(&event);
  140. if (isnan(event.relative_humidity)) {
  141. Serial.println("Error reading humidity!");
  142. }
  143. else {
  144. Serial.print("Humidity: ");
  145. humToString = String(event.relative_humidity, 2);
  146. Serial.print(event.relative_humidity);
  147. Serial.println("%");
  148. }
  149.  
  150. //delay(5000);
  151. ++value;
  152.  
  153. float temp = event.temperature;
  154.  
  155. tempToString.replace('.', 'p');
  156. humToString.replace('.', 'p');
  157.  
  158. // Serial.print("connecting to ");
  159. // Serial.println(host);
  160.  
  161. // Use WiFiClient class to create TCP connections
  162. WiFiClient client;
  163. const int httpPort = 80;
  164. if (!client.connect(host, httpPort)) {
  165. Serial.println("connection failed");
  166. return;
  167. }
  168.  
  169.  
  170.  
  171.  
  172. // We now create a URI for the request
  173. String url = "http://web.zone.ee/asjadeinternet2017/katse.php?temp=" + tempToString + "&hum=" + humToString;
  174. // url += streamId;
  175. // url += "?private_key=";
  176. // url += privateKey;
  177. // url += "&value=";
  178. // url += value;
  179.  
  180. Serial.print("Requesting URL: ");
  181. Serial.println(url);
  182.  
  183. // This will send the request to the server
  184. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  185. "Host: " + host + "\r\n" +
  186. "Connection: close\r\n\r\n");
  187. unsigned long timeout = millis();
  188. while (client.available() == 0) {
  189. if (millis() - timeout > 5000) {
  190. Serial.println(">>> Client Timeout !");
  191. client.stop();
  192. return;
  193. }
  194. }
  195.  
  196. // Read all the lines of the reply from server and print them to Serial
  197. while(client.available()){
  198. String line = client.readStringUntil('\r');
  199. //Serial.println(line);
  200. if(line.indexOf("+++") != -1) {
  201. Serial.println("andmed");
  202. // break;
  203. }
  204. Serial.print(line);
  205. }
  206.  
  207. Serial.println();
  208. Serial.println("closing connection");
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement