本帖对于这次测评RVB2601进行一个总结。当时申请测评时,定的目标是:实现一个智能风扇,能够通过手机远程的对风扇进行控制。最初的计划是使用蓝牙将手机与开发板连接,不过开始使用之后发现官方并没有移植蓝牙,因此修改了方案,使用wifi将开发板与手机连接。
一、主要功能
- 读取温湿度传感器,开发板通过I2C接口与DHT12传感器进行通信,获取温湿度数据;
- PWM控制,使用L298N模块驱动风扇,开发板输出PWM信号控制风扇的转速;
- 屏幕显示,使用LVGL作为GUI,在开发板上显示温湿度和风扇档位信息;
- TCP客户端,利用官方提供的驱动,使用W800里的函数实现TCP客户端,与手机进行通信;
- 编写安卓的APP,在安卓手机上实现TCP服务器,接收开发板发送的温湿度数据,并将风扇档位的控制数据发送给开发板。
二、系统框图
三、作品图片
四、演示视频
五、源代码
//风扇控制的PWM代码
csi_timer_t l_timer;
csi_pwm_t blue_led_pwm;
#define FAN_PERIOD 31
void pwm_io_init()
{
csi_pin_set_mux(PA4,PA4_PWM_CH4);
csi_pwm_init(&blue_led_pwm, 0);
csi_pwm_out_config(&blue_led_pwm, 4/2, FAN_PERIOD, 0, PWM_POLARITY_HIGH);
csi_pwm_out_start(&blue_led_pwm, 4/2);
}
void change_pwm_ratio(uint8_t ratio)
{
uint8_t value = 0;
char speed_value[6] = {0,18,22,25,28,30};
if(ratio < 6){
value = speed_value[ratio];
}else{
value = 0;
}
csi_pwm_out_config(&blue_led_pwm, 2, FAN_PERIOD, value, PWM_POLARITY_HIGH);
}
//DHT12的读取函数
static csi_iic_t g_iic;
void dht12_init()
{
csi_pin_set_mux(PA8, PA8_IIC0_SCL);
csi_pin_set_mux(PA9, PA9_IIC0_SDA);
csi_iic_init(&g_iic, 0);
csi_iic_mode(&g_iic, IIC_MODE_MASTER);
csi_iic_addr_mode(&g_iic, IIC_ADDRESS_7BIT);
csi_iic_speed(&g_iic, IIC_BUS_SPEED_STANDARD);
}
void dht12_get_data(unsigned char *value)
{
unsigned char buf[10]={0};
char str_data[50];
csi_iic_master_send(&g_iic,0x5c,buf,1,1000);
csi_iic_master_receive(&g_iic,0x5c,buf,5,1000);
memcpy(value,buf,5);
LOGD(TAG,"dht12: %x %x %x %x %x\n",buf[0],buf[1],buf[2],buf[3],buf[4]);
}
//读取DHT12数据并发送给手机的任务
void dht12_task(void *paras)
{
char str[50];
unsigned char buf[10];
dht12_init();
while(1){
dht12_get_data(buf);
sprintf(str,"temperature:%d.%d;humity:%d.%d;\n",buf[2],buf[3],buf[0],buf[1]);
char sum = buf[0] + buf[1] + buf[2] + buf[3];
if(sum == buf[4]){
put_temperature_in_string(&buf[2]);
put_humity_in_string(&buf[0]);
w800_send_data(str,strlen(str),1000);
aos_msleep(2000);
}else{
LOGD(TAG,"dht wrong\n");
}
}
}
//wifi收到数据的回调函数
void w800_in(int linkid, void *data, size_t len, char remote_ip[16], uint16_t remote_ports)
{
uint8_t* d;
uint8_t speed;
d=(uint8_t *)data;
printf("rev:%s\n",data);
char *p_str;
p_str = strstr(data, "speed:");
if (p_str != NULL) {
speed = p_str[6]-'0';
put_speed_in_string(&speed);
change_pwm_ratio(speed);
}
}
//wifi事件的回调函数
static void network_event(uint32_t event_id, const void *param, void *context)
{
switch(event_id) {
case EVENT_NETMGR_GOT_IP:
LOGD(TAG, "net got ip");
w800_connect_remote(0,2,REMOTE_IP,REMOTE_PORT);
aos_task_new("dht12_task",dht12_task,NULL,1024);
break;
case EVENT_NETMGR_NET_DISCON:
LOGD(TAG, "net disconnect");
break;
}
/*do exception process */
app_exception_event(event_id);
}
//显示页面的初始化
lv_obj_t * label_temp;
lv_obj_t * label_humity;
lv_obj_t * label_speed;
void show_page_init()
{
lv_obj_t * label1 = lv_label_create(lv_scr_act(),NULL);
lv_label_set_long_mode(label1, LV_LABEL_LONG_BREAK);
lv_label_set_align(label1, LV_LABEL_ALIGN_CENTER);
lv_label_set_text(label1,"Smart Fan");
lv_obj_set_width(label1, 128);
lv_obj_align(label1, NULL, LV_ALIGN_IN_TOP_MID, 0, 0);
label_temp = lv_label_create(lv_scr_act(),NULL);
lv_label_set_long_mode(label_temp, LV_LABEL_LONG_BREAK);
lv_label_set_align(label_temp, LV_LABEL_ALIGN_LEFT);
sprintf(temp,"TEMP\t: -- C");
lv_label_set_text(label_temp,temp);
lv_obj_set_width(label_temp, 128);
lv_obj_align(label_temp, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 16);
label_humity = lv_label_create(lv_scr_act(),NULL);
lv_label_set_long_mode(label_humity, LV_LABEL_LONG_BREAK);
lv_label_set_align(label_humity, LV_LABEL_ALIGN_LEFT);
sprintf(humity,"RH\t: -- %%");
lv_label_set_text(label_humity,humity);
lv_obj_set_width(label_humity, 128);
lv_obj_align(label_humity, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 32);
label_speed = lv_label_create(lv_scr_act(),NULL);
lv_label_set_long_mode(label_speed, LV_LABEL_LONG_BREAK);
lv_label_set_align(label_speed, LV_LABEL_ALIGN_LEFT);
sprintf(fan_speed,"Speed\t: 0");
lv_label_set_text(label_speed,fan_speed);
lv_obj_set_width(label_speed, 128);
lv_obj_align(label_speed, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 48);
}
//装填显示数据的函数
char temp[50];
char humity[50];
char fan_speed[50];
void put_temperature_in_string(uint8_t *data)
{
sprintf(temp,"TEMP\t: %d.%d C",data[0],data[1]);
}
void put_humity_in_string(uint8_t* data)
{
sprintf(humity,"RH\t: %d.%d %%",data[0],data[1]);
}
void put_speed_in_string(uint8_t* data)
{
sprintf(fan_speed,"Speed\t: %d",data[0]);
}
//主函数及显示任务
int main(void)
{
static unsigned int count;
board_yoc_init();
pwm_io_init();
lv_init();
oled_init();
show_page_init();
/* Subscribe */
event_subscribe(EVENT_NETMGR_GOT_IP, network_event, NULL);
event_subscribe(EVENT_NETMGR_NET_DISCON, network_event, NULL);
while(1){
lv_task_handler();
aos_msleep(100);
lv_tick_inc(100);
count++;
if(count % 5 == 0){
lv_label_set_text(label_temp,temp);
lv_label_set_text(label_humity,humity);
lv_label_set_text(label_speed,fan_speed);
LOGD(TAG,"fulsh show\n");
}
}
}
【平头哥RVB2601创意应用开发】智能风扇.docx
(1.32 MB, 下载次数: 9)