自己根据MQTT协议报文在STM32上成功运行。
单片机源程序如下:
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "string.h"
- #include "xianshispi.h"
- #include "MQTT_8266.H"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* 大于printf重定向 */
- uint8_t USART1_TX_BUF[200];
- #define u1_printf(...) HAL_UART_Transmit(&huart1,USART1_TX_BUF,sprintf((char *)USART1_TX_BUF,__VA_ARGS__),0xffff)
-
- uint8_t USART2_TX_BUF[200];
- #define u2_printf(...) HAL_UART_Transmit(&huart2,USART2_TX_BUF,sprintf((char *)USART2_TX_BUF,__VA_ARGS__),0xffff)
- /* esp8266加入的WIFI(热点)名称和密码 */
- #define WIFI_Name "EnHahaha7"
- #define WIFI_Key "hjj1234567890"
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- uint8_t Uart2_RX_Buf,WIFI_IP[15];
- uint8_t num,Buf[150],time;
- extern uint8_t Connect_Flag;
- extern uint16_t MQTT_PING_Time; //PING报文计数变量
- uint8_t PING_Flag;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* 连接WIFI程序 */
- void ESP_Connect_WIFI()
- {
- char *Start;
- char *End;
- u1_printf("正在连接WIFI...\r\n");
-
- u2_printf("AT\r\n"); //测试AT启动
- HAL_Delay(300);
- u2_printf("AT+CWMODE=1\r\n"); //设置esp8266 STA模式
- HAL_Delay(300);
- u2_printf("AT+CWJAP_CUR=\"%s\",\"%s\"\r\n",WIFI_Name,WIFI_Key); //临时接入WIFI(热点)
- HAL_Delay(11000);
- u2_printf("AT+CIFSR\r\n"); //获取IP地址
- HAL_Delay(300);
-
- if(strstr((char *)Buf,"\"") != NULL) //软检测到IP地址,OLED显示IP地址
- {
- //显示IP地址
- OLED_ShowString(0,0,"IP:",1);
- Start = strstr((char *)Buf,"\"") + 1;
- End = strstr(Start,"\"");
- memcpy(WIFI_IP,Start,End - Start);
- WIFI_IP[End - Start] = '\0';
- OLED_ShowString(0,2,WIFI_IP,1);
-
- //串口1打印热点名、IP地址
- u1_printf("WIFI Name:%s\nWIFI IP:%s\r\n",WIFI_Name,WIFI_IP);
- }
- else u1_printf("WIFI连接失败");
- }
-
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
-
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- MX_USART2_UART_Init();
- MX_TIM1_Init();
- /* USER CODE BEGIN 2 */
- HAL_UART_Receive_IT(&huart2,&Uart2_RX_Buf,1); //开启串口2接收中断
- HAL_TIM_Base_Start_IT(&htim1); //开启T1中断
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- OLED_Init(); //OLED初始化
-
- ESP_Connect_WIFI(); //ESP8266连接WIFI,并显示在OLED
-
- Connect_Sever(); //TCP连接服务器
- MQTT_Conect(); //连接云平台
-
- MQTT_SUBSCRIBE(); //订阅主题
- // MQTT_PUBLISH("HJJ"); //发送消息
- while (1)
- {
- if(PING_Flag == 1)
- {
- PING_Flag=0;
- MQTT_PING(); //发送心跳包
- }
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)//串口接收中断回调函数
- {
- if(huart == &huart2)
- {
- HAL_UART_Receive_IT(&huart2,&Uart2_RX_Buf,1);//开启串口2接收中断
- Buf[num] = Uart2_RX_Buf;//将串口2接收到的数据存入Buf缓存区
- num++;time=0;
- }
- }
- //uint8_t i;
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//定时器中断回调函数
- {
- time++;
- if(time>=20 && num>0)
- {
- Buf[num] = '\0';
- MQTT_RxData((char *)Buf,num); //判断串口2接收到的数据
- // for(i=0;i<num;i++) u1_printf("%c",Buf[i]); //串口1打印串口2接收的数据
- time=0;num=0;
- }
-
- if(Connect_Flag == 1) //只有在连接了云平台且订阅了主题才需要发送PING报文
- {
- MQTT_PING_Time++; //每隔10S发送一次心跳包,防止服务器将客户端踢走
- if(MQTT_PING_Time >= 40000)
- {
- PING_Flag=1;
- MQTT_PING_Time=0;
- }
- }
- }
- /* USER CODE END 4 */
- /**
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
MQTT_Demo.7z
(4.18 MB, 下载次数: 120)
|