STM32+ESP8266+TLINK 远程控制LED

目录

整体框架

单片机部分

ESP8266部分

TLINK平台

实验结果

项目地址


整体框架

STM32+ESP8266+TLINK 远程控制LED_第1张图片

单片机部分

  • USART通信(本项目中STM32利用USART2与ESP8266通信)
    void USART2_IRQHandler(void)
    {
      if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
      {
    		bufferHead < ESP8266RXBufferMaxLength ? esp8266RXBuffer[bufferHead++] = USART2->DR : USART2->DR;
      }
      if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
    	{
    		USART2->DR; //清除中断
    		esp8266RXBuffer[bufferHead] = '\0';
    		USARTSendArrar(USART1, (uint8_t *)esp8266RXBuffer);
    		bufferHead = 0;
    	}
    }

     

  • SysTick延时(延时函数)
    void Delay_us(__IO u32 nTime)
    {
      TimingDelay = nTime;
      while(TimingDelay != 0);
    }

     

  • Tim计时器(用于计算ESP8266网络时延)
    void TIM2_IRQHandler(void)
    {
    	if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    	{
    	  TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update);
          TIM2_Time++;
    	}
    }

     

  • GPIO初始化(LED灯IO端口初始化)
    void LED_GPIO_Config(void)
    {
      GPIO_InitTypeDef GPIO_InitStructure;
      RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE); //使能PC端口时钟
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //选择对应的引脚
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
      GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化PC端口
      GPIO_SetBits(GPIOC, GPIO_Pin_13 ); //关闭所有LED
    }

     

ESP8266部分

ESP8266库引用了github

  • 初始化
    bool esp8266Begin()
    {
    	if(esp8266Test())
    	{
    		if (esp8266SetMux(0))
    			return TRUE;
    		return FALSE;
    	}
    	return FALSE;
    }

     

  • Wifi连接
    bool esp8266ConnectAP(const char* ssid, const char* pwd)
    {
    	bool rc = FALSE;
    	if(esp8266SetMode(ESP8266_MODE_STA))
    	{
    		esp8266ClearBuffer();
    		USARTSendArrar(ESP8266_USART, "AT");
    		USARTSendArrar(ESP8266_USART, (uint8_t *)ESP8266_CONNECT_AP);
    		USARTSendArrar(ESP8266_USART, "=\"");
    		USARTSendArrar(ESP8266_USART, (uint8_t *)ssid);
    		USARTSendArrar(ESP8266_USART, "\"");
    		if (pwd != NULL)
    		{
    			USARTSendArrar(ESP8266_USART, ",");
    			USARTSendArrar(ESP8266_USART, "\"");
    			USARTSendArrar(ESP8266_USART, (uint8_t *)pwd);
    			USARTSendArrar(ESP8266_USART, "\"");
    		}
    		USARTSendArrar(ESP8266_USART, "\r\n");
    		rc = esp8266ReadForResponses(RESPONSE_OK, RESPONSE_FAIL, WIFI_CONNECT_TIMEOUT);
    		return rc;
    	}
    	return FALSE;
    }

     

  • TCP连接

    bool esp8266TCPConnect(uint8_t * destination, uint8_t * port)
    {
    	bool rsp = FALSE;
    	esp8266ClearBuffer();
    	USARTSendArrar(ESP8266_USART, "AT");
    	USARTSendArrar(ESP8266_USART, (uint8_t *)ESP8266_TCP_CONNECT);
    	USARTSendArrar(ESP8266_USART, "=");
    	USARTSendArrar(ESP8266_USART, "\"TCP\",");
    	USARTSendArrar(ESP8266_USART, "\"");
    	USARTSendArrar(ESP8266_USART, destination);
    	USARTSendArrar(ESP8266_USART, "\",");
    	USARTSendArrar(ESP8266_USART, port);
    	USARTSendArrar(ESP8266_USART, "\r\n");
    	rsp = esp8266ReadForResponses(RESPONSE_OK, RESPONSE_ERROR, CLIENT_CONNECT_TIMEOUT);
    	
    	if(rsp == FALSE)
    	{
    		rsp = esp8266SearchBuffer("ALREADY");
    		if (rsp)
    			return TRUE;
    		return FALSE;
    	}
    	return TRUE;
    }

     

  • TCP发送和接收

    bool esp8266TCPSend(uint8_t *buf)
    {
    	bool rsp = FALSE;
    	uint16_t size = strlen((char *)buf);
    	uint8_t i = 0;
    	uint8_t *p = buf;
    	char params[8];
    	if (size > 2048)
    		return FALSE; //ESP8266_CMD_BAD
    	sprintf(params, "%d", size);
    	esp8266SendCommand(ESP8266_TCP_SEND, ESP8266_CMD_SETUP, params);
    	
    	rsp = esp8266ReadForResponses(RESPONSE_OK, RESPONSE_ERROR, COMMAND_RESPONSE_TIMEOUT);
    	//if (rsp > 0)
    	if(rsp)
    	{
    		esp8266ClearBuffer();
    		//usartSendArrar(ESP8266_USART, buf);
    		//usartSendArrar(ESP8266_USART, "\r");
    		for(i=0; i

     

TLINK平台

  • 注册账号:Tlink
  • 添加设备:参考Tlink帮助中心
  • 建立连接
    esp8266TCPSend((uint8_t *)"此处填写申请的序列号")

     

  • 收发处理数据
    while(1)
    	{
    		if(esp8266TLINKBufferAvailable())
    		{
    			TlinkProcess();
    		}
    		Delay_us(100000);
    	}

     

接线:

  • ESP8266接STM32的USART2
  • 电源

实验结果

STM32+ESP8266+TLINK 远程控制LED_第2张图片

STM32+ESP8266+TLINK 远程控制LED_第3张图片

项目地址

smarthouse

你可能感兴趣的:(单片机)