Demo

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
nightseas
Date:
Thu May 19 15:52:24 2016 +0000
Parent:
1:0c1053275589
Commit message:
AirBoxProtoDemo

Changed in this revision

AplicationLayer/DebugCommander.cpp Show annotated file Show diff for this revision Revisions of this file
PeripheralLayer/AirAdaptorLib.cpp Show annotated file Show diff for this revision Revisions of this file
PeripheralLayer/AirAdaptorLib.h Show annotated file Show diff for this revision Revisions of this file
PeripheralLayer/PeMod_BMP180.cpp Show annotated file Show diff for this revision Revisions of this file
PeripheralLayer/PeMod_BMP180.h Show annotated file Show diff for this revision Revisions of this file
PeripheralLayer/PeMod_ESP01.cpp Show annotated file Show diff for this revision Revisions of this file
PeripheralLayer/PeMod_ESP01.h Show annotated file Show diff for this revision Revisions of this file
PeripheralLayer/PeMod_MHZ19.cpp Show annotated file Show diff for this revision Revisions of this file
PeripheralLayer/PeMod_MHZ19.h Show annotated file Show diff for this revision Revisions of this file
PeripheralLayer/PeMod_UV370.cpp Show annotated file Show diff for this revision Revisions of this file
PeripheralLayer/PeMod_UV370.h Show annotated file Show diff for this revision Revisions of this file
SysConfig.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 0c1053275589 -r 0ee90da44162 AplicationLayer/DebugCommander.cpp
--- a/AplicationLayer/DebugCommander.cpp	Thu Jul 09 05:14:58 2015 +0000
+++ b/AplicationLayer/DebugCommander.cpp	Thu May 19 15:52:24 2016 +0000
@@ -114,6 +114,7 @@
         uart_db.printf(" w. Test WiFi module.\n\r");
         uart_db.printf(" m. Manual switch SMUX channel.\n\r");        
         uart_db.printf(" q. Exit demo.\n\r\n\r");
+        uart_db.printf(" Input:\n\r\n\r");
                               
         while(!uart_db.readable())
         {
@@ -171,6 +172,16 @@
             case '6':
                 DebugFunc_UvLevel();
                 break;    
+                
+            case 'z':
+                MHZ19_CalZero();
+                break;    
+                
+                
+            case 'f':
+                MHZ19_CalFull();
+                break;  
+            
             default:
                 uart_db.printf("Incorrect input!\n\r");
                 break;
diff -r 0c1053275589 -r 0ee90da44162 PeripheralLayer/AirAdaptorLib.cpp
--- a/PeripheralLayer/AirAdaptorLib.cpp	Thu Jul 09 05:14:58 2015 +0000
+++ b/PeripheralLayer/AirAdaptorLib.cpp	Thu May 19 15:52:24 2016 +0000
@@ -16,6 +16,8 @@
 //GPIO for CD4052 serial mux control
 DigitalOut smux_a(PB_14, 0), smux_b(PB_13, 0), smux_oe_n(PB_15, 1);
 
+AnalogIn adc_ch10(PC_0), adc_ch11(PC_1);
+
 
 //Init board library
 //--> Success return 0
@@ -159,4 +161,4 @@
 {
     for(int ledNum = 0; ledNum < LED_NUM_MAX; ledNum++)
         LedOff(ledNum);    
-}
\ No newline at end of file
+}
diff -r 0c1053275589 -r 0ee90da44162 PeripheralLayer/AirAdaptorLib.h
--- a/PeripheralLayer/AirAdaptorLib.h	Thu Jul 09 05:14:58 2015 +0000
+++ b/PeripheralLayer/AirAdaptorLib.h	Thu May 19 15:52:24 2016 +0000
@@ -17,6 +17,9 @@
 //GPIO for CD4052 serial mux control
 extern DigitalOut smux_a, smux_b, smux_oe_n;
 
+//ADC for analog sensors
+extern AnalogIn adc_ch10, adc_ch11;
+
 //Init functions
 extern int BoardLibInit(void);
 
