RVB2601 基于 W800 接入生活物联网平台(飞燕平台)

本文源自:RISC-V 大赛 - 那我就自己来吧

本文作者:Murphy

本文转自:芯片开放社区(OCC)


一、准备

  • RVB2601 开发板
  • 飞燕平台产品
  • CDK 软件开发环境

RVB2601 基于 W800 接入生活物联网平台(飞燕平台)


二、飞燕平台创建产品

打开 飞燕平台 按照提示开启该功能,并创建项目。

RVB2601 基于 W800 接入生活物联网平台(飞燕平台)


1.创建项目

2.创建产品
可以选择使用 led 进行测试,使用默认的功能定义。

RVB2601 基于 W800 接入生活物联网平台(飞燕平台)


3.添加测试设备

点击上步骤创建的产品,然后进入产品功能配置。然后点击 3-设备调试,随便选择一个 WiFi 模组,然后新增测试设备。

RVB2601 基于 W800 接入生活物联网平台(飞燕平台)


4.获取设备管理的四元组

  • 产品 ID
  • 产品 Secret
  • 设备名称
  • 设备 Secret

RVB2601 基于 W800 接入生活物联网平台(飞燕平台)


三、工程准备

打开 CDK,进入 Welcome Page。

RVB2601 基于 W800 接入生活物联网平台(飞燕平台)


然后点击 创建工程 ,在搜索框输入 ch2601_webplayer_demo 找到示例工程,添加到本地。

三、设备端添加驱动代码

在工程的 sdk_chip_ch2601 --> drv_wifi_at_w800 里找到 w800_api.c/h 文件。


在这 w800_api.c 文件里添加以下代码:

int w800_living_platform_attribute_upload(const char *dev_id, 
        const char *msg, int *packet_id)
{
    int ret = -1;
    
    int rsp_dev_id = -1;
    int rsp_packet_id = -1;
    int rsp_code = -1;
    int rsp_reply_len = -1;
    int id = -1;
    char event_str[64] = {0};
    char unknown_str[64] = {0};
    
    if (!dev_id || !msg || !packet_id) {
        return ret;
    }
    
    aos_mutex_lock(&g_cmd_mutex, AOS_WAIT_FOREVER);
    atparser_clr_buf(g_atparser_uservice_t);
    
    /**
     * AT+IDMPP= 0,"{\"powerstate\":1}"
    */
    char at_string_msg[64] = {0};
    snprintf(at_string_msg, 64, "AT+IDMPP=0,\"%s\"", msg);
    LOGD(TAG, "Send msg: %s\r\n", at_string_msg);
    if (atparser_send(g_atparser_uservice_t, at_string_msg) == 0) {
        if (((atparser_recv(g_atparser_uservice_t, "+IDMPP:%d\n", packet_id) == 0) && 
            (atparser_recv(g_atparser_uservice_t, "OK\n") == 0)) ||
            (atparser_recv(g_atparser_uservice_t, "+IDMPP:%d,%d,%d,%d,%s\n", 
                    &rsp_dev_id, &rsp_packet_id, &rsp_code, &rsp_reply_len, event_str) == 0)) {
            ret = 0;
            LOGD(TAG, "Send at cmd ok\r\n");
        }
    } else {
        LOGD(TAG, "Send at cmd err\r\n");
    }
    
    atparser_cmd_exit(g_atparser_uservice_t);
    aos_mutex_unlock(&g_cmd_mutex);
    
    return ret;
}

int w800_living_platform_event_upload(const char *dev_id, 
        const char *event_id, const char *event_payload, int *packet_id)
{
    int ret = -1;
    int rsp_dev_id = -1;
    int rsp_packet_id = -1;
    int rsp_code = -1;
    int rsp_reply_len = -1;
    int id = -1;
    char event_str[64] = {0};
    char unknown_str[64] = {0};
    
    if (!dev_id || !event_id || !packet_id || !event_payload) {
        return ret;
    }
    
    aos_mutex_lock(&g_cmd_mutex, AOS_WAIT_FOREVER);
    atparser_clr_buf(g_atparser_uservice_t);
    
    char at_string_msg[64] = {0};
    snprintf(at_string_msg, 64, "AT+IDMEP=%s,\"%s\",\"%s\"", dev_id, event_id, event_payload);
    LOGD(TAG, "Send msg: %s\r\n", at_string_msg);
    
    if (atparser_send(g_atparser_uservice_t, at_string_msg) == 0) {
        if ((atparser_recv(g_atparser_uservice_t, "+IDMEP:%d\n", packet_id) == 0) && 
            (atparser_recv(g_atparser_uservice_t, "OK\n") == 0)) {
            ret = 0;
        }
    }
    
    atparser_cmd_exit(g_atparser_uservice_t);
    aos_mutex_unlock(&g_cmd_mutex);
    
    return ret;
}

/**
 * w800_living_platform_recv_handler_register
 * 
 * @param cmd
 *        example: "+IDMPS:"
*/
void w800_living_platform_recv_handler_register(const char *cmd, void *cb, void *context)
{
    atparser_oob_create(g_atparser_uservice_t, cmd, cb, context);
}


并在 w800_api.h 文件里添加以下代码:

extern int w800_living_platform_config(const char* product_key, const char* dev_name, 
        const char* dev_secret, const char* product_secret);
