Xiaohai Li
/
AirBoxProto
Demo
PeripheralLayer/PeMod_ESP01.cpp@1:0c1053275589, 2015-07-09 (annotated)
- Committer:
- nightseas
- Date:
- Thu Jul 09 05:14:58 2015 +0000
- Revision:
- 1:0c1053275589
- Child:
- 2:0ee90da44162
Update commander and fix sensor bugs.
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 | 1:0c1053275589 | 3 | #define ESP01_CMD_LEN 9 |
nightseas | 1:0c1053275589 | 4 | #define ESP01_DAT_LEN 9 |
nightseas | 1:0c1053275589 | 5 | |
nightseas | 1:0c1053275589 | 6 | #define uart_esp01 uart_sen |
nightseas | 1:0c1053275589 | 7 | //#define uart_db uart_pc |
nightseas | 1:0c1053275589 | 8 | |
nightseas | 1:0c1053275589 | 9 | const uint8_t ESP01_CmdManualMode[ESP01_CMD_LEN] = {0xFF, 0x01, 0x78, 0x41, 0x00, 0x00, 0x00, 0x00, 0x46}; |
nightseas | 1:0c1053275589 | 10 | const uint8_t ESP01_CmdReadCO2[ESP01_CMD_LEN] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; |
nightseas | 1:0c1053275589 | 11 | |
nightseas | 1:0c1053275589 | 12 | int ESP01_Init(void) |
nightseas | 1:0c1053275589 | 13 | { |
nightseas | 1:0c1053275589 | 14 | //Change ESP01 to manual request & reply mode |
nightseas | 1:0c1053275589 | 15 | for(int i=0; i<ESP01_CMD_LEN; i++) |
nightseas | 1:0c1053275589 | 16 | uart_esp01.putc(ESP01_CmdManualMode[i]); |
nightseas | 1:0c1053275589 | 17 | |
nightseas | 1:0c1053275589 | 18 | return 0; |
nightseas | 1:0c1053275589 | 19 | } |
nightseas | 1:0c1053275589 | 20 | |
nightseas | 1:0c1053275589 | 21 | int ESP01_ReadCO2(void) |
nightseas | 1:0c1053275589 | 22 | { |
nightseas | 1:0c1053275589 | 23 | int co2Vol, i, sum = 0; |
nightseas | 1:0c1053275589 | 24 | uint8_t data[ESP01_DAT_LEN]; |
nightseas | 1:0c1053275589 | 25 | |
nightseas | 1:0c1053275589 | 26 | for(i=0; i<ESP01_CMD_LEN; i++) |
nightseas | 1:0c1053275589 | 27 | uart_esp01.putc(ESP01_CmdReadCO2[i]); |
nightseas | 1:0c1053275589 | 28 | |
nightseas | 1:0c1053275589 | 29 | #if defined uart_db |
nightseas | 1:0c1053275589 | 30 | uart_db.printf("\n\rESP01 return "); |
nightseas | 1:0c1053275589 | 31 | #endif |
nightseas | 1:0c1053275589 | 32 | |
nightseas | 1:0c1053275589 | 33 | for(i=0; i<ESP01_DAT_LEN; i++) |
nightseas | 1:0c1053275589 | 34 | { |
nightseas | 1:0c1053275589 | 35 | while(!uart_esp01.readable()); |
nightseas | 1:0c1053275589 | 36 | data[i] = uart_esp01.getc(); |
nightseas | 1:0c1053275589 | 37 | #if defined uart_db |
nightseas | 1:0c1053275589 | 38 | uart_db.printf("0x%02X ", data[i]); |
nightseas | 1:0c1053275589 | 39 | #endif |
nightseas | 1:0c1053275589 | 40 | } |
nightseas | 1:0c1053275589 | 41 | |
nightseas | 1:0c1053275589 | 42 | #if defined uart_db |
nightseas | 1:0c1053275589 | 43 | uart_db.printf(".\n\r"); |
nightseas | 1:0c1053275589 | 44 | #endif |
nightseas | 1:0c1053275589 | 45 | |
nightseas | 1:0c1053275589 | 46 | sum = ESP01_CalCheckSum(data); |
nightseas | 1:0c1053275589 | 47 | if(data[ESP01_DAT_LEN - 1] == sum) |
nightseas | 1:0c1053275589 | 48 | { |
nightseas | 1:0c1053275589 | 49 | co2Vol = data[2] *256 + data[3]; |
nightseas | 1:0c1053275589 | 50 | return co2Vol; |
nightseas | 1:0c1053275589 | 51 | } |
nightseas | 1:0c1053275589 | 52 | else |
nightseas | 1:0c1053275589 | 53 | { |
nightseas | 1:0c1053275589 | 54 | #if defined uart_db |
nightseas | 1:0c1053275589 | 55 | uart_db.printf("Incorrect checksum 0x%02X, expect 0x%02X.\n\r", sum, data[ESP01_DAT_LEN - 1]); |
nightseas | 1:0c1053275589 | 56 | #endif |
nightseas | 1:0c1053275589 | 57 | return -1; |
nightseas | 1:0c1053275589 | 58 | } |
nightseas | 1:0c1053275589 | 59 | } |
nightseas | 1:0c1053275589 | 60 | |
nightseas | 1:0c1053275589 | 61 | uint8_t ESP01_CalCheckSum(uint8_t *packet) |
nightseas | 1:0c1053275589 | 62 | { |
nightseas | 1:0c1053275589 | 63 | uint8_t i, checksum = 0; |
nightseas | 1:0c1053275589 | 64 | for( i = 1; i < 8; i++) |
nightseas | 1:0c1053275589 | 65 | { |
nightseas | 1:0c1053275589 | 66 | checksum += packet[i]; |
nightseas | 1:0c1053275589 | 67 | } |
nightseas | 1:0c1053275589 | 68 | checksum = 0xff - checksum; |
nightseas | 1:0c1053275589 | 69 | checksum += 1; |
nightseas | 1:0c1053275589 | 70 | return checksum; |
nightseas | 1:0c1053275589 | 71 | } |