diff -r 0c1053275589 -r 0ee90da44162 PeripheralLayer/PeMod_BMP180.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PeripheralLayer/PeMod_BMP180.cpp	Thu May 19 15:52:24 2016 +0000
@@ -0,0 +1,49 @@
+#include "SysConfig.h"
+
+#define BMP180_ADDR          (0x40<<1)   //BMP180 address
+#define BMP180_CMD_TMP_NH    (0xF3)      //Non-hold temp trigger command
+#define BMP180_CMD_RH_NH     (0xF5)      //Non-hold temp trigger command
+
+#define i2c_bmp180 i2c_sen
+//#define uart_db uart_pc
+
+int BMP180_Init(void)
+{
+    return 0;    
+}
+
+float BMP180_ReadTemp(void)
+{
+    char cmd = BMP180_CMD_TMP_NH, data[3]; 
+    float temp;
+    
+    i2c_bmp180.write(BMP180_ADDR, &cmd, 1, 1);
+    wait_us(20);
+    i2c_bmp180.stop();
+    
+    wait_ms(100);
+    i2c_bmp180.read(BMP180_ADDR, data, 3);
+    #if defined uart_db
+    uart_db.printf("\n\rBMP180 return 0x%02X 0x%02X 0x%02X\n\r", data[0], data[1], data[2]);
+    #endif
+    temp = (256.0 * data[0] + data[1]) * 175.72 / (0x1 << 16) - 46.85;
+    return temp;
+}
+
+float BMP180_ReadRh(void)
+{
+    char cmd = BMP180_CMD_RH_NH, data[3]; 
+    float rh;
+    
+    i2c_bmp180.write(BMP180_ADDR, &cmd, 1, 1);
+    wait_us(20);
+    i2c_bmp180.stop();
+    
+    wait_ms(100);
+    i2c_bmp180.read(BMP180_ADDR, data, 3);
+    #if defined uart_db
+    uart_db.printf("\n\rBMP180 return 0x%02X 0x%02X 0x%02X\n\r", data[0], data[1], data[2]);
+    #endif
+    rh = (256.0 * data[0] + data[1]) * 125 / (0x1 << 16) - 6;
+    return rh;    
+}
\ No newline at end of file
diff -r 0c1053275589 -r 0ee90da44162 PeripheralLayer/PeMod_BMP180.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PeripheralLayer/PeMod_BMP180.h	Thu May 19 15:52:24 2016 +0000
@@ -0,0 +1,8 @@
+#ifndef _PE_BMP180_
+#define _PE_BMP180_
+
+extern int BMP180_Init(void);
+extern float BMP180_ReadTemp(void);
+extern float BMP180_ReadRh(void);
+
+#endif
\ No newline at end of file
diff -r 0c1053275589 -r 0ee90da44162 PeripheralLayer/PeMod_ESP01.cpp
--- 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());
+}
+
diff -r 0c1053275589 -r 0ee90da44162 PeripheralLayer/PeMod_ESP01.h
--- a/PeripheralLayer/PeMod_ESP01.h	Thu Jul 09 05:14:58 2015 +0000
+++ b/PeripheralLayer/PeMod_ESP01.h	Thu May 19 15:52:24 2016 +0000
@@ -2,8 +2,8 @@
 #define _PE_ESP01_
 
 extern int ESP01_Init(void);
-extern int ESP01_ReadCO2(void);
-
-extern uint8_t ESP01_CalCheckSum(uint8_t *packet);
+extern int ESP01_InterfaceCheck(void);
+extern void ESP01_SendAtCmd(string strCmd);
+extern int ESP01_AckCheck(void);
 
 #endif
