51单片机——wifi模块

51单片机——wifi模块

1.AT指令

#include "reg52.h"
#include "intrins.h"
#include 


#define SIZE 12
sfr AUXR = 0x8E;
sbit D5 = P3^7;
char cmd[SIZE];

code char LJWL[]  = "AT+CWJAP=\"TP-LINK_3E30\",\"18650711783\"\r\n";
code char LJFWQ[] = "AT+CIPSTART=\"TCP\",\"192.168.0.113\",8880\r\n"; 

char TCMS[]  = "AT+CIPMODE=1\r\n";
char SJCS[]  = "AT+CIPSEND\r\n";
 
void UartInit(void)		//[email protected]
{
	AUXR = 0x01;
	SCON = 0x50; //配置串口工作方式1,REN使能接收
	TMOD &= 0x0F;
	TMOD |= 0x20;//定时器1工作方式位8位自动重装
	
	TH1 = 0xFD;
	TL1 = 0xFD;//9600波特率的初值
	TR1 = 1;//启动定时器
	
	EA = 1;//开启总中断
	ES = 1;//开启串口中断
}

void Delay1000ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	i = 8;
	j = 1;
	k = 243;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void sendByte(char data_msg)
{
	SBUF = data_msg;
	while(!TI);
	TI = 0;
}

void sendString(char* str)
{
	while( *str != '\0'){
		sendByte(*str);
		str++;
	}
}

void main()
{

	D5 = 1;
	//配置C51串口的通信方式
	UartInit();
	
	while(1){
		//Delay1000ms();
		//往发送缓冲区写入数据,就完成数据的发送
		//sendString("chenlichen shuai\r\n");
		sendString(LJWL);
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
	
		sendString(LJFWQ);
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
		sendString(TCMS);
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
		sendString(SJCS);
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
		Delay1000ms();
	}
}

void Uart_Handler() interrupt 4
{
	static int i = 0;//静态变量,被初始化一次
	if(RI)//中断处理函数中,对于接收中断的响应
	{
			RI = 0;//清除接收中断标志位
			cmd[i] = SBUF;
			i++;
			if(i == SIZE){
				i = 0;
			}
			if(strstr(cmd,"en")){
				D5 = 0;//点亮D5
				i = 0;
				memset(cmd,'\0',SIZE);
			}
			if(strstr(cmd,"se")){
				D5 = 1;//熄灭D5
				i = 0;
				memset(cmd,'\0',SIZE);
			}
	}
		
	if(TI);
}

2.TCP通信点灯

#include "reg52.h"
#include "intrins.h"
#include 


#define SIZE 12
sfr AUXR = 0x8E;
sbit D5 = P3^7;
char cmd[SIZE];

code char LJWL[]  = "AT+CWJAP=\"TP-LINK_3E30\",\"18650711783\"\r\n";
code char LJFWQ[] = "AT+CIPSTART=\"TCP\",\"192.168.0.113\",8880\r\n"; 

char TCMS[]  = "AT+CIPMODE=1\r\n";
char SJCS[]  = "AT+CIPSEND\r\n";
 
void UartInit(void)		//[email protected]
{
	AUXR = 0x01;
	SCON = 0x50; //配置串口工作方式1,REN使能接收
	TMOD &= 0x0F;
	TMOD |= 0x20;//定时器1工作方式位8位自动重装
	
	TH1 = 0xFD;
	TL1 = 0xFD;//9600波特率的初值
	TR1 = 1;//启动定时器
	
	EA = 1;//开启总中断
	ES = 1;//开启串口中断
}

void Delay1000ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	i = 8;
	j = 1;
	k = 243;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void sendByte(char data_msg)
{
	SBUF = data_msg;
	while(!TI);
	TI = 0;
}

void sendString(char* str)
{
	while( *str != '\0'){
		sendByte(*str);
		str++;
	}
}

void main()
{
	int mark = 0;
	D5 = 1;
	//配置C51串口的通信方式
	UartInit();
	
	while(1){
		//Delay1000ms();
		//往发送缓冲区写入数据,就完成数据的发送
		//sendString("chenlichen shuai\r\n");
		if(mark == 0){
			sendString(LJWL);
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
		
			sendString(LJFWQ);
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
			sendString(TCMS);
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
			sendString(SJCS);
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
			Delay1000ms();
			mark = 1;
		}else{
			sendString("chenlichen shuai\r\n");
			Delay1000ms();
		}
	}
}

