2019年11月19日 星期二

用SensorBoard 玩 Arduino (11) 土壤溼度感測器與智能澆水系統

一、教學目標:學習類比輸入,將土壤的溼度作立即的測量,並顯示於顯示器

二、接線圖
感測器腳位 與sensor Boradi 接法
  • VCC→3.3V或5V(電源腳位)
  • GND→GND(接地腳位)
  • AO→A3(類比腳位)本範例是接在此腳位A3


1.先利用R11轉4 PIN 杜邦端子, 與感測器相接


2再將lcd 液晶顯示器一樣用RJ11線接在右孔



程式碼:
在LCD第一行顯示計數器,第二行顯示溼度值(0-1023),在空氣中顯示為1023,放在水了會變小。




整合應用,結合繼電器與抽水馬達,並以可變電阻可自設定限值,成為智能澆水系統。
(一)接線圖



(二)程式碼
limit:極限值
sensor:土壤溼度值




用SensorBoard 玩 Arduino (10) 繼電器的使用與控制抽水馬達

教學目標:學習繼電器的操作與控制抽水馬達

繼電器是一種電子控制器件,是用小的電流去控制較大電流的一種「自動開關」。因為抽水馬達所需的電流比較大,故無法直接利用Arduino的電源去推動。故我們以外加的電源模組進行供電。

一、繼電器規格:
模組工作電壓:+5VDC
RELAY 規格:10A 250VAC, 10A 125VAC, 10A 30VDC
DC+接正極
DC-接負極
IN 接訊號


com:共用端
NO:常開端
NC:常閉端 

(1)繼電器沒有電壓時,是公共端常閉端 接通。
(2)繼電器 有 電壓時,是公共端常開端 接通。

二、接線圖
DC+接
DC-接
IN 接訊號->D3


三、程式碼

每1秒開關繼電器一次



抽水馬達規格(需注意正負極)
電流:接12V時300 ma  功率:3.6W(4.5~12V)
流量:12V時 2L/分鐘
進水口:內徑7.5mm  外徑10mm
出水口:內徑5.5mm 外徑8mm




接線圖

程式碼

將以可變電阻(A0),作為是否啟動馬達的依據。當A0的值大於500,將啟動繼電器,讓馬達運轉。





2019年11月1日 星期五

108a-卓越組報告時程表

一、教材網址:
http://2013cloud.blogspot.com/search/label/0-SensorBoard

二、時程表

11/23(六)確認每組的報告題目內容

12/7(六)成果發表

(1)說明影片(5秒~)
(2)作品說明文稿(作品名稱、作品照片、主要功能與特色、參考資料....)

三、分組名單




四、google classroom 的網址

作答方式:請使用「剪取工具」將程式碼貼上,上傳至google classroom 中(點選圖片,可以下載檔案)



五、參考資料


2.一個簡單的專題-會報時的溫度計




2019年7月10日 星期三

用wemos di dimi 玩物聯網(10)實作智能雲端澆灌系統

一、教學目標:整合土壤的溼度感測器、繼電器、抽水馬達、液晶顯示器等,建置一雲端澆灌控制系統,並將土壤溼度值記錄雲端

二、接線圖


(一)土壤溼度感測器
感測器腳位 D1 mini 接法 
VCC→5V(電源腳位)
GND→GND(接地腳位)
 AO→A0

 (二)2x16LCD顯示器
 D1 Mini LCD1602
GND GND
3v3 VCC
D2 SDA
D1 SCL
(三)繼電器
感測器腳位 d1 mini 接法 

1. DC+→3.3V或5V(電源腳位)
2. DC-→GND(接地腳位)     
3. IN→D6(數位腳位)

com:接馬達負極
NO:接電源負極


三、程式碼

