1673 views|0 replies

282

Posts

2

Resources
The OP
 

[RVB2601 Creative Application Development] 7. IoT Control [Copy link]

 

I. Introduction

The onboard w800 wifi module of RVB2601 can be easily connected to the Feiyan platform of Alibaba Cloud. This article will explain how to upload MCU data to the Feiyan platform and how to download and transfer data from the Feiyan platform to our MCU.

2. Create products on the Feiyan platform

1. Open the Feiyan platform and log in to your Alibaba Cloud account

Website: https://living.aliyun.com/

2. Create a new project. I have already created a network alarm clock project here. This is also the work I completed in this competition. Click on the newly created project to proceed to the next step.

3. Click Create New Product. I created a new product called an Internet alarm clock. Fill in the relevant parameters according to the actual situation. This is not very important because the following data points do not use the default ones. They are all created by me. Because there is no alarm clock in the selected product, I chose a watch. Click the newly created product to proceed to the next step.

4. Add functions. I deleted all the standard functions because they are not needed in my product. I chose custom functions and added some data attributes of my own.

The following figure only shows some of the properties I created. The type of property depends on your actual situation.

5. Click Next to jump to the human-computer interaction page. This part can be added according to your needs and will not be elaborated here.

6. Click Next to jump to the device debugging page, where you need to select a wifi module, just choose one, and then click Add Debug Device.

Note: Remember the Product Key and Product Secret in the upper right corner of the page, as they will be used in the code.

Click to view the certificate, you can view some keys, which are needed when setting the quintuple in the code.

7. Debug properties. At this step, the device is basically created successfully. If online debugging is required, click debug. The premise of debugging is that we have completed the data interaction with the platform on the MCU. The next step is to start the development of the MCU code.

3. Code Development

The operations related to initialization and connecting to the router are not described in detail here. You can refer to the web_player demo project. The following only describes how RVB2601 interacts with the cloud platform.

For the AT commands related to using w800 to connect to the cloud platform, please refer to the official document "RVB2601_W800 Network Transmitting AT Command Set.pdf".

1. Create a new task to handle data interaction. Iot_TaskEntry is the task entry function, where we handle periodic tasks;

2. Register the attribute data receiving and sending task, atparser_oob_create(g_atparser_uservice_t, "+IDMPS:", Iot_recv_handler, NULL); is to install the attribute data receiving service function. After receiving the attribute data, it will automatically enter the function Iot_recv_handler, where we will process the received CJSON data;

3. Issue tasks in the connection state in the registration platform, atparser_oob_create(g_atparser_uservice_t, "+IDMSTA:", Iot_connsta_handler, NULL); is the installation connection state service function. When the state change is detected, it will automatically enter this function. We will use this function to determine whether we are connected to the cloud platform;

/* 初始化函数 */
void Iot_Init(void)
{
	int task_s;
	
	//属性设置OOB注册
	atparser_oob_create(g_atparser_uservice_t, "+IDMPS:", Iot_recv_handler, NULL);   
    //连接状态OOB注册
	atparser_oob_create(g_atparser_uservice_t, "+IDMSTA:", Iot_connsta_handler, NULL);   
	
	aos_mutex_new(&iot_cmd_mutex);

	task_s = aos_task_new_ext(&iot_task_h,
								"iot_task", 
								Iot_TaskEntry, 
								NULL, 
								2048, 
								6);
	if (0 == task_s)
	{
		printf("APP_IOT Task create success\r\n");
	}
	else
	{
		printf("APP_IOT Task create failed\r\n");
	}
}

4. Set the device quintuple

To successfully connect to the cloud platform, you need to set the device quintuple first. The function is implemented as follows:

char *my_key = "xxxx";       //ProductKey
char *my_name = "test";         //DeviceName
char *my_secret = "xxxx";//DeviceSecret
char *my_p_secret = "xxxxx";//Product Secret

