Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API TMP_nrf51 mbed nRF51822
Fork of VTT_NODEV3_BMP180_Si7021 by
main.cpp@2:b221ba23b37f, 2016-01-25 (annotated)
- Committer:
- jejuho
- Date:
- Mon Jan 25 14:36:57 2016 +0000
- Revision:
- 2:b221ba23b37f
- Parent:
- 1:bd7fd35251ab
- Child:
- 3:101b0f081435
Initial version. Values from sensors not verified. Some problems with Si7021.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jejuho | 1:bd7fd35251ab | 1 | /** |
| jejuho | 2:b221ba23b37f | 2 | * Temperature/humidity/pressure broadcast example for mbed / VTT Node V3 |
| jejuho | 2:b221ba23b37f | 3 | * o Using BMP180 & Si7021 and onboard temperature sensor |
| jejuho | 1:bd7fd35251ab | 4 | * Juho Eskeli, VTT |
| jejuho | 1:bd7fd35251ab | 5 | */ |
| jejuho | 1:bd7fd35251ab | 6 | |
| jejuho | 1:bd7fd35251ab | 7 | |
| jejuho | 1:bd7fd35251ab | 8 | #include "NodeV3PinNames.h" //This should come before mbed.h to override pin configuration |
| jejuho | 0:de3e4a57ebe0 | 9 | #include "mbed.h" |
| jejuho | 0:de3e4a57ebe0 | 10 | #include "ble/BLE.h" |
| jejuho | 0:de3e4a57ebe0 | 11 | |
| jejuho | 0:de3e4a57ebe0 | 12 | #define USE_DFU |
| jejuho | 0:de3e4a57ebe0 | 13 | |
| jejuho | 0:de3e4a57ebe0 | 14 | #ifdef USE_DFU |
| jejuho | 0:de3e4a57ebe0 | 15 | #include "DFUService.h" |
| jejuho | 0:de3e4a57ebe0 | 16 | #endif |
| jejuho | 0:de3e4a57ebe0 | 17 | |
| jejuho | 0:de3e4a57ebe0 | 18 | #include "AT45.h" |
| jejuho | 1:bd7fd35251ab | 19 | #include "TMP_nrf51.h" |
| jejuho | 0:de3e4a57ebe0 | 20 | |
| jejuho | 2:b221ba23b37f | 21 | #include "BMP180.h" |
| jejuho | 2:b221ba23b37f | 22 | #include "Si7021.h" |
| jejuho | 2:b221ba23b37f | 23 | |
| jejuho | 0:de3e4a57ebe0 | 24 | //interrupt /gpio configuration |
| jejuho | 0:de3e4a57ebe0 | 25 | #include "nrf_gpio.h" |
| jejuho | 0:de3e4a57ebe0 | 26 | #include "nrf_gpiote.h" |
| jejuho | 0:de3e4a57ebe0 | 27 | #include "nrf_soc.h" |
| jejuho | 0:de3e4a57ebe0 | 28 | |
| jejuho | 0:de3e4a57ebe0 | 29 | //const static char DEVICE_NAME[] = "BAc0N"; |
| jejuho | 0:de3e4a57ebe0 | 30 | //static const uint16_t uuid16_list[] = {GattService::UUID_STREAMING_SERVICE}; |
| jejuho | 0:de3e4a57ebe0 | 31 | |
| jejuho | 0:de3e4a57ebe0 | 32 | BLE ble; |
| jejuho | 0:de3e4a57ebe0 | 33 | |
| jejuho | 1:bd7fd35251ab | 34 | #define MOSI SPI_PSELMOSI0 |
| jejuho | 1:bd7fd35251ab | 35 | #define MISO SPI_PSELMISO0 |
| jejuho | 1:bd7fd35251ab | 36 | #define CS SPI_PSELSS0 |
| jejuho | 1:bd7fd35251ab | 37 | #define SCLK SPI_PSELSCK0 |
| jejuho | 2:b221ba23b37f | 38 | |
| jejuho | 2:b221ba23b37f | 39 | static TMP_nrf51 tempSensor; |
| jejuho | 2:b221ba23b37f | 40 | BMP180 bmp180(I2C_SDA0, I2C_SCL0); |
| jejuho | 2:b221ba23b37f | 41 | SI7021_I2C si7021(I2C_SDA0, I2C_SCL0); |
| jejuho | 0:de3e4a57ebe0 | 42 | |
| jejuho | 1:bd7fd35251ab | 43 | DigitalOut myled1(LED1); |
| jejuho | 1:bd7fd35251ab | 44 | DigitalOut myled2(LED2); |
| jejuho | 0:de3e4a57ebe0 | 45 | |
| jejuho | 1:bd7fd35251ab | 46 | DigitalOut LIS_CS_0(CS); //LIS3DH CS |
| jejuho | 1:bd7fd35251ab | 47 | DigitalOut AT_CS(p5); //Dataflash CS |
| jejuho | 1:bd7fd35251ab | 48 | DigitalOut AT_RS(p6); //Dataflash reset |
| jejuho | 0:de3e4a57ebe0 | 49 | |
| jejuho | 0:de3e4a57ebe0 | 50 | __packed struct ApplicationData_t { |
| jejuho | 0:de3e4a57ebe0 | 51 | uint16_t applicationSpecificId; /* An ID used to identify temperature value in the manufacture specific AD data field */ |
| jejuho | 2:b221ba23b37f | 52 | TMP_nrf51::tmpSensorValue_t tmpSensorValue; /* User defined application data */ |
| jejuho | 2:b221ba23b37f | 53 | int16_t bmp180_temp; |
| jejuho | 2:b221ba23b37f | 54 | uint32_t bmp180_press; |
| jejuho | 2:b221ba23b37f | 55 | float si7021_temperature; |
| jejuho | 2:b221ba23b37f | 56 | float si7021_humidity; |
| jejuho | 0:de3e4a57ebe0 | 57 | }; |
| jejuho | 0:de3e4a57ebe0 | 58 | |
| jejuho | 0:de3e4a57ebe0 | 59 | void setupApplicationData(ApplicationData_t &appData) |
| jejuho | 0:de3e4a57ebe0 | 60 | { |
| jejuho | 0:de3e4a57ebe0 | 61 | static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE; |
| jejuho | 0:de3e4a57ebe0 | 62 | appData.applicationSpecificId = APP_SPECIFIC_ID_TEST; |
| jejuho | 2:b221ba23b37f | 63 | appData.tmpSensorValue = tempSensor.get(); |
| jejuho | 2:b221ba23b37f | 64 | appData.bmp180_temp = bmp180.BMP180GetTemperature(); |
| jejuho | 2:b221ba23b37f | 65 | appData.bmp180_press = bmp180.BMP180GetPressure(); |
| jejuho | 2:b221ba23b37f | 66 | si7021.Measure_Humidity_Temp((float*)&(appData.si7021_humidity), (float*)&(appData.si7021_temperature)); |
| jejuho | 0:de3e4a57ebe0 | 67 | } |
| jejuho | 0:de3e4a57ebe0 | 68 | |
| jejuho | 0:de3e4a57ebe0 | 69 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
| jejuho | 0:de3e4a57ebe0 | 70 | { |
| jejuho | 0:de3e4a57ebe0 | 71 | ble.gap().startAdvertising(); |
| jejuho | 0:de3e4a57ebe0 | 72 | } |
| jejuho | 0:de3e4a57ebe0 | 73 | |
| jejuho | 0:de3e4a57ebe0 | 74 | void senseCallback(void) |
| jejuho | 0:de3e4a57ebe0 | 75 | { |
| jejuho | 0:de3e4a57ebe0 | 76 | ApplicationData_t appData; |
| jejuho | 0:de3e4a57ebe0 | 77 | setupApplicationData(appData); |
| jejuho | 0:de3e4a57ebe0 | 78 | ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); |
| jejuho | 0:de3e4a57ebe0 | 79 | } |
| jejuho | 0:de3e4a57ebe0 | 80 | |
| jejuho | 1:bd7fd35251ab | 81 | void setup_CS_LIS3DH() |
| jejuho | 0:de3e4a57ebe0 | 82 | { |
| jejuho | 1:bd7fd35251ab | 83 | //Without this setup CS lines of AT45 & LIS3DH are in conflict, causing huge current consumption |
| jejuho | 0:de3e4a57ebe0 | 84 | LIS_CS_0 = 1; //not selected |
| jejuho | 0:de3e4a57ebe0 | 85 | AT_CS = 1; //not selected |
| jejuho | 0:de3e4a57ebe0 | 86 | AT_RS = 0; //asserted == reset state |
| jejuho | 0:de3e4a57ebe0 | 87 | wait_ms(100); |
| jejuho | 0:de3e4a57ebe0 | 88 | AT_RS = 1; |
| jejuho | 1:bd7fd35251ab | 89 | } |
| jejuho | 1:bd7fd35251ab | 90 | |
| jejuho | 2:b221ba23b37f | 91 | bool init_bmp180(void) |
| jejuho | 2:b221ba23b37f | 92 | { |
| jejuho | 2:b221ba23b37f | 93 | // Read the WHO_AM_I register of the BMP-180, this is a good test of communication |
| jejuho | 2:b221ba23b37f | 94 | uint8_t c = bmp180.readByte(BMP180_ADDRESS, BMP180_WHO_AM_I); |
| jejuho | 2:b221ba23b37f | 95 | if(c == 0x55) { |
| jejuho | 2:b221ba23b37f | 96 | //BMP-180 should be 0x55 |
| jejuho | 2:b221ba23b37f | 97 | //BMP-180 online... |
| jejuho | 2:b221ba23b37f | 98 | bmp180.BMP180Calibration(); |
| jejuho | 2:b221ba23b37f | 99 | //BMP-180 calibration complete |
| jejuho | 2:b221ba23b37f | 100 | return true; |
| jejuho | 2:b221ba23b37f | 101 | } |
| jejuho | 2:b221ba23b37f | 102 | else |
| jejuho | 2:b221ba23b37f | 103 | { |
| jejuho | 2:b221ba23b37f | 104 | return false; |
| jejuho | 2:b221ba23b37f | 105 | } |
| jejuho | 2:b221ba23b37f | 106 | } |
| jejuho | 2:b221ba23b37f | 107 | |
| jejuho | 1:bd7fd35251ab | 108 | int main() |
| jejuho | 1:bd7fd35251ab | 109 | { |
| jejuho | 1:bd7fd35251ab | 110 | //LEDS off |
| jejuho | 1:bd7fd35251ab | 111 | myled1 = 0; |
| jejuho | 1:bd7fd35251ab | 112 | myled2 = 0; |
| jejuho | 1:bd7fd35251ab | 113 | |
| jejuho | 1:bd7fd35251ab | 114 | setup_CS_LIS3DH(); |
| jejuho | 0:de3e4a57ebe0 | 115 | |
| jejuho | 0:de3e4a57ebe0 | 116 | //Initialize SPI interface |
| jejuho | 0:de3e4a57ebe0 | 117 | SPI spi(MOSI, MISO, SCLK); // mosi, miso, sclk |
| jejuho | 0:de3e4a57ebe0 | 118 | spi.format(8,3); |
| jejuho | 0:de3e4a57ebe0 | 119 | spi.frequency(8000000); |
| jejuho | 0:de3e4a57ebe0 | 120 | //setup AT45 dataflash to powersaving mode |
| jejuho | 0:de3e4a57ebe0 | 121 | AT45* flash = new AT45(spi, p5); |
| jejuho | 2:b221ba23b37f | 122 | flash->ultra_deep_power_down(true); |
| jejuho | 0:de3e4a57ebe0 | 123 | |
| jejuho | 2:b221ba23b37f | 124 | init_bmp180(); |
| jejuho | 0:de3e4a57ebe0 | 125 | |
| jejuho | 0:de3e4a57ebe0 | 126 | //Initialize BLE stuff |
| jejuho | 0:de3e4a57ebe0 | 127 | ble.init(); |
| jejuho | 0:de3e4a57ebe0 | 128 | ble.gap().onDisconnection(disconnectionCallback); |
| jejuho | 0:de3e4a57ebe0 | 129 | |
| jejuho | 0:de3e4a57ebe0 | 130 | #ifdef USE_DFU |
| jejuho | 0:de3e4a57ebe0 | 131 | DFUService dfu(ble); |
| jejuho | 0:de3e4a57ebe0 | 132 | #endif |
| jejuho | 0:de3e4a57ebe0 | 133 | |
| jejuho | 0:de3e4a57ebe0 | 134 | /* Setup advertising. */ |
| jejuho | 0:de3e4a57ebe0 | 135 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
| jejuho | 0:de3e4a57ebe0 | 136 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); |
| jejuho | 0:de3e4a57ebe0 | 137 | ApplicationData_t appData; |
| jejuho | 0:de3e4a57ebe0 | 138 | setupApplicationData(appData); |
| jejuho | 0:de3e4a57ebe0 | 139 | //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
| jejuho | 0:de3e4a57ebe0 | 140 | //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
| jejuho | 0:de3e4a57ebe0 | 141 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); |
| jejuho | 0:de3e4a57ebe0 | 142 | |
| jejuho | 0:de3e4a57ebe0 | 143 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
| jejuho | 0:de3e4a57ebe0 | 144 | //ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
| jejuho | 0:de3e4a57ebe0 | 145 | ble.gap().setAdvertisingInterval(100); /* 1000ms. */ |
| jejuho | 0:de3e4a57ebe0 | 146 | ble.gap().startAdvertising(); |
| jejuho | 0:de3e4a57ebe0 | 147 | |
| jejuho | 0:de3e4a57ebe0 | 148 | Ticker ticker; |
| jejuho | 0:de3e4a57ebe0 | 149 | ticker.attach(senseCallback, 0.1); |
| jejuho | 0:de3e4a57ebe0 | 150 | |
| jejuho | 0:de3e4a57ebe0 | 151 | ///Could have main loop here doing something |
| jejuho | 0:de3e4a57ebe0 | 152 | while(true) { |
| jejuho | 0:de3e4a57ebe0 | 153 | ble.waitForEvent(); |
| jejuho | 0:de3e4a57ebe0 | 154 | } |
| jejuho | 0:de3e4a57ebe0 | 155 | } |
| jejuho | 0:de3e4a57ebe0 | 156 |
