欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: 11. ESP8266串口WiFi模塊 AP+STATION 模式測試程序 [打印本頁]

作者: 飛飛啊    時間: 2018-10-7 18:00
標題: 11. ESP8266串口WiFi模塊 AP+STATION 模式測試程序
/************************************************************************************

#include "stc15f2k60s2.h"            // 單片機STC15F2K60S2頭文件,可以不再加入reg51.h
#include <intrins.h>                                        // 加入此頭文件后,可使用_nop_庫函數
#include "delay.h"                        // 延時函數頭文件
#include "uart.h"                                // 串行通信函數頭文件
#include <string.h>           // 加入此頭文件后,可使用strstr庫函數

//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";                                                        //  聯機指令,返回"OK"
char code str2[]="AT+CWMODE=3\r\n";                                               //  設置ESP8266的工作模式,返回"OK"或者"no change"
char code str3[]="AT+CWJAP=\"lces\",\"88518851\"\r\n";                                   //  連接到WiFi熱點,lces為熱點名稱,88518851為密碼;連接成功返回“OK”     
char code str4[]="AT+CIFSR\r\n";                                             //         本機IP地址查詢指令
char code str5[]="AT+CIPSTART=\"TCP\",\"192.168.191.1\",8234\r\n";    //  連接到TCP服務器,返回“Linked”
char code str6[]="AT+CIPSEND=6\r\n";                                                                                                                             //  發送數據指令
char code str7[]="hello!\r\n";                                                                                                                                                           //  數據內容
char code str8[]="AT+CIPSERVER=1,5000\r\n";                                                                                                   //  建立TCP服務器,開放端口8888                                                       
char code str9[]="AT+CIPMUX=1\r\n";                                                                                                                                   //        打開多連接       
char code str10[]="AT+RST\r\n";                                                                                                                                                         //  軟件復位
char code str11[]="AT+CIPSEND=0,15\r\n";                                                                                                                   //  發送數據指令,基于多路連接模式
char code str12[]="Command Executed!\r\n";                                                                                                           //  數據內容

void main()                                                  // 主函數     
{       
//        bit Order_flag;
  P1M0 = 0x00;
  P1M1 = 0x00;
  P3M0 = 0x00;
  P3M1 = 0x00;
  P5M0 = 0x00;
  P5M1 = 0x00;       
//        RST = 1;                                      // ESP8266復位功能腳,拉低會將ESP8266復位
        UartInit();                                                                                                // 初始化串口
        ES = 1;                                       // 串口1中斷打開
        IE2 = 0x01;                                   // 串口2中斷打開
        EA = 1;                                       // 總中斷打開
        DelayMS(1000);                                                                          // 延時一段時間,讓ESP8266啟動
        DelayUS(100);
  U1SendString(Rec_Buf);                        // 將ESP8266啟動信息通過串口1打印出  
  U1SendString("\r\n");       
  U1SendString("Welcome to LCE STUDIO, Please wait while we are getting the device ready\r\n");       
        CLR_Buf();                                                 //清除緩存內容       

       
        while(!Hand("OK"))                            //判斷是否握手成功,如果不成功延時一會,再發送AT握手指令
        {
                U2SendString(str1);                         //發送聯機指令
                DelayMS(500);
        }
        CLR_Buf();                                    //清除緩存內容
  U1SendString("OK,Succeed Establish connection with ESP8266\r\n");               


       
        while(!(Hand("OK")|Hand("no change")))        //判斷是否設置成功,如不成功,延時后再次發送
        {               
                U2SendString(str2);                         //發送設置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);                                //設置為多路連接      
                DelayMS(500);
        }
        CLR_Buf();

        while(!Hand("OK"))                                   // 建立TCP 服務器,并開放端口8888
        {
                U2SendString(str8);                          
                DelayMS(500);
        }
        CLR_Buf();       

        while(!Hand("OK"))                                    //  查詢模塊當前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)                                                        // 主循環  
    {
//  U1SendString(Rec_Buf);                        // 將ESP8266啟動信息通過串口1打印出                       
//                                        DelayMS(1000);       
//                                        DelayMS(1000);                       

                        if(Hand("ESPGLED1"))                            //  收到關閉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"))                            //  收到關閉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"))                            //  收到關閉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"))                            //  收到關閉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"))                            //  收到關閉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"))                            //  收到關閉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"))                            //  收到關閉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"))                            //  收到關閉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"))                                                                             //判斷是否發送數據成功,如不成功,延時后再次發送
                                        {

                                                U2SendString(str11); //數據發送指令
                                                DelayMS(100);               

                                                U2SendString(str12); //數據內容       
                                                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;               
}






歡迎光臨 (http://m.raoushi.com/bbs/) Powered by Discuz! X3.1