1.整合成智能澆灌系統
  1. #include <Wire.h>
  2. #include <esp_LiquidCrystal_I2C.h>
  3. esp_LiquidCrystal_I2C mylcd(0x27, 16, 2);
  4. const int AnalogIn = A0; //設定類比腳位
  5. int readingIn = 0; //測量值
  6. int no = 0;//計數器
  7. int relayPin = 12; //繼電器引腳
  8. int limitValue = 700; //限值(需要調整),
  9. //在空氣中的溼度感測器時是1024 或是 0
  10. void setup() {
  11. Serial.begin(9600);
  12. pinMode(relayPin , OUTPUT);
  13. mylcd.init();
  14. mylcd.backlight();
  15. mylcd.setCursor(0, 0);
  16. mylcd.print("Welcome to");
  17. mylcd.setCursor(0, 1);
  18. mylcd.print("My sensor");
  19. delay(1000);
  20. }
  21. void loop() {
  22. no++;
  23. readingIn = analogRead(AnalogIn); //讀取數值
  24. Serial.println(readingIn);
  25. mylcd.clear();
  26. mylcd.setCursor(0, 0);//移到第一行第一個位置
  27. mylcd.print("NO:");
  28. mylcd.print(no);
  29. mylcd.setCursor(0, 1);//移到第二行第一個位置
  30. mylcd.print(readingIn);
  31. if (readingIn > limitValue) {
  32. digitalWrite(relayPin , HIGH);
  33. mylcd.setCursor(7, 1);//移到第二行第8個位置
  34. mylcd.print("Pump ON");
  35. delay(1000);
  36. } else {
  37. digitalWrite(relayPin , LOW);
  38. mylcd.setCursor(7, 1);//移到第二行第8個位置
  39. mylcd.print("Pump Off");
  40. delay(1000);
  41. }
  42. delay(1000);//每秒測量一次
  43. }

2.將資料送至雲端記錄
  1. #include <Wire.h>
  2. #include <esp_LiquidCrystal_I2C.h>
  3. #include <ESP8266WiFi.h>
  4. const char* ssid = "mywifi"; //wifi 帳號
  5. const char* password = "aaaa";// wifi 密碼
  6. const char* safekey = "1234";//檢查碼
  7. const uint16_t port = 80; //上傳的主機port
  8. const char* host = "192.168.0.100"; //上傳的主機ip
  9. const char* php_url = "/myfarm/t2.php";//上傳在主機的程式
  10. byte sensor_id = 1; //偵測器ID
  11. esp_LiquidCrystal_I2C mylcd(0x27, 16, 2);
  12. const int AnalogIn = A0; //設定類比腳位
  13. int readingIn = 0; //測量值
  14. int no = 0;//計數器
  15. int relayPin = 12; //繼電器引腳
  16. int limitValue = 800; //限值(需要調整)0
  17. WiFiClient client;
  18. void setup()
  19. {
  20. Serial.begin(9600);
  21. pinMode(relayPin , OUTPUT);
  22. mylcd.init();
  23. mylcd.backlight();
  24. mylcd.clear();
  25. mylcd.setCursor(0, 0);
  26. mylcd.print("System start!");
  27. WiFi.begin(ssid, password);
  28. Serial.println("");
  29. int t = 0;
  30. mylcd.setCursor(0, 1);
  31. while (WiFi.status() != WL_CONNECTED) {
  32. t++;
  33. if (t > 15) break;
  34. Serial.print(".");
  35. mylcd.print(".");
  36. delay(500);
  37. }
  38. Serial.println("");
  39. Serial.print("Connected to ");
  40. Serial.println(ssid);
  41. Serial.print("IP address: ");
  42. Serial.println(WiFi.localIP());
  43. mylcd.setCursor(0, 1);
  44. mylcd.print(WiFi.localIP());
  45. delay(2000);
  46. mylcd.clear();
  47. }
  48. void loop()
  49. {
  50. no++;
  51. readingIn = analogRead(AnalogIn); //讀取數值
  52. Serial.println(readingIn);
  53. if (readingIn > 0) {
  54. no++;
  55. String things_request = "GET " + String(php_url);
  56. things_request += "?safekey=" + String(safekey);
  57. things_request += "&sensor_id=" + String(sensor_id);
  58. things_request += "&hh=" + String(readingIn) + "\r\n\r\n";
  59. Serial.println(things_request);
  60. if (client.connect(host, port)) {
  61. client.print(things_request);
  62. Serial.println("Request: [ok]");
  63. client.stop();
  64. } else {
  65. Serial.println("Request: [error]");
  66. }
  67. mylcd.clear();
  68. mylcd.setCursor(0, 0);
  69. mylcd.print(no);
  70. mylcd.setCursor(7, 0);
  71. mylcd.print(String("H:") + String(readingIn));
  72. if (readingIn > limitValue) {
  73. digitalWrite(relayPin , HIGH);
  74. mylcd.setCursor(0, 1);//移到第二行第8個位置
  75. mylcd.print("Pump ON");
  76. delay(1000);
  77. } else {
  78. digitalWrite(relayPin , LOW);
  79. mylcd.setCursor(0, 1);//移到第二行第8個位置
  80. mylcd.print("Pump Off");
  81. delay(1000);
  82. }
  83. delay(5000);//間隔5秒送一次資料,建議修改
  84. }
  85. }
