Data logger: Sensors -> Barometer & temperature (BMP180), Humidity & temp. (RHT03), Sunshine (Cds): Display -> 20 chracters x 4 lines: Strage -> EEPROM (AT24C1024): Special functions -> Enter sleep mode to save current, reading the logging data via serial line

Dependencies:   AT24C1024 BMP180 M41T62 RHT03 TextLCD WakeUp mbed

Fork of LPC1114_barometer_with_data_logging by Kenji Arai

Please refer following Notebook.
http://mbed.org/users/kenjiArai/notebook/mbed-lpc1114fn28-data-logger/

Revision:
18:2a1d75bc8ebc
Parent:
16:f164f8912201
Child:
19:19dd6332d729
--- a/main.cpp	Thu Jul 03 23:07:13 2014 +0000
+++ b/main.cpp	Mon Aug 11 21:56:18 2014 +0000
@@ -6,7 +6,7 @@
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
  *      Created: May       21st, 2014
- *      Revised: July       3rd, 2014
+ *      Revised: August    12th, 2014
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -17,7 +17,7 @@
 /*
  * Function
  *  measure atmospheric pressure and temprerature using Bosch BMP180 pressure sensor
- *  show measured data on AQM0802A LCD and logging such data in EEPROM
+ *  show measured data on a LCD and logging such data in I2C EEPROM
  */
 
 //  Include ---------------------------------------------------------------------------------------
@@ -26,20 +26,11 @@
 #include "RHT03.h"              // Std. lib./ Humidity sensor
 #include "TextLCD.h"            // Std. lib./ LCD control
 #include "m41t62_rtc.h"         // Own lib. / RTC control
+#include "WakeUp.h"
+#include "WakeInterruptIn.h"
 #include "dt_log.h"
 
 //  Definition ------------------------------------------------------------------------------------
-#define USE_MON                 1
-#define USE_BARO                0
-
-#define BOUND                   5       // chattering
-
-// Define cyclic period
-#define SHOW_LED                1       // Time for LED on x mS
-#define CNV_WAIT_T              25      // Waiting time for temp. conversion
-#define CNV_WAIT_P              50      // Waiting time for pressure conversion
-#define LOOP_WAIT               (1000 - (SHOW_LED + CNV_WAIT_T +CNV_WAIT_P))
-
 // ADC related definition
 #define VREF_VOLT               2.482   // TA76431F Vref real measued data
 #define R_FIX                   9930    // 10K ohm <- real measued data
@@ -47,34 +38,36 @@
 #define CDS_TBL_SIZE            13
 
 // Waiting time
-#define STATE_CHANGE_TIME       3
-#define TIME_INTERVAL           600     // 10 minutes
+//#define TIME_INTERVAL           600     // 10 minutes
+#define TIME_INTERVAL           60     // 1 minutes
 
 typedef enum {CDS = 0, VREF, VOL} ADC_Select;
 
+#define BAUD(x)                 pcx.baud(x)
+#define GETC(x)                 pcx.getc(x)
+#define PUTC(x)                 pcx.putc(x)
+#define PRINTF(...)             pcx.printf(__VA_ARGS__)
+#define READABLE(x)             pcx.readable(x)
+
 //  Object ----------------------------------------------------------------------------------------
-Timeout     to;                // wake-up from sleep()
+Serial      pcx(dp16,dp15);
 I2C         i2c(dp5,dp27);      // SDA, SCL
-DigitalOut  myled0(dp28);       // LED for Debug
-DigitalOut  myled1(dp14);       // Indicate state transition
+WakeInterruptIn event(dp25);    // wake-up from deepsleep mode by this interrupt
+DigitalOut  myled(dp14);        // LED for Debug
 DigitalOut  analog_pwr(dp6);    // VCC for analog interface (vol, cds and vref)
 DigitalOut  vref_pwr(dp4);      // VCC for Vref
-DigitalIn   sw_chng(dp1,PullUp);// SW for select
-DigitalIn   sw_mode(dp2,PullUp);// SW for Mode change
 AnalogIn    cds(dp11);          // Input / CDS data
 AnalogIn    vref(dp9);          // Input / Bandgap 2.5V
 AnalogIn    vol(dp10);          // Input / contrast volume
 RHT03       humtemp(dp26);      // RHT03 interface
 BMP180      bmp180(i2c);        // Bosch sensor
 M41T62      m41t62(i2c);        // STmicro RTC(M41T62)
