Xiaohai Li
/
AirBoxProto
Demo
PeripheralLayer/PeMod_ESP01.cpp@2:0ee90da44162, 2016-05-19 (annotated)
- Committer:
- nightseas
- Date:
- Thu May 19 15:52:24 2016 +0000
- Revision:
- 2:0ee90da44162
- Parent:
- 1:0c1053275589
AirBoxProtoDemo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nightseas | 1:0c1053275589 | 1 | #include "SysConfig.h" |
nightseas | 1:0c1053275589 | 2 | |
nightseas | 2:0ee90da44162 | 3 | //ACK waiting time: 2000ms |
nightseas | 2:0ee90da44162 | 4 | #define ESP01_ACK_TIMEOUT 5000 |
nightseas | 2:0ee90da44162 | 5 | |
nightseas | 2:0ee90da44162 | 6 | #define uart_esp uart_sen |
nightseas | 2:0ee90da44162 | 7 | #define uart_db uart_pc |
nightseas | 1:0c1053275589 | 8 | |
nightseas | 2:0ee90da44162 | 9 | //For serial waiting timeout counting |
nightseas | 2:0ee90da44162 | 10 | Timer timer_esp; |
nightseas | 1:0c1053275589 | 11 | |
nightseas | 2:0ee90da44162 | 12 | string ESP01_CmdInitMixMode = "CWMODE=3"; |
nightseas | 2:0ee90da44162 | 13 | string ESP01_CmdInitApInfo = "CWSAP=\"AirBoxProto01\",\"12345678\",6,3"; |
nightseas | 2:0ee90da44162 | 14 | string ESP01_CmdReset = "AT+RST"; |
nightseas | 2:0ee90da44162 | 15 | string ESP01_CmdMuxConnEn = "AT+CIPMUX=1"; |
nightseas | 2:0ee90da44162 | 16 | string ESP01_CmdInitTcpSvr = "AT+CIPSERVER=1,2222"; |
nightseas | 1:0c1053275589 | 17 | |
nightseas | 1:0c1053275589 | 18 | int ESP01_Init(void) |
nightseas | 1:0c1053275589 | 19 | { |
nightseas | 2:0ee90da44162 | 20 | if(ESP01_InterfaceCheck() != 0) |
nightseas | 2:0ee90da44162 | 21 | { |
nightseas | 2:0ee90da44162 | 22 | #if defined uart_db |
nightseas | 2:0ee90da44162 | 23 | uart_db.printf("ESP01_InterfaceCheck failed!\n\r"); |
nightseas | 2:0ee90da44162 | 24 | #endif |
nightseas | 2:0ee90da44162 | 25 | return -1; |
nightseas | 2:0ee90da44162 | 26 | } |
nightseas | 2:0ee90da44162 | 27 | |
nightseas | 2:0ee90da44162 | 28 | ESP01_SendAtCmd(ESP01_CmdInitMixMode); |
nightseas | 2:0ee90da44162 | 29 | |
nightseas | 2:0ee90da44162 | 30 | ESP01_SendAtCmd(ESP01_CmdInitApInfo); |
nightseas | 2:0ee90da44162 | 31 | |
nightseas | 2:0ee90da44162 | 32 | ESP01_SendAtCmd(ESP01_CmdReset); |
nightseas | 2:0ee90da44162 | 33 | |
nightseas | 2:0ee90da44162 | 34 | wait_ms(8000); |
nightseas | 2:0ee90da44162 | 35 | |
nightseas | 2:0ee90da44162 | 36 | ESP01_SendAtCmd(ESP01_CmdMuxConnEn); |
nightseas | 2:0ee90da44162 | 37 | |
nightseas | 2:0ee90da44162 | 38 | ESP01_SendAtCmd(ESP01_CmdInitTcpSvr); |
nightseas | 1:0c1053275589 | 39 | |
nightseas | 1:0c1053275589 | 40 | return 0; |
nightseas | 1:0c1053275589 | 41 | } |
nightseas | 1:0c1053275589 | 42 | |
nightseas | 2:0ee90da44162 | 43 | int ESP01_AckCheck(void) |
nightseas | 2:0ee90da44162 | 44 | { |
nightseas | 2:0ee90da44162 | 45 | int timeStart; |
nightseas | 2:0ee90da44162 | 46 | string data; |
nightseas | 2:0ee90da44162 | 47 | timer_esp.start(); |
nightseas | 2:0ee90da44162 | 48 | timeStart = timer_esp.read_ms(); |
nightseas | 2:0ee90da44162 | 49 | |
nightseas | 2:0ee90da44162 | 50 | while(timer_esp.read_ms() - timeStart < ESP01_ACK_TIMEOUT) |
nightseas | 1:0c1053275589 | 51 | { |
nightseas | 2:0ee90da44162 | 52 | if(uart_esp.readable()) |
nightseas | 2:0ee90da44162 | 53 | { |
nightseas | 2:0ee90da44162 | 54 | while(uart_esp.getc() == 'O') |
nightseas | 2:0ee90da44162 | 55 | { |
nightseas | 2:0ee90da44162 | 56 | if(uart_esp.getc() == 'K') |
nightseas | 2:0ee90da44162 | 57 | return 0; |
nightseas | 2:0ee90da44162 | 58 | } |
nightseas | 2:0ee90da44162 | 59 | } |
nightseas | 2:0ee90da44162 | 60 | } |
nightseas | 1:0c1053275589 | 61 | |
nightseas | 2:0ee90da44162 | 62 | timer_esp.stop(); |
nightseas | 2:0ee90da44162 | 63 | timer_esp.reset(); |
nightseas | 2:0ee90da44162 | 64 | uart_db.printf("ESP01_AckCheck timeout %dms!\n\r", ESP01_ACK_TIMEOUT); |
nightseas | 2:0ee90da44162 | 65 | return -2; |
nightseas | 1:0c1053275589 | 66 | } |
nightseas | 1:0c1053275589 | 67 | |
nightseas | 2:0ee90da44162 | 68 | int ESP01_InterfaceCheck(void) |
nightseas | 1:0c1053275589 | 69 | { |
nightseas | 2:0ee90da44162 | 70 | uart_esp.printf("AT\r\n"); |
nightseas | 2:0ee90da44162 | 71 | return ESP01_AckCheck(); |
nightseas | 1:0c1053275589 | 72 | } |
nightseas | 2:0ee90da44162 | 73 | |
nightseas | 2:0ee90da44162 | 74 | void ESP01_SendAtCmd(string strCmd) |
nightseas | 2:0ee90da44162 | 75 | { |
nightseas | 2:0ee90da44162 | 76 | uart_esp.printf("AT+%s\r\n", strCmd.c_str()); |
nightseas | 2:0ee90da44162 | 77 | } |
nightseas | 2:0ee90da44162 | 78 |