VTT TinyNode, slightly modified code from example (https://developer.mbed.org/users/jejuho/code/VTT_NODEV3_BMP180_Si7021)

Dependencies:   BLE_API TMP_nrf51 mbed nRF51822

Fork of VTT_NODEV3_BMP180_Si7021 by Juho Eskeli

Committer:
JarkkoS
Date:
Fri May 27 07:38:42 2016 +0000
Revision:
3:101b0f081435
Parent:
2:b221ba23b37f
VTT TinyNodeV2, slightly modified code from example (https://developer.mbed.org/users/jejuho/code/VTT_NODEV3_BMP180_Si7021)

Who changed what in which revision?

UserRevisionLine numberNew 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};
JarkkoS 3:101b0f081435 31
jejuho 0:de3e4a57ebe0 32
jejuho 0:de3e4a57ebe0 33 BLE ble;
jejuho 0:de3e4a57ebe0 34
jejuho 1:bd7fd35251ab 35 #define MOSI SPI_PSELMOSI0
jejuho 1:bd7fd35251ab 36 #define MISO SPI_PSELMISO0
jejuho 1:bd7fd35251ab 37 #define CS SPI_PSELSS0
jejuho 1:bd7fd35251ab 38 #define SCLK SPI_PSELSCK0
jejuho 2:b221ba23b37f 39
jejuho 2:b221ba23b37f 40 static TMP_nrf51 tempSensor;
jejuho 2:b221ba23b37f 41 BMP180 bmp180(I2C_SDA0, I2C_SCL0);
jejuho 2:b221ba23b37f 42 SI7021_I2C si7021(I2C_SDA0, I2C_SCL0);
jejuho 0:de3e4a57ebe0 43
jejuho 1:bd7fd35251ab 44 DigitalOut myled1(LED1);
jejuho 1:bd7fd35251ab 45 DigitalOut myled2(LED2);
jejuho 0:de3e4a57ebe0 46
jejuho 1:bd7fd35251ab 47 DigitalOut LIS_CS_0(CS); //LIS3DH CS
jejuho 1:bd7fd35251ab 48 DigitalOut AT_CS(p5); //Dataflash CS
jejuho 1:bd7fd35251ab 49 DigitalOut AT_RS(p6); //Dataflash reset
jejuho 0:de3e4a57ebe0 50
jejuho 0:de3e4a57ebe0 51 __packed struct ApplicationData_t {
jejuho 0:de3e4a57ebe0 52 uint16_t applicationSpecificId; /* An ID used to identify temperature value in the manufacture specific AD data field */
jejuho 2:b221ba23b37f 53 TMP_nrf51::tmpSensorValue_t tmpSensorValue; /* User defined application data */
jejuho 2:b221ba23b37f 54 int16_t bmp180_temp;
jejuho 2:b221ba23b37f 55 uint32_t bmp180_press;
jejuho 2:b221ba23b37f 56 float si7021_temperature;
jejuho 2:b221ba23b37f 57 float si7021_humidity;
JarkkoS 3:101b0f081435 58 uint16_t applicationSpecificId2;
jejuho 0:de3e4a57ebe0 59 };
jejuho 0:de3e4a57ebe0 60
jejuho 0:de3e4a57ebe0 61 void setupApplicationData(ApplicationData_t &appData)
jejuho 0:de3e4a57ebe0 62 {
jejuho 0:de3e4a57ebe0 63 static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE;
jejuho 0:de3e4a57ebe0 64 appData.applicationSpecificId = APP_SPECIFIC_ID_TEST;
JarkkoS 3:101b0f081435 65 appData.applicationSpecificId2 = APP_SPECIFIC_ID_TEST;
jejuho 2:b221ba23b37f 66 appData.tmpSensorValue = tempSensor.get();
jejuho 2:b221ba23b37f 67 appData.bmp180_temp = bmp180.BMP180GetTemperature();
jejuho 2:b221ba23b37f 68 appData.bmp180_press = bmp180.BMP180GetPressure();
jejuho 2:b221ba23b37f 69 si7021.Measure_Humidity_Temp((float*)&(appData.si7021_humidity), (float*)&(appData.si7021_temperature));
jejuho 0:de3e4a57ebe0 70 }
jejuho 0:de3e4a57ebe0 71
jejuho 0:de3e4a57ebe0 72 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
jejuho 0:de3e4a57ebe0 73 {
jejuho 0:de3e4a57ebe0 74 ble.gap().startAdvertising();
jejuho 0:de3e4a57ebe0 75 }
jejuho 0:de3e4a57ebe0 76
jejuho 0:de3e4a57ebe0 77 void senseCallback(void)
jejuho 0:de3e4a57ebe0 78 {
jejuho 0:de3e4a57ebe0 79 ApplicationData_t appData;
jejuho 0:de3e4a57ebe0 80 setupApplicationData(appData);
jejuho 0:de3e4a57ebe0 81 ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t));
jejuho 0:de3e4a57ebe0 82 }
jejuho 0:de3e4a57ebe0 83
jejuho 1:bd7fd35251ab 84 void setup_CS_LIS3DH()
jejuho 0:de3e4a57ebe0 85 {
jejuho 1:bd7fd35251ab 86 //Without this setup CS lines of AT45 & LIS3DH are in conflict, causing huge current consumption
jejuho 0:de3e4a57ebe0 87 LIS_CS_0 = 1; //not selected
jejuho 0:de3e4a57ebe0 88 AT_CS = 1; //not selected
jejuho 0:de3e4a57ebe0 89 AT_RS = 0; //asserted == reset state
jejuho 0:de3e4a57ebe0 90 wait_ms(100);
jejuho 0:de3e4a57ebe0 91 AT_RS = 1;
jejuho 1:bd7fd35251ab 92 }
jejuho 1:bd7fd35251ab 93
jejuho 2:b221ba23b37f 94 bool init_bmp180(void)
jejuho 2:b221ba23b37f 95 {
jejuho 2:b221ba23b37f 96 // Read the WHO_AM_I register of the BMP-180, this is a good test of communication
jejuho 2:b221ba23b37f 97 uint8_t c = bmp180.readByte(BMP180_ADDRESS, BMP180_WHO_AM_I);
jejuho 2:b221ba23b37f 98 if(c == 0x55) {
jejuho 2:b221ba23b37f 99 //BMP-180 should be 0x55
jejuho 2:b221ba23b37f 100 //BMP-180 online...
jejuho 2:b221ba23b37f 101 bmp180.BMP180Calibration();
jejuho 2:b221ba23b37f 102 //BMP-180 calibration complete
jejuho 2:b221ba23b37f 103 return true;
jejuho 2:b221ba23b37f 104 }
jejuho 2:b221ba23b37f 105 else
jejuho 2:b221ba23b37f 106 {
jejuho 2:b221ba23b37f 107 return false;
jejuho 2:b221ba23b37f 108 }
jejuho 2:b221ba23b37f 109 }
jejuho 2:b221ba23b37f 110
jejuho 1:bd7fd35251ab 111 int main()
jejuho 1:bd7fd35251ab 112 {
jejuho 1:bd7fd35251ab 113 //LEDS off
jejuho 1:bd7fd35251ab 114 myled1 = 0;
jejuho 1:bd7fd35251ab 115 myled2 = 0;
jejuho 1:bd7fd35251ab 116
jejuho 1:bd7fd35251ab 117 setup_CS_LIS3DH();
jejuho 0:de3e4a57ebe0 118
jejuho 0:de3e4a57ebe0 119 //Initialize SPI interface
jejuho 0:de3e4a57ebe0 120 SPI spi(MOSI, MISO, SCLK); // mosi, miso, sclk
jejuho 0:de3e4a57ebe0 121 spi.format(8,3);
jejuho 0:de3e4a57ebe0 122 spi.frequency(8000000);
jejuho 0:de3e4a57ebe0 123 //setup AT45 dataflash to powersaving mode
jejuho 0:de3e4a57ebe0 124 AT45* flash = new AT45(spi, p5);
JarkkoS 3:101b0f081435 125 // flash->ultra_deep_power_down(true);
jejuho 0:de3e4a57ebe0 126
jejuho 2:b221ba23b37f 127 init_bmp180();
jejuho 0:de3e4a57ebe0 128
jejuho 0:de3e4a57ebe0 129 //Initialize BLE stuff
jejuho 0:de3e4a57ebe0 130 ble.init();
jejuho 0:de3e4a57ebe0 131 ble.gap().onDisconnection(disconnectionCallback);
jejuho 0:de3e4a57ebe0 132
jejuho 0:de3e4a57ebe0 133 #ifdef USE_DFU
jejuho 0:de3e4a57ebe0 134 DFUService dfu(ble);
jejuho 0:de3e4a57ebe0 135 #endif
jejuho 0:de3e4a57ebe0 136
jejuho 0:de3e4a57ebe0 137 /* Setup advertising. */
jejuho 0:de3e4a57ebe0 138 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
jejuho 0:de3e4a57ebe0 139 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER);
jejuho 0:de3e4a57ebe0 140 ApplicationData_t appData;
jejuho 0:de3e4a57ebe0 141 setupApplicationData(appData);
jejuho 0:de3e4a57ebe0 142 //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
jejuho 0:de3e4a57ebe0 143 //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
jejuho 0:de3e4a57ebe0 144 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t));
jejuho 0:de3e4a57ebe0 145
jejuho 0:de3e4a57ebe0 146 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jejuho 0:de3e4a57ebe0 147 //ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
jejuho 0:de3e4a57ebe0 148 ble.gap().setAdvertisingInterval(100); /* 1000ms. */
jejuho 0:de3e4a57ebe0 149 ble.gap().startAdvertising();
jejuho 0:de3e4a57ebe0 150
jejuho 0:de3e4a57ebe0 151 Ticker ticker;
jejuho 0:de3e4a57ebe0 152 ticker.attach(senseCallback, 0.1);
jejuho 0:de3e4a57ebe0 153
jejuho 0:de3e4a57ebe0 154 ///Could have main loop here doing something
jejuho 0:de3e4a57ebe0 155 while(true) {
jejuho 0:de3e4a57ebe0 156 ble.waitForEvent();
jejuho 0:de3e4a57ebe0 157 }
jejuho 0:de3e4a57ebe0 158 }
jejuho 0:de3e4a57ebe0 159