Xiaohai Li
/
AirBoxProto
Demo
Diff: PeripheralLayer/PeMod_ESP01.cpp
- Revision:
- 1:0c1053275589
- Child:
- 2:0ee90da44162
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PeripheralLayer/PeMod_ESP01.cpp Thu Jul 09 05:14:58 2015 +0000 @@ -0,0 +1,71 @@ +#include "SysConfig.h" + +#define ESP01_CMD_LEN 9 +#define ESP01_DAT_LEN 9 + +#define uart_esp01 uart_sen +//#define uart_db uart_pc + +const uint8_t ESP01_CmdManualMode[ESP01_CMD_LEN] = {0xFF, 0x01, 0x78, 0x41, 0x00, 0x00, 0x00, 0x00, 0x46}; +const uint8_t ESP01_CmdReadCO2[ESP01_CMD_LEN] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; + +int ESP01_Init(void) +{ + //Change ESP01 to manual request & reply mode + for(int i=0; i<ESP01_CMD_LEN; i++) + uart_esp01.putc(ESP01_CmdManualMode[i]); + + return 0; +} + +int ESP01_ReadCO2(void) +{ + int co2Vol, i, sum = 0; + uint8_t data[ESP01_DAT_LEN]; + + for(i=0; i<ESP01_CMD_LEN; i++) + uart_esp01.putc(ESP01_CmdReadCO2[i]); + + #if defined uart_db + uart_db.printf("\n\rESP01 return "); + #endif + + for(i=0; i<ESP01_DAT_LEN; i++) + { + while(!uart_esp01.readable()); + data[i] = uart_esp01.getc(); + #if defined uart_db + uart_db.printf("0x%02X ", data[i]); + #endif + } + + #if defined uart_db + uart_db.printf(".\n\r"); + #endif + + sum = ESP01_CalCheckSum(data); + if(data[ESP01_DAT_LEN - 1] == sum) + { + co2Vol = data[2] *256 + data[3]; + return co2Vol; + } + else + { + #if defined uart_db + uart_db.printf("Incorrect checksum 0x%02X, expect 0x%02X.\n\r", sum, data[ESP01_DAT_LEN - 1]); + #endif + return -1; + } +} + +uint8_t ESP01_CalCheckSum(uint8_t *packet) +{ + uint8_t i, checksum = 0; + for( i = 1; i < 8; i++) + { + checksum += packet[i]; + } + checksum = 0xff - checksum; + checksum += 1; + return checksum; +}