STM32 and relay module to control smart home appliances
Posted: Wed Sep 22, 2021 9:31 am
This project shows how to use STONE display screen, STM32 MCU, relay module. The aim of the project is to be able to control home appliances through the STONE display as a human-computer interface.
STONE 8-inch TFT LCD

Relay module -8 channels

STM32F411 MCU core board

Connection diagram

GUI design

Source code
The STM32 uses serial port 1 and touch screen communication, the use of baud rate is 115200.
Refresh all display states before MCU initialization
Receive the data sent by the screen to the MCU, and control the status of IO:
Determine the IO's status, and display the current output status feedback screen
Video demo
https://www.youtube.com/watch?v=cMOueXYxGMM
STONE 8-inch TFT LCD
Relay module -8 channels
STM32F411 MCU core board
Connection diagram
GUI design
Source code
The STM32 uses serial port 1 and touch screen communication, the use of baud rate is 115200.
Code: Select all
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();
}
}
The configuration of HAL library GPIO is as follows:
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, A8_Pin|A7_Pin|A6_Pin|A5_Pin, GPIO_PIN_SET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, A4_Pin|A3_Pin|A2_Pin|A1_Pin, GPIO_PIN_SET);
/*Configure GPIO pins : PAPin PAPin PAPin PAPin */
GPIO_InitStruct.Pin = A8_Pin|A7_Pin|A6_Pin|A5_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PBPin PBPin PBPin PBPin */
GPIO_InitStruct.Pin = A4_Pin|A3_Pin|A2_Pin|A1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
Code: Select all
data_send[4]=0x00;//
data_send[5]=0x20;//
data_send[6]=0xB9;//
data_send[7]=0xD8;
data_send[8]=0xB1;
data_send[9]=0xD5;
UART1_Send_Array(data_send,12);//
data_send[5]=0x30;//
UART1_Send_Array(data_send,12);//
data_send[5]=0x40;//
UART1_Send_Array(data_send,12);//
data_send[5]=0x50;//
UART1_Send_Array(data_send,12);//
data_send[5]=0x60;//
UART1_Send_Array(data_send,12);//
data_send[5]=0x70;//
UART1_Send_Array(data_send,12);//
data_send[5]=0x80;//
UART1_Send_Array(data_send,12);//
Code: Select all
if ((char *)buffer != NULL)
{
if (strlen((char *)buffer) == 0)
{
//buffer
}
else//
{
switch(buffer[5])
{
case 0x01 : HAL_GPIO_WritePin(A1_GPIO_Port,A1_Pin,GPIO_PIN_RESET); break;//
case 0x02 : HAL_GPIO_WritePin(A2_GPIO_Port,A2_Pin,GPIO_PIN_RESET); break;//
case 0x03 : HAL_GPIO_WritePin(A3_GPIO_Port,A3_Pin,GPIO_PIN_RESET); break;//
case 0x04 : HAL_GPIO_WritePin(A4_GPIO_Port,A4_Pin,GPIO_PIN_RESET); break;//
case 0x05 : HAL_GPIO_WritePin(A5_GPIO_Port,A5_Pin,GPIO_PIN_RESET); break;//
case 0x06 : HAL_GPIO_WritePin(A6_GPIO_Port,A6_Pin,GPIO_PIN_RESET); break;//
case 0x07 : HAL_GPIO_WritePin(A7_GPIO_Port,A7_Pin,GPIO_PIN_RESET); break;//
case 0x11 : HAL_GPIO_WritePin(A1_GPIO_Port,A1_Pin,GPIO_PIN_SET); break;//
case 0x12 : HAL_GPIO_WritePin(A2_GPIO_Port,A2_Pin,GPIO_PIN_SET); break;//
case 0x13 : HAL_GPIO_WritePin(A3_GPIO_Port,A3_Pin,GPIO_PIN_SET); break;//
case 0x14 : HAL_GPIO_WritePin(A4_GPIO_Port,A4_Pin,GPIO_PIN_SET); break;//
case 0x15 : HAL_GPIO_WritePin(A5_GPIO_Port,A5_Pin,GPIO_PIN_SET); break;//
case 0x16 : HAL_GPIO_WritePin(A6_GPIO_Port,A6_Pin,GPIO_PIN_SET); break;//
case 0x17 : HAL_GPIO_WritePin(A7_GPIO_Port,A7_Pin,GPIO_PIN_SET); break;//
}
}
CLR();//
}
Code: Select all
// Determine the value of IO and display the corresponding status value in the status box if(!HAL_GPIO_ReadPin(A1_GPIO_Port,A1_Pin))//
{//
data_send[4]=0x00;//
data_send[5]=0x20;//
data_send[6]=0xB4;//
data_send[7]=0xF2;
data_send[8]=0xBF;
data_send[9]=0xAA;
UART1_Send_Array(data_send,12);//
}
else
{//
data_send[4]=0x00;//
data_send[5]=0x20;//
data_send[6]=0xB9;//
data_send[7]=0xD8;
data_send[8]=0xB1;
data_send[9]=0xD5;
UART1_Send_Array(data_send,12);//
}
https://www.youtube.com/watch?v=cMOueXYxGMM