四、執行結果










用wemos di dimi 玩物聯網(9)繼電器的使用

一、教學目標:學習繼電器的操作與控制抽水馬達

繼電器是一種電子控制器件,是用小的電流去控制較大電流的一種「自動開關」。因為抽水馬達所需的電流比較大,故無法直接利用di mini 的電源去推動。故我們增加了一個麵包板電源模組進行供電。並將其接地線(負極與 d1 mini 上的G 腳位相接)


1.繼電器規格:
模組工作電壓:+5VDC
RELAY 規格:10A 250VAC, 10A 125VAC, 10A 30VDC



com:共用端
NO:常開端
NC:常閉端 

(1)繼電器沒有電壓時,是公共端常閉端 接通。
(2)繼電器 有 電壓時,是公共端常開端 接通。

2.麵包板電源模組規格
輸入電壓:6.5-12V﹝直流﹞或USB供電
輸出電壓:3.3V、5V可切換
最大輸出電流:<700ma
上下兩路獨立控制,可切換為0V、3.3V、5V
板載兩組3.3V、5V直流輸出插針,方便引用,並有電源指示led燈


3.抽水馬達規格
電流:接12V時300 ma  功率:3.6W(4.5~12V)
流量:12V時 2L/分鐘
進水口:內徑7.5mm  外徑10mm
出水口:內徑5.5mm 外徑8mm



二、接線圖




感測器腳位 d1 mini 接法 

1. DC+→3.3V或5V(電源腳位)
2. DC-→GND(接地腳位)     
3. IN→D6(數位腳位)



com:接馬達負極

NO:接電源負極


三、程式碼
1.每3秒鐘,讓繼電器起動一次,並讓抽水馬達

  1. // 繼電器控制練習
  2. //1、DC+:接電源正極 5v
  3. //2、DC-:接電源負極
  4. //3、IN:D6 (gpio 12)
  5. int relayPin = 12; //繼電器引腳
  6. void setup() {
  7. // initialize digital pin LED_BUILTIN as an output.
  8. pinMode(relayPin , OUTPUT);
  9. }
  10.  
  11. void loop() {
  12. digitalWrite(relayPin , HIGH);
  13. delay(3000);
  14. digitalWrite(relayPin , LOW);
  15. delay(3000);
  16. }
  17.  

四、執行結果


五、參考資料


https://tutorials.webduino.io/zh-tw/docs/basic/component/relay.html

https://www.icshop.com.tw/product_info.php/products_id/26022/Twesid/5857e09e18c2ab428f8b7fb52cc92b5c

用wemos di dimi 玩物聯網(8)土壤溼度感測器的使用

一、教學目標:學習類比輸入,將土壤的溼度作立即的測量,並顯示於顯示器

二、接線圖
感測器腳位 d1 mini 接法
1. VCC→3.3V或5V(電源腳位)
2. GND
→GND(接地腳位)
3. DO→D1~D9(數位腳位) 可以設定邊界值(藍色的可變電阻是用於土壤濕度的閥值調節)
4. AO→A0(類比腳位), 本範例是接在此腳位A0




三、程式碼
1.利用serial monitor(序串埠監視窗)顯示結果

  1. const int AnalogIn = A0; //設定類比腳位
  2. int readingIn = 0; //測量值
  3.  
  4. void setup() {
  5. Serial.begin(9600);
  6. }
  7.  
  8. void loop() {
  9. readingIn = analogRead(AnalogIn); //讀取數值
  10. Serial.println(readingIn);
  11. delay(500);
  12. }
  13.  

