环境监测系统项目
项目概述
本项目是一个基于RT-Thread Studio的环境监测系统,使用STM32L4IOT开发板作为核心控制器,集成温度、湿度和光照强度监测功能。
硬件准备
- STM32L4IOT开发板
- AHT10温湿度传感器
- AP3216C光照强度传感器
- LCD显示屏
- LED灯和蜂鸣器
- 按键矩阵
- ST-Link烧录器
- 电源适配器
软件环境搭建
安装RT-Thread Studio:
配置开发环境:
- 打开RT-Thread Studio,创建新工程。
- 选择STM32L4IOT开发板,按照向导完成工程创建。
代码获取与编译
获取项目代码:
- 从GitHub仓库克隆项目代码:
1
| git clone https://github.com/yourusername/environment-monitoring-system.git
|
- 将克隆的代码导入RT-Thread Studio。
代码编译:
- 在RT-Thread Studio中加载项目工程文件。
- 点击编译按钮,生成固件文件(通常为
.bin
或.hex
格式)。
硬件连接与固件烧录
硬件连接:
- 按照设计报告中的硬件连接图,将AHT10传感器、AP3216C传感器、LCD显示屏、LED灯、蜂鸣器和按键矩阵连接到STM32L4IOT开发板。
固件烧录:
- 使用ST-Link或其他烧录工具,将编译生成的固件烧录到STM32L4IOT开发板。
系统运行与测试
系统启动:
功能测试:
- 温度监测:按下按键0,切换至温度监测模块,观察LCD显示的温度值及报警指示。
- 湿度监测:按下按键1,切换至湿度监测模块,观察LCD显示的湿度值及报警指示。
- 光照强度监测:按下按键2,切换至光照强度监测模块,观察LCD显示的光照强度值及报警指示。
代码关键部分说明
温度监测线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| void temperature_monitor_thread_entry(void *parameter) { float temperature; aht10_device_t dev; const char *i2c_bus_name = "i2c4"; int blink_interval;
rt_thread_mdelay(2000); dev = aht10_init(i2c_bus_name); if (dev == RT_NULL) { rt_kprintf("The sensor initializes failure"); return; }
rt_pin_mode(PIN_LED_R, PIN_MODE_OUTPUT); rt_pin_mode(PIN_LED_G, PIN_MODE_OUTPUT); rt_pin_mode(PIN_LED_B, PIN_MODE_OUTPUT); rt_pin_mode(BUZZER_PIN, PIN_MODE_OUTPUT); lcd_clear(WHITE);
while (1) { temperature = aht10_read_temperature(dev); rt_kprintf("temperature: %d.%d", (int)temperature, (int)(temperature * 10) % 10); char temperature_str[20]; rt_snprintf(temperature_str, sizeof(temperature_str), "Temp: %d.%d C", (int)temperature, (int)(temperature * 10) % 10); lcd_show_string(10, 75, 24, temperature_str);
if (temperature <= TEMP_LOW_THRESHOLD || temperature > TEMP_HIGH_THRESHOLD) { lcd_set_color(WHITE, RED); lcd_show_string(10, 130, 24, "The temperature is abnormal!"); rt_pin_write(BUZZER_PIN, PIN_HIGH); } else { lcd_set_color(WHITE, BLACK); lcd_show_string(10, 130, 24, " "); rt_pin_write(BUZZER_PIN, PIN_LOW); }
int group_current = (temperature > TEMP_LOW_THRESHOLD && temperature <= TEMP_HIGH_THRESHOLD) ? 0 : 1; blink_interval = (group_current == 0) ? GREEN_BLINK_INTERVAL : RED_BLINK_INTERVAL;
rt_pin_write(PIN_LED_R, _blink_tab[group_current][0]); rt_pin_write(PIN_LED_G, _blink_tab[group_current][1]); rt_pin_write(PIN_LED_B, _blink_tab[group_current][2]);
rt_thread_mdelay(blink_interval); rt_pin_write(PIN_LED_R, LED_OFF); rt_pin_write(PIN_LED_G, LED_OFF); rt_pin_write(PIN_LED_B, LED_OFF); rt_thread_mdelay(blink_interval); } }
|
湿度监测线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| void humidity_thread_entry(void *parameter) { float humidity; aht10_device_t dev; const char *i2c_bus_name = "i2c4"; int blink_interval;
rt_thread_mdelay(2000); dev = aht10_init(i2c_bus_name); if (dev == RT_NULL) { rt_kprintf("The sensor initializes failure"); return; }
rt_pin_mode(PIN_LED_R, PIN_MODE_OUTPUT); rt_pin_mode(PIN_LED_G, PIN_MODE_OUTPUT); rt_pin_mode(PIN_LED_B, PIN_MODE_OUTPUT); rt_pin_mode(BUZZER_PIN, PIN_MODE_OUTPUT); lcd_clear(WHITE);
while (1) { humidity = aht10_read_humidity(dev); rt_kprintf("humidity: %d.%d %%", (int)humidity, (int)(humidity * 10) % 10); char humidity_str[20]; rt_snprintf(humidity_str, sizeof(humidity_str), "Humidity: %d.%d %%", (int)humidity, (int)(humidity * 10) % 10); lcd_show_string(10, 75, 24, humidity_str);
if (humidity < HUMIDITY_LOW_THRESHOLD || humidity > HUMIDITY_HIGH_THRESHOLD) { lcd_set_color(WHITE, RED); lcd_show_string(10, 130, 24, "The humidity is abnormal!"); rt_pin_write(BUZZER_PIN, PIN_HIGH); } else { lcd_set_color(WHITE, BLACK); lcd_show_string(10, 130, 24, " "); rt_pin_write(BUZZER_PIN, PIN_LOW); }
int group_current = (humidity >= HUMIDITY_LOW_THRESHOLD && humidity <= HUMIDITY_HIGH_THRESHOLD) ? 0 : 1; blink_interval = (group_current == 0) ? GREEN_BLINK_INTERVAL : RED_BLINK_INTERVAL;
rt_pin_write(PIN_LED_R, _blink_tab[group_current][0]); rt_pin_write(PIN_LED_G, _blink_tab[group_current][1]); rt_pin_write(PIN_LED_B, _blink_tab[group_current][2]);
rt_thread_mdelay(blink_interval); rt_pin_write(PIN_LED_R, LED_OFF); rt_pin_write(PIN_LED_G, LED_OFF); rt_pin_write(PIN_LED_B, LED_OFF); rt_thread_mdelay(blink_interval); } }
|
光照强度监测线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| void light_intensity_thread_entry(void *parameter) { int brightness; ap3216c_device_t dev1; const char *i2c_bus_name = "i2c3";
rt_thread_mdelay(2000); dev1 = ap3216c_init(i2c_bus_name); if (dev1 == RT_NULL) { rt_kprintf("The sensor initializes failure"); }
rt_pin_mode(RED_LED_PIN, PIN_MODE_OUTPUT); rt_pin_mode(GREEN_LED_PIN, PIN_MODE_OUTPUT); rt_pin_mode(BUZZER_PIN, PIN_MODE_OUTPUT); lcd_clear(WHITE);
while (1) { brightness = ap3216c_read_ambient_light(dev1); rt_kprintf("Brightness: %d\n", brightness); lcd_clear(WHITE); char brightness_str[20]; rt_snprintf(brightness_str, sizeof(brightness_str), "Brightness: %d", brightness); lcd_show_string(10, 75, 24, brightness_str);
if (brightness < LIGHT_MIN || brightness > LIGHT_MAX) { lcd_set_color(WHITE, RED); lcd_show_string(10, 130, 24, "Light is abnormal!"); rt_pin_write(RED_LED_PIN, LED_ON); rt_pin_write(GREEN_LED_PIN, LED_OFF); rt_pin_write(BUZZER_PIN, PIN_HIGH); } else { lcd_set_color(WHITE, BLACK); lcd_show_string(10, 130, 24, " "); rt_pin_write(RED_LED_PIN, LED_OFF); rt_pin_write(GREEN_LED_PIN, LED_ON); rt_pin_write(BUZZER_PIN, PIN_LOW); }
rt_thread_mdelay(1000); } }
|
主函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| int main(void) { unsigned int count = 1; rt_thread_t tid = RT_NULL;
rt_pin_mode(PIN_KEY0, PIN_MODE_INPUT); rt_pin_mode(PIN_KEY1, PIN_MODE_INPUT); rt_pin_mode(PIN_KEY2, PIN_MODE_INPUT); rt_pin_mode(PIN_WK_UP, PIN_MODE_INPUT); lcd_show_string(5, 75, 16, "Press Key0 for Temp"); lcd_show_string(5, 105, 16, "Press Key1 for Humidity"); lcd_show_string(5, 135, 16, "Press Key2 for Light");
while (count > 0) { if (rt_pin_read(PIN_KEY0) == PIN_LOW) { rt_thread_mdelay(50); if (rt_pin_read(PIN_KEY0) == PIN_LOW) { rt_kprintf("KEY0 pressed!\n"); if (tid != RT_NULL) { rt_thread_delete(tid); tid = RT_NULL; } tid = rt_thread_create("temp_mon", temperature_monitor_thread_entry, RT_NULL, 1024, 10, 10); if (tid != RT_NULL) { rt_thread_startup(tid); } else { rt_kprintf("Failed to create temperature monitor thread!\n"); } } }
if (rt_pin_read(PIN_KEY1) == PIN_LOW) { rt_thread_mdelay(50); if (rt_pin_read(PIN_KEY1) == PIN_LOW) { rt_kprintf("KEY1 pressed!\n"); if (tid != RT_NULL) { rt_thread_delete(tid); tid = RT_NULL; } tid = rt_thread_create("humidity", humidity_thread_entry, RT_NULL, 1024, 25, 10); if (tid != RT_NULL) { rt_thread_startup(tid); } else { rt_kprintf("Failed to create humidity thread!\n"); } } }
if (rt_pin_read(PIN_KEY2) == PIN_LOW) { rt_thread_mdelay(50); if (rt_pin_read(PIN_KEY2) == PIN_LOW) { rt_kprintf("KEY2 pressed!\n"); if (tid != RT_NULL) { rt_thread_delete(tid); tid = RT_NULL; } tid = rt_thread_create("light_int", light_intensity_thread_entry, RT_NULL, 1024, 20, 10); if (tid != RT_NULL) { rt_thread_startup(tid); } else { rt_kprintf("Failed to create light intensity thread!\n"); } } }
rt_thread_mdelay(10); count++; }
return 0; }
|
注意事项
- 确保所有硬件连接正确无误后再上电。
- 在修改阈值或系统配置时,需重新编译并烧录固件。
- 本系统默认工作在RT-Thread Studio开发环境下,如需在其他环境下运行,可能需要进行相应的配置调整。
联系方式
希望以上内容能够帮助你完整地复现环境监测系统项目。如果有任何问题或需要进一步的帮助,请随时联系。