#include "wifi.h"
extern uint8_t CONNECT_FLEG;
void WIFI_BUFF_MEMSET(void)
{
	memset(wifi.BUFF,0,BUFF_MAX);
	wifi.buff_flag = 0;
	wifi.buff_length = 0;
}

void WIFI_SEND(u8 *data)
{
	USART1_SEND(data,strlen((char *)data));
}
/**
 * 功能：检测WIFI的应答
 * 参数：
 *			 src：WIFI接收到数据的缓存区
 *       dest：要检测的应答信号
 *       timeout: 等待应答信号的查询超时时间
 * 返回值：
        1 代表指令成功
		0 代表响应超时
 *				 						
 */
uint8_t WIFI_CheckAck(char* src, char* dest, int timeout)
{
	char *check = NULL;
	//清空缓冲
	 WIFI_BUFF_MEMSET();
	
	 WIFI_SEND(src);
	 timeout = timeout/10;
	 while(timeout--)
	 {
		delay_ms(10);//延时1ms
		if(wifi.buff_flag==1)
		 {
			check = strstr(wifi.BUFF,dest);
			if(check != NULL)
				return 1; 		
		 }

	 }
	 WIFI_BUFF_MEMSET();//数据有误 清空	
	 return 0;//超时	
}
void C511_TO_MQTT(void)
{
	WIFI_CheckAck("AT+MDISCONNECT\r\n","OK",1500);
	delay_ms(1000);
	WIFI_CheckAck("ATE0\r\n","OK",1500);
	while(!WIFI_CheckAck("AT+QICSGP=1,1,\"cmnbiot\",\"\",\"\"\r\n","OK",5000));
	WIFI_BUFF_MEMSET();//数据有误 清空	
	delay_ms(1000);
	if(WIFI_CheckAck("AT+NETOPEN?\r\n","NETOPEN:0",3000))
	{
		while(!WIFI_CheckAck("AT+NETOPEN\r\n","NETOPEN:SUCCESS",7000));
	}
	WIFI_CheckAck("AT+MCONFIG=\"4G_TEST\"\r\n","OK",3000);
	
	while(!WIFI_CheckAck("AT+MIPSTART=\"broker.emqx.io\",1883,4\r\n","MIPSTART: SUCCESS",3000));
	
	while(!WIFI_CheckAck("AT+MCONNECT=1,60\r\n","MCONNECT: SUCCESS",3000));
	
	while(!WIFI_CheckAck("AT+MSUB=\"app\",0\r\n","MSUB: SUCCESS",3000));
	
	while(!WIFI_CheckAck("AT+MPUB=\"4G\",0,0,\"hello world\"\r\n","MPUB: SUCCESS",3000));
}
void C511_TO_UDP(u8* IP ,u8 *PORT)
{
	u8 buf[64];
	WIFI_CheckAck("AT+RESET\r\n","OK",1500);
	while(!WIFI_CheckAck("ATE0\r\n","OK",1500));
	
	if(WIFI_CheckAck("AT+CIPMODE?\r\n","CIPMODE:0",3000))
	{
		WIFI_CheckAck("AT+CIPMODE=1\r\n","OK",1500);
	}
	while(!WIFI_CheckAck("AT+QICSGP=1,1,\"cmnbiot\",\"\",\"\"\r\n","OK",5000));

	WIFI_BUFF_MEMSET();//数据有误 清空	
	delay_ms(1000);
	if(WIFI_CheckAck("AT+NETOPEN?\r\n","NETOPEN:0",3000))
	{
		while(!WIFI_CheckAck("AT+NETOPEN\r\n","NETOPEN:SUCCESS",7000));
	}
	delay_ms(1000);
	sprintf((char *)buf,"AT+CIPOPEN=0,\"UDP\",\"%s\",%s\r\n",IP,PORT);
	while(!WIFI_CheckAck((char *)buf,">",5000));
	
	WIFI_BUFF_MEMSET();
	CONNECT_FLEG = 1;
}
void C511_TO_TCP(u8* IP ,u8 *PORT)
{
	u8 buf[64];
	WIFI_CheckAck("AT+RESET\r\n","OK",1500);
	while(!WIFI_CheckAck("ATE0\r\n","OK",1500));
	
	if(WIFI_CheckAck("AT+CIPMODE?\r\n","CIPMODE:0",3000))
	{
		WIFI_CheckAck("AT+CIPMODE=1\r\n","OK",1500);
	}
	while(!WIFI_CheckAck("AT+QICSGP=1,1,\"cmnbiot\",\"\",\"\"\r\n","OK",5000));

	WIFI_BUFF_MEMSET();//数据有误 清空	
	delay_ms(1000);
	if(WIFI_CheckAck("AT+NETOPEN?\r\n","NETOPEN:0",3000))
	{
		while(!WIFI_CheckAck("AT+NETOPEN\r\n","NETOPEN:SUCCESS",7000));
	}
	delay_ms(1000);
	sprintf((char *)buf,"AT+CIPOPEN=0,\"TCP\",\"%s\",%s\r\n",IP,PORT);
	while(!WIFI_CheckAck((char *)buf,">",5000));
	
	WIFI_BUFF_MEMSET();
	CONNECT_FLEG = 1;
}
/**
 * 功能：连接至TCP网络进行通信
 * 参数：
 *		 id:WIFI名称
 *       password:WIFI密码
 * 返回值：
        1 代表指令成功
		0 代表响应超时
 *				 						
 */
