由于是基于上一个项目来进行开发,因此不进行时钟配置。(可参考上一章【RA4M2设计挑战赛】2、H3003温湿度传感器使用)
(1)新建线程
(2)将DA16200的WiFi连接和MQTT连接封装成库方便线程使用
以下是封装的文件
hal_wifi.h
/*
* hal_wifi.h
*
* Created on: 2023年2月4日
* Author: MagicKingC
*/
#ifndef HAL_DATA_STRUCT_HAL_WIFI_HAL_WIFI_H_
#define HAL_DATA_STRUCT_HAL_WIFI_HAL_WIFI_H_
#include <stdint.h>
#include <stdbool.h>
enum _AT_CMD_{
ECHO = 0,
RESTART_WIFI,
CONNECT_WIFI,
DISCONNECT_WIFI,
SET_BOOT_MODE,
SET_WIFI_MODE,
ENABLE_MQTT_CLIENT,
SET_MQTT_CLIENT_ID,
};
enum _STA_STATUS{
USE_STA=0,
NO_USE_STA
};
typedef struct _HAL_WIFI_DATA_{
uint8_t *cmd; //命令
uint8_t cmd_len; //命令长度
}HalWiFiData;
extern void InitWiFi(void);
//连接wifi
extern bool ConnectWiFi(char *_ssid,char *_passwd,char *_data);
//断开wifi连接
extern void DisconnectWiFi(void);
//mqtt 客户端连接
extern void MqttClientConnect(char *_host ,uint16_t _port, char *_client_id,uint8_t _qos,char *_user, char* _passwd);
//订阅主题
extern void MqttTopicSub(uint8_t _num, char **_sub_topic);
//主题推送
extern void MqttTopicPub(char *_pub_topic, char* _msg);
#endif /* HAL_DATA_STRUCT_HAL_WIFI_HAL_WIFI_H_ */
hal_wifi.c
/*
* hal_wifi.c
*
* Created on: 2023年2月4日
* Author: MagicKingC
*/
#include "hal_wifi/hal_wifi.h"
#include "FreeRTOS.h"
#include "task.h"
#include "hal_data.h"
#include "debug_log.h"
#include "r_sci_uart.h"
#include "stdio.h"
#include "string.h"
static uint8_t Uart0Buff[256]={0};
static bool Rx_Flage= false;
static uint16_t indexBuff = 0;
void WiFiUart0CallBack(uart_callback_args_t * p_args)
{
switch (p_args->event)
{
case UART_EVENT_RX_CHAR:
{
/* 把串口接收到的数据发送回去 */
Uart0Buff[indexBuff]=(uint8_t )p_args->data;
if(Uart0Buff[indexBuff] == 'K' && indexBuff >= 1){
indexBuff--;
if(Uart0Buff[indexBuff] == 'O')
Rx_Flage = true;
indexBuff++;
}
indexBuff++;
break;
}
case UART_EVENT_TX_COMPLETE:
{
//uart_send_complete_flag = true;
break;
}
default:
break;
}
}
static char CmdData[1024] = {0};
static uint16_t CmdDataLen = 0;
HalWiFiData halWiFiData[]={
[ECHO]={
.cmd = (uint8_t *)"ATE\r\n", //回显
.cmd_len = 5
},
[RESTART_WIFI] = {
.cmd = (uint8_t *)"AT+RESTART\r\n", //重启模块
.cmd_len = 12
},
[CONNECT_WIFI]={
.cmd = (uint8_t *)"AT+WFJAP=",
.cmd_len = 9
},
[DISCONNECT_WIFI]={
.cmd = (uint8_t *)"AT+WFQAP\r\n", //断开wifi连接
.cmd_len = 10
},
[SET_BOOT_MODE]={
.cmd = (uint8_t *)"AT+TMRFNOINIT=0\r\n",//0为正常模式 1为射频测试模式
.cmd_len = 17
},
[SET_WIFI_MODE]={
.cmd = (uint8_t *)"AT+WFMODE=0\r\n",//0为sta模式, 1为ap模式
.cmd_len = 13
},
[ENABLE_MQTT_CLIENT]={
.cmd = (uint8_t *)"AT+NWMQCL=1\r\n",//0关闭mqtt客户端,1打关mqtt客户端
.cmd_len = 13
},
[SET_MQTT_CLIENT_ID]={
.cmd = (uint8_t *)"AT+NWMQCID=",//设置mqtt客户端id
.cmd_len = 13
}
};
static bool SendAndGetData(uint8_t *_cmd, uint16_t _cmd_len,char *_data){
uint8_t contTime = 0;
Rx_Flage= false;
indexBuff = 0;
memset(Uart0Buff,0,sizeof(Uart0Buff));
DebugLog("send_data:%s\n",_cmd);
if( R_SCI_UART_Write(&WiFiUart0_ctrl,_cmd, _cmd_len) == FSP_SUCCESS){
while(true){
contTime++;
vTaskDelay(1000);
if(contTime > 3){
return false;
}
if(Rx_Flage == true){
break;
}
}
DebugLog("ack_data:%s\n",Uart0Buff);
if(_data != NULL)
memcpy(_data, Uart0Buff, indexBuff);
return true;
}
return true;
}
//
void InitWiFi(){
R_SCI_UART_Open(&WiFiUart0_ctrl, &WiFiUart0_cfg);
SendAndGetData(halWiFiData[RESTART_WIFI].cmd, halWiFiData[RESTART_WIFI].cmd_len,NULL);
SendAndGetData(halWiFiData[ECHO].cmd, halWiFiData[ECHO].cmd_len,NULL);
SendAndGetData(halWiFiData[SET_BOOT_MODE].cmd, halWiFiData[SET_BOOT_MODE].cmd_len,NULL);
SendAndGetData(halWiFiData[SET_WIFI_MODE].cmd, halWiFiData[SET_WIFI_MODE].cmd_len,NULL);
}
//连接wifi
bool ConnectWiFi(char *_ssid,char *_passwd,char *_data){
char CmdData[128] = {0};
uint16_t CmdDataLen = 0;
sprintf(CmdData,"%s%s,%d,%d,%s\r\n",(char *)halWiFiData[CONNECT_WIFI].cmd,_ssid,4,1,_passwd);
CmdDataLen = (uint16_t)strlen(CmdData);
SendAndGetData((uint8_t *)CmdData, CmdDataLen,_data);
return true;
}
//断开wifi连接
void DisconnectWiFi(void){
SendAndGetData(halWiFiData[DISCONNECT_WIFI].cmd, halWiFiData[DISCONNECT_WIFI].cmd_len,NULL);
}
//mqtt 客户端连接
void MqttClientConnect(char *_host ,uint16_t _port, char *_client_id,uint8_t _qos,char *_user, char* _passwd){
//设置mqtt客户端id
sprintf(CmdData,"%s%s\r\n",(char *)halWiFiData[SET_MQTT_CLIENT_ID].cmd,_client_id);
CmdDataLen = (uint16_t)strlen(CmdData);
SendAndGetData((uint8_t *)CmdData, CmdDataLen,NULL);
//设置mqtt服务器地址和端口
memset(CmdData,0,sizeof(CmdData));
sprintf(CmdData,"AT+NWMQBR=%s,%d\r\n",_host,_port);
CmdDataLen = (uint16_t)strlen(CmdData);
SendAndGetData((uint8_t *)CmdData, CmdDataLen,NULL);
//设置qos
memset(CmdData,0,sizeof(CmdData));
sprintf(CmdData,"AT+NWMQQOS=%d\r\n",_qos);
CmdDataLen = (uint16_t)strlen(CmdData);
SendAndGetData((uint8_t *)CmdData, CmdDataLen,NULL);
//0关闭tls,1打开
memset(CmdData,0,sizeof(CmdData));
sprintf(CmdData,"AT+NWMQTLS=0\r\n",_qos);
CmdDataLen = (uint16_t)strlen(CmdData);
SendAndGetData((uint8_t *)CmdData, CmdDataLen,NULL);
//进行mqtt客户端连接
SendAndGetData(halWiFiData[ENABLE_MQTT_CLIENT].cmd, halWiFiData[ENABLE_MQTT_CLIENT].cmd_len,NULL);
}
//订阅主题
void MqttTopicSub(uint8_t _num, char **_sub_topic){
uint8_t index = 0;
memset(CmdData,0,sizeof(CmdData));
// sprintf(CmdData,"AT+NWMQTS=%d",_num);
// for(index=0;index<_num;index++){
// sprintf(CmdData,"%s,%s",CmdData,_sub_topic[index]);
// }
// sprintf(CmdData,"%s\r\n",CmdData);
// CmdDataLen = (uint16_t)strlen(CmdData);
// SendAndGetData((uint8_t *)CmdData, CmdDataLen,NULL);
}
//主题推送
void MqttTopicPub(char *_pub_topic, char* _msg){
memset(CmdData,0,sizeof(CmdData));
sprintf(CmdData,"AT+NWMQMSG=%s,%s\r\n",_msg,_pub_topic);
CmdDataLen = (uint16_t)strlen(CmdData);
SendAndGetData((uint8_t *)CmdData, CmdDataLen,NULL);
}
(3)在线程中调用
#include "WiFiThread.h"
#include "debug_log.h"
#include "hal_data_struct/hal_wifi/hal_wifi.h"
/* WiFiThread entry function */
/* pvParameters contains TaskHandle_t */
void WiFiThread_entry(void *pvParameters)
{
static char ackData[1024]={0};
char data_str[128]={0};
FSP_PARAMETER_NOT_USED (pvParameters);
/* TODO: add your own code here */
DebugLog("WiFiThread beginning\n");
InitWiFi();
ConnectWiFi(wifi名,wifi密码,ackData);
MqttClientConnect(mqtt服务器地址,端口号,mqtt id,0,mqtt用户名,mqtt登录密码);
while (1)
{
MqttTopicPub("/iot/pub",data_str);
vTaskDelay (300);
}
}
结果
更多回帖