2.將測量結果顯示在2x16LCD上
  1. #include <Wire.h>
  2. #include <esp_LiquidCrystal_I2C.h>
  3. esp_LiquidCrystal_I2C mylcd(0x27, 16, 2);
  4.  
  5. const int AnalogIn = A0; //設定類比腳位
  6. int readingIn = 0; //測量值
  7. int no = 0;//計數器
  8.  
  9. void setup() {
  10. Serial.begin(9600);
  11. mylcd.init();
  12. mylcd.backlight();
  13. mylcd.setCursor(0, 0);
  14. mylcd.print("Welcome to");
  15. mylcd.setCursor(0, 1);
  16. mylcd.print("my sensor");
  17. delay(2000);
  18. }
  19. void loop() {
  20. no++;
  21. readingIn = analogRead(AnalogIn); //讀取數值
  22. Serial.println(readingIn);
  23. mylcd.clear();
  24. mylcd.setCursor(0, 0);//移到第一行第一個位置
  25. mylcd.print("NO:");
  26. mylcd.print(no);
  27. mylcd.setCursor(0, 1);//移到第二行第一個位置
  28. mylcd.print(readingIn);
  29. delay(1000);//每秒測量一次
  30. }
四、執行結果
五、參考資料
http://lihan.shinyo.bixone.com/Arduino/arduino-2015.10.13-5.html

2019年6月18日 星期二

用wemos d1 mini 玩物聯網(7) PMS5003T 實作PM2.5 感測器

一、感測器介紹

PMS5003T是一款可以同時監測空氣中顆粒物濃度及溫濕度的二合一感測器。其中顆粒物濃度的監測基於鐳射散射原理,可連續採集並計算單位體積內空氣中不同粒徑的懸浮顆粒物個數,即顆粒物濃度分佈,進而換算成為質量濃度。


二、接線圖
VCC-> 5V
GND->GND
RXD->D6
TXD->D5 

