ESP32 กับ AM2301 (DHT21)

ไอโอทีเส้นทางสู่เมืองอัจฉริยะ

Created with Sketch.

ESP32 กับ AM2301 (DHT21)

วันนี้มาลองเล่น ESP32 กับ AM2301  (DHT21)  ตามรูป

ตัวนี่ก็มีความแม่นยำดี  สายลากยาวไปได้  100  เมตร

(อ้างอิงตามเวบไซต์ http://www.esp32learning.com/code/esp32-and-am2301-sensor-example.php)

การต่อ  ESP32  กับ  AM2301

  • 3V3 กับสายสีแดง
  • G2 เข้ากับ เหลือง
  • GND-GND
  • และมี  R 4.7 kOhm  แต่ตัวอย่างนี้  ใช้  R 1 k  จำนวน  5 ตัวต่ออนุกรมกันซะเลย   โดยทั้งหมดต่อลงใน Shield ESP32 V1, ขามันไม่พอกับ V3  เลยต้องเอาเกณฑ์ขา  3V3 ตรงกันกับบอร์ด  ที่เหลือก็ไล่ลำดับเอา  (ซื้อมาผิดเอง  ก็ต้องปรับใช้เอา)

Code แรก เป็น Code ทดสอบ การเชื่อมต่อว่าทำงานได้ตามปกติ  ก็เล่นซะพักใหญ่   ในเวบหลายเวบแนะนำใช้ขา 15 แต่ไม่ใช่อะนะ ครับ  ต้องลงที่ขา 2

โดยอาการที่บอกว่าต่อผิด  เช่น  อัพโหลดโปรแกรมไม่ได้  ทั้งๆ ที่ compile ผ่าน  บางเวบแนะนว่าตอนอัพโหลด  ให้ถอด ตัว Sensor ออกก่อน  แล้วอัพโหลดได้  แต่ก็ใช้ไม่ได้  กระทั่งต่อมาใช้ขา 2 แล้วอัพโหลด  แบบไม่ต้องถอด Sensor  ก็เป็นอันเรียบร้อย

  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. // Depends on the following Arduino libraries:
  6. // – Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
  7. // – DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
  8. // Use 3V3 GIOP 2 ESP32 Dev Module
  9. // Programmer AVRSIP mkll
  10. #include <Adafruit_Sensor.h>
  11. #include <DHT.h> // ต้องใช้คู่กับ DHT_U.h
  12. #include <DHT_U.h>
  13. #define DHTPIN 2 // Pin which is connected to the DHT sensor.
  14. // Uncomment the type of sensor in use:
  15. //#define DHTTYPE DHT11 // DHT 11
  16. //#define DHTTYPE DHT22 // DHT 22 (AM2302)
  17. #define DHTTYPE DHT21 // DHT 21 (AM2301)
  18. // See guide for details on sensor wiring and usage:
  19. // https://learn.adafruit.com/dht/overview
  20. DHT_Unified dht(DHTPIN, DHTTYPE);
  21. uint32_t delayMS;
  22. void setup() {
  23. Serial.begin(115200);
  24. // Initialize device.
  25. dht.begin();
  26. Serial.println(“DHTxx Unified Sensor Example”);
  27. // Print temperature sensor details.
  28. sensor_t sensor;
  29. dht.temperature().getSensor(&sensor);
  30. Serial.println(“————————————“);
  31. Serial.println(“Temperature”);
  32. Serial.print (“Sensor: “); Serial.println(sensor.name);
  33. Serial.print (“Driver Ver: “); Serial.println(sensor.version);
  34. Serial.print (“Unique ID: “); Serial.println(sensor.sensor_id);
  35. Serial.print (“Max Value: “); Serial.print(sensor.max_value); Serial.println(” *C”);
  36. Serial.print (“Min Value: “); Serial.print(sensor.min_value); Serial.println(” *C”);
  37. Serial.print (“Resolution: “); Serial.print(sensor.resolution); Serial.println(” *C”);
  38. Serial.println(“————————————“);
  39. // Print humidity sensor details.
  40. dht.humidity().getSensor(&sensor);
  41. Serial.println(“————————————“);
  42. Serial.println(“Humidity”);
  43. Serial.print (“Sensor: “); Serial.println(sensor.name);
  44. Serial.print (“Driver Ver: “); Serial.println(sensor.version);
  45. Serial.print (“Unique ID: “); Serial.println(sensor.sensor_id);
  46. Serial.print (“Max Value: “); Serial.print(sensor.max_value); Serial.println(“%”);
  47. Serial.print (“Min Value: “); Serial.print(sensor.min_value); Serial.println(“%”);
  48. Serial.print (“Resolution: “); Serial.print(sensor.resolution); Serial.println(“%”);
  49. Serial.println(“————————————“);
  50. // Set delay between sensor readings based on sensor details.
  51. delayMS = sensor.min_delay / 1000;
  52. }
  53. void loop() {
  54. // Delay between measurements.
  55. delay(delayMS);
  56. // Get temperature event and print its value.
  57. sensors_event_t event;
  58. dht.temperature().getEvent(&event);
  59. if (isnan(event.temperature)) {
  60. Serial.println(“Error reading temperature!”);
  61. }
  62. else {
  63. Serial.print(“Temperature: “);
  64. Serial.print(event.temperature);
  65. Serial.println(” *C”);
  66. }
  67. // Get humidity event and print its value.
  68. dht.humidity().getEvent(&event);
  69. if (isnan(event.relative_humidity)) {
  70. Serial.println(“Error reading humidity!”);
  71. }
  72. else {
  73. Serial.print(“Humidity: “);
  74. Serial.print(event.relative_humidity);
  75. Serial.println(“%”);
  76. }
  77. }

ส่วนที่  2  เป็น Code สำหรับการเชื่อมต่อกับ  Server  (เราเน้นเล่นการต่อกับ  Server เป็นหลัก)

เวบไซต์อ้างอิง  : http://www.esp32learning.com/code/esp32-and-am2301-sensor-example.php

  1. /*
  2. * This sketch sends data via HTTP GET requests to data.sparkfun.com service.
  3. *
  4. * You need to get streamId and privateKey at data.sparkfun.com and paste them
  5. * below. Or just customize this script to talk to other HTTP servers.
  6. * Set board NodeMCU-32S pr ESP32 Dev Module
  7. * Set Programmer AVRISP mkll
  8. */
  9. //===== AM2301 – DHT21 setting
  10. // Unified Sensor Library Example
  11. // Written by Tony DiCola for Adafruit Industries
  12. // Released under an MIT license.
  13. // Depends on the following Arduino libraries:
  14. // – Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
  15. // – DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
  16. // Use 3V3 GIOP 2 ESP32 Dev Module
  17. // Programmer AVRSIP mkll
  18. #include <Adafruit_Sensor.h>
  19. #include <DHT.h> // กรณีนี้ต้องใช้คู่กันกับ DHT_U.h
  20. #include <DHT_U.h>
  21. #define DHTPIN 2 // Pin which is connected to the DHT sensor.
  22. // Uncomment the type of sensor in use:
  23. //#define DHTTYPE DHT11 // DHT 11
  24. //#define DHTTYPE DHT22 // DHT 22 (AM2302)
  25. #define DHTTYPE DHT21 // DHT 21 (AM2301)
  26. // See guide for details on sensor wiring and usage:
  27. // https://learn.adafruit.com/dht/overview
  28. DHT_Unified dht(DHTPIN, DHTTYPE);
  29. uint32_t delayMS;
  30. //=========
  31. #include <WiFi.h>
  32. //======
  33. //====
  34. const char* ssid = “Arsenae_2.4G”;
  35. const char* password = “kb075699212”;
  36. const char* host = “otrixiot.com”;
  37. const char* code = “ESP32-002”;
  38. float temp_0 = 0;
  39. float humid_0 = 0;
  40. float vHumidity = 0;
  41. float vTemperature = 0;
  42. float data1=15;
  43. float data2=33;
  44. float data3=33;
  45. float data4=100;
  46. float data5=33;
  47. float data6=33;
  48. float data7=33;
  49. float data8=33;
  50. float data9=33;
  51. float data10=33;
  52. float data11=33;
  53. float data12=33;
  54. float data13=33;
  55. float data14=33;
  56. float data15=33;
  57. float data16=33;
  58. float data17=33;
  59. float data18=33;
  60. float data19=33;
  61. float data20=33;
  62. /*
  63. void setup_Sensor_Box_Temperature()
  64. {
  65. dht.begin();
  66. }
  67. */
  68. /// ===
  69. void AM2301_setup() {
  70. Serial.begin(115200);
  71. // Initialize device.
  72. dht.begin();
  73. Serial.println(“DHTxx Unified Sensor Example”);
  74. // Print temperature sensor details.
  75. sensor_t sensor;
  76. dht.temperature().getSensor(&sensor);
  77. Serial.println(“————————————“);
  78. Serial.println(“Temperature”);
  79. Serial.print (“Sensor: “); Serial.println(sensor.name);
  80. Serial.print (“Driver Ver: “); Serial.println(sensor.version);
  81. Serial.print (“Unique ID: “); Serial.println(sensor.sensor_id);
  82. Serial.print (“Max Value: “); Serial.print(sensor.max_value); Serial.println(” *C”);
  83. Serial.print (“Min Value: “); Serial.print(sensor.min_value); Serial.println(” *C”);
  84. Serial.print (“Resolution: “); Serial.print(sensor.resolution); Serial.println(” *C”);
  85. Serial.println(“————————————“);
  86. // Print humidity sensor details.
  87. dht.humidity().getSensor(&sensor);
  88. Serial.println(“————————————“);
  89. Serial.println(“Humidity”);
  90. Serial.print (“Sensor: “); Serial.println(sensor.name);
  91. Serial.print (“Driver Ver: “); Serial.println(sensor.version);
  92. Serial.print (“Unique ID: “); Serial.println(sensor.sensor_id);
  93. Serial.print (“Max Value: “); Serial.print(sensor.max_value); Serial.println(“%”);
  94. Serial.print (“Min Value: “); Serial.print(sensor.min_value); Serial.println(“%”);
  95. Serial.print (“Resolution: “); Serial.print(sensor.resolution); Serial.println(“%”);
  96. Serial.println(“————————————“);
  97. // Set delay between sensor readings based on sensor details.
  98. delayMS = sensor.min_delay / 1000;
  99. }
  100. // Main setup ========
  101. void setup()
  102. {
  103. Serial.begin(115200);
  104. AM2301_setup();
  105. delay(10);
  106. // We start by connecting to a WiFi network
  107. Serial.println();
  108. Serial.println();
  109. Serial.print(“Connecting to “);
  110. Serial.println(ssid);
  111. WiFi.begin(ssid, password);
  112. while (WiFi.status() != WL_CONNECTED) {
  113. delay(500);
  114. Serial.print(“.”);
  115. }
  116. Serial.println(“”);
  117. Serial.println(“WiFi connected”);
  118. Serial.println(“IP address: “);
  119. Serial.println(WiFi.localIP());
  120. }
  121. int value = 0;
  122. void loop()
  123. {
  124. delay(5000);
  125. AM2301_loop();
  126. // scanSensor_Box_Temperature();
  127. //data1 = vTemperature;
  128. ++value;
  129. data1 = temp_0;
  130. data2 = humid_0;
  131. Serial.print(“connecting to “);
  132. Serial.println(host);
  133. // Use WiFiClient class to create TCP connections
  134. WiFiClient client;
  135. const int httpPort = 80;
  136. if (!client.connect(host, httpPort)) {
  137. Serial.println(“connection failed”);
  138. return;
  139. }
  140. // We now create a URI for the request
  141. String url = “/api/insertData?device_id=” + String(65)+”&code=”+String(code)+”&data1=” +String(data1) +”&data2=”
  142. + String(data2)+”&data3=” +String(data3)+”&data4=” +String(data4)+”&data5=” +String(data5)
  143. +”&data6=” +String(data6)+”&data7=” +String(data7)+”&data8=” +String(data8)+”&data9=” +String(data9)
  144. +”&data10=” +String(data10)+”&data11=” +String(data11)+”&data12=” +String(data12)+”&data13=” +String(data13)
  145. +”&data14=” +String(data14)+”&data15=” +String(data15)+”&data16=” +String(data16)+”&data17=” +String(data17)
  146. +”&data18=” +String(data18)+”&data19=” +String(data19)+”&data20=” +String(data20);
  147. Serial.print(“Requesting URL: “);
  148. Serial.println(url);
  149. // This will send the request to the server
  150. client.print(String(“GET “) + url + ” HTTP/1.1\r\n” +
  151. “Host: ” + host + “\r\n” +
  152. “Connection: close\r\n\r\n”);
  153. unsigned long timeout = millis();
  154. while (client.available() == 0) {
  155. if (millis() – timeout > 5000) {
  156. Serial.println(“>>> Client Timeout !”);
  157. client.stop();
  158. return;
  159. }
  160. }
  161. // Read all the lines of the reply from server and print them to Serial
  162. while(client.available()) {
  163. String line = client.readStringUntil(‘\r’);
  164. //Serial.print(line);
  165. }
  166. Serial.println();
  167. Serial.println(“closing connection”);
  168. delay(50000);
  169. }
  170. //====
  171. void AM2301_loop() {
  172. // Delay between measurements.
  173. delay(delayMS);
  174. // Get temperature event and print its value.
  175. sensors_event_t event;
  176. dht.temperature().getEvent(&event);
  177. if (isnan(event.temperature)) {
  178. Serial.println(“Error reading temperature!”);
  179. }
  180. else {
  181. Serial.print(“Temperature: “);
  182. Serial.print(event.temperature);
  183. Serial.println(” *C”);
  184. temp_0 = event.temperature;
  185. }
  186. // Get humidity event and print its value.
  187. dht.humidity().getEvent(&event);
  188. if (isnan(event.relative_humidity)) {
  189. Serial.println(“Error reading humidity!”);
  190. }
  191. else {
  192. Serial.print(“Humidity: “);
  193. Serial.print(event.relative_humidity);
  194. Serial.println(“%”);
  195. humid_0 = event.relative_humidity;
  196. }
  197. }
  198. //====

ว่าแล้วก็ติดตั้งชุดอุปกรณ์ไว้กับบ้านในห้องครัวละกัน  และเชื่อมต่อไปยัง  IOT Platform (ตามเคย)

วันนี้แดดไม่ร้อยมากนัก  32.7’c  ความชื้นสัมพัทธ์   64 %