void Uart_Handler() interrupt 4
{
	static int i = 0;//静态变量,被初始化一次
	if(RI)//中断处理函数中,对于接收中断的响应
	{
			RI = 0;//清除接收中断标志位
			cmd[0] = SBUF;
			
			if(cmd[0] == '1'){
				D5 = 0;//点亮D5
			}
			if(cmd[0] == '0'){
				D5 = 1;//熄灭D5
			}
	}
		
	if(TI);
}

3.优化连接过程

#include "reg52.h"
#include "intrins.h"
#include 


#define SIZE 12
sfr AUXR = 0x8E;
sbit D5 = P3^7;
sbit D6 = P3^6;

char buffer[SIZE];
code char LJWL[]  = "AT+CWJAP=\"TP-LINK_3E30\",\"18650711783\"\r\n"; //入网指令
code char LJFWQ[] = "AT+CIPSTART=\"TCP\",\"192.168.0.113\",8880\r\n"; //连接服务器指令
char TCMS[]  = "AT+CIPMODE=1\r\n";  //透传指令
char SJCS[]  = "AT+CIPSEND\r\n";	//数据传输开始指令
char RESET[] = "AT+RST\r\n";		//重启模块指令
char AT_OK_Flag = 0;				//OK返回值的标志位
char AT_Connect_Net_Flag = 0;		//WIFI GOT IP返回值的标志位
 
void UartInit(void)		//[email protected]
{
	AUXR = 0x01;
	SCON = 0x50; //配置串口工作方式1,REN使能接收
	TMOD &= 0x0F;
	TMOD |= 0x20;//定时器1工作方式位8位自动重装
	
	TH1 = 0xFD;
	TL1 = 0xFD;//9600波特率的初值
	TR1 = 1;//启动定时器
	
	EA = 1;//开启总中断
	ES = 1;//开启串口中断
}

void Delay1000ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	i = 8;
	j = 1;
	k = 243;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void sendByte(char data_msg)
{
	SBUF = data_msg;
	while(!TI);
	TI = 0;
}

void sendString(char* str)
{
	while( *str != '\0'){
		sendByte(*str);
		str++;
	}
}

void main()
{
	int mark = 0;
	D5 = D6 = 1;//灭状态灯
	//配置C51串口的通信方式
	UartInit();
	Delay1000ms();//给espwifi模块上电时间
	
	//发送联网AT指令并等待成功
	sendString(LJWL);
	//while(!AT_Connect_Net_Flag);
	while(!AT_OK_Flag);
	AT_OK_Flag = 0;
	//发送连服务器指令并等待成功
	sendString(LJFWQ);
	while(!AT_OK_Flag);
	AT_OK_Flag = 0;
	//发送透传模式指令并等待成功
	sendString(TCMS);
	while(!AT_OK_Flag);
	AT_OK_Flag = 0;
	//发送数据传输指令并等待成功
	sendString(SJCS);
	while(!AT_OK_Flag);
	
	if(AT_Connect_Net_Flag){
			D5 = 0;//点亮D5,代表入网成功
	}
	if(AT_OK_Flag){
		D6 = 0;//点亮D6,代表连接服务器并打开透传模式成功
	}
	
	while(1){
		Delay1000ms();
		//“心跳包”
		sendString("chenlichen shuai\r\n");
	}
}

void Uart_Handler() interrupt 4
{
	static int i = 0;//静态变量,被初始化一次
	char tmp;

	if(RI)//中断处理函数中,对于接收中断的响应
	{
			RI = 0;//清除接收中断标志位
			tmp = SBUF;
			if(tmp == 'W' || tmp == 'O' || tmp == 'L' || tmp == 'F'){
				i = 0;
			}
			buffer[i++] = tmp;
			//入网成功的判断依据WIFI GOT IP
			if(buffer[0] == 'W' && buffer[5] == 'G'){
				AT_Connect_Net_Flag	= 1;
				memset(buffer, '\0', SIZE);
			}
			//连接服务器等OK返回值指令的判断
			if(buffer[0] == 'O' && buffer[1] == 'K'){
				AT_OK_Flag	= 1;
				memset(buffer, '\0', SIZE);
			}
			//联网失败出现FAIL字样捕获
			if(buffer[0] == 'F' && buffer[1] == 'A'){
				for(i=0;i<5;i++){
					D5 = 0;
					Delay1000ms();
					D5 = 1;
					Delay1000ms();
				}
				sendString(RESET);
				memset(buffer, '\0', SIZE);
			}
			//灯控指令
			if(buffer[0] == 'L' && buffer[2] == '1'){
				D5 = 0;//点亮D5
				memset(buffer, '\0', SIZE);
			}
			if(buffer[0] == 'L' && buffer[2] == '0'){
				D5 = 1;//熄灭D5
				memset(buffer, '\0', SIZE);
			}
			if(i == 12) i = 0;
	}
		
}

