我用ESP8266与单片机连接,但是客户端总是连接不上服务端,想请大佬帮忙看看哪里出错了,谢谢


    服务端
   /********************************************************************/ 
#include <REGX51.H>
#define uint unsigned int 
#define uchar unsigned char   
sbit LED0=P1^0;  	//红灯
sbit LED1=P1^1; 
sbit LED2=P1^2;
sbit LED3=P1^3;
    
/*****************相关变量**************/ 
uchar Receive,i,qj,yz,zz,ht;                              
uint n;  
uchar Recive_table[40];  		//用于接收wifi模块反馈到MCU上的数据  

/******************************************************************* 
名称:延时函数  作用:毫秒级延时,微妙级延时函数,为数据收发完成作等待.......  
********************************************************************/ 
  void ms_delay(uint t) 
{  
	uint i,j;  
	for(i=t;i>0;i--)   
	for(j=110;j>0;j--); 
}   

void us_delay(uchar t) 
{  
	while(t--); 
}         

void Uart_Init()				//使用定时器1作为波特率发生器(STC89C52、STC89C51、AT89C51等均可) 
{  
    TMOD = 0x20;
    SCON = 0x50;		 		//设置串行方式
    TH1 = 0xFD;			 		//波特率9600
    TL1 = TH1;
    PCON = 0x00;
    EA = 1;						//总中断打开 
    ES = 1;						//开串口中断  
    TR1 = 1;					//启动定时器1
} 

/******************************************************************** 
名称:串口发送函数  功能:MCU向无线WIFI模块ESP8266发送数据  
********************************************************************/ 
void Send_Uart(uchar value) 
{  
	ES=0;  						//关闭串口中断  
	TI=0;   					//清发送完毕中断请求标志位   
	SBUF=value; 				//发送  
	while(TI==0); 				//等待发送完毕   
	TI=0;   					//清发送完毕中断请求标志位   
	ES=1;  						//允许串口中断  
}  

/******************************************************************** 
名称:WIFI模块设置函数  作用: 启动模块,以便可以实现无线接入和控制  
********************************************************************/ 

void ESP8266_Set(uchar *puf) 	// 数组指针*puf指向字符串数组                
{    
	while(*puf!='\0')    		//遇到空格跳出循环  
	{   
		Send_Uart(*puf);  		//向WIFI模块发送控制指令。   
		us_delay(5);   
		puf++;    
	}  
	us_delay(5);  
	Send_Uart('\r'); 			//回车  
	us_delay(5);  
	Send_Uart('\n');   			//换行  
} 

/****************************************************
名称:ESP8266发送数据函数
功能:用于与wifi模块相连的终端发送数据
*****************************************************/
void ESP8266_Sent1(uchar *puf) // 数组指针*puf指向字符串数组 
{
ESP8266_Set("AT+CIPSEND=0,1");
while(*puf!='\0') //遇到空格跳出循环
{
Send_Uart(*puf); //向WIFI模块发送控制指令。
us_delay(5);
puf++; 
}
us_delay(5);
Send_Uart('\n'); //换行
ms_delay(10);
} 

void ESP8266_Sent2(uchar *puf) // 数组指针*puf指向字符串数组 
{
ESP8266_Set("AT+CIPSEND=1,1");
while(*puf!='\0') //遇到空格跳出循环
{
Send_Uart(*puf); //向WIFI模块发送控制指令。
us_delay(5);
puf++; 
}
us_delay(5);
Send_Uart('\n'); //换行
ms_delay(10);
} 

void ESP8266_Sent3(uchar *puf) // 数组指针*puf指向字符串数组 
{
ESP8266_Set("AT+CIPSEND=2,1");
while(*puf!='\0') //遇到空格跳出循环
{
Send_Uart(*puf); //向WIFI模块发送控制指令。
us_delay(5);
puf++; 
}
us_delay(5);
Send_Uart('\n'); //换行
ms_delay(10);
} 

void ESP8266_Sent4(uchar *puf) // 数组指针*puf指向字符串数组 
{
ESP8266_Set("AT+CIPSEND=3,1");
while(*puf!='\0') //遇到空格跳出循环
{
Send_Uart(*puf); //向WIFI模块发送控制指令。
us_delay(5);
puf++; 
}
us_delay(5);
Send_Uart('\n'); //换行
ms_delay(10);
} 


/******************************************************************** 
名称:主函数  作用:程序的执行入口  
********************************************************************/ 

