项目要求
- SHT20检测温湿度, 将温湿度的数值上传到OLCD屏
- 当湿度高于设置阈值, 控制风扇排风
- 当湿度低于设置阈值, 控制马达震动模拟加湿过程
- 通过按键,可以修改阈值(阈值信息在OLCD显示)
- STM32 与 ESP8266 基于 TCP通信
- 实现TCP服务器(Linux端)控制加湿器, 当STM32接收到:
FAN_ON, 控制风扇旋转
FAN_OFF,控制风扇停止
MOTOR_ON, 控制马达震动
MOTOR_OFF,停止马达震动
HUM:(阈值), TCP服务器修改湿度阈值
TUM:(阈值), TCP服务器修改湿度阈值 - STM32 每隔2s 将SHT20数据 上传到 TCP服务器(Linux端), Linux端可以将SHT20数据上传的Sqlite数据库中
- STM32可以向ESP8266发送指令 "CLEAR" : 清除TCP服务器SQLITE数据库中的数据
代码
cpp
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();
/* Configure the System Power */
SystemPower_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
MX_SPI1_Init();
MX_UART5_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
float tep,hum;
uint32_t val;
char distephum_str[50];
char hummax_str[20];
HAL_TIM_Base_Start_IT(&htim3);
//开启接受中断
HAL_UART_Receive_IT(&huart5,buf,sizeof(buf));
//开启空闲中断
__HAL_UART_ENABLE_IT(&huart5,UART_IT_IDLE);
//wifi模式
uint32_t ret =wifi_config("AT",100);
if(ret == -1)
{
u1_printf("AT is err\n");
}
ret =wifi_config("AT+CWMOD=1",100);
if(ret == -1)
{
u1_printf("CWMOD is err\n");
}
ret =wifi_connect("HQYJ-SCB-2.4","hqyj666666");
if(ret == -1)
{
u1_printf("wifi is err\n");
}
ret =wifi_connect_tcp("192.168.0.103",1111);
if(ret == -1)
{
u1_printf("tcp is err\n");
}
ret =wifi_config("AT+CIPMODE=1",10);
if(ret == -1)
{
u1_printf("CIPMODE is err\n");
}
ret =wifi_config("AT+CIPSEND",10);
if(ret == -1)
{
u1_printf("CIPSEND is err\n");
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* 外设初始化 */
ILI9341_Init();
ILI9341_Clear(RED);
uint32_t max;
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//TCP_send("hello\n");
HAL_Delay(50);
Recv_FromTCP();//接收指令并控制
//读取温湿度
val= SHT20_Read_huntep(&tep,&hum);
sprintf(hummax_str, "hum_max = %.2f ",(float)hum_max);
// 将浮点数转换为字符串
sprintf(distephum_str, "tep = %.2f , hum =%.2f", tep,hum);
if(hummax_plus_flag ==1)
{
hummax_plus_flag=0;
hum_max+=2;
}
if(hummax_minus_flag ==1)
{
hummax_minus_flag=0;
hum_max-=2;
}
//1.将温湿度的数值发送给服务器
if(sendtoTCP_flag ==1)
{
sendtoTCP_flag=0;
TCP_send(distephum_str);
}
//1.将温湿度的数值上传到OLCD屏
Gui_DrawFont_GBK16(0, 0, BLUE, RED,(unsigned char*)hummax_str);
Gui_DrawFont_GBK16(0, 20, BLUE, RED,(unsigned char*)distephum_str);
//当2当湿度高于设置阈值, 控制风扇排风
//3.当湿度低于设置阈值, 控制马达震动模拟加湿过程
if(hum>=hum_max)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,0);
}else
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,0);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,1);
}
HAL_Delay(1000);
}
/* USER CODE END 3 */
}