当前位置:   article > 正文

STM32+ESP8266连接阿里云物联网平台

STM32+ESP8266连接阿里云物联网平台

一、硬件准备

我本次使用的是stm32f103c8t6,esp8266,DHT11温湿度传感器模块,还需要在 阿里云上注册账号。接线看我上一个文章。

二、阿里云物联网

上面这些就是创建阿里云物联网平台,接下来就是写代码

三、代码部分

main.c

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "dma.h"
  22. #include "spi.h"
  23. #include "tim.h"
  24. #include "usart.h"
  25. #include "gpio.h"
  26. #include "stdio.h"
  27. #include "string.h"
  28. #include "dht11.h"
  29. #include "oled.h"
  30. #include "cJSON.h"
  31. #include "time_data.h"
  32. /* Private includes ----------------------------------------------------------*/
  33. /* USER CODE BEGIN Includes */
  34. /* USER CODE END Includes */
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* USER CODE BEGIN PTD */
  37. /* USER CODE END PTD */
  38. /* Private define ------------------------------------------------------------*/
  39. /* USER CODE BEGIN PD */
  40. /* USER CODE END PD */
  41. /* Private macro -------------------------------------------------------------*/
  42. /* USER CODE BEGIN PM */
  43. /* USER CODE END PM */
  44. /* Private variables ---------------------------------------------------------*/
  45. /* USER CODE BEGIN PV */
  46. /* USER CODE END PV */
  47. /* Private function prototypes -----------------------------------------------*/
  48. void SystemClock_Config(void);
  49. int WD;
  50. int SD;
  51. uint8_t WIFI_Connect[64];
  52. uint8_t FWQ_Connect[64];
  53. uint8_t MQTT_Connect[400];
  54. uint8_t MQTT_ClientId[252];
  55. uint8_t MQTT_CONN[252];
  56. uint8_t MQTT_DL[252];
  57. uint8_t MQTT_WD[128];
  58. uint8_t MQTT_SD[128];
  59. /* USER CODE BEGIN PFP */
  60. /* USER CODE END PFP */
  61. /* Private user code ---------------------------------------------------------*/
  62. /* USER CODE BEGIN 0 */
  63. /* USER CODE END 0 */
  64. /**
  65. * @brief The application entry point.
  66. * @retval int
  67. */
  68. int main(void)
  69. {
  70. /* USER CODE BEGIN 1 */
  71. /* USER CODE END 1 */
  72. /* MCU Configuration--------------------------------------------------------*/
  73. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  74. HAL_Init();
  75. /* USER CODE BEGIN Init */
  76. /* USER CODE END Init */
  77. /* Configure the system clock */
  78. SystemClock_Config();
  79. /* USER CODE BEGIN SysInit */
  80. /* USER CODE END SysInit */
  81. /* Initialize all configured peripherals */
  82. MX_GPIO_Init();
  83. MX_DMA_Init();
  84. MX_SPI1_Init();
  85. MX_USART1_UART_Init();
  86. MX_USART3_UART_Init();
  87. MX_TIM2_Init();
  88. /* USER CODE BEGIN 2 */
  89. DHT11_Init(); //DHT11初始化
  90. printf("%s\r\n","text");
  91. HAL_UART_Transmit(&huart1,(uint8_t*)AT,strlen(AT),HAL_MAX_DELAY); //发送AT测试
  92. HAL_Delay(200);
  93. HAL_UART_Transmit(&huart1,(uint8_t*)WiFi_Mode,strlen(WiFi_Mode),HAL_MAX_DELAY); //配置STA模式
  94. HAL_Delay(200);
  95. sprintf(WIFI_Connect,"AT+CWJAP=\"%s\",\"%s\"\r\n",ssid,pass); //连接wifi
  96. HAL_UART_Transmit(&huart1,(uint8_t*)WIFI_Connect,strlen(WIFI_Connect),HAL_MAX_DELAY);
  97. HAL_Delay(1500);
  98. sprintf(FWQ_Connect,"AT+CIPSNTPCFG=\%d,\%d,\"%s\"\r\n",1,8,"ntp1.aliyun.com");
  99. HAL_UART_Transmit(&huart1,(uint8_t*)FWQ_Connect,strlen(FWQ_Connect),HAL_MAX_DELAY); //查询/设置时区和SNTP服务器
  100. HAL_Delay(300);
  101. sprintf(MQTT_Connect,"AT+MQTTUSERCFG=\%d,\%d,\"%s\",\"%s\",\"%s\",\%d,\%d,\"%s\"\r\n",0,1,"NULL","物联网平台MQTT参数中的username","物联网平台MQTT参数中的passwd",0,0,"");
  102. HAL_UART_Transmit(&huart1,(uint8_t*)MQTT_Connect,strlen(MQTT_Connect),HAL_MAX_DELAY); //设置MQTT用户的属性 AT+MQTTUSERCFG=<LinkID>,<scheme>,<"client_id">,<"username">,<"password">,<cert_key_ID>,<CA_ID>,<"path">
  103. HAL_Delay(300);
  104. sprintf(MQTT_ClientId,"AT+MQTTCLIENTID=\%d,\"%s\"\r\n",0,charID); //k11dqdmRz06.DHT11|securemode=2\,signmethod=hmacsha256\,timestamp=1712478216024|
  105. HAL_UART_Transmit(&huart1,(uint8_t*)MQTT_ClientId,strlen(MQTT_ClientId),HAL_MAX_DELAY);
  106. HAL_Delay(300);
  107. sprintf(MQTT_CONN,"AT+MQTTCONN=\%d,\"%s\",\%d,\%d\r\n",0,"iot-06z00dg5d86ynoc.mqtt.iothub.aliyuncs.com",1883,1);
  108. HAL_UART_Transmit(&huart1,(uint8_t*)MQTT_CONN,strlen(MQTT_CONN),HAL_MAX_DELAY);
  109. HAL_Delay(1000);
  110. sprintf(MQTT_DL,"AT+MQTTSUB=\%d,\"%s\",\%d\r\n",0,"/k11dqdmRz06/DHT11/user/get",1); //顶阅阿里云物联网的DHT11
  111. HAL_UART_Transmit(&huart1,(uint8_t*)MQTT_DL,strlen(MQTT_DL),HAL_MAX_DELAY);
  112. HAL_Delay(1000);
  113. //HAL_UART_Transmit(&huart1,(uint8_t*)mqtt_tc,strlen(mqtt_tc),HAL_MAX_DELAY); //退出
  114. //HAL_Delay(200);
  115. /* USER CODE END 2 */
  116. /* Infinite loop */
  117. /* USER CODE BEGIN WHILE */
  118. while (1)
  119. {
  120. uint8_t res=DHT11_Read_Data(&temperature,&humidity);
  121. if(res==0){
  122. DHT11_Read_Data(&temperature,&humidity);
  123. WD=temperature>>8;
  124. SD=humidity>>8;
  125. //OLED_ShowNum(30,24,16,WD,2);
  126. //OLED_ShowNum(30,44,16,SD,2);
  127. //OLED_Refresh();
  128. printf("DHT11 temperature=%d.%d \r\n",temperature>>8,temperature&0xff);
  129. printf("DHT11 humidity=%d.%d%%\r\n",humidity>>8,humidity&0xff);
  130. sprintf(MQTT_WD,"AT+MQTTPUB=\%d,\"%s\",\"{\\\"params\\\":{\\\"Humi\\\":\%d}}\",\%d,\%d\r\n",0,"/sys/k11dqdmRz06/DHT11/thing/event/property/post",SD,0,0);
  131. HAL_UART_Transmit(&huart1,(uint8_t*)MQTT_WD,strlen(MQTT_WD),HAL_MAX_DELAY);
  132. HAL_Delay(300);
  133. sprintf(MQTT_SD,"AT+MQTTPUB=\%d,\"%s\",\"{\\\"params\\\":{\\\"temp\\\":\%d}}\",\%d,\%d\r\n",0,"/sys/k11dqdmRz06/DHT11/thing/event/property/post",WD,0,0);
  134. HAL_UART_Transmit(&huart1,(uint8_t*)MQTT_SD,strlen(MQTT_SD),HAL_MAX_DELAY);
  135. HAL_Delay(300);
  136. HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_SET);
  137. HAL_Delay(5000);
  138. }
  139. /* USER CODE END WHILE */
  140. /* USER CODE BEGIN 3 */
  141. }
  142. /* USER CODE END 3 */
  143. }
  144. /**
  145. * @brief System Clock Configuration
  146. * @retval None
  147. */
  148. void SystemClock_Config(void)
  149. {
  150. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  151. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  152. /** Initializes the RCC Oscillators according to the specified parameters
  153. * in the RCC_OscInitTypeDef structure.
  154. */
  155. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  156. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  157. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  158. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  159. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  160. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  161. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  162. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  163. {
  164. Error_Handler();
  165. }
  166. /** Initializes the CPU, AHB and APB buses clocks
  167. */
  168. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  169. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  170. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  171. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  172. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  173. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  174. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  175. {
  176. Error_Handler();
  177. }
  178. }
  179. /* USER CODE BEGIN 4 */
  180. /* USER CODE END 4 */
  181. /**
  182. * @brief This function is executed in case of error occurrence.
  183. * @retval None
  184. */
  185. void Error_Handler(void)
  186. {
  187. /* USER CODE BEGIN Error_Handler_Debug */
  188. /* User can add his own implementation to report the HAL error return state */
  189. __disable_irq();
  190. while (1)
  191. {
  192. }
  193. /* USER CODE END Error_Handler_Debug */
  194. }
  195. #ifdef USE_FULL_ASSERT
  196. /**
  197. * @brief Reports the name of the source file and the source line number
  198. * where the assert_param error has occurred.
  199. * @param file: pointer to the source file name
  200. * @param line: assert_param error line source number
  201. * @retval None
  202. */
  203. void assert_failed(uint8_t *file, uint32_t line)
  204. {
  205. /* USER CODE BEGIN 6 */
  206. /* User can add his own implementation to report the file name and line number,
  207. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  208. /* USER CODE END 6 */
  209. }
  210. #endif /* USE_FULL_ASSERT */