void main() 
{  	
	Uart_Init();											//波特率发生器 
	ms_delay(500);
	ESP8266_Set("AT+CWMODE=3\r\n"); 							//设置路由器模式1 station,模式2 AP,模式3 station+AP混合模式   
	ms_delay(2000);
	ESP8266_Set("AT+CWSAP=\"wifi\",\"12345678\",11,4,4\r\n");  	//AT+CWSAP="wifi","12345678",11,4  设置模块SSID:WIFI, PWD:密码 及安全类型加密模式(WPA2-PSK) 
	ms_delay(2000);
	ESP8266_Set("AT+CIPMUX=1\r\n");								//开启多连接模式,允许多个各客户端接入 
	ms_delay(2000);		
	ESP8266_Set("AT+CIPSERVER=1,5000\r\n");  					//启动TCP/IP 实现基于网络//控制 	ESP8266_Set("AT+CIPSERVER=1,5000");  
	ms_delay(2000);	
	ESP8266_Set("AT+CIPSTO=0\r\n"); 							//永远不超时
	ES=1;           										//允许串口中断 		
	qj=1;
	zz=1;
	yz=1;
	ht=1;
    LED0=0; 			//红灯亮
	LED1=0;	
	LED2=0;
	LED3=0;	
    while(1) 
	while(1)   
	{    
	if((Recive_table[0]=='+')&&(Recive_table[1]=='I')&&(Recive_table[2]=='P'))//MCU接收到的数据为+IPD时进入判断控制0\1来使小灯亮与灭    
		{           
			if((Recive_table[9]=='G')&&(Recive_table[10]=='P'))            
			{      
				if(Recive_table[15]=='1')      
				{       
					LED0=1; 			//红灯亮
					LED1=0;	
					LED2=0;
					LED3=0;	
					ESP8266_Sent1("1");
					ESP8266_Sent2("1");	
					ESP8266_Sent3("1");
					ESP8266_Sent4("1");					  
				}                                
				    else      
				     if(Recive_table[15]=='2')      
				   {                 
					LED0=0; 		
					LED1=1;	
					LED2=0;
					LED3=0;	
					ESP8266_Sent1("2");
					ESP8266_Sent2("2");	
					ESP8266_Sent3("2");
					ESP8266_Sent4("2");
					} 		
			     	   else      
				     if(Recive_table[15]=='3')      
				   {                 
					LED0=0; 		
					LED1=0;	
					LED2=1;
					LED3=0;	
					ESP8266_Sent1("3");
					ESP8266_Sent2("3");
					ESP8266_Sent3("3");
					ESP8266_Sent4("3");
					} 		
					   else      
				     if(Recive_table[15]=='4')      
				   {                 
					LED0=0; 		
					LED1=0;	
					LED2=0;
					LED3=1;	
					ESP8266_Sent1("4");
					} 		
					   else      
				     if(Recive_table[15]=='5')      
				   {                 
					LED0=0; 		
					LED1=0;	
					LED2=0;
					LED3=0;	
					ESP8266_Sent1("5");
					} 		
							 											  
			}  
		}
  	}    
}    

/*********************************************************************  
名称:串行通讯中断  作用:发送或接收结束后进入该函数,对相应的标志位软件清0,实现模块对数据正常的收发。  
********************************************************************/ 

void Uart_Interrupt() interrupt 4         
{    
	static uchar i=0;  
	if(RI==1)  
	{   
		RI=0;   
		Receive=SBUF;        						//MCU接收wifi模块反馈回来的数据       
		Recive_table[i]=Receive;   
		if((Recive_table[i]=='\n'))
		{		     
			i=0;
		}    
		else i++;  										//遇到换行 重新装值  
	}   
	else TI=0;   
}  

客户端

/********************************************************************/ 
#include <REGX51.H>
#define uint unsigned int 
#define uchar unsigned char   
sbit LED0=P1^0;  	//红灯
sbit LED1=P1^1; 
sbit LED2=P1^2;
sbit LED3=P1^3;
    
/*****************相关变量**************/ 
uchar Receive,i,qj,yz,zz,ht;                              
uint n;  
uchar Recive_table[40];  		//用于接收wifi模块反馈到MCU上的数据  

/******************************************************************* 
名称:延时函数  作用:毫秒级延时,微妙级延时函数,为数据收发完成作等待.......  
********************************************************************/ 
 
 
  void ms_delay(uint t) 
{  
	uint i,j;  
	for(i=t;i>0;i--)   
	for(j=110;j>0;j--); 
}   

void us_delay(uchar t) 
{  
	while(t--); 
}         

void Uart_Init()				//使用定时器1作为波特率发生器(STC89C52、STC89C51、AT89C51等均可) 
{  
    TMOD = 0x20;
    SCON = 0x50;		 		//设置串行方式
    TH1 = 0xFD;			 		//波特率9600
    TL1 = TH1;
    PCON = 0x00;
    EA = 1;						//总中断打开 
    ES = 1;						//开串口中断  
    TR1 = 1;					//启动定时器1
} 

