Barometer program : Data Logger function includes Barometer & temperature (BMP180), Humidity & temp. (RHT03), Sunshine (Cds), RTC(M41T62) data. : Logging data saves into EEPROM (AT24C1024) using ring buffer function.

Dependencies:   AT24C1024 RHT03 TextLCD BMP180 M41T62

Fork of mbed_blinky by Mbed

Please see https://mbed.org/users/kenjiArai/notebook/mbed-lpc1114fn28-barometer-with-data-logging/#

main.cpp

Committer:
kenjiArai
Date:
2014-06-15
Revision:
11:bccfd75e84a0
Parent:
10:398f62bb41f7
Child:
12:1e21119688fe

File content as of revision 11:bccfd75e84a0:

/*
 * mbed Application program for the mbed LPC1114FN28
 * Test program -> Check LED & Switch function 
 *
 * Copyright (c) 2014 Kenji Arai / JH1PJL
 *  http://www.page.sannet.ne.jp/kenjia/index.html
 *  http://mbed.org/users/kenjiArai/
 *      Created: June      13th, 2014
 *      Revised: June      14th, 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
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "mbed.h"
#include "BMP180.h"             // Pressure sensor             
#include "RHT03.h"              // Humidity sensor
#include "TextLCD.h"

#define VREF_VOLT       2.482   // TA76431F Vref real measued data
#define R_FIX           9930    // 10K ohm <- real measued data
#define VOL_OFFSET      4       // Offset data ,= real measured data
#define CDS_TBL_SIZE    13


I2C         i2c(dp5,dp27);      // SDA, SCL
DigitalOut  myled0(dp28);       // LED for Debug
DigitalOut  myled1(dp14);       // Indicate state transition
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
Serial      pc(dp16,dp15);      // UART (vertual COM)
BMP180      bmp180(i2c);        // Bosch sensor
TextLCD_I2C_N i2clcd(&i2c, 0x7c, TextLCD::LCD8x2);

typedef enum {CDS = 0, VREF, VOL} ADC_Select;

//  ADC
float av_cds, av_vref, av_vol, cal_vcc;
float r_cds, lux;
uint32_t nor_vol;

//  Humidity Sensor
float humidity_temp, humidity;

// Cds GL5528 (Dark Resistance 1 Mohm type) SENBA OPTICAL & ELECTRONIC CO.,LTD.
//      Table value referrence: http://homepage3.nifty.com/skomo/f35/hp35_20.htm
const float lux_cds[CDS_TBL_SIZE][2] =
    {{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}};

//-------------------------------------------------------------------------------------------------
//  Control Program
//-------------------------------------------------------------------------------------------------
// Normalize ADC data
void adc_normalize( ADC_Select n ){
int i;
float x1,y1,dx;

    switch (n){
    case CDS:
        // v_adc = Rfix / (Rcds + Rfix) ->  Rcds = ( Rfix / v_adc ) - Rfix 
        r_cds = (R_FIX / av_cds) - R_FIX;
        // CDS resistance to Lux conversion using convertion table (luc_cds[][])
        for (i =0; i < CDS_TBL_SIZE; i++){  // search table
            if ( r_cds <= lux_cds[i][0]){ break; }
        }
        // Check table position
        if (i == 0){
            lux = lux_cds[0][1];
            break;
        } else if ( i == CDS_TBL_SIZE ){
            if ( r_cds <= lux_cds[i][0] ){
                lux = lux_cds[i-1][1];
                break;
            }
        }
        //  Linear interpolation
        y1 = lux_cds[i-1][1] - lux_cds[i][1];
        x1 = lux_cds[i][0] - lux_cds[i-1][0];
        dx = r_cds - lux_cds[i-1][0];
        lux = lux_cds[i-1][1] - ((dx/x1) * y1);
        break;
    case VREF:
        //  vref = VREF_VOLT / VCC -> VCC = VREF_VOLT / vref
        cal_vcc = VREF_VOLT / vref;
        break;
    case VOL:
        // Vol center = 1.00 (actual 100)
        nor_vol = (uint32_t)(av_vol * 200) + VOL_OFFSET;
        break;
    }
}

//  Read adc data and averaging
void adc_all_read( void){
    if (av_cds == 0){   av_cds = cds.read();
    } else {            av_cds = av_cds *0.5 + cds.read() * 0.5;
    }
    if (av_vref == 0){  av_vref = vref.read();
    } else {            av_vref = av_vref *0.9 + vref.read() * 0.1;
    }
    if (av_vol == 0){   av_vol = vol.read();
    } else {            av_vol = av_vol *0.2 + vol.read() * 0.8;
    } 
}

// Read Humidity sensor data
void hum_RHT03_read( void){
    while (true){   // wait data
        if ( humtemp.readData() == RHT_ERROR_NONE ){ break; }
    }
    if (humidity_temp == 0){humidity_temp = humtemp.getTemperatureC();
    } else {                humidity_temp = humidity_temp * 0.9 + humtemp.getTemperatureC() * 0.1;
    }
    if ( humidity == 0 ){   humidity = humtemp.getHumidity();
    } else {                humidity = humidity * 0.9 + humtemp.getHumidity() * 0.1;
    }
}

//-------------------------------------
// Application program starts here
//-------------------------------------
int main() {
    pc.baud(9600);
    pc.printf("\r\nmbed LPC1114FN28 test program by JH1PJL created on "__DATE__"\r\n");
    i2clcd.setContrast(25);
    i2clcd.locate(0, 0);
    i2clcd.printf("LPC1114F");
    i2clcd.locate(0, 1);
    i2clcd.printf(" JH1PJL ");
    // Initialize data
    av_cds = 0;
    av_vref = 0;
    av_vol = 0;
    humidity_temp = 0;
    humidity = 0;
    // Show initial screen
    wait(5.0);
    while(1) {
    //  ---------- Cds Sensor, Vref, Volume ---------------------------------------------------
        // 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);       
        myled0 = 1;
        if (sw_chng == 1){  // SW Off
            pc.printf( "\r\nCds:%.0fohm -> %.1flux, Vcc:%.3fV, Vol:%d\r\n",
                r_cds, lux, cal_vcc, nor_vol );
        } else {            // SW On
            pc.printf( "\r\nCds:%f, Vref:%f, Vol:%f\r\n", av_cds, av_vref, av_vol );
        }
        myled0 = 0;
        i2clcd.locate(0, 0);    // 1st line top
        //             12345678
        i2clcd.printf("L: %4.1f", lux);
        i2clcd.locate(0, 1);    // 2nd line top
        i2clcd.printf("V: %1.3f", cal_vcc);
        wait(2.0);
    //  ---------- Barometer Sensor / BMP180 --------------------------------------------------
        bmp180.normalize();
        i2clcd.locate(0, 0);    // 1st line top
        i2clcd.printf("P:%4.1f", bmp180.read_pressure());
        i2clcd.locate(0, 1);    // 2nd line top
        i2clcd.printf("T: %\+-0.1f", bmp180.read_temperature());
        myled1 = 1;
        pc.printf("Pres:%4.1fhPa, Temp:%\+-0.1fdegC\r\n",
                    bmp180.read_pressure(), bmp180.read_temperature());
        myled1 = 0;
        wait(4.0);
    //  ---------- Humidity Sensor / RHT03 ----------------------------------------------------
        hum_RHT03_read();       // Read Humidity data then avaraging
        i2clcd.locate(0, 0);    // 1st line top
        i2clcd.printf("H:  %2.1f", humidity);
        i2clcd.locate(0, 1);    // 2nd line top
        i2clcd.printf("T: %\+-0.1f", humidity_temp);
        myled1 = 1;
        pc.printf("Humid: %0.1f%cRH, Temp:%\+-0.1fdegC\r\n", humidity, '%', humidity_temp);        
        myled1 = 0;  
        wait(2.0);
    }
}