阿里云IoT平台CoAP接入 4 高级版CoAP协议接入测试

关于阿里云IoT平台CoAP接入,可见笔记阿里云IoT平台CoAP接入

小能手另外分享了NB-IoT的应用开发心得,先从模组端对几个常见模块进行试用分析,接着对更上层具体的CoAP协议进行研究,并且演示如何对接主流的第三方物联网平台,可以见NB-IoT应用开发笔记


1 前言

2.3 阿里云IoT平台接入 了解下物模型完成了高级版接入所必须的物模型概念的梳理。

这篇笔记将会修改SDK,完成高级版产品(烟感设备为例)的协议接入。

2 设备属性准备

上一篇中完成的设备属性上报 Json 如下:

{
    "id":"123",
    "version":"1.0",
    "params":{
        "SmokeSensorState":{
            "value":0,
            "time":1526051254000
        },
        "RemoveState":{
            "value":0,
            "time":1526051254000
        },
        "BatteryPercentage":{
            "value":99,
            "time":1526051254000
        },
        "Period":{
            "value":10,
            "time":1526051254000
        }
    },
    "method":"thing.event.property.post"
}

3 SDK代码

3.1 组装好 payload

int iotx_propreport_payload_sdm200(char *msg, char smoke_state, char remove_state, char bat_per, int period)
{
    int     ret;
    char    para_smoke[REPORT_PARA_LEN] = {0};
    char    para_remove[REPORT_PARA_LEN] = {0};
    char    para_bat[REPORT_PARA_LEN] = {0};
    char    para_period[REPORT_PARA_LEN] = {0};
    char    time_stamp_str[REPORT_TS_LEN] = {0};

    Get_timestamp_str(time_stamp_str, sizeof(time_stamp_str));

    HAL_Snprintf(para_smoke,
                REPORT_PARA_LEN,
                "\"SmokeSensorState\":{\"value\":%d,\"time\":%s}",
                smoke_state,
                time_stamp_str);                
    HAL_Snprintf(para_remove,
                REPORT_PARA_LEN,
                "\"RemoveState\":{\"value\":%d,\"time\":%s}",
                remove_state,
                time_stamp_str); 
    HAL_Snprintf(para_bat,
                REPORT_PARA_LEN,
                "\"BatteryPercentage\":{\"value\":%d,\"time\":%s}",
                bat_per,
                time_stamp_str); 
    HAL_Snprintf(para_period,
                REPORT_PARA_LEN,
                "\"Period\":{\"value\":%d,\"time\":%s}",
                period,
                time_stamp_str); 

    ret = HAL_Snprintf(msg,
                       REPORT_PAYLOAD_LEN,
                       "{\"id\":\"123\",\"version\":\"1.0\",\"params\":{%s,%s,%s,%s},\"method\":\"thing.event.property.post\"}",
                       para_smoke,
                       para_remove,
                       para_bat,
                       para_period);

    return ret;
}

其中,时间戳函数如下:

static void Get_timestamp_str(char *buf, int len)
{
    uint64_t ret = 0;
    int retry = 0;

    do {
        ret = utils_get_epoch_time_from_ntp(buf, len);
    } while (ret == 0 && ++retry < 10);

    if (retry > 1) {
        log_err("utils_get_epoch_time_from_ntp() retry = %d.", retry);
    }

    if (ret == 0) {
        log_err("utils_get_epoch_time_from_ntp() failed!");
    }

    return;
}

3.2 组装好 topic

int iotx_propreport_topic(char *topic_name, char *topic_head, char *product_key, char *device_name)
{
    int ret;
    ret = HAL_Snprintf(topic_name,
                       IOTX_URI_MAX_LEN,
                       "%s/sys/%s/%s/thing/event/property/post",
                       topic_head,
                       product_key,
                       device_name);
    return ret;
}

3.3 CoAP 发送之

/* report prop */
static int iotx_coap_report_prop(iotx_coap_context_t *p_context)
{
    int                     ret;
    char                    topic_name[IOTX_URI_MAX_LEN + 1];
    iotx_message_t          message;
    iotx_coap_t            *p_iotx_coap = (iotx_coap_t *)p_context;
    CoAPContext            *p_coap_ctx = NULL;

    if (NULL == p_iotx_coap) {
        log_err("Invalid param: p_context is NULL");
        return FAIL_RETURN;
    }

    log_debug("DeviceProp Report: started in CoAP");
    p_coap_ctx = (CoAPContext *)p_iotx_coap->p_coap_ctx;

    /* 1,generate json data */
    char *msg = HAL_Malloc(REPORT_PAYLOAD_LEN);
    if (NULL == msg) {
        log_err("allocate mem failed");
        return FAIL_RETURN;
    }

    iotx_propreport_payload_sdm200(msg, 0, 0, 99, 10);

    log_debug("Dev Report: json data = '%s'", msg);

    memset(&message, 0, sizeof(iotx_message_t));

    message.p_payload = (unsigned char *)msg;
    message.payload_len = (unsigned short)strlen(msg);
    message.resp_callback = iotx_coap_mid_rsphdl;
    message.msg_type = IOTX_MESSAGE_NON;
    message.content_type = IOTX_CONTENT_TYPE_JSON;

    /* 2,generate topic name */
    ret = iotx_propreport_topic(topic_name,
                               "/topic",
                               p_iotx_coap->p_devinfo->product_key,
                               p_iotx_coap->p_devinfo->device_name);

    log_debug("Dev Report: topic name = '%s'", topic_name);

    if (ret < 0) {
        log_err("generate topic name of info failed");
        HAL_Free(msg);
        return FAIL_RETURN;
    }

    if (IOTX_SUCCESS != (ret = IOT_CoAP_SendMessage(p_context, topic_name, &message))) {
        log_err("send CoAP msg failed, ret = %d", ret);
        HAL_Free(msg);
        return FAIL_RETURN;
    }
    HAL_Free(msg);
    log_debug("Dev Report: IOT_CoAP_SendMessage() = %d", ret);

    ret = CoAPMessage_recv(p_coap_ctx, CONFIG_COAP_AUTH_TIMEOUT, 1);
    log_debug("Dev Report: finished, ret = CoAPMessage_recv() = %d", ret);

    return SUCCESS_RETURN;
}

4 平台结果

SDK 改完之后,编译运行,顺利的话就能在 管理控制台->设备管理->运行状态 中看到设备上报的数据。

5 其他说明

注意:格外注意的是,由于阿里云高级版才推出不久,因此在 产品管理->在线调试 是看不到任何数据的。设备状态一直都显示“未激活”。

目前依赖于阿里云的伙伴人工反馈确认,效率不高。这次调试时,阿里伙伴在深夜十一点多依旧在系统里给我答复,在此表示感谢。

阿里云下一步推出日志功能,相信大家调试会更加方便了。

6 END


猜你喜欢

转载自blog.csdn.net/iotisan/article/details/80275815
今日推荐