-TextLCD_I2C_N i2clcd(&i2c, 0x7c, TextLCD::LCD8x2);  // LCD(Akizuki AQM0802A)
+TextLCD     lcd(dp1, dp28, dp17, dp18, dp2, dp13, TextLCD::LCD20x4); // rs, e, d4-d7
 
 //  Function prototypes ---------------------------------------------------------------------------
 extern int mon( void);          // only use for debug purpose
 
 //  RAM -------------------------------------------------------------------------------------------
-int flag;
-
 //  ADC
 float av_cds, av_vref, av_vol, cal_vcc;
 float r_cds, lux;
@@ -90,9 +83,6 @@
 float baro;
 float baro_temp;
 
-// EEPROM
-//extern xEeprom_ptr log_inf;
-
 //  ROM / Constant data ---------------------------------------------------------------------------
 // Cds GL5528 (Dark Resistance 1 Mohm type) SENBA OPTICAL & ELECTRONIC CO.,LTD.
 //      Table value referrence: http://homepage3.nifty.com/skomo/f35/hp35_20.htm
@@ -100,31 +90,15 @@
     {{50,21194},{100,8356},{200,3294},{400,1299},{800,512},{1600,202},{3200,79.6},{6400,31.4},
     {12800,12.4},{25600,4.88},{51200,1.92},{102400,0.758},{409600,0.118}};
 
-// LCD screen data
-enum Screen{
-    SCRN_Clear=0, SCRN_Opening, SCRN_Goto_mon, SCRN_Backto_normal
-};
-//                        Clear       Opening      Goto_mon     Backto_normal 
-char scrn_1st[4][10] = { "        ", "LPC1114F",  "GOTO MON",  "Back to"};
-char scrn_2nd[4][10] = { "        ", " JH1PJL ",  " 9600BPS",  " Normal"};
-
-// Disply on LCD
-enum Disp_num{
-    DSP_INIT=0, DSP_BARO, DSP_HUMD, DSP_LUX, DSP_TIME, DSP_LOG, DSP_RTURN
-};
+//                              12345678901234567890
+static char *const msg_clear = "                    ";
+static char *const msg_msg0  = "  mbed LPC1114FN28  ";
+static char *const msg_msg1  = "   by JH1PJL K.Arai ";
+static char *const msg_msg2  = "  Barometer Logger  ";
+static char *const msg_msg3  = "                    ";
+static char *const msg_msg4  = "Goto Monitor/9600bps";
+static char *const msg_msg5  = "  Back to main      ";
 
-// loop time = STATE_CHANGE_TIME * 20 = 3 * 20 = 60 sec (current)
-// rule 1) DSP_INIT is top position, 2)DSP_RTURN is end position, 3) Total number 20
-const uint8_t lcd_disp_tbl[20]
-//         1,        2,        3,        4,        5,
- = {DSP_INIT, DSP_BARO, DSP_BARO, DSP_BARO, DSP_TIME,
-//         6,        7,        8,        9,       10, 
-    DSP_TIME, DSP_HUMD, DSP_HUMD,  DSP_LUX,  DSP_LUX,
-//        11,       12,       13,       14,       15,
-    DSP_BARO, DSP_BARO, DSP_TIME, DSP_TIME, DSP_HUMD,
-//        16,      17,        18,       19,        20 
-    DSP_HUMD, DSP_LUX,   DSP_LUX,  DSP_LOG, DSP_RTURN};
- 
 //-------------------------------------------------------------------------------------------------
 //  Control Program
 //-------------------------------------------------------------------------------------------------
@@ -190,155 +164,129 @@
     }
 }
 
-void set_lcd_screen(int n){
-    i2clcd.locate(0, 0);
-    i2clcd.printf("%s", scrn_1st[n]);
-    i2clcd.locate(0, 1);
-    i2clcd.printf("%s", scrn_2nd[n]);
-}
-
 //-------------------------------------------------------------------------------------------------
 // Application program
 //-------------------------------------------------------------------------------------------------