三、程式碼


  1. #include <Wire.h>
  2. #include <esp_LiquidCrystal_I2C.h>
  3. #include <ESP8266WiFi.h>
  4.  
  5. #include <SoftwareSerial.h>
  6.  
  7. const char* ssid = "HJN"; //wifi 帳號
  8. const char* password = "11111111";// wifi 密碼
  9. const char* safekey = "fhjh";//檢查碼
  10. const uint16_t port = 80; //上傳的主機port
  11. const char* host = "192.168.137.1"; //上傳的主機ip
  12. const char* php_url = "/airbox/t2.php";//上傳在主機的程式
  13. byte sensor_id = 1; //偵測器ID
  14.  
  15. long lPmcf10;
  16. long lPmcf25;
  17. long lPmcf100;
  18. long lTemperature;
  19. long lHumidity;
  20. esp_LiquidCrystal_I2C mylcd(0x27, 16, 2);
  21. SoftwareSerial pms5003t_Serial(D5, D6);
  22.  
  23. #define PMS5003T_PM10 1
  24. #define PMS5003T_PM25 2
  25. #define PMS5003T_PM100 3
  26. #define PMS5003T_TEMPER 4
  27. #define PMS5003T_HUMI 5
  28. long pms_pmcf10 = 0;
  29. long pms_pmcf25 = 0;
  30. long pms_pmcf100 = 0;
  31. long pms_pmat10 = 0;
  32. long pms_pmat25 = 0;
  33. long pms_pmat100 = 0;
  34. long pms_Temperature = 0;
  35. long pms_Humidity = 0;
  36. int no = 0;
  37.  
  38.  
  39. WiFiClient client;
  40.  
  41. void setup()
  42. {
  43. Serial.begin(9600);
  44. mylcd.init();
  45. mylcd.backlight();
  46.  
  47. pms5003t_Serial.begin(9600);
  48.  
  49. lPmcf10 = 0;
  50. lPmcf25 = 0;
  51. lPmcf100 = 0;
  52. lTemperature = 0;
  53. lHumidity = 0;
  54. mylcd.clear();
  55. mylcd.setCursor(0, 0);
  56. mylcd.print("Yo Airbox");
  57. WiFi.begin(ssid, password);
  58. Serial.println("");
  59.  
  60. int t = 0;
  61. mylcd.setCursor(0, 1);
  62. while (WiFi.status() != WL_CONNECTED) {
  63. t++;
  64. if (t > 15) break;
  65.  
  66. Serial.print(".");
  67. mylcd.print(".");
  68. delay(500);
  69. }
  70. Serial.println("");
  71. Serial.print("Connected to ");
  72. Serial.println(ssid);
  73. Serial.print("IP address: ");
  74. Serial.println(WiFi.localIP());
  75. mylcd.setCursor(0, 1);
  76. mylcd.print(WiFi.localIP());
  77. delay(2000);
  78. mylcd.clear();
  79. }
  80.  
  81.  
  82. void loop()
  83. {
  84. lPmcf10 = 0;
  85. lPmcf10 = pms5003t_read_(PMS5003T_PM10);
  86. Serial.println((String("PM1.0: ") + String(lPmcf10)));
  87. delay(500);
  88. lPmcf25 = pms5003t_read_(PMS5003T_PM25);
  89. Serial.println((String("PM2.5: ") + String(lPmcf25)));
  90. delay(500);
  91. lPmcf100 = pms5003t_read_(PMS5003T_PM100);
  92. Serial.println((String("PM10: ") + String(lPmcf100)));
  93. delay(500);
  94. lTemperature = pms5003t_read_(PMS5003T_TEMPER);
  95. Serial.println((String("Temperature: ") + String(lTemperature)));
  96. delay(500);
  97. lHumidity = pms5003t_read_(PMS5003T_HUMI);
  98. Serial.println((String("Humidity: ") + String(lHumidity)));
  99. delay(500);
  100.  
  101. if (lPmcf10 > 0) {
  102. no++;
  103. String things_request = "GET " + String(php_url);
  104. things_request += "?safekey=" + String(safekey);
  105. things_request += "&sensor_id=" + String(sensor_id);
  106. things_request += "&pm10=" + String(lPmcf10);
  107. things_request += "&pm25=" + String(lPmcf25);
  108. things_request += "&pm100=" + String(lPmcf100);
  109. things_request += "&tt=" + String(lTemperature);
  110. things_request += "&hh=" + String(lHumidity) + "\r\n\r\n";
  111. Serial.println(things_request);
  112. if (client.connect(host, port)) {
  113. client.print(things_request);
  114. Serial.println("Request: [ok]");
  115. client.stop();
  116. } else {
  117. Serial.println("Request: [error]");
  118. }
  119.  
  120.  
  121.  
  122.  
  123. if (client.connect("api.thingspeak.com", 80)) {
  124. String things_request = "GET /update?api_key=Your_Write_API_Key&field1=" + String(lPmcf10) + "&field2=" + String(lPmcf25) + "&field3=" + String(lPmcf100) + "&field4=" + String(lTemperature) + "&field5=" + String(lHumidity) + "\r\n\r\n";
  125. // client.print(things_request);
  126. client.stop();
  127. }
  128. mylcd.clear();
  129. mylcd.setCursor(0, 0);
  130. mylcd.print(no);
  131. mylcd.setCursor(5, 0);
  132. mylcd.print(String(" PM2.5: ") + String(lPmcf25) );
  133. mylcd.setCursor(0, 1);
  134. mylcd.print(String("T:") + String(lTemperature) + String("C"));
  135. mylcd.setCursor(9, 1);
  136. mylcd.print(String("H:") + String(lHumidity) + String("%"));
  137. delay(5000);
  138. }
  139.  
  140. }
  141.  
  142. //G5t check
  143. long pms5003t_read_(int datatype) {
  144. int count = 0;
  145. unsigned char c;
  146. unsigned char high;
  147.  
  148. pms5003t_Serial.listen();
  149. while (pms5003t_Serial.available()) {
  150. c = pms5003t_Serial.read();
  151. if ((count == 0 && c != 0x42) || (count == 1 && c != 0x4d)) {
  152. Serial.println("pms5003t check failed");
  153. break;
  154. }
  155. if (count > 27) {
  156. Serial.println("pms5003t completed");
  157. break;
  158. }
  159. else if (count == 4 || count == 6 || count == 8 || count == 10 || count == 12 || count == 14 || count == 24 || count == 26) {
  160. high = c;
  161. }
  162. else if (count == 5) {
  163. pms_pmcf10 = 256 * high + c;
  164. }
  165. else if (count == 7) {
  166. pms_pmcf25 = 256 * high + c;
  167. }
  168. else if (count == 9) {
  169. pms_pmcf100 = 256 * high + c;
  170. }
  171. else if (count == 11) {
  172. pms_pmat10 = 256 * high + c;
  173. }
  174. else if (count == 13) {
  175. pms_pmat25 = 256 * high + c;
  176. }
  177. else if (count == 15) {
  178. pms_pmat100 = 256 * high + c;
  179. }
  180. else if (count == 25) {
  181. pms_Temperature = (256 * high + c) / 10;
  182. }
  183. else if (count == 27) {
  184. pms_Humidity = (256 * high + c) / 10;
  185. }
  186. count++;
  187. }
  188. while (pms5003t_Serial.available()) pms5003t_Serial.read();
  189.  
  190. if (datatype == PMS5003T_PM10)
  191. return pms_pmcf10;
  192. else if (datatype == PMS5003T_PM25)
  193. return pms_pmcf25;
  194. else if (datatype == PMS5003T_PM100)
  195. return pms_pmcf100;
  196. else if (datatype == PMS5003T_TEMPER)
  197. return pms_Temperature;
  198. else if (datatype == PMS5003T_HUMI)
  199. return pms_Humidity;
  200. }
