使用WIFI连接新大陆云平台(基于RT_Thread操作系统)

前言

使用RT-Thread Studio 连接WIFI
首先我们需要配置WIFI,具体的配置参考上面这篇文章,下面将会讲述使用WIFI连接到新大陆云平台。




相关的AT指令

1.设置WIFI为Station模式

AT+CWMODE=1

2.重启模块

AT+RST

3.连接WIFI

AT+CWJAP="YaoJin","qwertyuiop111"

4.单路连接模式

AT+CIPMUX=0

5.连接云平台

AT+CIPSTART="TCP","ndp.nlecloud.com",8600
AT+CIPSTART="MQTT","mqtt.nlecloud.com",1883

6.设置透传

AT+CIPMODE=0
然后进透传入>
AT+CIPSEND=100

7.发送数据

device后面的是设备ID,key是设备标识符,更换成自己的.

{"t":1,"device":"SmartHomeLucky","key":"4094ef2dbebf44b78601a54d2a39c451","ver":"v0.0.0.0"}



创建任务

首先创建一个任务,任务创建就启动任务
static rt_thread_t tid1 = RT_NULL;
tid1 = rt_thread_create("t1", thread_entry, (void*)1, 1024, 25, 5);
    if (tid1 != RT_NULL){
        LOG_D("successful");
        rt_thread_startup(tid1);
    }


将上面AT指令转换成代码

void parseResponse(int result,at_response_t resp){
    if (result ==RT_EOK){
            //解析到了数据
        const char *line_buffer = RT_NULL;
        LOG_D("Response buffer");
        for(rt_size_t line_num = 1; line_num <= resp->line_counts; line_num++)
        {
            if((line_buffer = at_resp_get_line(resp, line_num)) != RT_NULL)
            {
                LOG_D("line %d buffer : %s", line_num, line_buffer);
            }
            else
            {
                LOG_E("Parse line buffer error!");
            }
        }
    }
}

static void thread_entry(void *parameter){
    at_response_t resp = at_create_resp(256, 0, rt_tick_from_millisecond(5000));
    if (resp == RT_NULL){
       LOG_D("No memory for response structure!");
    }
    int result = at_exec_cmd(resp,"AT+CIFSR");  //查询IP地址
    parseResponse(result, resp);
    result = at_exec_cmd(resp,"AT+CIPMUX=0");
    parseResponse(result, resp);
    result = at_exec_cmd(resp,"AT+CIPSTART=\"TCP\",\"ndp.nlecloud.com\",8600");
    parseResponse(result, resp);
    result = at_exec_cmd(resp,"AT+CIPMODE=0");
    parseResponse(result, resp);
    result = at_exec_cmd(resp,"AT+CIPSEND=92");
    parseResponse(result, resp);
    result = at_exec_cmd(resp,"{\"t\":1,\"device\":\"SmartHomeLucky\",\"key\":\"4094ef2dbebf44b78601a54d2a39c451\",\"ver\":\"v0.0.0.0\"}");
    parseResponse(result, resp);
    //result = at_exec_cmd(resp,"AT+CIPMUX=0");
    LOG_D("OK");
}



发送心跳包

result = at_exec_cmd(resp,"AT+CIPSEND=7");
parseResponse(result, resp);
result = at_exec_cmd(resp,"$#AT#\r");
parseResponse(result, resp);



上传数据

result = at_exec_cmd(resp,"AT+CIPSEND=76");
result = at_exec_cmd(resp,"{\"t\":3,\"datatype\":2,\"datas\":{\"Lux\":{\"2020-05-20 14:30:33\":50}},\"msgid\":001}");
parseResponse(result, resp);

完整代码

#include 

#include 
#include 
#include 
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include 

/* PLEASE DEFINE the LED0 pin for your board, such as: PA5 */
#define LED0_PIN    GET_PIN(B, 9)
static rt_thread_t tid1 = RT_NULL;
int count = 0;

void parseResponse(int result,at_response_t resp){
    if (result ==RT_EOK){
            //解析到了数据
        const char *line_buffer = RT_NULL;
        LOG_D("Response buffer");
        for(rt_size_t line_num = 1; line_num <= resp->line_counts; line_num++)
        {
            if((line_buffer = at_resp_get_line(resp, line_num)) != RT_NULL)
            {
                LOG_D("line %d buffer : %s", line_num, line_buffer);
            }
            else
            {
                LOG_E("Parse line buffer error!");
            }
        }
    }
}

static void thread_entry(void *parameter){
    at_response_t resp = at_create_resp(256, 0, rt_tick_from_millisecond(5000));
    if (resp == RT_NULL){
       LOG_D("No memory for response structure!");
    }
    int result = at_exec_cmd(resp,"AT+CIFSR");  //查询IP地址
    result = at_exec_cmd(resp,"AT+CIPMUX=0");
    result = at_exec_cmd(resp,"AT+CIPSTART=\"TCP\",\"ndp.nlecloud.com\",8600");
    result = at_exec_cmd(resp,"AT+CIPMODE=0");
    result = at_exec_cmd(resp,"AT+CIPSEND=92");
    //result = at_exec_cmd(resp,"{\"t\":1,\"device\":\"SmartHomeLucky\",\"key\":\"4094ef2dbebf44b78601a54d2a39c451\",\"ver\":\"v0.0.0.0\"}");
    result = at_exec_cmd(resp,"{\"t\":1,\"device\":\"SmartHomeLucky\",\"key\":\"4094ef2dbebf44b78601a54d2a39c451\",\"ver\":\"v0.0.0.0\"}");
    parseResponse(result, resp);
    LOG_D("OK");
    for(;;){
        rt_pin_write(LED0_PIN, count % 2);
        result = at_exec_cmd(resp,"AT+CIPSEND=7");
        result = at_exec_cmd(resp,"$#AT#\r");
        parseResponse(result, resp);
        result = at_exec_cmd(resp,"AT+CIPSEND=76");//62
        result = at_exec_cmd(resp,"{\"t\":3,\"datatype\":2,\"datas\":{\"Lux\":{\"2020-05-20 14:30:33\":50}},\"msgid\":001}");
        parseResponse(result, resp);
        rt_thread_mdelay(5000);
        count = count + 1;
    }
}
int main(void)
{
    /* set LED0 pin mode to output */
    rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
    //创建线程
    tid1 = rt_thread_create("t1", thread_entry, (void*)1, 1024, 25, 10);
    if (tid1 != RT_NULL){
        LOG_D("successful");
        rt_thread_startup(tid1);
    }
    else{
        LOG_D("fail");
    }

    return RT_EOK;
}

发送成功,可以在云平台历史记录查看到数据
使用WIFI连接新大陆云平台(基于RT_Thread操作系统)_第1张图片




改进

一般长度并不需要我们来计算,可以通过spirntf赋值即可。

rt_uint8_t *m = "AT";
rt_uint8_t buf[60];
rt_sprintf(buf,"%s=%d",m,rt_strlen(m));
LOG_D(buf);

在这里插入图片描述
如果没有定时发送心跳包,那么就会断开跟新大陆云平台的连接,这里放在循环里延时5s发送。同时这里的发送数据可以放在另一个任务里面,然后把该任务设置成定时器周期,那么就可以定时的发送心跳请求。在任务的采集以及任务之间的通信可以采用信号量来保持同步等。

你可能感兴趣的:(RTT,新大陆)