/* 设置设备五元组 */
static int Iot_w800_living_idmau(const char *mykey,const char *myname,const char *mysecret,const char *mypsecret)
{
    int ret = -1;
	
    aos_mutex_lock(&iot_cmd_mutex, AOS_WAIT_FOREVER);
    atparser_clr_buf(g_atparser_uservice_t);
    if (atparser_send(g_atparser_uservice_t,"AT+IDMAU=\"%s\",\"%s\",\"%s\",\"%s\"", mykey , myname , mysecret ,mypsecret) == 0) 
    {
        if (atparser_recv(g_atparser_uservice_t, "OK\n") == 0) 
        {
            ret = 0;
        }
        else 
        {
            printf("AT+IDMAU: 五元组设置失败!\r\n");
        }
    }
    atparser_cmd_exit(g_atparser_uservice_t);
    if (ret == 0) 
    {
        printf("AT+IDMAU: 设置五元组成功!\r\n");
    }
    aos_mutex_unlock(&iot_cmd_mutex);
	
    return ret;
}

5. Connect to Feiyan Platform

/* 连接飞燕物联网平台 */
static int Iot_w800_living_idmcon(void)
{
    int ret = -1;
	
    aos_mutex_lock(&iot_cmd_mutex, AOS_WAIT_FOREVER);
    atparser_clr_buf(g_atparser_uservice_t);
    if (atparser_send(g_atparser_uservice_t, "AT+IDMCON") == 0) 
    {
        if (atparser_recv(g_atparser_uservice_t, "OK\n") == 0) 
        {
            ret = 0;
		} 
        else 
        {
            printf("AT+IDMCON Destination Host Unreachable!\r\n");
		}
    }
    atparser_cmd_exit(g_atparser_uservice_t);
    if (ret == 0) 
    {
        printf("AT+IDMCON: 连接飞燕平台成功 \r\n");
    }
    aos_mutex_unlock(&iot_cmd_mutex);
	
    return ret;
}

6. Disconnect from Feiyan platform

/* 断开飞燕物联网平台 */
static int Iot_w800_living_idmcls(void)
{
    int ret = -1;
	
    aos_mutex_lock(&iot_cmd_mutex, AOS_WAIT_FOREVER);
    atparser_clr_buf(g_atparser_uservice_t);
    if (atparser_send(g_atparser_uservice_t, "AT+IDMCLS") == 0) 
    {
        if (atparser_recv(g_atparser_uservice_t, "OK\n") == 0) 
        {
            ret = 0;
		} 
        else 
        {
            printf("AT+IDMCLS: 连接不到飞燕平台\r\n");
		}
    }
    atparser_cmd_exit(g_atparser_uservice_t);
    if (ret == 0) 
    {
        printf("AT+IDMCLS: 断开飞燕平台 \r\n");
    }
    aos_mutex_unlock(&iot_cmd_mutex);
	
    return ret;
}

7. Reporting attribute data

This function is used to report attributes. The first parameter is the device ID, which defaults to 0. If there is only one device, it is 0. The second number is the data to be uploaded, which is a JSON data. The third id also defaults to 0.

/* 上报状态 */
static int Iot_w800_living_idmpp(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;
    char event_str[64];
    char at_string_msg[120];

    if ((!dev_id) || (!msg) || (!packet_id)) 
    {
        return ret;
    }

    aos_mutex_lock(&iot_cmd_mutex, AOS_WAIT_FOREVER);
    atparser_clr_buf(g_atparser_uservice_t);
    
    /* 上传报文生成 */
    sprintf(at_string_msg, "AT+IDMPP=0,\"%s\"", msg);
    printf("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;
            printf("AT+IDMPP: 数据上传成功\r\n");
        }
    } 
    else 
    {
        printf("AT+IDMSTA FAILED\r\n");
    }
    atparser_cmd_exit(g_atparser_uservice_t);
    aos_mutex_unlock(&iot_cmd_mutex);

    return ret;
}

8. State service function

In this function, the platform connection status is judged. To upload or download data, you need to ensure that the status connection is successful. The return value 2 indicates a successful connection. For details, see "RVB2601_W800 Network Transmit AT Instruction Set.pdf"