/******************************************************************** 
名称:串口发送函数  功能:MCU向无线WIFI模块ESP8266发送数据  
********************************************************************/ 
void Send_Uart(uchar value) 
{  
	ES=0;  						//关闭串口中断  
	TI=0;   					//清发送完毕中断请求标志位   
	SBUF=value; 				//发送  
	while(TI==0); 				//等待发送完毕   
	TI=0;   					//清发送完毕中断请求标志位   
	ES=1;  						//允许串口中断  
}  

/******************************************************************** 
名称:WIFI模块设置函数  作用: 启动模块,以便可以实现无线接入和控制  
********************************************************************/ 

void ESP8266_Set(uchar *puf) 	// 数组指针*puf指向字符串数组                
{    
	while(*puf!='\0')    		//遇到空格跳出循环  
	{   
		Send_Uart(*puf);  		//向WIFI模块发送控制指令。   
		us_delay(5);   
		puf++;    
	}  
	us_delay(5);  
	Send_Uart('\r'); 			//回车  
	us_delay(5);  
	Send_Uart('\n');   			//换行  
} 




/******************************************************************** 
名称:主函数  作用:程序的执行入口  
********************************************************************/ 

void main() 
{  	
	Uart_Init();											//波特率发生器 
	ms_delay(500);
	ESP8266_Set("AT+CWMODE=3\r\n"); 							//设置路由器模式1 station,模式2 AP,模式3 station+AP混合模式   
	ms_delay(2000);
	ESP8266_Set("AT+CWJAP=\"wifi\",\"12345678\"\r\n");  	//AT+CWSAP="wifi","12345678",11,4  设置模块SSID:WIFI, PWD:密码 及安全类型加密模式(WPA2-PSK) 
	ms_delay(10000);
	ESP8266_Set("AT+CIPMUX=0\r\n");
	ms_delay(2000);	
	ESP8266_Set("AT+CIPMODE=1\r\n");
	ms_delay(2000);
    ESP8266_Set("AT+CIPSTART=\"TCP\",\"192.168.4.1\",5000\r\n");  	//启动TCP/IP 实现基于网络//控制 	ESP8266_Set("AT+CIPSERVER=1,5000");  	
	ms_delay(10000);
	ESP8266_Set("AT+CIPSTO=0\r\n"); 							//永远不超时
		 
	ES=1;           										//允许串口中断 		
	qj=1;
	zz=1;
	yz=1;
	ht=1;
	LED0=0;
	LED1=0;
	LED2=0;
	LED3=0;
    while(1)   
	{    
		if((Recive_table[0]=='+')&&(Recive_table[1]=='I')&&(Recive_table[2]=='P'))//MCU接收到的数据为+IPD时进入判断控制0\1来使小灯亮与灭    
		{           
			if((Recive_table[9]=='G')&&(Recive_table[10]=='P'))            
			{      
				if(Recive_table[15]=='1')      
				{       
					LED0=1; 			//红灯亮
					LED1=0;	
					LED2=0;
					LED3=0;		  
				}                                
				   else      
				     if(Recive_table[15]=='2')      
				   {                 
					LED0=0; 		
					LED1=1;	
					LED2=0;
					LED3=0;	
					} 		
			     	   else      
				     if(Recive_table[15]=='3')      
				   {                 
					LED0=0; 		
					LED1=0;	
					LED2=1;
					LED3=0;	
					} 		
					   else      
				     if(Recive_table[15]=='4')      
				   {                 
					LED0=0; 		
					LED1=0;	
					LED2=0;
					LED3=1;	
					} 		
					   else      
				     if(Recive_table[15]=='5')      
				   {                 
					LED0=0; 		
					LED1=0;	
					LED2=0;
					LED3=0;	
					} 		
							 											  
			}  
		}
  	}    
}    

/*********************************************************************  
名称:串行通讯中断  作用:发送或接收结束后进入该函数,对相应的标志位软件清0,实现模块对数据正常的收发。  
********************************************************************/ 

void Uart_Interrupt() interrupt 4         
{    
	static uchar i=0;  
	if(RI==1)  
	{   
		RI=0;   
		Receive=SBUF;        						//MCU接收wifi模块反馈回来的数据       
		Recive_table[i]=Receive;   
		if((Recive_table[i]=='\n'))
		{		     
			i=0;
		}    
		else i++;  										//遇到换行 重新装值  
	}   
	else TI=0;   
}  
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值