4.esp工作在路由器模式

#include "reg52.h"
#include "intrins.h"
#include 


#define SIZE 12
sfr AUXR = 0x8E;
sbit D5 = P3^7;
sbit D6 = P3^6;

char buffer[SIZE];

//1 工作在路由模式
char LYMO[] = "AT+CWMODE=2\r\n";
//2 使能多链接
char DLJ[] = "AT+CIPMUX=1\r\n"; 
//3 建立TCPServer
char JLFW[] = "AT+CIPSERVER=1\r\n"; // default port = 333 

//发送数据
char FSSJ[] = "AT+CIPSEND=0,5\r\n";


char AT_OK_Flag = 0;				//OK返回值的标志位
char AT_Connect_Net_Flag = 0;		//WIFI GOT IP返回值的标志位
char Client_Connect_Flag = 0;
 
void UartInit(void)		//[email protected]
{
	AUXR = 0x01;
	SCON = 0x50; //配置串口工作方式1,REN使能接收
	TMOD &= 0x0F;
	TMOD |= 0x20;//定时器1工作方式位8位自动重装
	
	TH1 = 0xFD;
	TL1 = 0xFD;//9600波特率的初值
	TR1 = 1;//启动定时器
	
	EA = 1;//开启总中断
	ES = 1;//开启串口中断
}

void Delay1000ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	i = 8;
	j = 1;
	k = 243;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void sendByte(char data_msg)
{
	SBUF = data_msg;
	while(!TI);
	TI = 0;
}

void sendString(char* str)
{
	while( *str != '\0'){
		sendByte(*str);
		str++;
	}
}

void main()
{
	int mark = 0;
	D5 = D6 = 1;//灭状态灯
	//配置C51串口的通信方式
	UartInit();
	Delay1000ms();//给espwifi模块上电时间
	
	
	sendString(LYMO);
	while(!AT_OK_Flag);
	AT_OK_Flag = 0;
	sendString(DLJ);
	while(!AT_OK_Flag);
	AT_OK_Flag = 0;
	sendString(JLFW);

	while(!Client_Connect_Flag);
	AT_OK_Flag = 0;	
	if(Client_Connect_Flag){
		D5 = 0;//点亮D5,代表有客户端接入
		D6 = 0;
	}
	
	
	while(1){
		
		//4 发送数据
		sendString(FSSJ);
		Delay1000ms();
		Delay1000ms();
		sendString("Hello");
		Delay1000ms();
		Delay1000ms();
   
	}
}

void Uart_Handler() interrupt 4
{
	static int i = 0;//静态变量,被初始化一次
	char tmp;

	if(RI)//中断处理函数中,对于接收中断的响应
	{
			RI = 0;//清除接收中断标志位
			tmp = SBUF;
			if(tmp == 'W' || tmp == 'O' || tmp == 'L' || tmp == '0' || tmp == ':'){
				i = 0;
			}
			buffer[i++] = tmp;
			//入网成功的判断依据WIFI GOT IP
			if(buffer[0] == 'W' && buffer[5] == 'G'){
				AT_Connect_Net_Flag	= 1;
				memset(buffer, '\0', SIZE);
			}
			//连接服务器等OK返回值指令的判断
			if(buffer[0] == 'O' && buffer[1] == 'K'){
				AT_OK_Flag	= 1;
				memset(buffer, '\0', SIZE);
			}
			
			if(buffer[0] == '0' && buffer[2] == 'C'){
				Client_Connect_Flag	= 1;
				memset(buffer, '\0', SIZE);
			}
		
			//灯控指令
			if(buffer[0] == ':' && buffer[1] == 'o' && buffer[2] == 'p'){
				D5 = 0;//点亮D5
				memset(buffer, '\0', SIZE);
			}
			if(buffer[0] == ':' && buffer[1] == 'c' && buffer[2] == 'l'){
				D5 = 1;//熄灭D5
				memset(buffer, '\0', SIZE);
			}
			if(i == 12) i = 0;
	}
		
}

你可能感兴趣的:(51单片机,51单片机,单片机,嵌入式硬件)