四、執行結果

五、參考資料

用wemos d1 mini 玩物聯網(6) IFTTT 的介紹與基本使用

一、IFTTT簡介

FTTT 簡單來說就是 當 什麼 發生時 就做 什麼 事, 這 2 個「什麼」就是「服務」, 讓不同的平台、服務可以整合在一起。

二、使用方法
(1)註冊 官網:https://ifttt.com/  ,請點選 Sign Up 註冊帳號,  或是直接按Sign In ,可以利用GMAIL帳號作同步認證作登入






(2)登入後,點選個人帳號,再點選New Applet,將會有一系列的步驟來協助建立。

再點選 「+this」



步驟1.Choose a service, 我們選擇 webhook進行篩選,再點選藍色方塊,進入下個步驟。

        所謂的webhook的利用傳送一段網址給ifttt, 來觸發程式。


步驟2.Choose trigger(選擇觸發器),將你的「事件」命名,我們 命名為tt123  ,是利用「收到網絡請求」的方式來進行。


步驟3. 接下來設定系統要做什麼事情,點選「+that」

    選擇一個行動的服務,我們選擇用「email」來通知該事件被觸發,也就是事件觸發後立即發信。內建信箱是登入的email, 當然也可以選擇line,

步驟4 .點選,send me a email 藍色方塊,進行設定




步驟5 建立通知email的內容格式設定,有支援中文。
(1) evenName:代表觸發器名稱,以本例為tt123

(2)OccurredAt 代表發生時間

(3)Value1 , Value2, Valu3代表要傳送的資料

用兩個大括號,前後夾,代表它是一個變數,不要去動到。






步驟6 完成後,點選下 Receive notifications when this Applet runs 即可完成啟動。


步驟 7
接下來進行 尋找觸發的網址格式,我們直接進到以下網址 
https://ifttt.com/maker_webhooks,選擇右上角的「Document」

觸發器名稱:tt123
your key=系統會給您一串文字

填入觸發器的名稱,按下「Test It!」即可進行測試。

也可以直接打入以下網址:
即可以試驗,其中value1=a代表會傳遞a
https://maker.ifttt.com/trigger/tt123/with/key/your key?valu1=a&value=b&value=c




t3.php

<?php
$url ="http://maker.ifttt.com/trigger/line_0606/with/key/bEcMfXQzyIpvECSsXvEJ2n?";
$url .="value1=a&value22=b&value3=c";

