赞
踩
一、硬件准备
我本次使用的是stm32f103c8t6,esp8266,DHT11温湿度传感器模块,还需要在 阿里云上注册账号。接线看我上一个文章。
二、阿里云物联网
上面这些就是创建阿里云物联网平台,接下来就是写代码
三、代码部分
main.c
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "dma.h"
- #include "spi.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- #include "stdio.h"
- #include "string.h"
- #include "dht11.h"
- #include "oled.h"
- #include "cJSON.h"
- #include "time_data.h"
-
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
-
- /* USER CODE END Includes */
-
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
-
- /* USER CODE END PTD */
-
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
-
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
-
- /* USER CODE END PM */
-
- /* Private variables ---------------------------------------------------------*/
-
- /* USER CODE BEGIN PV */
-
- /* USER CODE END PV */
-
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- int WD;
- int SD;
- uint8_t WIFI_Connect[64];
- uint8_t FWQ_Connect[64];
- uint8_t MQTT_Connect[400];
- uint8_t MQTT_ClientId[252];
- uint8_t MQTT_CONN[252];
- uint8_t MQTT_DL[252];
- uint8_t MQTT_WD[128];
- uint8_t MQTT_SD[128];
- /* USER CODE BEGIN PFP */
-
- /* 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_DMA_Init();
- MX_SPI1_Init();
- MX_USART1_UART_Init();
- MX_USART3_UART_Init();
- MX_TIM2_Init();
- /* USER CODE BEGIN 2 */
- DHT11_Init(); //DHT11初始化
- printf("%s\r\n","text");
-
- HAL_UART_Transmit(&huart1,(uint8_t*)AT,strlen(AT),HAL_MAX_DELAY); //发送AT测试
- HAL_Delay(200);
-
- HAL_UART_Transmit(&huart1,(uint8_t*)WiFi_Mode,strlen(WiFi_Mode),HAL_MAX_DELAY); //配置STA模式
- HAL_Delay(200);
-
- sprintf(WIFI_Connect,"AT+CWJAP=\"%s\",\"%s\"\r\n",ssid,pass); //连接wifi
- HAL_UART_Transmit(&huart1,(uint8_t*)WIFI_Connect,strlen(WIFI_Connect),HAL_MAX_DELAY);
- HAL_Delay(1500);
-
- sprintf(FWQ_Connect,"AT+CIPSNTPCFG=\%d,\%d,\"%s\"\r\n",1,8,"ntp1.aliyun.com");
- HAL_UART_Transmit(&huart1,(uint8_t*)FWQ_Connect,strlen(FWQ_Connect),HAL_MAX_DELAY); //查询/设置时区和SNTP服务器
- HAL_Delay(300);
-
- sprintf(MQTT_Connect,"AT+MQTTUSERCFG=\%d,\%d,\"%s\",\"%s\",\"%s\",\%d,\%d,\"%s\"\r\n",0,1,"NULL","物联网平台MQTT参数中的username","物联网平台MQTT参数中的passwd",0,0,"");
- 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">
- HAL_Delay(300);
-
- sprintf(MQTT_ClientId,"AT+MQTTCLIENTID=\%d,\"%s\"\r\n",0,charID); //k11dqdmRz06.DHT11|securemode=2\,signmethod=hmacsha256\,timestamp=1712478216024|
- HAL_UART_Transmit(&huart1,(uint8_t*)MQTT_ClientId,strlen(MQTT_ClientId),HAL_MAX_DELAY);
- HAL_Delay(300);
-
- sprintf(MQTT_CONN,"AT+MQTTCONN=\%d,\"%s\",\%d,\%d\r\n",0,"iot-06z00dg5d86ynoc.mqtt.iothub.aliyuncs.com",1883,1);
- HAL_UART_Transmit(&huart1,(uint8_t*)MQTT_CONN,strlen(MQTT_CONN),HAL_MAX_DELAY);
- HAL_Delay(1000);
-
- sprintf(MQTT_DL,"AT+MQTTSUB=\%d,\"%s\",\%d\r\n",0,"/k11dqdmRz06/DHT11/user/get",1); //顶阅阿里云物联网的DHT11
- HAL_UART_Transmit(&huart1,(uint8_t*)MQTT_DL,strlen(MQTT_DL),HAL_MAX_DELAY);
- HAL_Delay(1000);
-
-
-
- //HAL_UART_Transmit(&huart1,(uint8_t*)mqtt_tc,strlen(mqtt_tc),HAL_MAX_DELAY); //退出
- //HAL_Delay(200);
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
-
-
- uint8_t res=DHT11_Read_Data(&temperature,&humidity);
- if(res==0){
- DHT11_Read_Data(&temperature,&humidity);
- WD=temperature>>8;
- SD=humidity>>8;
- //OLED_ShowNum(30,24,16,WD,2);
- //OLED_ShowNum(30,44,16,SD,2);
- //OLED_Refresh();
- printf("DHT11 temperature=%d.%d \r\n",temperature>>8,temperature&0xff);
- printf("DHT11 humidity=%d.%d%%\r\n",humidity>>8,humidity&0xff);
-
- 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);
- HAL_UART_Transmit(&huart1,(uint8_t*)MQTT_WD,strlen(MQTT_WD),HAL_MAX_DELAY);
- HAL_Delay(300);
-
- 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);
- HAL_UART_Transmit(&huart1,(uint8_t*)MQTT_SD,strlen(MQTT_SD),HAL_MAX_DELAY);
- HAL_Delay(300);
-
- HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_SET);
- HAL_Delay(5000);
- }
-
-
- /* 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 RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
-
- /** Initializes the CPU, AHB and APB buses 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 */
-
- /* USER CODE END 4 */
-
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
-
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
main.h
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.h
- * @brief : Header for main.c file.
- * This file contains the common defines of the application.
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
-
- /* Define to prevent recursive inclusion -------------------------------------*/
- #ifndef __MAIN_H
- #define __MAIN_H
- #define AT "AT\r\n"
- #define WiFi_Mode "AT+CWMODE=1\r\n"
- #define ssid "homi"
- #define pass "xhp5544671"
- #define charID "k11dqdmRz06.DHT11|securemode=2\\,signmethod=hmacsha256\\,timestamp=1712478216024|"
- #define mqtt_tc "AT+MQTTCLEAN=0\r\n"
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f1xx_hal.h"
-
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
-
- /* USER CODE END Includes */
-
- /* Exported types ------------------------------------------------------------*/
- /* USER CODE BEGIN ET */
-
- /* USER CODE END ET */
-
- /* Exported constants --------------------------------------------------------*/
- /* USER CODE BEGIN EC */
-
- /* USER CODE END EC */
-
- /* Exported macro ------------------------------------------------------------*/
- /* USER CODE BEGIN EM */
-
- /* USER CODE END EM */
-
- /* Exported functions prototypes ---------------------------------------------*/
- void Error_Handler(void);
-
- /* USER CODE BEGIN EFP */
-
- /* USER CODE END EFP */
-
- /* Private defines -----------------------------------------------------------*/
- #define LED_Pin GPIO_PIN_2
- #define LED_GPIO_Port GPIOA
- #define DHT11_DATA_Pin GPIO_PIN_3
- #define DHT11_DATA_GPIO_Port GPIOA
- #define OLED_CS_Pin GPIO_PIN_4
- #define OLED_CS_GPIO_Port GPIOA
- #define OLED_RES_Pin GPIO_PIN_0
- #define OLED_RES_GPIO_Port GPIOB
- #define OLED_DC_Pin GPIO_PIN_1
- #define OLED_DC_GPIO_Port GPIOB
- /* USER CODE BEGIN Private defines */
-
- /* USER CODE END Private defines */
-
- #ifdef __cplusplus
- }
- #endif
-
- #endif /* __MAIN_H */
实验结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。