/*云连接状态上报OOB处理*/
static int Iot_connsta_handler(atparser_uservice_t *at, void *priv, oob_data_t *oob_data)
{
	int ret_status;
    LOGI(TAG,"Iot==>connect status: %s(%d)", oob_data->buf, oob_data->len);
	
    /* 将连接状态转换为数字 */
	ret_status = oob_data->buf[0] - 0x30U;

	if((0 == ret_status) || (1 == ret_status) || (2 == ret_status))
	{
		dev_conn_st = ret_status;
	}

    return 0;
}

9. Attribute data receiving and processing function

Here we mainly process the received JSON data, which requires importing the Cjson library. See my previous post for details.

Iot_apply needs to be implemented by the partners themselves, and data is json data for the partners to process by themselves. If you are not sure how to process it, I will take a look at the completed processing method in the code of the work I submitted.

* 数据接收回调处理函数 */
static int Iot_recv_handler(atparser_uservice_t *at, void *priv, oob_data_t *oob_data)
{
    int id = 0;
    int len = 0;
    char data[128] = {0};
	
    LOGI(TAG, "%s(%d)", oob_data->buf, oob_data->len);
    char *str = strchr(oob_data->buf, ':');
	
	if (str != NULL) 
    {
		sscanf(oob_data->buf, "%d,%d,%s\r\n", &id, &len, data);
		LOGI(TAG, "==>Iot received: %d(%d):%s\r\n", id, len, data);

		Iot_apply(data);
	}
 
    /* recv data is not enough need recv more */
    return 0;
}

10. Data upload example

Here we will use an example to illustrate how to convert our attribute data into JSON format and upload it to the IoT platform

In the screenshot of the previous attribute creation, there is a ntpEn attribute. Taking this as an example, the code implementation is as follows:

In this example, this function is called only when the Feiyan platform connection status is 2. The data will be automatically sent once when it is called for the first time, in order to align with the data on the cloud platform. If the data to be sent changes later, it will be sent again. For specific usage, please refer to my work submission code.

/* 上报NTP设置状态 */
static void Iot_NtpSettingUpload(void)
{
    static uint8_t last_ntp_status = 0;
    static uint8_t first_enter = 1;    /* 是否第一次进入这个函数 */
	char msg_buf[100];
	int pkt_id = 0;
    uint8_t ntp_en_status;
    int ret = -1;

    ntp_en_status = 0;   /*0 或者 1,这里写用户的函数。返回状态*/

    /* 数据变化了或者第一次进入都上报状态 */
    if ((ntp_en_status != last_ntp_status) || (1 == first_enter))
    {
        first_enter = 0;

        sprintf(msg_buf, "{\\\"ntpEn\\\":%d}", ntp_en_status);    /*将需要上传的属性变为json格式*/
        ret = Iot_w800_living_idmpp("0", msg_buf, &pkt_id);    /* 上传数据 */
        if (-1 == ret)
            printf(">>ntpEn setting data upload error\n");
        else
            printf(">>ntpEn Data Upload Success\n");
    }

    last_ntp_status = ntp_en_status;
}

4. Debugging

After we connect to the Feiyan platform, the test device status will show connected. At this time, we click Debug to enter the debugging interface.

5. Create a web visualization development page

In order to facilitate our operations, we need to make some buttons or edit boxes on the web visualization interface to send data.

Web visualization interface creation tutorial link: https://help.aliyun.com/document_detail/141613.html

Application development website: https://studio.iot.aliyun.com/?spm=a2c4g.11186623.0.0.58531a5fYJegwy

Next, we will create a web visualization interface.

1. Create a new web application

You can choose web application or mobile application here. They are actually the same, but mobile application seems more convenient on mobile phones.

2. Create a new switch

Here we only take the creation of a switch as an example. If you need to create other controls, please refer to the documentation. I will not go into details here.

3. Configure the data source

Select the switch and click Configure Data Source in the extended window menu on the right.

This configuration is used to obtain the attribute data uploaded by the lower computer (MCU).

4.Configure data interaction

Select Interaction, here is to configure the switch action function, select Value Change here, which means that after the switch value changes, the attribute data will be sent to the lower computer.

5. After the above steps, we have completed a simple web interactive page. Other settings such as edit boxes can refer to this setting. The logic is the same.

6. Testing

After editing the web page, click Preview in the upper right corner to start verifying whether the communication is successful.

 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list