/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * *

© Copyright (c) 2024 STMicroelectronics. * All rights reserved.

* * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "adc.h" #include "i2c.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "stdio.h" #include "stm32f1xx_hal.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ #define WIFI_SSID "AILA" #define WIFI_PASSWORD "77777777" #define SERVER_IP "112.74.107.30" #define SERVER_PORT 9008 #define TX_BUFFER_SIZE 512 #define DHT11_PIN GPIO_PIN_0 #define DHT11_PORT GPIOA uint8_t humidity = 0, temperature = 0; void Delay_us(uint16_t us) { uint16_t differ = 0xffff - us - 5; __HAL_TIM_SET_COUNTER(&htim1, differ); HAL_TIM_Base_Start(&htim1); while (differ < 0xffff - 5) { differ = __HAL_TIM_GET_COUNTER(&htim1); } HAL_TIM_Base_Stop(&htim1); } /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ uint8_t txBuffer[TX_BUFFER_SIZE]; void sendATCommand(char *command, uint16_t timeout) { HAL_UART_Transmit(&huart2, (uint8_t *)command, strlen(command), HAL_MAX_DELAY); HAL_Delay(timeout); } void ESP8266_Init() { sendATCommand("AT+RST\r\n", 2000); sendATCommand("AT+CWMODE=1\r\n", 1000); snprintf((char *)txBuffer, TX_BUFFER_SIZE, "AT+CWJAP=\"%s\",\"%s\"\r\n", WIFI_SSID, WIFI_PASSWORD); sendATCommand((char *)txBuffer, 5000); snprintf((char *)txBuffer, TX_BUFFER_SIZE, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", SERVER_IP, SERVER_PORT); sendATCommand((char *)txBuffer, 3000); } void ESP8266_SendData(char *data) { snprintf((char *)txBuffer, TX_BUFFER_SIZE, "AT+CIPSEND=%d\r\n", strlen(data)); sendATCommand((char *)txBuffer, 1000); HAL_UART_Transmit(&huart2, (uint8_t *)data, strlen(data), HAL_MAX_DELAY); HAL_Delay(1000); } /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ uint8_t Data[5] = {0x00, 0x00, 0x00, 0x00, 0x00}; /*------------------------------*/ void DHT_GPIO_SET_OUTPUT(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = GPIO_PIN_0; GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); } void DHT_GPIO_SET_INPUT(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = GPIO_PIN_0; GPIO_InitStructure.Mode = GPIO_MODE_INPUT; GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); } /*------------------------------*/ uint8_t DHT_Read_Byte(void) { uint8_t ReadData = 0; uint8_t temp; uint8_t retry = 0; uint8_t i; for (i = 0; i < 8; i++) { while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0 && retry < 100) { Delay_us(1); retry++; } retry = 0; Delay_us(40); if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 1) temp = 1; else temp = 0; while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 1 && retry < 100) { Delay_us(1); retry++; } retry = 0; ReadData <<= 1; ReadData |= temp; } return ReadData; } /*------------------------------*/ /* uint8_t DHT_Read(void)表达完整时序 */ uint8_t DHT_Read(void) { uint8_t retry = 0; uint8_t i; DHT_GPIO_SET_OUTPUT(); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET); HAL_Delay(18); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET); Delay_us(20); DHT_GPIO_SET_INPUT(); Delay_us(20); if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0) { while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0 && retry < 100) { Delay_us(1); retry++; } retry = 0; while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 1 && retry < 100) { Delay_us(1); retry++; } retry = 0; for (i = 0; i < 5; i++) { Data[i] = DHT_Read_Byte(); } Delay_us(50); } uint32_t sum = Data[0] + Data[1] + Data[2] + Data[3]; // 校验 if ((sum) == Data[4]) return 1; else return 0; } /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ uint32_t adc_value = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* 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_USART1_UART_Init(); MX_USART2_UART_Init(); MX_USART3_UART_Init(); MX_ADC1_Init(); MX_I2C1_Init(); MX_TIM1_Init(); /* USER CODE BEGIN 2 */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); ESP8266_Init(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET); HAL_Delay(1000); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET); HAL_Delay(1000); if (DHT_Read()) { humidity = Data[0]; // 更新湿度 temperature = Data[2]; // 更新温度 } HAL_ADC_Start(&hadc1); // 开始ADC转换 if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK) // 等待转换完成,超时时间10ms { adc_value = HAL_ADC_GetValue(&hadc1); // 获取ADC值 } HAL_ADC_Stop(&hadc1); // 停止ADC char jsonData[TX_BUFFER_SIZE]; snprintf(jsonData, TX_BUFFER_SIZE, "{\"MODEL\":\"AIR100\",\"IMEI\":\"JkyAir000000099\",\"PM25\":\"16\",\"PM1\":\"11\",\"PM10\":\"15\",\"CO2\":\"320\",\"HCHO\":\"%d\",\"TEMP\":\"%d\",\"TVOC\":\"0.06\",\"HUMI\":\"%d\",\"TIME\":\"2019-06-04 11:44:04\",\"VER\":\"0.9\"}", adc_value, temperature, humidity); ESP8266_SendData(jsonData); /* 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}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {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(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ void CE_CO2_nonfud() { } /* 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 */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/