-// Nothing is done but just wake-up from sleep condition
-void wakeup() {
-    flag = 1;
-    myled1 = !myled1;
-}
-
 // Measure pressure and show it on LCD
 void conv_and_disp(void) {
 tm t;
 time_t seconds = 0;
 time_t old_seconds = 0;
-uint32_t step = 0, dt;
-uint8_t num;
+uint32_t dt;
 
+    // Wakeup from deep sleep
+    WakeUp::calibrate();
+    // Initialize data
+    av_cds = 0;
+    av_vref = 0;
+    av_vol = 0;
+    humidity_temp = 0;
+    humidity = 0;
+    // RTC
+    m41t62.set_sq_wave(RTC_SQW_NONE);
+    m41t62.read_rtc_std(&t);
+    old_seconds = mktime(&t);
     while (true) {          // infinit loop for measure and display
-        // Call wakeup()function after specific time
-        to.attach(&wakeup, STATE_CHANGE_TIME);    // this is for wake-up
-//---- State Control ----
-        num = lcd_disp_tbl[step++];
-        switch (num){
-    //  ---------- Initialize data ----------------------------------------------------------------
-        case DSP_INIT:
-            av_cds = 0;
-            av_vref = 0;
-            av_vol = 0;
-            humidity_temp = 0;
-            humidity = 0;
-            // RTC
-            m41t62.set_sq_wave(RTC_SQW_NONE);
-            m41t62.read_rtc_std(&t);
-            old_seconds = mktime(&t); 
-            break;
-    //  ---------- Cds Sensor, Vref, Volume -------------------------------------------------------
-        case DSP_LUX:
-            // Power on / Analog sensor
-            analog_pwr = 1;
-            vref_pwr = 1;
-            wait(0.2);
-            adc_all_read();
-            // Power off / Analog sensor
-            analog_pwr = 0; 
-            // Normalize
-            adc_normalize(CDS);
-            adc_normalize(VREF);
-            adc_normalize(VOL);
-            set_lcd_screen(SCRN_Clear);
-            i2clcd.locate(0, 0);    // 1st line top
-            //             12345678
-            i2clcd.printf("L:%.1f", lux);
-            i2clcd.locate(0, 1);    // 2nd line top
-            i2clcd.printf("V:%.3f", cal_vcc);
-            break;
     //  ---------- Barometer Sensor / BMP180 ------------------------------------------------------
-        case DSP_BARO:
-            bmp180.normalize();
-            baro = bmp180.read_pressure();
-            baro_temp = bmp180.read_temperature();
-            set_lcd_screen(SCRN_Clear);
-            i2clcd.locate(0, 0);    // 1st line top
-            i2clcd.printf("P:%.1f", baro);
-            i2clcd.locate(0, 1);    // 2nd line top
-            i2clcd.printf("T:%\+-6.1f", baro_temp);
-            break;
+        bmp180.normalize();
+        baro = bmp180.read_pressure();
+        baro_temp = bmp180.read_temperature();
     //  ---------- Humidity Sensor / RHT03 --------------------------------------------------------
-        case DSP_HUMD:
-            hum_RHT03_read();       // Read Humidity data then avaraging
-            set_lcd_screen(SCRN_Clear);
-            i2clcd.locate(0, 0);    // 1st line top
-            i2clcd.printf("H:%.1f", humidity);
-            i2clcd.locate(0, 1);    // 2nd line top
-            i2clcd.printf("T:%\+-6.1f", humidity_temp);
-            break;
+        hum_RHT03_read();       // Read Humidity data then avaraging
+    //  ---------- Cds Sensor, Vref, Volume -------------------------------------------------------
+        wait(0.2);
+        adc_all_read();
+        // Normalize
+        adc_normalize(CDS);
+        adc_normalize(VREF);
+        adc_normalize(VOL);
     //  ---------- RTC ----------------------------------------------------------------------------
-        case DSP_TIME:
-            m41t62.read_rtc_std(&t);
-            seconds = mktime(&t);
-            set_lcd_screen(SCRN_Clear);
-            i2clcd.locate(0, 0);    // 1st line top
-            i2clcd.printf("%02d/%02d/%02d", t.tm_year % 100, t.tm_mon + 1, t.tm_mday);
-            i2clcd.locate(0, 1);    // 2nd line top
-            i2clcd.printf("%02d:%02d:%02d", t.tm_hour, t.tm_min, t.tm_sec);
-            break;
+        m41t62.read_rtc_std(&t);
+        seconds = mktime(&t);
     //  ---------- EEPROM Logging -----------------------------------------------------------------
-        case DSP_LOG:
-            if (seconds >= old_seconds + TIME_INTERVAL){
-                // Data Logging action
-                old_seconds += TIME_INTERVAL;
-                dtlog_data_pack();  // Get EEPROM resource
-                dtlog_one_write();  // Write data to EEPROM
-                dt = (uint32_t)dtlog_buf_occupation();
-                dt = (dt * 1000)/ BLK_NO;
-            }     
-            set_lcd_screen(SCRN_Clear);
-            i2clcd.locate(0, 0);    // 1st line top
-            i2clcd.printf("Logging");
-            i2clcd.locate(0, 1);    // 2nd line top
-            i2clcd.printf("%d.%01d%%", dt / 10, dt % 10);
-            break;
-    //  ---------- return (loop) ------------------------------------------------------------------
-        case DSP_RTURN:
-        default:
-            //  <State> State change 
-            step = 1;   // if something wrong, go to reset condition
+        if (seconds >= old_seconds + TIME_INTERVAL){
+            // Data Logging action
+            old_seconds += TIME_INTERVAL;
+            dtlog_data_pack();  // Get EEPROM resource
+            dtlog_one_write();  // Write data to EEPROM
         }
+        dt = (uint32_t)dtlog_buf_occupation();
+        dt = (dt * 1000)/ BLK_NO;
+    //  ---------- Display all data on a LCD ------------------------------------------------------      
+        //  Printf:: Barometer Sensor / BMP180
+        lcd.locate(0, 0);    // 1st line top
+        lcd.printf("%s", msg_clear);
+        lcd.locate(0, 0);    // 1st line top
+        lcd.printf("%6.1fhPa    %\+-5.1f%cC", baro, baro_temp, 0xdf);
+        //  Printf:: Humidity Sensor / RHT03 + Vcc
+        lcd.locate(0, 1);    // 2nd line top
+        lcd.printf("%s", msg_clear);
+        lcd.locate(0, 1);    // 2nd line top
+#if 0
+        lcd.printf("%4.1f%%(%\+-4.1f%cC) %.2fV", humidity, humidity_temp, 0xdf, cal_vcc);
+#else
+        lcd.printf("%.2fV  %4.1f%% %\+-4.1f%cC", cal_vcc, humidity, humidity_temp, 0xdf);
+#endif
+        //  Printf:: Cds Sensor + EEPROM Logging
+        lcd.locate(0, 2);    // 3rd line top
+        lcd.printf("%s", msg_clear);
+        lcd.locate(0, 2);    // 3rd line top
+        if (dt <= 998){
+            lcd.printf("%8.1fLx Log:%2d.%01d%%", lux,  dt / 10, dt % 10);
+        } else {
+            lcd.printf("%8.1fLx Log: full", lux);
+        }
+        //  Printf:: RTC (Date & Time)
+        lcd.locate(0, 3);    // 4th line top
+        lcd.printf("%s", msg_clear);
+        lcd.locate(0, 3);    // 4th line top
+#if 0
+        lcd.printf("20%02d/%02d/%02d  %02d:%02d:%02d",
+                    t.tm_year % 100, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
+#else
+        lcd.printf("20%02d/%02d/%02d %02d:%02d PJL",
+                    t.tm_year % 100, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min);
+#endif
     //  ---------- back to top --------------------------------------------------------------------
-        myled0 = !myled0;
-        while (flag == 0){ wait(0.01);}
-        wait(0.1);
-        myled1 = !myled1;
-        flag = 0;
+        myled = 1;
+        wait(0.001);
+        myled = 0;
+        // Power off - Analog sensor
+        analog_pwr = 0;
+        vref_pwr = 0;
+        // Wait with sleep
+        WakeUp::set(10);
+        wait(0.001);    // looks important for works well
+        myled = 1;
+        wait(0.001);
+        myled = 0;
+        // Power on - Analog sensor
+        analog_pwr = 1;
+        vref_pwr = 1;
     }
 }
 
 // Application program starts here
 int main() {
-    flag = 0;
-    i2clcd.setContrast(25);
-    set_lcd_screen(SCRN_Opening);
-    if (sw_chng == 0){  // SW ON
+    // Initial screen
+    lcd.locate(0, 0);
+    lcd.printf(msg_msg0);
+    lcd.printf(msg_msg1);
+    lcd.printf(msg_msg2);
+    lcd.printf(msg_msg3);
+    myled = 1;
+    wait(3);    // show initial screen and wait serial input
+    myled = 0;
     //--  Enter Monitor Mode  --
-        myled1 = 1;     wait(0.5);
-        myled1 = 0;     wait(0.5);
-        myled1 = 1;     wait(0.5);
-        if (sw_chng == 0){  // Still SW ON
-            myled1 = 0;     wait(0.5);
-            myled1 = 1;     wait(0.5);
-            if (sw_mode == 0){ // SW ON
-                set_lcd_screen(SCRN_Clear);
-                set_lcd_screen(SCRN_Goto_mon);
-                myled1 = 0;
-                mon();          // mon.cpp
-            }
-            set_lcd_screen(SCRN_Clear);
-            set_lcd_screen(SCRN_Backto_normal);
-        }
+    if (READABLE()){
+        lcd.locate(0, 0);
+        lcd.printf(msg_msg4);
+        mon();          // mon.cpp
+        lcd.locate(0, 0);
+        lcd.printf(msg_msg5);
+        wait(0.5);
     }
     //--  Enter Normal Mode  --
-    myled0 = 1;
-    myled1 = 0;
     while (true) {  // Start main program
         conv_and_disp();
     }