Xiaohai Li / Mbed 2 deprecated AirBoxProto

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PeMod_ESP01.cpp Source File

PeMod_ESP01.cpp

00001 #include "SysConfig.h"
00002 
00003 //ACK waiting time: 2000ms
00004 #define ESP01_ACK_TIMEOUT    5000
00005 
00006 #define uart_esp uart_sen
00007 #define uart_db uart_pc
00008 
00009 //For serial waiting timeout counting
00010 Timer timer_esp;
00011 
00012 string ESP01_CmdInitMixMode = "CWMODE=3";
00013 string ESP01_CmdInitApInfo = "CWSAP=\"AirBoxProto01\",\"12345678\",6,3";
00014 string ESP01_CmdReset = "AT+RST";
00015 string ESP01_CmdMuxConnEn = "AT+CIPMUX=1";
00016 string ESP01_CmdInitTcpSvr = "AT+CIPSERVER=1,2222";
00017 
00018 int ESP01_Init(void)
00019 {
00020     if(ESP01_InterfaceCheck() != 0)
00021     {
00022         #if defined uart_db
00023         uart_db.printf("ESP01_InterfaceCheck failed!\n\r");
00024         #endif
00025         return -1;   
00026     }
00027     
00028     ESP01_SendAtCmd(ESP01_CmdInitMixMode);
00029         
00030     ESP01_SendAtCmd(ESP01_CmdInitApInfo);
00031     
00032     ESP01_SendAtCmd(ESP01_CmdReset);
00033     
00034     wait_ms(8000);
00035     
00036     ESP01_SendAtCmd(ESP01_CmdMuxConnEn);
00037     
00038     ESP01_SendAtCmd(ESP01_CmdInitTcpSvr);
00039     
00040     return 0;   
00041 }
00042 
00043 int ESP01_AckCheck(void)
00044 {   
00045     int timeStart;
00046     string data;
00047     timer_esp.start();
00048     timeStart = timer_esp.read_ms();
00049     
00050     while(timer_esp.read_ms() - timeStart < ESP01_ACK_TIMEOUT)
00051     {
00052         if(uart_esp.readable())
00053         {
00054             while(uart_esp.getc() == 'O')
00055             {
00056                 if(uart_esp.getc() == 'K')
00057                     return 0;
00058             }     
00059         }  
00060     }
00061     
00062     timer_esp.stop();
00063     timer_esp.reset();
00064     uart_db.printf("ESP01_AckCheck timeout %dms!\n\r", ESP01_ACK_TIMEOUT); 
00065     return -2;
00066 }
00067 
00068 int ESP01_InterfaceCheck(void)
00069 {
00070     uart_esp.printf("AT\r\n");
00071     return ESP01_AckCheck();
00072 }
00073 
00074 void ESP01_SendAtCmd(string strCmd)
00075 {
00076     uart_esp.printf("AT+%s\r\n", strCmd.c_str());
00077 }
00078