testBG

Dependencies:   BMP180 Si7020 mbed

MAXWSNENV_sensors

Committer:
bgrattan
Date:
Thu Jul 02 15:12:39 2015 +0000
Revision:
0:c73e35945f19
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bgrattan 0:c73e35945f19 1 /* mbed Microcontroller Library
bgrattan 0:c73e35945f19 2 * Copyright (c) 2006-2013 ARM Limited
bgrattan 0:c73e35945f19 3 *
bgrattan 0:c73e35945f19 4 * Licensed under the Apache License, Version 2.0 (the "License");
bgrattan 0:c73e35945f19 5 * you may not use this file except in compliance with the License.
bgrattan 0:c73e35945f19 6 * You may obtain a copy of the License at
bgrattan 0:c73e35945f19 7 *
bgrattan 0:c73e35945f19 8 * http://www.apache.org/licenses/LICENSE-2.0
bgrattan 0:c73e35945f19 9 *
bgrattan 0:c73e35945f19 10 * Unless required by applicable law or agreed to in writing, software
bgrattan 0:c73e35945f19 11 * distributed under the License is distributed on an "AS IS" BASIS,
bgrattan 0:c73e35945f19 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bgrattan 0:c73e35945f19 13 * See the License for the specific language governing permissions and
bgrattan 0:c73e35945f19 14 * limitations under the License.
bgrattan 0:c73e35945f19 15 */
bgrattan 0:c73e35945f19 16
bgrattan 0:c73e35945f19 17 #include <string.h>
bgrattan 0:c73e35945f19 18 #include "mbed.h"
bgrattan 0:c73e35945f19 19 #include "BMP180.h"
bgrattan 0:c73e35945f19 20 #include "Si7020.h"
bgrattan 0:c73e35945f19 21
bgrattan 0:c73e35945f19 22 // User I/O objects
bgrattan 0:c73e35945f19 23 DigitalOut red(LED_RED, LED_OFF);
bgrattan 0:c73e35945f19 24
bgrattan 0:c73e35945f19 25 // Sensor objects
bgrattan 0:c73e35945f19 26 I2C i2c(I2C_SDA, I2C_SCL);
bgrattan 0:c73e35945f19 27 AnalogIn light_ain(AIN_0D);
bgrattan 0:c73e35945f19 28 DigitalOut light_en(P0_2, 1);
bgrattan 0:c73e35945f19 29
bgrattan 0:c73e35945f19 30 BMP180 bmp180(&i2c);
bgrattan 0:c73e35945f19 31 Si7020 si7020(&i2c);
bgrattan 0:c73e35945f19 32
bgrattan 0:c73e35945f19 33 int currentPressure;
bgrattan 0:c73e35945f19 34 float currentHumidity;
bgrattan 0:c73e35945f19 35 float currentTemperature;
bgrattan 0:c73e35945f19 36 float currentLight;
bgrattan 0:c73e35945f19 37
bgrattan 0:c73e35945f19 38 // *****************************************************************************
bgrattan 0:c73e35945f19 39 int main(void)
bgrattan 0:c73e35945f19 40 {
bgrattan 0:c73e35945f19 41 printf("\n\nEnvironmental sensor demo\n");
bgrattan 0:c73e35945f19 42
bgrattan 0:c73e35945f19 43 /* Setup BMP180 */
bgrattan 0:c73e35945f19 44 while(bmp180.init() != 0) {
bgrattan 0:c73e35945f19 45 printf("Failed to initialize barometer\n");
bgrattan 0:c73e35945f19 46 wait(1);
bgrattan 0:c73e35945f19 47 }
bgrattan 0:c73e35945f19 48
bgrattan 0:c73e35945f19 49 while (true) {
bgrattan 0:c73e35945f19 50
bgrattan 0:c73e35945f19 51 // Start non-blocking readings
bgrattan 0:c73e35945f19 52 bmp180.startPressure(BMP180::STANDARD);
bgrattan 0:c73e35945f19 53 si7020.startHumidity();
bgrattan 0:c73e35945f19 54
bgrattan 0:c73e35945f19 55 // Toggle LED and wait between readings
bgrattan 0:c73e35945f19 56 red = 1;
bgrattan 0:c73e35945f19 57 wait(1);
bgrattan 0:c73e35945f19 58 red = 0;
bgrattan 0:c73e35945f19 59
bgrattan 0:c73e35945f19 60 // Enable light sensor to charge capacitor
bgrattan 0:c73e35945f19 61 light_en = 0;
bgrattan 0:c73e35945f19 62
bgrattan 0:c73e35945f19 63 // Get Pressure
bgrattan 0:c73e35945f19 64 if(bmp180.getPressure(&currentPressure) != 0) {
bgrattan 0:c73e35945f19 65 printf("Error getting pressure\n");
bgrattan 0:c73e35945f19 66 }
bgrattan 0:c73e35945f19 67
bgrattan 0:c73e35945f19 68 // Get Humidity
bgrattan 0:c73e35945f19 69 if(si7020.checkHumidity(&currentHumidity) != 0) {
bgrattan 0:c73e35945f19 70 printf("Error getting humidity\n");
bgrattan 0:c73e35945f19 71 }
bgrattan 0:c73e35945f19 72
bgrattan 0:c73e35945f19 73 // Get temperature
bgrattan 0:c73e35945f19 74 if(si7020.getPrevTemperature(&currentTemperature) != 0) {
bgrattan 0:c73e35945f19 75 printf("Error getting temperature\n");
bgrattan 0:c73e35945f19 76 }
bgrattan 0:c73e35945f19 77
bgrattan 0:c73e35945f19 78 // Get light
bgrattan 0:c73e35945f19 79 currentLight = light_ain.read();
bgrattan 0:c73e35945f19 80
bgrattan 0:c73e35945f19 81 // Disable light sensor
bgrattan 0:c73e35945f19 82 light_en = 1;
bgrattan 0:c73e35945f19 83
bgrattan 0:c73e35945f19 84 printf("Press = %0.1f kPa\n", (float)(currentPressure/1000.0));
bgrattan 0:c73e35945f19 85 printf("Humid = %0.1f %%\n", currentHumidity);
bgrattan 0:c73e35945f19 86 printf("Temp = %0.1f C\n", currentTemperature);
bgrattan 0:c73e35945f19 87 printf("Light = %0.1f %% \n", currentLight*100.0f);
bgrattan 0:c73e35945f19 88 printf("\n");
bgrattan 0:c73e35945f19 89 }
bgrattan 0:c73e35945f19 90 }