void CONNECT_TO_TCP(u8* id,u8 *password)
{
	u8 buf[50];
	WIFI_CheckAck("+++",id,1500); //确保其退出透传
	while(!WIFI_CheckAck("AT+RESTORE\r\n","OK",5000));
	while(!WIFI_CheckAck("AT\r\n","OK",1000));
	while(!WIFI_CheckAck("AT+CWMODE=0\r\n","OK",2000)); 
	sprintf((char *)buf,"AT+CWJAP=%s,%s\r\n",id,password);
	while(!WIFI_CheckAck((char *)buf,"+CWJAP:1,",5000));
	while(!WIFI_CheckAck("AT+CIPMODE=1\r\n","OK",1000)!=0);
	while(!WIFI_CheckAck("AT+CIPSTART=TCP,192.168.0.146,2347\r\n","+CIPSTART:1",3000));
	while(!WIFI_CheckAck("AT+CIPSEND\r\n","OK",1000));
	WIFI_SEND(":Connected\r\n");
	CONNECT_FLEG = 1;
}

void CONNECT_TO_UDP(u8* id,u8 *password)
{
	u8 buf[50];

	WIFI_CheckAck("+++",id,1500); //确保其退出透传
	while(!WIFI_CheckAck("AT+RESTORE\r\n","OK",5000));
	while(!WIFI_CheckAck("AT\r\n","OK",1000));
	while(!WIFI_CheckAck("AT+CWMODE=0\r\n","OK",2000)); 
	sprintf((char *)buf,"AT+CWJAP=%s,%s\r\n",id,password);
	while(!WIFI_CheckAck((char *)buf,"+CWJAP:1,",5000));
	while(!WIFI_CheckAck("AT+CIPMODE=1\r\n","OK",1000)!=0);
	while(!WIFI_CheckAck("AT+CIPSTART=UDP,192.168.0.146,2345,1112,0\r\n","+CIPSTART:1",3000)); //192.168.0.150,2345为IP地址 2345 端口号1112 是模块设置的端口号 0 UDP固定目标模式 
	while(!WIFI_CheckAck("AT+CIPSEND\r\n","OK",1000));
	WIFI_SEND(":Connected\r\n");
	CONNECT_FLEG = 1;
}
void CONNECT_TO_MQTT(u8* id,u8 *password)
{
	u8 buf[50];
	while(!WIFI_CheckAck("AT+MQTTCLEAN\r\n","OK",1000));
	while(!WIFI_CheckAck("AT+RESTORE\r\n","OK",5000));
	while(!WIFI_CheckAck("AT\r\n","OK",1000));
	while(!WIFI_CheckAck("AT+CWMODE=0\r\n","OK",2000)); //STA模式
	sprintf((char *)buf,"AT+CWJAP=%s,%s\r\n",id,password);
	while(!WIFI_CheckAck((char *)buf,"+CWJAP:1,",5000));//连接WIFI
	while(!WIFI_CheckAck("AT+MQTTLONGCLIENTID=WF-TEST2\r\n","OK",1000)!=0);//配置 MQTT 客户端所需的客户端 ID、用户名和密码
	while(!WIFI_CheckAck("AT+MQTTCONN=broker.emqx.io,1883,0\r\n","MQTTCONNECTED:",2000)!=0);//连接 MQTT 服务器
	while(!WIFI_CheckAck("AT+MQTTSUB=test-app,0\r\n","OK",2000)!=0);//订阅主题 test-app
	while(!WIFI_CheckAck("AT+MQTTPUBRAW=test-w,32,0,0\r\n","OK",2000)!=0);
	WIFI_SEND("MQTT_Connected\r\n");
	WIFI_BUFF_MEMSET();
	CONNECT_FLEG = 1;
}
/****************************************
 *  屏幕显示： OLED_SHOW()  
 *                                      
 * 说明  ： 大夏龙雀科技
 *            WF24 
*****************************************/
void  OLED_SHOW()
{
		OLED_ShowChinese(10,0,0,16,1); 
		OLED_ShowChinese(28,0,1,16,1);
		OLED_ShowChinese(44,0,2,16,1);
		OLED_ShowChinese(62,0,3,16,1);
		OLED_ShowChinese(80,0,4,16,1);
		OLED_ShowChinese(98,0,5,16,1);
	  OLED_ShowString(44,16,"WF24",16,1);
		OLED_Refresh();
}

