iot_esp8266_dht11.ino 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * esp8266 CLOUD version chip
  3. * (with light sensor and LED light at the two corners)
  4. *
  5. *
  6. * gpio2 - small power/status led
  7. * a0 - analog read
  8. *
  9. */
  10. #include <ESP8266WiFi.h>
  11. #include <DHT.h>
  12. #define DHTTYPE DHT11 // DHT 11
  13. // DHT Sensor
  14. const int DHTPin = 2;
  15. // Initialize DHT sensor.
  16. DHT dht(DHTPin, DHTTYPE);
  17. const char* ssid = "hazard18";
  18. const char* password = "pu1f0xylu1";
  19. // Host
  20. const char* host = "dweet.io";
  21. // Temporary variables
  22. static char celsiusTemp[7];
  23. static char fahrenheitTemp[7];
  24. static char humidityTemp[7];
  25. int c = 0;
  26. void setup() {
  27. //pinMode(2, OUTPUT);
  28. Serial.begin(115200);
  29. delay(100);
  30. Serial.print("Starting... ");
  31. dht.begin();
  32. // We start by connecting to a WiFi network
  33. Serial.println();
  34. Serial.println();
  35. Serial.print("Connecting to ");
  36. Serial.println(ssid);
  37. WiFi.begin(ssid, password);
  38. while (WiFi.status() != WL_CONNECTED) {
  39. delay(500);
  40. Serial.print(".");
  41. }
  42. Serial.println("");
  43. Serial.println("WiFi connected");
  44. Serial.println("IP address: ");
  45. Serial.println(WiFi.localIP());
  46. }
  47. float h,t,f;
  48. void loop() {
  49. Serial.print("Connecting to ");
  50. Serial.println(host);
  51. // Use WiFiClient class to create TCP connections
  52. WiFiClient client;
  53. const int httpPort = 80;
  54. if (!client.connect(host, httpPort)) {
  55. Serial.println("connection failed");
  56. return;
  57. }
  58. h = dht.readHumidity();
  59. t = dht.readTemperature();
  60. f = dht.readTemperature(true);
  61. if (isnan(h) || isnan(t) || isnan(f)) {
  62. Serial.println("Failed to read from DHT sensor!");
  63. strcpy(celsiusTemp,"Failed");
  64. strcpy(fahrenheitTemp, "Failed");
  65. strcpy(humidityTemp, "Failed");
  66. //digitalWrite(2, HIGH);
  67. client.print(String("GET /dweet/for/esp1881?tempC=") + String(celsiusTemp) + String(c++) +
  68. String("&tempF=") + String(fahrenheitTemp) +
  69. String("&humidity=") + String(humidityTemp) +
  70. " HTTP/1.1\r\n" +
  71. "Host: " + host + "\r\n" +
  72. "Connection: close\r\n\r\n");
  73. }
  74. else{
  75. // Computes temperature values in Celsius + Fahrenheit and Humidity
  76. float hic = dht.computeHeatIndex(t, h, false);
  77. float hif = dht.computeHeatIndex(f, h);
  78. sprintf(celsiusTemp,"%f", t);
  79. sprintf(fahrenheitTemp, "%f", f);
  80. sprintf(humidityTemp, "%f", h);
  81. Serial.println(celsiusTemp);
  82. Serial.println(fahrenheitTemp);
  83. Serial.println(humidityTemp);
  84. // Serial.println(String("GET /dweet/for/esp1881?tempC=") + String(celsiusTemp) +
  85. // String("&tempF=") + String(fahrenheitTemp) +
  86. // String("&humidity=") + String(humidityTemp) +
  87. // " HTTP/1.1\r\n" +
  88. // "Host: " + host + "\r\n" +
  89. // "Connection: close\r\n\r\n");
  90. client.print(String("GET /dweet/for/esp1881?tempC=") + String(hic, 2) +
  91. String("&tempF=") + String(hif, 2) +
  92. String("&humidity=") + String(h, 2) +
  93. " HTTP/1.1\r\n" +
  94. "Host: " + host + "\r\n" +
  95. "Connection: close\r\n\r\n");
  96. }
  97. delay(100);
  98. //digitalWrite(2, LOW);
  99. // Read all the lines of the reply from server and print them to Serial
  100. while(client.available()){
  101. String line = client.readStringUntil('\r');
  102. Serial.print(line);
  103. }
  104. //}
  105. delay(500);
  106. }