电子说
功能简介:
该项目是基于STM32F407-RT-SPARK开发板的智能花盆。通过温湿度传感器和光照传感器,它能够监测花盆土壤当前的温湿度和附近的光照强度,并将这三项数据实时显示在LCD屏上。它还能够监测花盆是否倾倒,监测到花盆倾倒后,蜂鸣器会发出警报,直到按下按键,警报停止。同时,它还配备了rw007模块,能将这些数据实时上传到onenet平台。
硬件框架:
使用到的模块包括:LCD屏、温湿度传感器、光照传感器、蜂鸣器、物理按键
图一 硬件实物图与使用到的模块
图二 硬件框架图
软件框架:
图三 软件框架图
软件模块说明及部分代码:
1.六轴传感器线程:
六轴传感器模块测量花盆的偏航角、横滚角和俯仰角,并将测量数据发送给CPU进行判断,若花盆倾倒,记录异常数据。
icm20608_device_t devvv = RT_NULL;
rt_err_t result;
devvv = icm20608_init(i2c_bus_namee);
if (devvv == RT_NULL)
{
LOG_E("The sensor initializes failure");
return 0;
}
else
{
LOG_D("The sensor initializes success");
}
/* 对 icm20608 进 行 零 值 校 准: 采 样 10 次, 求 取 平 均 值 作 为 零 值 */
result = icm20608_calib_level(devvv, 10);
if (result == RT_EOK)
{
LOG_D("The sensor calibrates success");
LOG_D("accel_offset: X%6d Y%6d Z%6d", devvv- >accel_offset.x, devvv- >accel_offset.y, devvv- >accel_offset.z);
}
else
{
LOG_E("The sensor calibrates failure");
icm20608_deinit(devvv);
return 0;
}
rt_int16_t accel_x, accel_y, accel_z;
/* 读 取 三 轴 加 速 度 */
result = icm20608_get_accel(devvv, &accel_x, &accel_y, &accel_z);
if (result == RT_EOK)
{
LOG_D("current accelerometer: accel_x%6d, accel_y%6d, accel_z%6d",
accel_x, accel_y, accel_z);
}
else
{
LOG_E("The sensor does not work");
break;
}
2.LCD显示线程:
该线程初始化后会持续读取温度、湿度和光照数据,并将其实时显示在LCD显示屏上。
lcd_clear(WHITE);
lcd_set_color(WHITE, RED);
lcd_show_string(10, 0, 16, "Hello!");
lcd_set_color(WHITE, BLACK);
lcd_show_string(10, 26, 32, "Temperature:");
lcd_show_string(10, 90, 32, "Humidity:");
lcd_show_string(10, 160, 32, "Light:");
lcd_set_color(WHITE, BLUE);
lcd_show_num(30, 26+32, temperature, 2, 32);
lcd_show_string(65,26+32,32,"C");
lcd_show_num(30, 90+32, humidity, 2, 32);
lcd_show_string(65, 90+32, 32, "%");
lcd_show_num(30, 160+32, brightness, 2, 32);
lcd_show_string(80, 160+32, 32, "lux");
3.光强传感器线程:
光强传感器模块将记录光强数据,用于LCD显示和云端显示。
brightness = ap3216c_read_ambient_light(devv);
LOG_D("current brightness: %d.%d(lux).", (int)brightness, ((int)(10 * brightness) % 10));
4.rw007线程(Onenet):
读取rw007模块传来的温湿度数据、光照数据并存储到云端以图表方式进行显示;若倾倒角度数据异常,会在云端进行报警。
int onenet_publish_digit(int argc, char **argv)
{
if (argc != 3)
{
LOG_E("onenet_publish [datastream_id] [value] - mqtt pulish digit data to OneNET.");
return -1;
}
if (onenet_mqtt_upload_digit(argv[1], atoi(argv[2])) < 0)
{
LOG_E("upload digit data has an error!n");
}
return 0;
}
int onenet_publish_string(int argc, char *argv)
{
if (argc != 3)
{
LOG_E("onenet_publish [datastream_id] [string] - mqtt pulish string data to OneNET.");
return -1;
}
if (onenet_mqtt_upload_string(argv[1], argv[2]) < 0)
{
LOG_E("upload string has an error!n");
}
return 0;
}
/ onenet mqtt command response callback function */
static void onenet_cmd_rsp_cb(uint8_t *recv_data, size_t recv_size, uint8_t **resp_data, size_t *resp_size)
{
char res_buf[] = { "cmd is received!n" };
LOG_D("recv data is %.sn", recv_size, recv_data);
/ user have to malloc memory for response data */
*resp_data = (uint8_t *) ONENET_MALLOC(strlen(res_buf));
strncpy((char *)*resp_data, res_buf, strlen(res_buf));
resp_size = strlen(res_buf);
}
/ set the onenet mqtt command response callback function */
int onenet_set_cmd_rsp(int argc, char *argv)
{ onenet_set_cmd_rsp_cb(onenet_cmd_rsp_cb);
return 0;
}
MSH_CMD_EXPORT(onenet_set_cmd_rsp, set cmd response function);
#endif / FINSH_USING_MSH */
5.温湿度线程:
温湿度传感器模块测量土壤的温湿度,并记录测量数据。
temperature=(int)temperature+(int)(temperature10)%10;
humidity=(int)humidity+(int)(humidity10)%10;
brightness=(int)brightness+(int)(brightness*10)%10;
rt_thread_mdelay(100);
onenet_mqtt_upload_digit("temperature", temperature);
rt_thread_mdelay(100);
onenet_mqtt_upload_digit("humidity", humidity);
rt_thread_mdelay(100);
onenet_mqtt_upload_digit("brightness", brightness);
6.蜂鸣器线程:
读取六轴传感器数据,并对花盆的倾倒状态进行判断。若花盆倾倒,蜂鸣器报警,可通过开发板上的按键关闭蜂鸣器。
/* 设置按键引脚为输入模式 */
rt_pin_mode(PIN_KEY1, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(PIN_WK_UP, PIN_MODE_INPUT_PULLUP);
/* 设置蜂鸣器引脚为输出模式 */
rt_pin_mode(PIN_BEEP, PIN_MODE_OUTPUT);
/* 设置按键中断模式与中断回调函数 */
rt_pin_attach_irq(PIN_KEY1, PIN_IRQ_MODE_FALLING, irq_callback, (void *) PIN_KEY1);
rt_pin_attach_irq(PIN_WK_UP, PIN_IRQ_MODE_FALLING, irq_callback, (void *) PIN_WK_UP);
/* 使能中断 */
rt_pin_irq_enable(PIN_KEY1, PIN_IRQ_ENABLE);
rt_pin_irq_enable(PIN_WK_UP, PIN_IRQ_ENABLE);
int topple;
int X,Y,Z;
X=devvv->accel_offset.x;
Y=devvv->accel_offset.y;
Z=devvv->accel_offset.z;
if(abs(Y)<=10000){
topple=0;
rt_pin_write(PIN_BEEP,PIN_LOW);
LOG_D("KEY1 interrupt. beep off.");
}
else{
topple=1;
rt_pin_write(PIN_BEEP,PIN_HIGH);
LOG_D("WK_UP interrupt. beep on.");
break;
}
Onenet页面展示
全部0条评论
快来发表一下你的评论吧 !