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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * mbed Application program for the mbed LPC1114FN28
00003  * Barometer program for only for LPC1114FN28  
00004  *
00005  * Copyright (c) 2014 Kenji Arai / JH1PJL
00006  *  http://www.page.sannet.ne.jp/kenjia/index.html
00007  *  http://mbed.org/users/kenjiArai/
00008  *      Created: May       21st, 2014
00009  *      Revised: August    19th, 2014
00010  *
00011  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
00012  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
00013  * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00014  * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00015  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00016  */
00017 /*
00018  * Function
00019  *  measure atmospheric pressure and temprerature using Bosch BMP180 pressure sensor
00020  *  show measured data on a LCD and logging such data in I2C EEPROM
00021  */
00022 
00023 //  Include ---------------------------------------------------------------------------------------
00024 #include "mbed.h"
00025 #include "BMP180.h"             // Own lib. / Pressure sensor             
00026 #include "RHT03.h"              // Std. lib./ Humidity sensor
00027 #include "TextLCD.h"            // Std. lib./ LCD control
00028 #include "m41t62_rtc.h"         // Own lib. / RTC control
00029 #include "WakeUp.h"
00030 #include "WakeInterruptIn.h"
00031 #include "dt_log.h"
00032 
00033 //  Definition ------------------------------------------------------------------------------------
00034 // ADC related definition
00035 #define VREF_VOLT               2.482   // TA76431F Vref real measued data
00036 #define R_FIX                   9930    // 10K ohm <- real measued data
00037 #define VOL_OFFSET              3       // Offset data ,= real measured data
00038 #define CDS_TBL_SIZE            13
00039 
00040 // Waiting time
00041 #define TIME_INTERVAL           600     // 10 minutes
00042 //#define TIME_INTERVAL           60     // 1 minutes
00043 
00044 typedef enum {CDS = 0, VREF, VOL} ADC_Select;
00045 
00046 #define BAUD(x)                 pcx.baud(x)
00047 #define GETC(x)                 pcx.getc(x)
00048 #define PUTC(x)                 pcx.putc(x)
00049 #define PRINTF(...)             pcx.printf(__VA_ARGS__)
00050 #define READABLE(x)             pcx.readable(x)
00051 
00052 //  Object ----------------------------------------------------------------------------------------
00053 Serial      pcx(dp16,dp15);
00054 I2C         i2c(dp5,dp27);      // SDA, SCL
00055 WakeInterruptIn event(dp25);    // wake-up from deepsleep mode by this interrupt
00056 DigitalOut  myled(dp14);        // LED for Debug
00057 DigitalOut  analog_pwr(dp6);    // VCC for analog interface (vol, cds and vref)
00058 DigitalOut  vref_pwr(dp4);      // VCC for Vref
00059 AnalogIn    cds(dp11);          // Input / CDS data
00060 AnalogIn    vref(dp9);          // Input / Bandgap 2.5V
00061 AnalogIn    vol(dp10);          // Input / contrast volume
00062 RHT03       humtemp(dp26);      // RHT03 interface
00063 BMP180      bmp180(i2c);        // Bosch sensor
00064 M41T62      m41t62(i2c);        // STmicro RTC(M41T62)
00065 TextLCD     lcd(dp1, dp28, dp17, dp18, dp2, dp13, TextLCD::LCD20x4); // rs, e, d4-d7
00066 
00067 //  Function prototypes ---------------------------------------------------------------------------
00068 extern int mon( void);          // only use for debug purpose
00069 
00070 //  RAM -------------------------------------------------------------------------------------------
00071 //  ADC
00072 float av_cds, av_vref, av_vol, cal_vcc;
00073 float r_cds, lux;
00074 uint32_t nor_vol;
00075 
00076 //  Humidity Sensor
00077 float humidity_temp, humidity;
00078 
00079 //  EEPROM
00080 uint8_t eep_buf[256 + 2];
00081 
00082 // Barometer
00083 float baro;
00084 float baro_temp;
00085 
00086 //  ROM / Constant data ---------------------------------------------------------------------------
00087 // Cds GL5528 (Dark Resistance 1 Mohm type) SENBA OPTICAL & ELECTRONIC CO.,LTD.
00088 //      Table value referrence: http://homepage3.nifty.com/skomo/f35/hp35_20.htm
00089 const float lux_cds[CDS_TBL_SIZE][2] =
00090     {{50,21194},{100,8356},{200,3294},{400,1299},{800,512},{1600,202},{3200,79.6},{6400,31.4},
00091     {12800,12.4},{25600,4.88},{51200,1.92},{102400,0.758},{409600,0.118}};
00092 
00093 //                              12345678901234567890
00094 static char *const msg_clear = "                    ";
00095 static char *const msg_msg0  = "  mbed LPC1114FN28  ";
00096 static char *const msg_msg1  = "   by JH1PJL K.Arai ";
00097 static char *const msg_msg2  = "  Barometer Logger  ";
00098 static char *const msg_msg3  = "                    ";
00099 static char *const msg_msg4  = "Goto Monitor/9600bps";
00100 static char *const msg_msg5  = "  Back to main      ";
00101 
00102 //-------------------------------------------------------------------------------------------------
00103 //  Control Program
00104 //-------------------------------------------------------------------------------------------------
00105 // Normalize ADC data
00106 void adc_normalize (ADC_Select n){
00107 int i;
00108 float x1,y1,dx;
00109 
00110     switch (n){
00111     case CDS:
00112         // v_adc = Rfix / (Rcds + Rfix) ->  Rcds = ( Rfix / v_adc ) - Rfix 
00113         r_cds = (R_FIX / av_cds) - R_FIX;
00114         // CDS resistance to Lux conversion using convertion table (luc_cds[][])
00115         for (i =0; i < CDS_TBL_SIZE; i++){  // search table
00116             if ( r_cds <= lux_cds[i][0]){ break; }
00117         }
00118         // Check table position
00119         if (i == 0){    lux = lux_cds[0][1];    break;
00120         } else if ( i == CDS_TBL_SIZE ){
00121             if ( r_cds <= lux_cds[i][0] ){
00122                 lux = lux_cds[i-1][1];
00123                 break;
00124             }
00125         }
00126         //  Linear interpolation
00127         y1 = lux_cds[i-1][1] - lux_cds[i][1];
00128         x1 = lux_cds[i][0] - lux_cds[i-1][0];
00129         dx = r_cds - lux_cds[i-1][0];
00130         lux = lux_cds[i-1][1] - ((dx/x1) * y1);
00131         break;
00132     case VREF:  //  vref = VREF_VOLT / VCC -> VCC = VREF_VOLT / vref
00133         cal_vcc = VREF_VOLT / vref;
00134         break;
00135     case VOL:   // Vol center = 1.00 (actual 100)
00136         nor_vol = (uint32_t)(av_vol * 200) + VOL_OFFSET;
00137         break;
00138     }
00139 }
00140 
00141 //  Read adc data and averaging
00142 void adc_all_read (void){
00143     if (av_cds == 0){   av_cds = cds.read();
00144     } else {            av_cds = av_cds *0.5 + cds.read() * 0.5;
00145     }
00146     if (av_vref == 0){  av_vref = vref.read();
00147     } else {            av_vref = av_vref *0.9 + vref.read() * 0.1;
00148     }
00149     if (av_vol == 0){   av_vol = vol.read();
00150     } else {            av_vol = av_vol *0.2 + vol.read() * 0.8;
00151     } 
00152 }
00153 
00154 // Read Humidity sensor data
00155 void hum_RHT03_read (void){
00156     while (true){   // wait data
00157         if ( humtemp.readData() == RHT_ERROR_NONE ){ break; }
00158     }
00159     if (humidity_temp == 0){humidity_temp = humtemp.getTemperatureC();
00160     } else {                humidity_temp = humidity_temp * 0.9 + humtemp.getTemperatureC() * 0.1;
00161     }
00162     if ( humidity == 0 ){   humidity = humtemp.getHumidity();
00163     } else {                humidity = humidity * 0.9 + humtemp.getHumidity() * 0.1;
00164     }
00165 }
00166 
00167 //-------------------------------------------------------------------------------------------------
00168 // Application program
00169 //-------------------------------------------------------------------------------------------------
00170 // Measure pressure and show it on LCD
00171 void conv_and_disp(void) {
00172 tm t;
00173 time_t seconds = 0;
00174 time_t old_seconds = 0;
00175 uint32_t dt;
00176 
00177     // Wakeup from deep sleep
00178     WakeUp::calibrate();
00179     // Initialize data
00180     av_cds = 0;
00181     av_vref = 0;
00182     av_vol = 0;
00183     humidity_temp = 0;
00184     humidity = 0;
00185     // RTC
00186     m41t62.set_sq_wave(RTC_SQW_NONE);
00187     m41t62.read_rtc_std(&t);
00188     old_seconds = mktime(&t);
00189     while (true) {          // infinit loop for measure and display
00190     //  ---------- Barometer Sensor / BMP180 ------------------------------------------------------
00191         bmp180.normalize();
00192         baro = bmp180.read_pressure();
00193         baro_temp = bmp180.read_temperature();
00194     //  ---------- Humidity Sensor / RHT03 --------------------------------------------------------
00195         hum_RHT03_read();       // Read Humidity data then avaraging
00196     //  ---------- Cds Sensor, Vref, Volume -------------------------------------------------------
00197         wait(0.2);
00198         adc_all_read();
00199         // Normalize
00200         adc_normalize(CDS);
00201         adc_normalize(VREF);
00202         adc_normalize(VOL);
00203     //  ---------- RTC ----------------------------------------------------------------------------
00204         m41t62.read_rtc_std(&t);
00205         seconds = mktime(&t);
00206     //  ---------- EEPROM Logging -----------------------------------------------------------------
00207         if (seconds >= old_seconds + TIME_INTERVAL){
00208             // Data Logging action
00209             old_seconds += TIME_INTERVAL;
00210             dtlog_data_pack();  // Get EEPROM resource
00211             dtlog_one_write();  // Write data to EEPROM
00212         }
00213         dt = (uint32_t)dtlog_buf_occupation();
00214         dt = (dt * 1000)/ BLK_NO;
00215     //  ---------- Display all data on a LCD ------------------------------------------------------      
00216         //  Printf:: Barometer Sensor / BMP180
00217         lcd.locate(0, 0);    // 1st line top
00218         lcd.printf("%s", msg_clear);
00219         lcd.locate(0, 0);    // 1st line top
00220         lcd.printf("%6.1fhPa    %\+-5.1f%cC", baro, baro_temp, 0xdf);
00221         //  Printf:: Humidity Sensor / RHT03 + Vcc
00222         lcd.locate(0, 1);    // 2nd line top
00223         lcd.printf("%s", msg_clear);
00224         lcd.locate(0, 1);    // 2nd line top
00225 #if 0
00226         lcd.printf("%4.1f%%(%\+-4.1f%cC) %.2fV", humidity, humidity_temp, 0xdf, cal_vcc);
00227 #else
00228         lcd.printf("%.2fV  %4.1f%% %\+-4.1f%cC", cal_vcc, humidity, humidity_temp, 0xdf);
00229 #endif
00230         //  Printf:: Cds Sensor + EEPROM Logging
00231         lcd.locate(0, 2);    // 3rd line top
00232         lcd.printf("%s", msg_clear);
00233         lcd.locate(0, 2);    // 3rd line top
00234         if (dt <= 998){
00235             lcd.printf("%8.1fLx Log:%2d.%01d%%", lux,  dt / 10, dt % 10);
00236         } else {
00237             lcd.printf("%8.1fLx Log: full", lux);
00238         }
00239         //  Printf:: RTC (Date & Time)
00240         lcd.locate(0, 3);    // 4th line top
00241         lcd.printf("%s", msg_clear);
00242         lcd.locate(0, 3);    // 4th line top
00243 #if 0
00244         lcd.printf("20%02d/%02d/%02d  %02d:%02d:%02d",
00245                     t.tm_year % 100, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
00246 #else
00247         lcd.printf("20%02d/%02d/%02d %02d:%02d PJL",
00248                     t.tm_year % 100, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min);
00249 #endif
00250     //  ---------- back to top --------------------------------------------------------------------
00251         myled = 1;
00252         wait(0.001);
00253         myled = 0;
00254         // Power off - Analog sensor
00255         analog_pwr = 0;
00256         vref_pwr = 0;
00257         // Wait with sleep
00258         WakeUp::set_ms(30000);
00259         //WakeUp::set(10);
00260         wait(0.001);    // looks important for works well
00261         myled = 1;
00262         wait(0.001);
00263         myled = 0;
00264         // Power on - Analog sensor
00265         analog_pwr = 1;
00266         vref_pwr = 1;
00267     }
00268 }
00269 
00270 // Application program starts here
00271 int main() {
00272     // Initial screen
00273     lcd.locate(0, 0);
00274     lcd.printf(msg_msg0);
00275     lcd.printf(msg_msg1);
00276     lcd.printf(msg_msg2);
00277     lcd.printf(msg_msg3);
00278     myled = 1;
00279     wait(3);    // show initial screen and wait serial input
00280     myled = 0;
00281     //--  Enter Monitor Mode  --
00282     if (READABLE()){
00283         lcd.locate(0, 0);
00284         lcd.printf(msg_msg4);
00285         mon();          // mon.cpp
00286         lcd.locate(0, 0);
00287         lcd.printf(msg_msg5);
00288         wait(0.5);
00289     }
00290     //--  Enter Normal Mode  --
00291     while (true) {  // Start main program
00292         conv_and_disp();
00293     }
00294 }