void mqtt_rx_Control(void)
{
		char *ptr = NULL;
		u8  number;
	if(wifi.buff_flag == 1)
	{
		if(strstr((char *)wifi.BUFF,"+MSUB: \"app\"")!=NULL)     //服务端接收数据判断
		{
			ptr = strrchr((char*)wifi.BUFF,',');           // 以最后一个,为准后面为数据
			number = *(ptr+2)-'0';
			switch(number)
							{
							 case 1: LED_OFF();  break;
							 case 2: LED_ON();   break;
							 case 3:OLED_SHOW(); break;
							 case 4:OLED_Clear();break;
							 case 5:Motor_Control1(); break;
							 case 6:Motor_Control2(); break;
							 case 7:Motor_Control3(); break;
							 default:  return;
							}
		}
	WIFI_BUFF_MEMSET();			
	}
}
/****************************************
 *  服务器接收判断函数： WIFI_Config()    
 *                                      
 *  LED开：1  LED关：2
 *  
 *  OLED开：3  OLED关：4
 *
 *  电机正转：5  电机反转：6 
*****************************************/
u8 Flag = 1;                  //客户端连接标志位
void wifi_rx_Control(void)
{
		char *ptr = NULL;
		u8  number;
		if(wifi.buff_flag == 1)
		{
			if(strstr((char *)wifi.BUFF,"192.168.0")!=NULL)     //服务端接收数据判断
			{
						ptr = strrchr((char*)wifi.BUFF,',');           // 以最后一个,为准后面为数据

					  if(strlen(ptr)-3==1)                      //判断接收到为一位数据   ,*\r\n
						{
							number = ptr[1]-'0';
							switch(number)
							{
							 case 1: LED_OFF();  break;
							 case 2: LED_ON();   break;
							 case 3:OLED_SHOW(); break;
							 case 4:OLED_Clear();break;
							 case 5:Motor_Control1(); break;
							 case 6:Motor_Control2(); break;
							 case 7:Motor_Control3(); break;
							 default:  return;
							}						 
						}
			 }  
			 else if(strstr((char *)wifi.BUFF,"RECV:test-app")!=NULL)
			{
				ptr = strrchr((char*)wifi.BUFF,',');           // 以最后一个,为准后面为数据

						  if(strlen(ptr)-3==1)                      //判断接收到为一位数据   ,*\r\n
							{
								number = ptr[1]-'0';
								switch(number)
								{
								 case 1: LED_OFF();  break;
								 case 2: LED_ON();   break;
								 case 3:OLED_SHOW(); break;
								 case 4:OLED_Clear();break;
								 case 5:Motor_Control1(); break;
								 case 6:Motor_Control2(); break;
								 case 7:Motor_Control3(); break;
								 default:  return;
								}						 
							}
				
			}
		}
		
		WIFI_BUFF_MEMSET();
}







