BME280 environmental recorder

Dependencies:   BME280 SB1602E SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SB1602E.h"
00003 #include "BME280.h"
00004 #include "SDFileSystem.h"
00005 
00006 #define USE_SDCARD  1
00007 #define USE_RTC     0
00008 
00009 #if USE_SDCARD
00010 DigitalOut sd_sdo(dp18, 0);
00011 DigitalOut sd_csb(dp15, 1);
00012 DigitalOut sd_gnd(dp12, 0);
00013 SDFileSystem sd(dp24, dp22, dp23, dp25, "sd"); // mosi, miso, sclk, cs
00014 #endif
00015 
00016 SB1602E lcd(dp11, dp10);
00017 DigitalIn btn(dp2);
00018 BME280 sensor(dp16, dp17);
00019 
00020 
00021 int main()
00022 {
00023     float temp, humd, pres;
00024     lcd.setCharsInLine(8);
00025     lcd.contrast(0x30);
00026     int last_mode = 0;
00027     btn.mode(PullUp);
00028 
00029     lcd.printf(0, 0, "BME280");
00030     lcd.printf(0, 1, "Demo");
00031     wait(3);
00032 
00033 #if USE_SDCARD
00034     uint32_t count = 0;
00035     mkdir("/sd/environmental", 0777);
00036     FILE *fp = fopen("/sd/environmental/log.csv", "w");
00037     if(fp == NULL) {
00038         error("Could not open file for write\n");
00039     }
00040     fprintf(fp, "Temerature,Humidity,Pressure\r\n");
00041 #endif
00042 
00043     while(1) {
00044         temp = sensor.getTemperature();
00045         humd = sensor.getHumidity();
00046         pres = sensor.getPressure();
00047         if (btn == 1) {
00048             if (last_mode == 0) {
00049                 lcd.clear();
00050             }
00051             lcd.printf(0, 0, "%2.2f%cC", temp, 0xdf);
00052             lcd.printf(0, 1, "%2.2f%%", humd);
00053             last_mode = 1;
00054         } else {
00055             if (last_mode == 1) {
00056                 lcd.clear();
00057             }
00058             lcd.printf(0, 0, "%2.2f%cC", temp, 0xdf);
00059             lcd.printf(0, 1, "%04.1fhPa", pres);
00060             last_mode = 0;
00061         }
00062 #if USE_SDCARD
00063         count++;
00064         if ((count % (60 * 5)) == 0) {
00065             fprintf(fp, "%2.2f,%2.2f,%4.1f\r\n", temp, humd, pres);
00066             fflush(fp);
00067             count = 0;
00068         }
00069 #endif
00070         wait(1);
00071     }
00072 
00073 #if USE_SDCARD
00074     fclose(fp);
00075     delete fp;
00076 #endif
00077 }