实现了WF24对WIFI的连接和MQTT的订阅与发布
This commit is contained in:
@@ -60,7 +60,7 @@ const osThreadAttr_t defaultTask_attributes = {
|
||||
osThreadId_t wifi_mqttHandle;
|
||||
const osThreadAttr_t wifi_mqtt_attributes = {
|
||||
.name = "wifi_mqtt",
|
||||
.stack_size = 1024 * 4,
|
||||
.stack_size = 3000 * 4,
|
||||
.priority = (osPriority_t) osPriorityHigh,
|
||||
};
|
||||
|
||||
@@ -70,7 +70,7 @@ const osThreadAttr_t wifi_mqtt_attributes = {
|
||||
/* USER CODE END FunctionPrototypes */
|
||||
|
||||
void StartDefaultTask(void *argument);
|
||||
void wifi_task_mqtt(void *argument);
|
||||
extern void wifi_task_mqtt(void *argument);
|
||||
|
||||
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
|
||||
|
||||
@@ -133,72 +133,11 @@ void StartDefaultTask(void *argument)
|
||||
for (;;) {
|
||||
|
||||
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
|
||||
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
|
||||
HAL_GPIO_TogglePin(MOTOR_GPIO_Port, MOTOR_Pin);
|
||||
osDelay(1000);
|
||||
}
|
||||
/* USER CODE END StartDefaultTask */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN Header_wifi_task_mqtt */
|
||||
/**
|
||||
* @brief Function implementing the wifi_mqtt thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_wifi_task_mqtt */
|
||||
void wifi_task_mqtt(void *argument)
|
||||
{
|
||||
/* USER CODE BEGIN wifi_task_mqtt */
|
||||
WIFI_RECV_DMA_Init(); // 初始化 WiFi DMA 接收
|
||||
|
||||
/* 等待模块启动 */
|
||||
elog_i(TAG, "Waiting for WiFi module to start...");
|
||||
osDelay(3000);
|
||||
|
||||
/* 连接MQTT服务器 */
|
||||
uint8_t connect_success = WIFI_Connect_MQTT(
|
||||
"ChinaNet_WBH", // WiFi名称
|
||||
"88888888", // WiFi密码
|
||||
"broker.emqx.io", // MQTT服务器
|
||||
1883, // MQTT端口
|
||||
"STM32_MQTT", // 客户端ID
|
||||
NULL, // MQTT用户名(NULL表示无需认证)
|
||||
NULL, // MQTT密码(NULL表示无需认证)
|
||||
"pet/control" // 订阅主题
|
||||
);
|
||||
|
||||
|
||||
if (connect_success) {
|
||||
elog_i(TAG, "MQTT connection successful!");
|
||||
} else {
|
||||
elog_e(TAG, "MQTT connection failed!");
|
||||
// 可以在这里添加失败处理,如重启模块或重试
|
||||
}
|
||||
|
||||
uint8_t rx_data[WIFI_RX_BUF_SIZE];
|
||||
/* Infinite loop */
|
||||
for (;;) {
|
||||
/* 检查并处理接收数据 */
|
||||
if (wifi.rx_flag) {
|
||||
int len = WIFI_Get_Received_Data(rx_data, sizeof(rx_data));
|
||||
if (len > 0) {
|
||||
// 处理 WiFi 返回数据
|
||||
elog_d(TAG, "WiFi response: %s", rx_data);
|
||||
|
||||
// 检查MQTT消息
|
||||
if (strstr((char*)rx_data, "+MQTTPUBLISH")) {
|
||||
elog_i(TAG, "Received MQTT message!");
|
||||
// TODO: 解析并处理MQTT消息
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
osDelay(100);
|
||||
}
|
||||
/* USER CODE END wifi_task_mqtt */
|
||||
}
|
||||
|
||||
/* Private application code --------------------------------------------------*/
|
||||
/* USER CODE BEGIN Application */
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ void MX_GPIO_Init(void)
|
||||
|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOB, LED2_Pin|LED1_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOB, LED2_Pin|LED1_Pin, GPIO_PIN_SET);
|
||||
|
||||
/*Configure GPIO pins : KEY1_Pin KEY2_Pin KEY3_Pin KEY3C3_Pin */
|
||||
GPIO_InitStruct.Pin = KEY1_Pin|KEY2_Pin|KEY3_Pin|KEY3C3_Pin;
|
||||
|
||||
@@ -182,7 +182,7 @@ void SystemClock_Config(void)
|
||||
|
||||
/**
|
||||
* @brief Period elapsed callback in non blocking mode
|
||||
* @note This function is called when TIM8 interrupt took place, inside
|
||||
* @note This function is called when TIM5 interrupt took place, inside
|
||||
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
|
||||
* a global variable "uwTick" used as application time base.
|
||||
* @param htim : TIM handle
|
||||
@@ -193,7 +193,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
||||
/* USER CODE BEGIN Callback 0 */
|
||||
|
||||
/* USER CODE END Callback 0 */
|
||||
if (htim->Instance == TIM8) {
|
||||
if (htim->Instance == TIM5) {
|
||||
HAL_IncTick();
|
||||
}
|
||||
/* USER CODE BEGIN Callback 1 */
|
||||
|
||||
@@ -25,13 +25,13 @@
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
TIM_HandleTypeDef htim8;
|
||||
TIM_HandleTypeDef htim5;
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void TIM8_IRQHandler(void);
|
||||
void TIM5_IRQHandler(void);
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief This function configures the TIM8 as a time base source.
|
||||
* @brief This function configures the TIM5 as a time base source.
|
||||
* The time source is configured to have 1ms time base with a dedicated
|
||||
* Tick interrupt priority.
|
||||
* @note This function is called automatically at the beginning of program after
|
||||
@@ -42,55 +42,64 @@ void TIM8_IRQHandler(void);
|
||||
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
|
||||
{
|
||||
RCC_ClkInitTypeDef clkconfig;
|
||||
uint32_t uwTimclock = 0U;
|
||||
uint32_t uwTimclock, uwAPB1Prescaler = 0U;
|
||||
|
||||
uint32_t uwPrescalerValue = 0U;
|
||||
uint32_t pFLatency;
|
||||
|
||||
HAL_StatusTypeDef status = HAL_OK;
|
||||
|
||||
/* Enable TIM8 clock */
|
||||
__HAL_RCC_TIM8_CLK_ENABLE();
|
||||
/* Enable TIM5 clock */
|
||||
__HAL_RCC_TIM5_CLK_ENABLE();
|
||||
|
||||
/* Get clock configuration */
|
||||
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
|
||||
|
||||
/* Compute TIM8 clock */
|
||||
uwTimclock = HAL_RCC_GetPCLK2Freq();
|
||||
/* Get APB1 prescaler */
|
||||
uwAPB1Prescaler = clkconfig.APB1CLKDivider;
|
||||
/* Compute TIM5 clock */
|
||||
if (uwAPB1Prescaler == RCC_HCLK_DIV1)
|
||||
{
|
||||
uwTimclock = HAL_RCC_GetPCLK1Freq();
|
||||
}
|
||||
else
|
||||
{
|
||||
uwTimclock = 2UL * HAL_RCC_GetPCLK1Freq();
|
||||
}
|
||||
|
||||
/* Compute the prescaler value to have TIM8 counter clock equal to 1MHz */
|
||||
/* Compute the prescaler value to have TIM5 counter clock equal to 1MHz */
|
||||
uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
|
||||
|
||||
/* Initialize TIM8 */
|
||||
htim8.Instance = TIM8;
|
||||
/* Initialize TIM5 */
|
||||
htim5.Instance = TIM5;
|
||||
|
||||
/* Initialize TIMx peripheral as follow:
|
||||
|
||||
+ Period = [(TIM8CLK/1000) - 1]. to have a (1/1000) s time base.
|
||||
+ Period = [(TIM5CLK/1000) - 1]. to have a (1/1000) s time base.
|
||||
+ Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
|
||||
+ ClockDivision = 0
|
||||
+ Counter direction = Up
|
||||
*/
|
||||
htim8.Init.Period = (1000000U / 1000U) - 1U;
|
||||
htim8.Init.Prescaler = uwPrescalerValue;
|
||||
htim8.Init.ClockDivision = 0;
|
||||
htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
htim5.Init.Period = (1000000U / 1000U) - 1U;
|
||||
htim5.Init.Prescaler = uwPrescalerValue;
|
||||
htim5.Init.ClockDivision = 0;
|
||||
htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
|
||||
status = HAL_TIM_Base_Init(&htim8);
|
||||
status = HAL_TIM_Base_Init(&htim5);
|
||||
if (status == HAL_OK)
|
||||
{
|
||||
/* Start the TIM time Base generation in interrupt mode */
|
||||
status = HAL_TIM_Base_Start_IT(&htim8);
|
||||
status = HAL_TIM_Base_Start_IT(&htim5);
|
||||
if (status == HAL_OK)
|
||||
{
|
||||
/* Enable the TIM8 global Interrupt */
|
||||
HAL_NVIC_EnableIRQ(TIM8_UP_IRQn);
|
||||
/* Enable the TIM5 global Interrupt */
|
||||
HAL_NVIC_EnableIRQ(TIM5_IRQn);
|
||||
/* Configure the SysTick IRQ priority */
|
||||
if (TickPriority < (1UL << __NVIC_PRIO_BITS))
|
||||
{
|
||||
/* Configure the TIM IRQ priority */
|
||||
HAL_NVIC_SetPriority(TIM8_UP_IRQn, TickPriority, 0U);
|
||||
HAL_NVIC_SetPriority(TIM5_IRQn, TickPriority, 0U);
|
||||
uwTickPrio = TickPriority;
|
||||
}
|
||||
else
|
||||
@@ -106,25 +115,25 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
|
||||
|
||||
/**
|
||||
* @brief Suspend Tick increment.
|
||||
* @note Disable the tick increment by disabling TIM8 update interrupt.
|
||||
* @note Disable the tick increment by disabling TIM5 update interrupt.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_SuspendTick(void)
|
||||
{
|
||||
/* Disable TIM8 update Interrupt */
|
||||
__HAL_TIM_DISABLE_IT(&htim8, TIM_IT_UPDATE);
|
||||
/* Disable TIM5 update Interrupt */
|
||||
__HAL_TIM_DISABLE_IT(&htim5, TIM_IT_UPDATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resume Tick increment.
|
||||
* @note Enable the tick increment by Enabling TIM8 update interrupt.
|
||||
* @note Enable the tick increment by Enabling TIM5 update interrupt.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_ResumeTick(void)
|
||||
{
|
||||
/* Enable TIM8 Update interrupt */
|
||||
__HAL_TIM_ENABLE_IT(&htim8, TIM_IT_UPDATE);
|
||||
/* Enable TIM5 Update interrupt */
|
||||
__HAL_TIM_ENABLE_IT(&htim5, TIM_IT_UPDATE);
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@
|
||||
extern DMA_HandleTypeDef hdma_usart1_tx;
|
||||
extern DMA_HandleTypeDef hdma_usart1_rx;
|
||||
extern UART_HandleTypeDef huart1;
|
||||
extern TIM_HandleTypeDef htim8;
|
||||
extern TIM_HandleTypeDef htim5;
|
||||
|
||||
/* USER CODE BEGIN EV */
|
||||
|
||||
@@ -210,17 +210,17 @@ void USART1_IRQHandler(void)
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles TIM8 update interrupt.
|
||||
* @brief This function handles TIM5 global interrupt.
|
||||
*/
|
||||
void TIM8_UP_IRQHandler(void)
|
||||
void TIM5_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM8_UP_IRQn 0 */
|
||||
/* USER CODE BEGIN TIM5_IRQn 0 */
|
||||
|
||||
/* USER CODE END TIM8_UP_IRQn 0 */
|
||||
HAL_TIM_IRQHandler(&htim8);
|
||||
/* USER CODE BEGIN TIM8_UP_IRQn 1 */
|
||||
/* USER CODE END TIM5_IRQn 0 */
|
||||
HAL_TIM_IRQHandler(&htim5);
|
||||
/* USER CODE BEGIN TIM5_IRQn 1 */
|
||||
|
||||
/* USER CODE END TIM8_UP_IRQn 1 */
|
||||
/* USER CODE END TIM5_IRQn 1 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
Reference in New Issue
Block a user