【平头哥RVB2601创意应用开发】+智能风扇
本帖最后由 manhuami2007 于 2022-5-5 15:56 编辑<div class="parsedown-markdown">
<p>本帖对于这次测评RVB2601进行一个总结。当时申请测评时,定的目标是:实现一个智能风扇,能够通过手机远程的对风扇进行控制。最初的计划是使用蓝牙将手机与开发板连接,不过开始使用之后发现官方并没有移植蓝牙,因此修改了方案,使用wifi将开发板与手机连接。</p>
<h3>一、主要功能</h3>
<ol>
<li>读取温湿度传感器,开发板通过I2C接口与DHT12传感器进行通信,获取温湿度数据;</li>
<li>PWM控制,使用L298N模块驱动风扇,开发板输出PWM信号控制风扇的转速;</li>
<li>屏幕显示,使用LVGL作为GUI,在开发板上显示温湿度和风扇档位信息;</li>
<li>TCP客户端,利用官方提供的驱动,使用W800里的函数实现TCP客户端,与手机进行通信;</li>
<li>编写安卓的APP,在安卓手机上实现TCP服务器,接收开发板发送的温湿度数据,并将风扇档位的控制数据发送给开发板。</li>
</ol>
<h3>二、系统框图</h3>
<p><img alt="" src="2022-05-05-15-10-11.png" target="_blank" /></p>
<h3>三、作品图片</h3>
<p><img alt="" src="2022-05-05-15-20-11.png" target="_blank" /></p>
<h3>四、演示视频</h3>
<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=1VA4y1S7uQ&page=1" style="background:#eee;margin-bottom:10px;" width="700"></iframe></p>
<h3>五、源代码</h3>
<pre>
<code class="language-cpp">//风扇控制的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 = {0,18,22,25,28,30};
if(ratio < 6){
value = speed_value;
}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={0};
char str_data;
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,buf,buf,buf,buf);
}
//读取DHT12数据并发送给手机的任务
void dht12_task(void *paras)
{
char str;
unsigned char buf;
dht12_init();
while(1){
dht12_get_data(buf);
sprintf(str,"temperature:%d.%d;humity:%d.%d;\n",buf,buf,buf,buf);
char sum = buf + buf + buf + buf;
if(sum == buf){
put_temperature_in_string(&buf);
put_humity_in_string(&buf);
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, 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-'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;
char humity;
char fan_speed;
void put_temperature_in_string(uint8_t *data)
{
sprintf(temp,"TEMP\t: %d.%d C",data,data);
}
void put_humity_in_string(uint8_t* data)
{
sprintf(humity,"RH\t: %d.%d %%",data,data);
}
void put_speed_in_string(uint8_t* data)
{
sprintf(fan_speed,"Speed\t: %d",data);
}
//主函数及显示任务
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");
}
}
}</code></pre>
<p> </p>
<p></p>
</div>
作品非常好,恭喜完成作业。看来我也得着手完成作业了。 lugl4313820 发表于 2022-5-5 16:49
作品非常好,恭喜完成作业。看来我也得着手完成作业了。
<p>加油,早点完成,早点安心<img height="28" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/grinning-face-with-smiling-eyes_1f601.png" width="28" /></p>
<p>手机APP需要连接服务器吗?还是与WIFI模块点对点连接呢?</p>
梦溪开物 发表于 2022-5-11 10:37
手机APP需要连接服务器吗?还是与WIFI模块点对点连接呢?
<p>现在是局域网的连接。有其它的坛友用的阿里云实现的。</p>
页:
[1]