当前位置:   article > 正文

6.编写初步程序【七天物联网智能家居训练营】_7天物联网智能家居代码

7天物联网智能家居代码

本文是百问网七天物联网智能家居训练营学习笔记,官网链接

1 程序流程回顾

先来回顾下TCP连接的流程:
在这里插入图片描述
下面看下UDP连接的流程:
在这里插入图片描述
整个程序的框架如下:
在这里插入图片描述

2 代码实现

这里我们使用串口2来操作,下面先实现串口2的基本操作:

static uint8_t rx_len;
static uint8_t rx_data;
static uint8_t usart_rx_buf[200];

// 串口2启动接收
void USART2_StartRx(void)
{
	HAL_UART_Receive_IT(&huart2, (uint8_t*)&rx_data, 1);
}

// 串口2中断回调函数(保存每次接收的数据,并启动下一次接收)
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	usart_rx_buf[rx_len%200] = rx_data;
	rx_len++;
	HAL_UART_Receive_IT(&huart2, (uint8_t*)&rx_data, 1);
}

// 串口2接收的数据
uint16_t USART2_Receive(uint8_t *pdata)
{
	memcpy(pdata, (uint8_t*)usart_rx_buf, rx_len);
	return rx_len;
}

// 串口2发送数据
void USART2_Transmit(uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
	HAL_UART_Transmit(&huart2, pData, Size, Timeout);
}

// 清理串口2接收的数据
void USART2_ClearBuf(void)
{
	rx_len = 0;
	memset((uint8_t*)usart_rx_buf, 0, 200);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

注意我们这里使用了一个数组来接收串口收到的数据,使用完数据记得及时调用Clear函数清除,否则可能导致缓冲区溢出,从而出现意料之外的情况发生。

下面我们来封装命令发送的函数,因为我们要判断返回的结果,所以这里采用死等的方式,其实裸机这么设计程序是有点问题的,会导致实时性变差,不过在要求不高的场合是可以这么处理的,下面看下实现代码:

// 向ESP8266发送AT指令
uint8_t ESP8266_SendCommand(char *cmd, char *reply, uint16_t timeout)
{
	// 1.保存传入的指令
	char buf[256] = {0};
	strcat(buf, cmd);
	
	// 2.处理AT指令
	if (strstr(buf, "\r\n") == 0)
	{
		strcat(buf, "\r\n");
	}
	
	// 3.清理前面接收的buf
	USART2_ClearBuf();
	
	// 4.发送数据
	USART2_Transmit((uint8_t *)buf, strlen(buf), 500);
	
	// 5.接收数据
	memset(buf, 0, 256);
	while(timeout != 0)
	{
		if(USART2_Receive((uint8_t *)buf))
		{
			if (strstr(buf, reply))
			{
				printf("%s Send ok!\r\n", cmd); // 发送成功
				
				if (strstr(buf, "CIFSR"))
					printf("%s\r\n", buf); // 打印IP地址
				
				return 0;
			}
			else
			{
				timeout--;
				HAL_Delay(1);
			}
		}
	}
	
	printf("%s Send error!\r\n", cmd); // 发送失败
	return 1;
}

// 向ESP8266发送TCP数据
uint8_t ESP8266_Send_TCP(char *data)
{
	// 1.准备发送的指令
	char buf[256] = {0};
	uint8_t len = strlen(data);
	sprintf(buf, "AT+CIPSEND=0,%d\r\n", len);
	// 2.发送指令
	if (ESP8266_SendCommand(buf, "OK", 500) == 0)
	{
		// 3.发送数据
		ESP8266_SendCommand(data, "OK", 1000);
		return 0;
	}
	return 1;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

最后我们再来看下main函数的实现:

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();

  /* Initialize interrupts */
  MX_NVIC_Init();
  /* USER CODE BEGIN 2 */
  
  //uint16_t data_len = 0;
  uint8_t rx_data[200] = {0};
  //uint8_t tx_data[200] = "AT\r\n";
  
  // 1.启动串口2接收(中断方式)
  USART2_StartRx();
  
  // 2.串口2发送AT指令,启动TCP服务
  //USART2_Transmit(tx_data, sizeof(tx_data), 500);
  ESP8266_SendCommand("AT+RST", "OK", 500);
  HAL_Delay(1000);
  ESP8266_SendCommand("AT+CWMODE=1", "OK", 500);
  ESP8266_SendCommand("AT+CWJAP=\"100ask\",\"100ask100ask\"", "OK", 5000);
  ESP8266_SendCommand("AT+CIPMUX=1", "OK", 500);
  ESP8266_SendCommand("AT+CIPSERVER=1,9999", "OK", 500);
  ESP8266_SendCommand("AT+CIFSR", "OK", 500);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	// 3.1 按键按下,发送门铃信号
	if (Key_GetFlag() == 1)
	{
		ESP8266_Send_TCP("{\"data\":\"doorbell\",\"status\":\"1\"}");
	}
	
	
	// 3.2 收到数据,控制灯
	if (USART2_Receive(rx_data)) //接收到数据
	{
		if (strstr((char *)rx_data, "\"dev\":\"led\",\"status\":\"0\""))
		{
			printf("led off\n\r");
			HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
			USART2_ClearBuf();
			memset(rx_data, 0, 200);
		}
		else if (strstr((char *)rx_data, "\"dev\":\"led\",\"status\":\"1\""))
		{
			printf("led on\n\r");
			HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
			USART2_ClearBuf();
			memset(rx_data, 0, 200);
		}
	}
  }
  /* USER CODE END 3 */
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

整个程序逻辑实现还是比较简单的,我们需要注意的就是在实现发送命令时添加了超时机制,这个超时机制的应用范围还是很广的,所以一定要掌握!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/572508
推荐阅读
相关标签
  

闽ICP备14008679号