#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit LED = P0^1;u8 dat;
//sbit AUXR = 0x8e;
void Init(void);//串口初始化函数
void Delay_ms(u16 n);//延时子函数
void Sent_ZF(u8 dat);//发送一个字节
void AT_Send_String(u8 *string);//发送字符串
void ESP8266_Init();//ESP8266初始化
void Init(void) //串口初始化函数
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFA;
TL1 = TH1;
PCON = 0x80;
EA = 1;
ES = 1;
TR1 = 1;
}
void main()
{
Init();
ESP8266_Init();
while(1);
}
void Sent_ZF(u8 dat) //发送一个字节
{
ES = 0;
TI=0;
SBUF = dat;
while(!TI);
TI = 0;
ES = 1;
}
void Delay_ms(u16 n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
void AT_Send_String(u8 *string) //发送字符串
{
while(*string)
{
Sent_ZF(*string++);
Delay_ms(5);
}
}
void ESP8266_Init()
{
AT_Send_String("AT\r\n"); //测试AT指令
Delay_ms(3000);
Delay_ms(3000);
AT_Send_String("AT+RST\r\n"); //重启模块
Delay_ms(3000);
Delay_ms(3000);
AT_Send_String("AT+CWMODE=2\r\n"); //设置为AP模式
Delay_ms(3000);
Delay_ms(3000);
AT_Send_String("AT+CWSAP=\"gua\",\"123456\",11,4\r\n"); //? 设置name password,加密方式
Delay_ms(3000);
Delay_ms(3000);
AT_Send_String("AT+CIFSR\r\n"); //?查看模块的ip地址
Delay_ms(3000);
Delay_ms(3000);
AT_Send_String("AT+CIPMODE=1\r\n"); //? 设置模块传输模式为TCP模式
Delay_ms(3000);
Delay_ms(3000);
AT_Send_String("AT+CIPMUX=1\r\n"); //设置为多连接模式,启动模块
Delay_ms(3000);
Delay_ms(3000);
AT_Send_String("AT+CIPSERVER=1,8090\r\n"); //服务器的设置端口
Delay_ms(3000);
Delay_ms(3000);
}
void InterruptUART() interrupt 4
{
RI = 0; //清零
dat = SBUF; //收到的数据占存到SBUF中
{
if(dat=='o')
{
LED =0;
}
if(dat=='f')
{
LED =1;
}
}
}