ESP32程式碼
#include "rgb_lcd.h" #include <DHT11.h> DHT11 dht11(4); rgb_lcd lcd; void setup() { Serial.begin(9600); lcd.begin(16, 2); lcd.print("Temp: "); lcd.setCursor(0, 1); // move cursor to start of 2nd row lcd.print("Humidity: "); } void loop() { char _buffer[10]; // Extended buffer to fit Chinese characters int temperature = dht11.readTemperature(); int humidity = dht11.readHumidity(); if (temperature != DHT11::ERROR_CHECKSUM && temperature != DHT11::ERROR_TIMEOUT && humidity != DHT11::ERROR_CHECKSUM && humidity != DHT11::ERROR_TIMEOUT) { Serial.print("Temperature: "); Serial.print(temperature); Serial.println("C"); Serial.print("Humidity: "); Serial.print(humidity); Serial.println("%"); } else { if (temperature == DHT11::ERROR_TIMEOUT || temperature == DHT11::ERROR_CHECKSUM) { Serial.print("Temperature Reading Error: "); Serial.println(DHT11::getErrorString(temperature)); } if (humidity == DHT11::ERROR_TIMEOUT || humidity == DHT11::ERROR_CHECKSUM) { Serial.print("Humidity Reading Error: "); Serial.println(DHT11::getErrorString(humidity)); } } delay(1000); sprintf(_buffer, "%03d C", temperature); lcd.setCursor(10, 0); lcd.print(_buffer); Serial.println(_buffer); sprintf(_buffer, "%03d %%", humidity); // Changed "in" to "%" for humidity. lcd.setCursor(10, 1); lcd.print(_buffer); Serial.println(_buffer); }
上視圖
程式碼ino檔
github下載
實作影片