main.h

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.h
  5. * @brief : Header for main.c file.
  6. * This file contains the common defines of the application.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Define to prevent recursive inclusion -------------------------------------*/
  21. #ifndef __MAIN_H
  22. #define __MAIN_H
  23. #define AT "AT\r\n"
  24. #define WiFi_Mode "AT+CWMODE=1\r\n"
  25. #define ssid "homi"
  26. #define pass "xhp5544671"
  27. #define charID "k11dqdmRz06.DHT11|securemode=2\\,signmethod=hmacsha256\\,timestamp=1712478216024|"
  28. #define mqtt_tc "AT+MQTTCLEAN=0\r\n"
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "stm32f1xx_hal.h"
  34. /* Private includes ----------------------------------------------------------*/
  35. /* USER CODE BEGIN Includes */
  36. /* USER CODE END Includes */
  37. /* Exported types ------------------------------------------------------------*/
  38. /* USER CODE BEGIN ET */
  39. /* USER CODE END ET */
  40. /* Exported constants --------------------------------------------------------*/
  41. /* USER CODE BEGIN EC */
  42. /* USER CODE END EC */
  43. /* Exported macro ------------------------------------------------------------*/
  44. /* USER CODE BEGIN EM */
  45. /* USER CODE END EM */
  46. /* Exported functions prototypes ---------------------------------------------*/
  47. void Error_Handler(void);
  48. /* USER CODE BEGIN EFP */
  49. /* USER CODE END EFP */
  50. /* Private defines -----------------------------------------------------------*/
  51. #define LED_Pin GPIO_PIN_2
  52. #define LED_GPIO_Port GPIOA
  53. #define DHT11_DATA_Pin GPIO_PIN_3
  54. #define DHT11_DATA_GPIO_Port GPIOA
  55. #define OLED_CS_Pin GPIO_PIN_4
  56. #define OLED_CS_GPIO_Port GPIOA
  57. #define OLED_RES_Pin GPIO_PIN_0
  58. #define OLED_RES_GPIO_Port GPIOB
  59. #define OLED_DC_Pin GPIO_PIN_1
  60. #define OLED_DC_GPIO_Port GPIOB
  61. /* USER CODE BEGIN Private defines */
  62. /* USER CODE END Private defines */
  63. #ifdef __cplusplus
  64. }
  65. #endif
  66. #endif /* __MAIN_H */

实验结果

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号