#include <LiquidCrystal_I2C.h> //lcd库文件
#include "DHTesp.h" //dht22传感器库
#include "RTClib.h" //时钟模块库
#define I2C_ADDR 0x27 //lcd 的iic地址
#define LCD_COLUMNS 20 //列宽20个字符
#define LCD_LINES 4 //行宽 4个字符
const int DHT_PIN = 15; //温湿度传感器的连接引脚
RTC_DS1307 rtc; //对象
DHTesp dhtSensor; //对象
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES); //对象
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //串口初始化 波特率115200
dhtSensor.setup(DHT_PIN, DHTesp::DHT22); //温湿度传感器初始化
if (! rtc.begin()) { //时钟模块初始化
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
lcd.init(); //lcd2004初始化
lcd.backlight(); //亮屏
// Print something
lcd.setCursor(0, 0); //光标位置
lcd.print("Smart greenhouse");
lcd.setCursor(0, 1);
lcd.print(" IoT");
delay(1000);
lcd.clear(); //清屏
}
void loop() {
DateTime now = rtc.now(); //获取最新时间
TempAndHumidity data = dhtSensor.getTempAndHumidity(); //获取温湿度
String shijian = String(now.year())+"/"+String(now.month())+"/"+String(now.day())+" "+String(now.hour())+":"+String(now.minute())+":"+String(now.second())+ " ";
int lx = map(analogRead(27), 4096, 0, 0, 10000); //映射数值
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(data.temperature, 2) + " C ");
lcd.setCursor(0, 1);
lcd.print("Humidity: " + String(data.humidity, 1) + "% ");
lcd.setCursor(0, 2);
lcd.print("illumin: " + String(lx) + " Lux ");
lcd.setCursor(0, 3);
lcd.print(String(shijian));
delay(10); // this speeds up the simulation
}