使用STM32CubeIDE编写基于stm32单片机的DTH11温湿度传感器和ESP-01S模块的代码
时间: 2024-03-10 22:48:19 浏览: 74
物联网项目实战开发之基于STM32的DHT11温湿度传感器测量程序代码
5星 · 资源好评率100%
好的,以下是基于STM32CubeIDE编写DTH11温湿度传感器和ESP-01S模块的代码:
```c
#include "main.h"
#include "dht11.h"
#include "esp8266.h"
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
while (1)
{
uint8_t dht11_data[5];
if(DHT11_Read_Data(dht11_data) == DHT11_OK)
{
char buffer[100];
sprintf(buffer,"Temperature: %d.%d Humidity: %d.%d\r\n",dht11_data[2],dht11_data[3],dht11_data[0],dht11_data[1]);
HAL_UART_Transmit(&huart1,(uint8_t*)buffer,strlen(buffer),100);
ESP8266_Send_AT_Command("AT+CIPSTART=\"TCP\",\"192.168.1.2\",80");
ESP8266_Send_AT_Command("AT+CIPSEND=50");
sprintf(buffer,"GET /esp8266.php?temperature=%d.%d&humidity=%d.%d\r\n",dht11_data[2],dht11_data[3],dht11_data[0],dht11_data[1]);
HAL_UART_Transmit(&huart1,(uint8_t*)buffer,strlen(buffer),100);
HAL_Delay(1000);
}
else
{
HAL_UART_Transmit(&huart1,(uint8_t*)"DHT11 Read Error\r\n",strlen("DHT11 Read Error\r\n"),100);
}
}
}
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_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
```
在以上代码中,我们首先进行了一些头文件和宏定义的引用和声明,然后在main函数中,我们首先进行了DHT11传感器数据的读取。如果读取成功,则会通过串口发送传感器的温度和湿度数据,并且通过ESP-01S模块发送这些数据到服务器。最后,我们在while循环中添加了一个延时函数,以便等待下一次的读取。
需要注意的是,为了使DHT11传感器和ESP-01S模块能够正常工作,我们需要引用dht11.h和esp8266.h这两个头文件,同时也需要提前进行相应的初始化操作。
阅读全文