extern int w800_living_platform_conn(void);
extern int w800_living_platform_link_status_get(int *state);
extern int w800_living_platform_attribute_upload(const char *dev_id, 
        const char *msg, int *packet_id);
extern int w800_living_platform_event_upload(const char *dev_id, 
        const char *event_id, const char *event_payload, int *packet_id);
extern void w800_living_platform_recv_handler_register(const char *cmd, void *cb, void *context);


四、设备端添加示例代码

修改 app --> src --> app_main.c 代码。

#include <aos/aos.h>
#include <devices/w800.h>
#include <yoc/atparser.h>

#define LIVING_PLATFORM_PROD_KEY      "xx"
#define LIVING_PLATFORM_PROD_SECRET   "xx"
#define LIVING_PLATFORM_DEV_NAME      "xxx"
#define LIVING_PLATFORM_DEV_SECRET    "xxx"


extern int w800_living_platform_config(const char* product_key, const char* dev_name, 
        const char* dev_secret, const char* product_secret);
extern int w800_living_platform_conn(void);
extern int w800_living_platform_link_status_get(int *state);
int app_living_platform_conn(void);

static aos_sem_t g_living_start_sem = {0};
int app_living_platform_init(void)
{
    int ret = -1;
    ret = aos_sem_new(&g_living_start_sem, 0);
    if (ret != 0)
    {
        goto __exit;
    }
    
__exit:
    return ret;
}

int app_living_platform_wait_start(void)
{
    int ret = -1;
    ret = aos_sem_wait(&g_living_start_sem, AOS_WAIT_FOREVER);
    if (ret != 0)
    {
        goto __exit;
    }
    
__exit:
    return ret;
}

void app_living_platform_notify_start(void)
{
    aos_sem_signal(&g_living_start_sem);
}

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");
        app_living_platform_notify_start();
        break;
    case EVENT_NETMGR_NET_DISCON:
        LOGD(TAG, "net disconnect");
        break;
    }
}

int main(void)
{
    board_yoc_init();
    if (0 != app_living_platform_init())
    {
        LOGE(TAG, "app_living_platform_init failed");
        return -1;
    }
    
    /* Subscribe */
    event_subscribe(EVENT_NETMGR_GOT_IP, network_event, NULL);
    event_subscribe(EVENT_NETMGR_NET_DISCON, network_event, NULL);
    
    if (0 == app_living_platform_wait_start())
    {
        if (0 != app_living_platform_conn())
        {
            LOGE(TAG, "app_living_platform_conn failed");
        }
    }
    else
    {
        LOGE(TAG, "app_living_platform_wait_start failed");
    }
    return 0;
}

static int _living_platform_attribute_set_handler(atparser_uservice_t *at, void *priv, oob_data_t *oob_data)
{
    int store_order = (int)priv;
    char *buf = (oob_data != NULL ? oob_data->buf : NULL);
    
    LOGD(TAG, "recv data len: %d; data: %s\n", store_order, buf);
    
    return 0;
}

int app_living_platform_conn(void)
{
    int ret = 0;
    
    w800_living_platform_recv_handler_register("+IDMPS:", _living_platform_attribute_set_handler, NULL);
    
    LOGD(TAG, "==> start config living platform");
    ret = w800_living_platform_config(
            LIVING_PLATFORM_PROD_KEY, LIVING_PLATFORM_DEV_NAME, 
            LIVING_PLATFORM_DEV_SECRET, LIVING_PLATFORM_PROD_SECRET);
    if (ret != 0)
    {
        goto __exit;
    }
    LOGD(TAG, "<== end config living platform");
    
    LOGD(TAG, "==> start connect living platform");
    ret = w800_living_platform_conn();
    if (ret != 0)
    {
        goto __exit;
    }
    LOGD(TAG, "==> end connect living platform");
    
    /* delay some time, then check link status */
    aos_msleep(4000);
    {
        int link_status = -1;
        
        LOGD(TAG, "==> start get link status");
        ret = w800_living_platform_link_status_get(&link_status);
        if (ret != 0)
        {
            goto __exit;
        }
        LOGD(TAG, "<== end get link status <%d>", link_status);
    }
    
    /* delay some time, then upload attribute */
    aos_msleep(1000);
    {
        extern int w800_living_platform_attribute_upload(const char *dev_id, 
            const char *msg, int *packet_id);
        extern int w800_living_platform_event_upload(const char *dev_id, 
            const char *event_id, const char *event_payload, int *packet_id);
        const char *dev_id = "0";
        const char *msg_on = "{\\\"powerstate\\\":1}";
        const char *msg_off = "{\\\"powerstate\\\":0}";
        int pkt_id = 0;
        LOGD(TAG, "==> start attribute upload");
        ret = w800_living_platform_attribute_upload(dev_id, msg_on, &pkt_id);
        if (ret != 0)
        {
            goto __exit;
        }
        LOGD(TAG, "<== end attribute upload, packet id <%d>", pkt_id);
    }
__exit:
    return ret;
}


五、源码获取

后面整理后放到 gitee 。

上一篇:安卓支持RISC-V架构的技术剖析


下一篇:谈谈一些有趣的CSS题目(十四)-- 纯 CSS 方式实现 CSS 动画的暂停与播放!