1、添加uart0,波特率為默認的115200。串口回調(diào)函數(shù)為默認的user_uart_callbakc,TXD、RXD 為P101、P100,剛好適配PMOD2(J7),來接DA16200。
2、添加AT—client包
3、打開uart0:
4、編寫DA16200.c的函數(shù):
主要的思路是先初始化at_clinet客戶端,根據(jù)da16200的mqtt命令初始化。如果注冊成功,向服務器訂閱與發(fā)布主題,詳見代碼:
/*
- Copyright (c) 2006-2021, RT-Thread Development Team
- SPDX-License-Identifier: Apache-2.0
- Change Logs:
- Date Author Notes
- 2023-02-24 Administrator the first version
/
#include
#include
#include
#include
// #ifndef ULOG_USING_SYSLOG
//#define LOG_TAG "AT_DA16200"
//#define LOG_LVL LOG_LVL_DBG
//#include
//#else
//#include
//#endif / ULOG_USING_SYSLOG /
/ 初始化 at_client /
/ 設置當前 AT 客戶端最大支持的一次接收數(shù)據(jù)的長度 */
#define AT_CLIENT_RECV_BUFF_LEN 512
#define AT_CLIENT_DEVICE_NAME "uart0"
#define AT_WAITE_OK_MAXTIME 3000
#define WIFI_SSID HUAWEI-H10R9U
#define WIFI_PWD 18977381885@
#define AT_ADDR_LEN 32
static rt_err_t wifi_connect_state = -1;
char ip[AT_ADDR_LEN] = {0};
char gateway[AT_ADDR_LEN] = {0}, netmask[AT_ADDR_LEN] = {0};
static void urc_conn_func(struct at_client *client ,const char data, rt_size_t size)
{
/ WIFI 連 接 成 功 信 息 */
rt_kprintf("AT Server device WIFI connect success!");
wifi_connect_state = RT_EOK;
}
static void urc_recv_func(struct at_client *client ,const char data, rt_size_t size)
{
/ 接收到服務器發(fā)送數(shù)據(jù) */
char *ptr;
char *p;
rt_kprintf("AT Client receive AT Server data!");
rt_kprintf("%s",data);
ptr = strtok(data, ":");
ptr = strtok(NULL, ",");
while (ptr != NULL) {
rt_kprintf("ptr=%sn", ptr);
ptr = strtok(NULL, ",");
}
}
int da16200_mqtt_pub(char *msg)
{
int result = RT_EOK;
at_response_t resp = RT_NULL;
if(wifi_connect_state != RT_EOK)
{
rt_kprintf("MQTT_client not ready!");
return -1;
}
resp = at_create_resp(256, 0, rt_tick_from_millisecond(5000));
if (resp == RT_NULL)
{
rt_kprintf("No memory for response structure!");
return -2;
}
result = at_exec_cmd(resp, "AT+NWMQMSG='%s'",msg);
if(resp < 0)
{
rt_kprintf("Pub msg ERRO! cord:%d", result);
}
at_delete_resp(resp);
return result;
}
static void urc_func(struct at_client *client ,const char data, rt_size_t size)
{
/ 設備啟動信息 /
rt_kprintf("AT Server device startup!");
rt_kprintf("%s",data);
}
static struct at_urc urc_table[] = {
{"+NWMQCL:1", "rn", urc_conn_func},
{"+NWMQMSG:", "rn", urc_recv_func},
{"RDY", "rn", urc_func},
};
int at_client_port_init(void)
{
/ 添加多種 URC 數(shù)據(jù)至 URC 列表中,當接收到同時匹配 URC 前綴和后綴的數(shù)據(jù),執(zhí)行 URC 函數(shù) /
at_set_urc_table(urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
return RT_EOK;
}
int at_client_16200_mqtt_init(void)
{
int result = RT_EOK;
at_response_t resp = RT_NULL;
/ 創(chuàng)建響應結(jié)構(gòu)體,設置最大支持響應數(shù)據(jù)長度為 256 字節(jié)
(最大響應長度用戶根據(jù)實際需求自定義),響應數(shù)據(jù)行數(shù)無限制,超時時間為 5 秒 /
resp = at_create_resp(256, 0, rt_tick_from_millisecond(5000));
if (resp == RT_NULL)
{
rt_kprintf("No memory for response structure!");
return -2;
}
result = at_client_init(AT_CLIENT_DEVICE_NAME, AT_CLIENT_RECV_BUFF_LEN);
if(result != RT_EOK)
{
rt_kprintf("at_clinet init erro %d",result);
return result;
}
/ 等待AT OK /
result = at_client_wait_connect(AT_WAITE_OK_MAXTIME);
if(result != RT_EOK)
{
rt_kprintf("client to da16200 at timeout %d",result);
return result;
}
/ 設置回顯 /
if(at_exec_cmd(resp, "ATE") != RT_EOK)
{
rt_kprintf("AT client send ATE failed,response error!");
return -RT_ERROR;
}
/ 解釋返回顯狀態(tài) 如果返回是on 則再發(fā)一條關(guān)閉 /
if(at_resp_get_line_by_kw(resp, "on") != NULL)
{
at_exec_cmd(resp, "ATE");
}
/ 設置 station模式 */
if(at_exec_cmd(resp, "AT+WFMODE=0") != RT_EOK)
{
rt_kprintf("AT client send AT+WFMODE failed,response error!");
return -RT_ERROR;
}
/* 連接到 ap */
if(at_exec_cmd(resp, "AT+WFJAPA=ssid,pwd") != RT_EOK)
{
rt_kprintf("AT client send AT+WFWFJAPA failed,response error!");
return -RT_ERROR;
}
rt_thread_delay(2000);
/* 查看IP服務器 */
if(at_exec_cmd(resp, "AT+NWIP") != RT_EOK)
{
rt_kprintf("AT client send AT+NWIP failed,response error!");
return -RT_ERROR;
}
rt_thread_delay(1000);
//LOG_D("recv:%s,recv line:%d",resp->buf,resp->line_num);
if(at_resp_parse_line_args(resp,2,"%*[^,],%[^,]",&ip)>0)
{
rt_kprintf("ip:%s ",ip);
}
else{
rt_kprintf("NWIP erro");
return -RT_ERROR;
}
/* 連接MQTT服務器*/
if(at_exec_cmd(resp, "AT+NWMQBR=%s,%d","192.168.3.192",1883) != RT_EOK)
{
rt_kprintf("AT client send AT+NWMQBR failed,response error!");
return -RT_ERROR;
}
/* 設置訂閱主題 */
if(at_exec_cmd(resp, "AT+NWMQTS=%d,%s",1,"lugl_sub") != RT_EOK)
{
rt_kprintf("AT client send AT+NWMQTS failed,response error!");
return -RT_ERROR;
}
/* 設置發(fā)布主題 */
if(at_exec_cmd(resp, "AT+NWMQTP=%s","lugl_pub") != RT_EOK)
{
rt_kprintf("AT client send AT+NWMQTP failed,response error!");
return -RT_ERROR;
}
at_client_port_init();
/* 使能MQTT */
if(at_exec_cmd(resp, "AT+NWMQCL=1") != RT_EOK)
{
rt_kprintf("AT client send AT+NWMQCL failed,response error!");
return -RT_ERROR;
}
/* 刪 除 服 務 器 響 應 結(jié) 構(gòu) 體 */
at_delete_resp(resp);
return RT_EOK;
}
hal_entry.c:
extern struct hs300x_device temp_humi_dev;
void hal_entry(void)
{
float temp_f,humi_f;
char mqtt_send_buf[128] = {0};
rt_kprintf("nHello RT-Thread!n");
rt_uint32_t led1_pin = rt_pin_get(LED1_PIN);
at_client_16200_mqtt_init();
while (1)
{
rt_pin_write(led1_pin, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(led1_pin, PIN_LOW);
rt_thread_mdelay(500);
hs300x_read_data(&temp_humi_dev, &humi_f, &temp_f);
rt_sprintf(mqtt_send_buf,"{"temp":"%d.%2d","humi":"%d.%2d"}",(int)temp_f,(int)(temp_f100)%100,(int)humi_f,(int)(humi_f100)%100);
da16200_mqtt_pub(mqtt_send_buf);
// rt_kprintf("HUMI: %d.%2dn", (int)humi_f, (int)(humi_f100)%100);
// rt_kprintf("HUMI: %d.%2dn", (int)temp_f, (int)(temp_f100)%100);
}
}
實現(xiàn)在的效果:
打開mqtt客戶端,收到數(shù)據(jù)
【項目總結(jié)】
作為國產(chǎn)的RT-Thread 實時操作系統(tǒng),生態(tài)系統(tǒng)越來越豐富。同時也有自己的RT-Thread Studio IDE來實現(xiàn)圖形化的配置,特別是集合了RASC的圖形化配置系統(tǒng)。使得入門非常容易。
【有待改善的地方】
1、FSP的更新不向前兼容,導致會出現(xiàn)很多問題,這樣新入門的朋友,可能被難倒在起點之處。
2、包更新后會覆蓋掉原來的一起代碼,如果不做備份,非常麻煩。
總之,通過這次DIY項目,加深了瑞薩+RT-Thread的編程印象,以后會經(jīng)常關(guān)注RTT,同時也會貢獻自己的力量。
-
PIN管
+關(guān)注
關(guān)注
0文章
36瀏覽量
6313 -
回調(diào)函數(shù)
+關(guān)注
關(guān)注
0文章
87瀏覽量
11543 -
MQTT協(xié)議
+關(guān)注
關(guān)注
0文章
97瀏覽量
5349 -
Uart串口
+關(guān)注
關(guān)注
0文章
29瀏覽量
6797 -
RTThread
+關(guān)注
關(guān)注
8文章
132瀏覽量
40811
發(fā)布評論請先 登錄
相關(guān)推薦
評論