\ No newline at end of file
diff -r 0c1053275589 -r 0ee90da44162 PeripheralLayer/PeMod_MHZ19.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PeripheralLayer/PeMod_MHZ19.cpp	Thu May 19 15:52:24 2016 +0000
@@ -0,0 +1,84 @@
+#include "SysConfig.h"
+
+#define MHZ19_CMD_LEN   9
+#define MHZ19_DAT_LEN   9
+
+#define uart_mhz19 uart_sen
+//#define uart_db uart_pc
+
+const uint8_t MHZ19_CmdMCalZero[MHZ19_CMD_LEN] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78};
+const uint8_t MHZ19_CmdMCalFull[MHZ19_CMD_LEN] = {0xFF, 0x01, 0x88, 0x07, 0xD0, 0x00, 0x00, 0x00, 0xA0};
+const uint8_t MHZ19_CmdReadCO2[MHZ19_CMD_LEN] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
+
+int MHZ19_Init(void)
+{
+    return 0;   
+}
+
+int MHZ19_ReadCO2(void)
+{
+    int co2Vol, i, sum = 0; 
+    uint8_t data[MHZ19_DAT_LEN];
+           
+    for(i=0; i<MHZ19_CMD_LEN; i++)
+        uart_mhz19.putc(MHZ19_CmdReadCO2[i]);
+        
+    #if defined uart_db
+    uart_db.printf("\n\rMHZ19 return ");
+    #endif
+        
+    for(i=0; i<MHZ19_DAT_LEN; i++)
+    {
+        while(!uart_mhz19.readable());
+        data[i] = uart_mhz19.getc();
+        #if defined uart_db
+        uart_db.printf("0x%02X ", data[i]);
+        #endif
+    }    
+    
+    #if defined uart_db
+    uart_db.printf(".\n\r");
+    #endif
+    
+    sum = MHZ19_CalCheckSum(data);
+    if(data[MHZ19_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[MHZ19_DAT_LEN - 1]);
+        #endif
+        return -1;
+    }
+}
+
+int MHZ19_CalZero(void)
+{ 
+    for(int i=0; i<MHZ19_CMD_LEN; i++)
+        uart_mhz19.putc(MHZ19_CmdMCalZero[i]);
+        
+    return 0;        
+}
+
+int MHZ19_CalFull(void)
+{
+    for(int i=0; i<MHZ19_CMD_LEN; i++)
+        uart_mhz19.putc(MHZ19_CmdMCalFull[i]);
+        
+    return 0;        
+}
+
+uint8_t MHZ19_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;
+}
diff -r 0c1053275589 -r 0ee90da44162 PeripheralLayer/PeMod_MHZ19.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PeripheralLayer/PeMod_MHZ19.h	Thu May 19 15:52:24 2016 +0000
@@ -0,0 +1,12 @@
+#ifndef _PE_MHZ19_
+#define _PE_MHZ19_
+
+extern int MHZ19_Init(void);
+extern int MHZ19_ReadCO2(void);
+
+extern int MHZ19_CalZero(void);
+extern int MHZ19_CalFull(void);
+
+extern uint8_t MHZ19_CalCheckSum(uint8_t *packet);
+
+#endif
\ No newline at end of file
diff -r 0c1053275589 -r 0ee90da44162 PeripheralLayer/PeMod_UV370.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PeripheralLayer/PeMod_UV370.cpp	Thu May 19 15:52:24 2016 +0000
@@ -0,0 +1,50 @@
+#include "SysConfig.h"
+
+#define UV_LV_MAX   11
+#define UV_SMP_AVG  5
+const int UvLvTable[] = {50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170};
+
+#define i2c_uv i2c_sen
+//#define uart_db uart_pc
+
+int UV_Init(void)
+{
+    return 0;    
+}
+
+float UV_ReadVolt(void)
+{    
+    return(adc_ch10.read() * 3300);
+}
+
+float UV_ReadLv(void)
+{
+    int i;
+    float volt = 0, level;
+    for(i=0; i<UV_SMP_AVG; i++)
+        volt += UV_ReadVolt();
+    volt = volt / UV_SMP_AVG ;
+    
+    if(volt < (float)UvLvTable[0])
+    {
+        level = 0;  
+        return level;
+    }
+    
+    if(volt > (float)UvLvTable[UV_LV_MAX])
+    {
+        level = 11;
+        return level;
+    }
+    
+    for(i=0; i<UV_LV_MAX; i++)
+    {
+        if(volt > (float)UvLvTable[i] && volt < (float)UvLvTable[i+1])
+        {
+            level = i + (volt - UvLvTable[i]) / (UvLvTable[i+1] - UvLvTable[i]);
+            return level;
+        }
+    }
+    
+    return -1;
+}
\ No newline at end of file
diff -r 0c1053275589 -r 0ee90da44162 PeripheralLayer/PeMod_UV370.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PeripheralLayer/PeMod_UV370.h	Thu May 19 15:52:24 2016 +0000
@@ -0,0 +1,8 @@
+#ifndef _PE_UV_
+#define _PE_UV_
+
+extern int UV_Init(void);
+extern float UV_ReadTemp(void);
+extern float UV_ReadRh(void);
+
+#endif
\ No newline at end of file
diff -r 0c1053275589 -r 0ee90da44162 SysConfig.h
--- a/SysConfig.h	Thu Jul 09 05:14:58 2015 +0000
+++ b/SysConfig.h	Thu May 19 15:52:24 2016 +0000
@@ -1,6 +1,7 @@
 
 //System Includings
 #include "mbed.h"
+#include <string>
 
 //Application Layer Modules
 #include "DebugCommander.h"
@@ -10,4 +11,5 @@
 #include "PeMod_SHT20.h"
 #include "PeMod_MHZ19.h"
 #include "PeMod_ZE08.h"
-#include "PeMod_ZPH01.h"
\ No newline at end of file
+#include "PeMod_ZPH01.h"
+#include "PeMod_ESP01.h"
\ No newline at end of file
diff -r 0c1053275589 -r 0ee90da44162 main.cpp
--- a/main.cpp	Thu Jul 09 05:14:58 2015 +0000
+++ b/main.cpp	Thu May 19 15:52:24 2016 +0000
@@ -28,6 +28,9 @@
     SerialMuxSel(1);   
     ret += ZPH01_Init();
     
+    SerialMuxSel(2); 
+    //ret += ESP01_Init();
+    
     SerialMuxSel(3);   
     ret += ZE08_Init();
     return ret;