testBG

Dependencies:   BMP180 Si7020 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <string.h>
00018 #include "mbed.h"
00019 #include "BMP180.h"
00020 #include "Si7020.h"
00021 
00022 // User I/O objects
00023 DigitalOut red(LED_RED, LED_OFF);
00024 
00025 // Sensor objects
00026 I2C i2c(I2C_SDA, I2C_SCL);
00027 AnalogIn light_ain(AIN_0D);
00028 DigitalOut light_en(P0_2, 1);
00029 
00030 BMP180 bmp180(&i2c);
00031 Si7020 si7020(&i2c);
00032 
00033 int currentPressure;
00034 float currentHumidity;
00035 float currentTemperature;
00036 float currentLight;
00037 
00038 // *****************************************************************************
00039 int main(void)
00040 {
00041     printf("\n\nEnvironmental sensor demo\n");
00042 
00043     /* Setup BMP180 */
00044     while(bmp180.init() != 0) {
00045         printf("Failed to initialize barometer\n");
00046         wait(1);
00047     }
00048 
00049     while (true) {
00050 
00051         // Start non-blocking readings
00052         bmp180.startPressure(BMP180::STANDARD);
00053         si7020.startHumidity();
00054 
00055         // Toggle LED and wait between readings
00056         red = 1;
00057         wait(1);
00058         red = 0;
00059     
00060         // Enable light sensor to charge capacitor
00061         light_en = 0;
00062         
00063         // Get Pressure
00064         if(bmp180.getPressure(&currentPressure) != 0) {
00065             printf("Error getting pressure\n");
00066         }
00067 
00068         // Get Humidity
00069         if(si7020.checkHumidity(&currentHumidity) != 0) {
00070             printf("Error getting humidity\n");
00071         }
00072 
00073         // Get temperature
00074         if(si7020.getPrevTemperature(&currentTemperature) != 0) {
00075             printf("Error getting temperature\n");
00076         }
00077 
00078         // Get light 
00079         currentLight = light_ain.read();
00080         
00081         // Disable light sensor
00082         light_en = 1;
00083         
00084         printf("Press = %0.1f kPa\n", (float)(currentPressure/1000.0));
00085         printf("Humid = %0.1f %%\n", currentHumidity);
00086         printf("Temp = %0.1f C\n", currentTemperature);
00087         printf("Light = %0.1f %% \n", currentLight*100.0f);
00088         printf("\n");
00089     }
00090 }