简介
电脑搭建一个TCP Server, ESP8266 串口设置好透传模式, 再由TCP Server发送指令控制灯的亮灭;
开灯指令: led_on+回车 ; 关灯指令: led_off+回车
主芯片: STM32L431RCT6
LED : PC13 \ 推挽输出即可 \ 高电平点亮
串口: Usart1 / LPUART
ESP8266 : 型号ESP-12F
步骤
设置ESP8266 透传
参考 [ESP8266 TCP/串口透传](https://blog.csdn.net/halo_hsuh/article/details/135134634?spm=1001.2014.3001.5501)
STM32项目创建
参考 BearPi Std 板从入门到放弃 - 引气入体篇(1)(由零创建工程到点亮LED)
初始化与ESP8266的串口LPUART1
开启中断
添加用户代码
/*
on :
0 : OFF
1 : ON
*/
void User_LED_Set(int on)
{
HAL_GPIO_WritePin(USER_LED_GPIO_Port, USER_LED_Pin, on ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
void Command_Handle(const uint8_t *buf, int length)
{
printf ("COMMAND HANDLE\r\n");
if ( '\r' == buf[length-1] )
{
length -= 1;
}
if (0 == strncmp("led_on", (const char *)buf, length))
{
HAL_GPIO_WritePin(USER_LED_GPIO_Port, USER_LED_Pin, GPIO_PIN_SET);
printf ("led_on!\r\n");
}
else if (0 == strncmp("led_off", (const char *)buf, length))
{
HAL_GPIO_WritePin(USER_LED_GPIO_Port, USER_LED_Pin, GPIO_PIN_RESET);
printf ("led_off!\r\n");
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if ( huart->Instance == LPUART1 )
{
if ( '\n' == Cmd_Buffer[Cmd_Index] )
{
Command_Handle(Cmd_Buffer, Cmd_Index);
Cmd_Index = 0;
}
else
{
++Cmd_Index;
}
HAL_UART_Receive_IT(&hlpuart1, &Cmd_Buffer[Cmd_Index], 1);
}
}
int fputc(int ch, FILE *f)
{
uint8_t temp[1] = {ch};
{
HAL_UART_Transmit(&huart1, temp, 1, 2);
}
return HAL_OK;
}
int main(void)
{
...
HAL_UART_Receive_IT(&hlpuart1, &Cmd_Buffer[Cmd_Index], 1); // 开启接收
while(1)
{
HAL_Delay(1000); //
}
...
}
代码
完整代码