Demo

Dependencies:   mbed

Revision:
2:0ee90da44162
Parent:
1:0c1053275589
--- a/PeripheralLayer/PeMod_ESP01.cpp	Thu Jul 09 05:14:58 2015 +0000
+++ b/PeripheralLayer/PeMod_ESP01.cpp	Thu May 19 15:52:24 2016 +0000
@@ -1,71 +1,78 @@
 #include "SysConfig.h"
 
-#define ESP01_CMD_LEN   9
-#define ESP01_DAT_LEN   9
+//ACK waiting time: 2000ms
+#define ESP01_ACK_TIMEOUT    5000
+
+#define uart_esp uart_sen
+#define uart_db uart_pc
 
-#define uart_esp01 uart_sen
-//#define uart_db uart_pc
+//For serial waiting timeout counting
+Timer timer_esp;
 
-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};
+string ESP01_CmdInitMixMode = "CWMODE=3";
+string ESP01_CmdInitApInfo = "CWSAP=\"AirBoxProto01\",\"12345678\",6,3";
+string ESP01_CmdReset = "AT+RST";
+string ESP01_CmdMuxConnEn = "AT+CIPMUX=1";
+string ESP01_CmdInitTcpSvr = "AT+CIPSERVER=1,2222";
 
 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]);
+    if(ESP01_InterfaceCheck() != 0)
+    {
+        #if defined uart_db
+        uart_db.printf("ESP01_InterfaceCheck failed!\n\r");
+        #endif
+        return -1;   
+    }
+    
+    ESP01_SendAtCmd(ESP01_CmdInitMixMode);
+        
+    ESP01_SendAtCmd(ESP01_CmdInitApInfo);
+    
+    ESP01_SendAtCmd(ESP01_CmdReset);
+    
+    wait_ms(8000);
+    
+    ESP01_SendAtCmd(ESP01_CmdMuxConnEn);
+    
+    ESP01_SendAtCmd(ESP01_CmdInitTcpSvr);
     
     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++)
+int ESP01_AckCheck(void)
+{   
+    int timeStart;
+    string data;
+    timer_esp.start();
+    timeStart = timer_esp.read_ms();
+    
+    while(timer_esp.read_ms() - timeStart < ESP01_ACK_TIMEOUT)
     {
-        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
+        if(uart_esp.readable())
+        {
+            while(uart_esp.getc() == 'O')
+            {
+                if(uart_esp.getc() == 'K')
+                    return 0;
+            }     
+        }  
+    }
     
-    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;
-    }
+    timer_esp.stop();
+    timer_esp.reset();
+    uart_db.printf("ESP01_AckCheck timeout %dms!\n\r", ESP01_ACK_TIMEOUT); 
+    return -2;
 }
 
-uint8_t ESP01_CalCheckSum(uint8_t *packet)
+int ESP01_InterfaceCheck(void)
 {
-    uint8_t i, checksum = 0;
-    for( i = 1; i < 8; i++)
-    {
-        checksum += packet[i];
-    }
-    checksum = 0xff - checksum;
-    checksum += 1;
-    return checksum;
+    uart_esp.printf("AT\r\n");
+    return ESP01_AckCheck();
 }
+
+void ESP01_SendAtCmd(string strCmd)
+{
+    uart_esp.printf("AT+%s\r\n", strCmd.c_str());
+}
+