|
/************************************************************************************
#include "stc15f2k60s2.h" // 單片機(jī)STC15F2K60S2頭文件,可以不再加入reg51.h
#include <intrins.h> // 加入此頭文件后,可使用_nop_庫(kù)函數(shù)
#include "delay.h" // 延時(shí)函數(shù)頭文件
#include "uart.h" // 串行通信函數(shù)頭文件
#include <string.h> // 加入此頭文件后,可使用strstr庫(kù)函數(shù)
//sbit RST = P2^6;
#define Buf_Max 50
unsigned char xdata Rec_Buf[Buf_Max];
unsigned char i = 0;
void CLR_Buf(void);
bit Hand(unsigned char *a);
sbit LED1 = P5^0; // 定義LED1為P5.0
sbit LED2 = P5^1; // 定義LED2為P5.1
sbit LED3 = P5^2; // 定義LED3為P5.2
sbit OP_relay = P3^5; // 定義光耦繼電器控制I/O為P3.5
char code str1[]="AT\r\n"; // 聯(lián)機(jī)指令,返回"OK"
char code str2[]="AT+CWMODE=3\r\n"; // 設(shè)置ESP8266的工作模式,返回"OK"或者"no change"
char code str3[]="AT+CWJAP=\"lces\",\"88518851\"\r\n"; // 連接到WiFi熱點(diǎn),lces為熱點(diǎn)名稱,88518851為密碼;連接成功返回“OK”
char code str4[]="AT+CIFSR\r\n"; // 本機(jī)IP地址查詢指令
char code str5[]="AT+CIPSTART=\"TCP\",\"192.168.191.1\",8234\r\n"; // 連接到TCP服務(wù)器,返回“Linked”
char code str6[]="AT+CIPSEND=6\r\n"; // 發(fā)送數(shù)據(jù)指令
char code str7[]="hello!\r\n"; // 數(shù)據(jù)內(nèi)容
char code str8[]="AT+CIPSERVER=1,5000\r\n"; // 建立TCP服務(wù)器,開(kāi)放端口8888
char code str9[]="AT+CIPMUX=1\r\n"; // 打開(kāi)多連接
char code str10[]="AT+RST\r\n"; // 軟件復(fù)位
char code str11[]="AT+CIPSEND=0,15\r\n"; // 發(fā)送數(shù)據(jù)指令,基于多路連接模式
char code str12[]="Command Executed!\r\n"; // 數(shù)據(jù)內(nèi)容
void main() // 主函數(shù)
{
// bit Order_flag;
P1M0 = 0x00;
P1M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
// RST = 1; // ESP8266復(fù)位功能腳,拉低會(huì)將ESP8266復(fù)位
UartInit(); // 初始化串口
ES = 1; // 串口1中斷打開(kāi)
IE2 = 0x01; // 串口2中斷打開(kāi)
EA = 1; // 總中斷打開(kāi)
DelayMS(1000); // 延時(shí)一段時(shí)間,讓ESP8266啟動(dòng)
DelayUS(100);
U1SendString(Rec_Buf); // 將ESP8266啟動(dòng)信息通過(guò)串口1打印出
U1SendString("\r\n");
U1SendString("Welcome to LCE STUDIO, Please wait while we are getting the device ready\r\n");
CLR_Buf(); //清除緩存內(nèi)容
while(!Hand("OK")) //判斷是否握手成功,如果不成功延時(shí)一會(huì),再發(fā)送AT握手指令
{
U2SendString(str1); //發(fā)送聯(lián)機(jī)指令
DelayMS(500);
}
CLR_Buf(); //清除緩存內(nèi)容
U1SendString("OK,Succeed Establish connection with ESP8266\r\n");
while(!(Hand("OK")|Hand("no change"))) //判斷是否設(shè)置成功,如不成功,延時(shí)后再次發(fā)送
{
U2SendString(str2); //發(fā)送設(shè)置ESP8266工作模式指令
DelayMS(500);
}
if(Hand("OK"))
{
CLR_Buf();
U2SendString(str10);
DelayMS(500);
}
CLR_Buf();
U1SendString("OK,ESP8266 has been set as AP+Station Mode\r\n");
while(!Hand("OK"))
{
U2SendString(str9); //設(shè)置為多路連接
DelayMS(500);
}
CLR_Buf();
while(!Hand("OK")) // 建立TCP 服務(wù)器,并開(kāi)放端口8888
{
U2SendString(str8);
DelayMS(500);
}
CLR_Buf();
while(!Hand("OK")) // 查詢模塊當(dāng)前IP地址
{
U2SendString(str4);
DelayMS(500);
}
U1SendString(Rec_Buf);
U1SendString("Congratulations, Everything is set up! TCP sever:192.168.4.1, Port: 5000");
CLR_Buf();
while (1) // 主循環(huán)
{
// U1SendString(Rec_Buf); // 將ESP8266啟動(dòng)信息通過(guò)串口1打印出
// DelayMS(1000);
// DelayMS(1000);
if(Hand("ESPGLED1")) // 收到關(guān)閉LED1的指令
{
ES = 0;
IE2 = 0x00;
LED1 = 1;
CLR_Buf();
U1SendString("Command: Turn off LED1, Executed!\r\n");
ES = 1;
IE2 = 0x01;
}
else if(Hand("ESPKLED1")) // 收到關(guān)閉LED1的指令
{
ES = 0;
IE2 = 0x00;
LED1 = 0;
CLR_Buf();
U1SendString("Command: Turn on LED1, Executed!\r\n");
ES = 1;
IE2 = 0x01;
}
else if(Hand("ESPGLED2")) // 收到關(guān)閉LED1的指令
{
ES = 0;
IE2 = 0x00;
LED2 = 1;
CLR_Buf();
U1SendString("Command: Turn off LED1, Executed!\r\n");
ES = 1;
IE2 = 0x01;
}
else if(Hand("ESPKLED2")) // 收到關(guān)閉LED1的指令
{
ES = 0;
IE2 = 0x00;
LED2 = 0;
CLR_Buf();
U1SendString("Command: Turn on LED1, Executed!\r\n");
ES = 1;
IE2 = 0x01;
}
else if(Hand("ESPGLED3")) // 收到關(guān)閉LED1的指令
{
ES = 0;
IE2 = 0x00;
LED3 = 1;
CLR_Buf();
U1SendString("Command: Turn off LED1, Executed!\r\n");
ES = 1;
IE2 = 0x01;
}
else if(Hand("ESPKLED3")) // 收到關(guān)閉LED1的指令
{
ES = 0;
IE2 = 0x00;
LED3 = 0;
CLR_Buf();
U1SendString("Command: Turn on LED1, Executed!\r\n");
ES = 1;
IE2 = 0x01;
}
else if(Hand("ESPKJDQ1")) // 收到關(guān)閉LED1的指令
{
ES = 0;
IE2 = 0x00;
OP_relay = 0;
CLR_Buf();
U1SendString("Command: OP relay Enable, Executed!\r\n");
ES = 1;
IE2 = 0x01;
}
else if(Hand("ESPGJDQ1")) // 收到關(guān)閉LED1的指令
{
ES = 0;
IE2 = 0x00;
OP_relay = 1;
CLR_Buf();
U1SendString("Command: OP relay Disable, Executed!\r\n");
ES = 1;
IE2 = 0x01;
}
/*
if(Order_flag == 1)
{
while(!Hand("SEND OK")) //判斷是否發(fā)送數(shù)據(jù)成功,如不成功,延時(shí)后再次發(fā)送
{
U2SendString(str11); //數(shù)據(jù)發(fā)送指令
DelayMS(100);
U2SendString(str12); //數(shù)據(jù)內(nèi)容
DelayMS(500);
}
CLR_Buf();
Order_flag = 0;
}
*/
}
}
bit Hand(unsigned char *a)
{
if(strstr(Rec_Buf,a)!=NULL)
return 1;
else
return 0;
}
void CLR_Buf(void)
{
unsigned char k;
for(k=0;k<Buf_Max;k++)
{
Rec_Buf[k] = 0;
}
i = 0;
}
void Uart1() interrupt 4 using 1
{
ES = 0;
if (RI)
{
RI = 0; //清除RI位
Rec_Buf[i] = SBUF;
i++;
if(i>Buf_Max)
{
i = 0;
}
}
if (TI)
{
TI = 0; //清除TI位
}
ES = 1;
}
void Uart2() interrupt 8 using 1
{
IE2 = 0x00;
if (S2CON & S2RI)
{
S2CON &= ~S2RI;
Rec_Buf[i] = S2BUF;
i++;
if(i>Buf_Max)
{
i = 0;
}
}
if (S2CON & S2TI)
{
S2CON &= ~S2TI;
}
IE2 = 0x01;
}
|
|