echo $response = file_get_contents($url);
?>


參考資料
  1. http://blog.ilc.edu.tw/blog/index.php?op=printView&articleId=675501&blogId=868
  2. http://hammer1007.blogspot.com/2018/07/esp82665-5ifttt-line.html







用wemos d1 mini 玩物聯網(5)-DHT11 溫溼度感測器

一、課程目標:

學習使用DHT11溫溼度感應器來測量溫度


二、匯入函式庫
Arduino IDE 整合環境
功能表點選 草稿碼 / 匯入程式庫 / 管理程式庫

我們選擇 SimpleDHT,再按安裝


安裝的程式庫位於「C:\使用者\電腦名稱\文件\Arduino\libraries」資料夾中。

三、接線圖
"+" vcc 極接5V,v
"-" Gnd 極接GND,
data pin接上要輸入的Pin腳=>接在13 (D7)
四、程式碼
點選「範例」->SimpleDHT->DHT11Default

  1. #include <SimpleDHT.h>
  2.  
  3. // for DHT11,
  4. // VCC: 5V or 3V
  5. // GND: GND
  6. // DATA: 2
  7. int pinDHT11 = 13;//data PIN 在D7=>也就是13
  8. SimpleDHT11 dht11(pinDHT11);
  9.  
  10. void setup() {
  11. Serial.begin(115200);
  12. }
  13.  
  14. void loop() {
  15. // start working...
  16. Serial.println("=================================");
  17. Serial.println("Sample DHT11...");
  18. // read without samples.
  19. byte temperature = 0;
  20. byte humidity = 0;
  21. int err = SimpleDHTErrSuccess;
  22. if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
  23. Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
  24. return;
  25. }
  26. Serial.print("Sample OK: ");
  27. Serial.print((int)temperature); Serial.print(" *C, ");
  28. Serial.print((int)humidity); Serial.println(" H");
  29. // DHT11 sampling rate is 1HZ.
  30. delay(1500);
  31. }

伍、執行結果



六、程式碼2
  1. #include <SimpleDHT.h>
  2. #include <Wire.h>
  3. #include <esp_LiquidCrystal_I2C.h>
  4.  
  5. esp_LiquidCrystal_I2C mylcd(0x27, 16, 2);
  6. // for DHT11,
  7. // VCC: 5V or 3V
  8. // GND: GND
  9. // DATA: 13
  10. int pinDHT11 = 13;//di mini D7 DHT11腳位在D7 , 也就是13
  11. SimpleDHT11 dht11(pinDHT11);
  12. int no = 0;//計數器
  13. void setup() {
  14. Serial.begin(115200);
  15. mylcd.init();
  16. mylcd.backlight();
  17. mylcd.setCursor(0, 0);
  18. mylcd.print("Welcome to");
  19. mylcd.setCursor(0, 1);
  20. mylcd.print("DHT 11");
  21. }
  22.  
  23. void loop() {
  24. // start working...
  25. Serial.println("=================================");
  26. Serial.println("Sample DHT11...");
  27. // read without samples.
  28. byte temperature = 0;
  29. byte humidity = 0;
  30. int err = SimpleDHTErrSuccess;
  31. if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
  32. Serial.print("Read DHT11 failed, err=");
  33. Serial.println(err);delay(1000);
  34. mylcd.setCursor(0, 0);
  35. mylcd.print("Read DHT11 failed");
  36. return;
  37. }
  38. Serial.print("Sample OK: ");
  39. Serial.print((int)temperature); Serial.print(" *C, ");
  40. Serial.print((int)humidity); Serial.println(" H");
  41.  
  42. mylcd.clear();
  43. mylcd.setCursor(0, 0);
  44. mylcd.print("NO:");
  45. mylcd.print(no);
  46. mylcd.setCursor(0, 1);
  47. mylcd.print(String("T:") + String(temperature) + String("C"));
  48. mylcd.setCursor(9, 1);
  49. mylcd.print(String("H:") + String(humidity) + String("%"));
  50. no++;
  51. delay(3000);//每三秒重新測量
  52. }