代码
- #include "DFRobot_LcdDisplay.h"
- #include <MsTimer2.h>
- DFRobot_Lcd_IIC lcd(&Wire, /*I2CAddr*/ 0x2c);
- //LTR390紫外线传感器
- #include "DFRobot_LTR390UV.h"
- #include <SoftwareSerial.h>
- #define UARTMODE //UART模式
- SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
- DFRobot_LTR390UV ltr390(/*addr =*/LTR390UV_DEVICE_ADDR, /*s =*/&mySerial);
- //BMP388气压传感器
- #include <DFRobot_BMP3XX.h>
- DFRobot_BMP388_I2C sensor(&Wire, sensor.eSDOVDD);
- #define CALIBRATE_ABSOLUTE_DIFFERENCE
- //DHT11温湿度传感器
- #include <dht11.h>
- dht11 DHT;
- #define DHT11_PIN 10
-
- uint8_t Tem, Hum, Pre, Ult, lcdTimeId,labelId1,labelId2,labelId3,labelId4;
- uint8_t hour = 9, Minute = 8, second = 56;
- uint8_t lastSecond = 0;
- void flash() //处理时间变化的中断处理程序
- {
- second++;
- if (second > 59) {
- second = 0;
- Minute++;
- }
- if (Minute > 59) {
- Minute = 0;
- hour++;
- }
- }
-
- void setup(void){
- lcd.begin();
- lcd.setBackgroundColor(WHITE);
- lcd.cleanScreen();
- delay(500);
- MsTimer2::set(1000, flash); // 中断设置功能,每1000ms进入一次中断
- MsTimer2::start(); // 开始计数
-
- lcdTimeId = lcd.drawLcdTime(10, 90, hour, Minute, second, 1, LIGHTGREY); //显示当前的时间
- lcd.drawLcdDate(10, 120, 4, 7, 7, 0, LIGHTGREY); //显示当前的日期
- lcd.drawGif(20, 5, "Cloudy.gif", 512); //创建图标
-
- //创建传感器对应图标
- lcd.drawIcon(124, -8, "/sensor icon/thermometer.png", 120);
- lcd.drawIcon(119, 35, "/sensor icon/raindrops.png", 120);
- lcd.drawIcon(110, 78, "/sensor icon/pressure.png", 120);
- lcd.drawIcon(109, 118, "/season icon/sunny.png", 120);
-
- //创建多个图标
- lcd.drawIcon(0, 176, "/botany icon/Potted plant flower.png", 256);
- lcd.drawIcon(53, 176, "/botany icon/cactus3.png", 256);
- lcd.drawIcon(106, 176, "/botany icon/grass.png", 256);
- lcd.drawIcon(159, 176, "/botany icon/grass1.png", 256);
- lcd.drawIcon(212, 176, "/botany icon/cactus1.png", 256);
- lcd.drawIcon(265, 176, "/botany icon/cactus2.png", 256);
-
- //创建进度条以显示温度
- Tem = lcd.creatBar(164, 22, 80, 15, ORANGE);
- labelId1 = lcd.drawString( 255, 22, "0lux", 0, ORANGE);
- //创建进度条以显示湿度
- Hum = lcd.creatBar(164, 62, 80, 15, RED);
- labelId2 = lcd.drawString( 255, 62, "0°C", 0, RED);
- //创建进度条以显示气压
- Pre = lcd.creatBar(164, 102, 80, 15, BLUE);
- labelId3 = lcd.drawString( 255, 102, "0%", 0, BLUE);
- //创建进度条以显示紫外线强度
- Ult = lcd.creatBar(164, 142, 80, 15, GREEN);
- labelId4 = lcd.drawString( 255, 142, "0m/s", 0, GREEN);
-
- //LTR390紫外线传感器初始化设置
- #define UARTMODE
- mySerial.begin(9600);
- Serial.begin(115200);
- while(ltr390.begin() != 0){
- Serial.println(" Sensor initialize failed!!");
- delay(1000);
- }
- Serial.println(" Sensor initialize success!!");
- ltr390.setALSOrUVSMeasRate(ltr390.e18bit,ltr390.e100ms);//18位数据,采样时间100ms
- ltr390.setALSOrUVSGain(ltr390.eGain3);//3倍增益
- ltr390.setMode(ltr390.eALSMode);//设置紫外线模式
-
- //BMP388气压传感器初始化设置
- int rslt;
- while( ERR_OK != (rslt = sensor.begin()) ){
- if(ERR_DATA_BUS == rslt){
- Serial.println("Data bus error!!!");
- }else if(ERR_IC_VERSION == rslt){
- Serial.println("Chip versions do not match!!!");
- }
- delay(3000);
- }
- Serial.println("Begin ok!");
- while( !sensor.setSamplingMode(sensor.eUltraPrecision) ){
- Serial.println("Set samping mode fail, retrying....");
- delay(3000);
- }
- delay(100);
- #ifdef CALIBRATE_ABSOLUTE_DIFFERENCE
- if( sensor.calibratedAbsoluteDifference(540.0) ){
- Serial.println("Absolute difference base value set successfully!");
- }
- #endif
- float sampingPeriodus = sensor.getSamplingPeriodUS();
- Serial.print("samping period : ");
- Serial.print(sampingPeriodus);
- Serial.println(" us");
-
- float sampingFrequencyHz = 1000000 / sampingPeriodus;
- Serial.print("samping frequency : ");
- Serial.print(sampingFrequencyHz);
- Serial.println(" Hz");
- delay(1000);
- }
-
- void loop(void){
- if(lastSecond != second){
- lastSecond = second;
- lcd.updateLcdTime(lcdTimeId, 10, 90, hour, Minute, lastSecond, 1, LIGHTGREY); //显示当前时间
- }
- delay(100);
- uint32_t date4 = 0;
- date4 = ltr390.readALSTransformData(); //获取环境光转换后数据
- float date3 = sensor.readPressPa(); //获取气压传感器的气压数据
- DHT.read(DHT11_PIN); //获取温湿度传感器的温度和湿度数据
- Serial.println("-------------");
- Serial.println(DHT.temperature);
- Serial.println(DHT.humidity);
- Serial.println(date3);
- Serial.println(date4);
-
- //设置进度条的值
- lcd.setBarValue(Tem, DHT.temperature);
- lcd.updateString(labelId1, 255, 22, String(DHT.temperature)+"°C", 0, ORANGE);
- delay(100);
- lcd.setBarValue(Hum, DHT.humidity);
- lcd.updateString(labelId2, 255, 62, String(DHT.humidity)+"%", 0, RED);
- delay(100);
- lcd.setBarValue(Pre, date3);
- lcd.updateString(labelId3, 255, 102, String(date3)+"Pa", 0, BLUE);
- delay(100);
- lcd.setBarValue(Ult, date4);
- lcd.updateString(labelId4, 255, 142, String(date4)+"lux", 0, GREEN);
- delay(100);
- }
复制代码
结语 在这个项目中,我们成功构建了一个功能齐全的气象站,它不仅可以显示各种天气数据,还可以提供美观且直接的界面。通过本项目,你学习了如何集成传感器和过程数据,以及如何使用 LVGL 库可视化数据和设计界面。
再展望一下,我们还可以通过添加更多类型的传感器或将天气数据上传到云端进行远程监控来进一步扩展和优化该项目。我们希望这个项目激发了你的创造力,激励你不断探索和创新,从而开发出更多有趣和实用的电子项目。