BLE_HTS_Demo

This BLE_HTS_Demo program enables you to collect Temperature and Humidity data reading from sensor and transmit to collector device such as smartphone.

Below documents teach you how to install app that can connect and read data from our DELTA-DFCM-NNN40 development board. There are two versions, Android and iPhone.

/media/uploads/Marcomissyou/ios_app_for_environment_sensor_0518.pdf

/media/uploads/Marcomissyou/android_app_for_environment_sensor.pdf

main.cpp

Committer:
Marcomissyou
Date:
2015-06-03
Revision:
0:ef0f188a6fdd
Child:
1:a7ecbb0ccc61

File content as of revision 0:ef0f188a6fdd:



/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "BatteryService.h"
#include "DeviceInformationService.h"
#include "BLEDevice.h"

#include "hts221.h"
#include "uvis25.h"
#include "nrf_gpio.h"

#include "nrf51_bitfields.h"

#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
                               * it will have an impact on code-size and power consumption. */

#if NEED_CONSOLE_OUTPUT
Serial  pc(p25, p23);
#define DEBUG(...) { pc.printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */

BLEDevice  ble;

InterruptIn Button0_LowInt(p7);   

DigitalOut  led0(p4);
DigitalOut  led1(p5);
DigitalOut  led2(p6);

static const uint8_t UUID_HUMI_AND_UVI[] = {0xf5, 0x59, 0xa2, 0x49, 0xbe, 0xb1, 0x4c, 0x54, 0xa1, 0x0a, 0xc7, 0x95, 0x7e, 0x17, 0xf8, 0x67};
uint8_t wrs_HumiUVI_payload[11] = {0, };
uint8_t htsTempPayload[7] = {0,};
/* Health Thermometer Service */
/* Service:  https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */
/* Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */
GattCharacteristic htsTemp       ( GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, htsTempPayload, 6, 13, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
GattCharacteristic wrs_HumiUVI   ( UUID_HUMI_AND_UVI, wrs_HumiUVI_payload, 1, 7, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
GattCharacteristic *htsChars[] = {&htsTemp, &wrs_HumiUVI};
GattService        htsService    ( GattService::UUID_HEALTH_THERMOMETER_SERVICE, htsChars, sizeof(htsChars) / sizeof(GattCharacteristic *));

float tempCelsius = 25.50;
int32_t tempCelsius_ix100;
int8_t exponent = -2;
uint8_t UTC[7] = { 222, 7, 9, 11, 13, 42, 59 }; 
float humi = 55;
int32_t humi_ix100;
uint8_t uvi = 8; 
uint32_t counter = 0; 

const static char     DEVICE_NAME[]        = "WRS_d7";
static const uint16_t uuid16_list[]        = {GattService::UUID_BATTERY_SERVICE,
                                              GattService::UUID_DEVICE_INFORMATION_SERVICE};
static volatile bool  triggerSensorPolling = false;

/**************************************************************************/
/*!
    @brief  This custom class can be used to override any GattServerEvents
            that you are interested in handling on an application level.
*/
/**************************************************************************/
//
void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    DEBUG("Disconnected!\n\r");
    DEBUG("Restarting the advertising process\n\r");
    ble.startAdvertising();
}

void periodicCallback(void)
{
    led0 = !led0; /* Do blinky on LED1 while we're waiting for BLE events */

    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
     * heavy-weight sensor polling from the main thread. */
    triggerSensorPolling = true;
}

void LowInt() {
    led0 = !led0;
}
/**************************************************************************/
/*!
    @brief  Program entry point
*/
/**************************************************************************/

int main(void)
{
    nrf_gpio_cfg_output(19);    // SWIO 
    nrf_gpio_pin_clear(19);
    
    Button0_LowInt.fall(&LowInt);
    
    if (hts221_init() ) //led2=1;
//    else led2=0;
    HTS221_Calib();
    /* Setup blinky: led1 is toggled in main, led2 is toggled via Ticker */
//    led1=1;
//    led2=0;
    
    Ticker ticker;
    ticker.attach(periodicCallback, 2);
    
    ble.init();
    ble.onDisconnection(disconnectionCallback);

    /* Setup auxiliary services. */
    BatteryService           battery(ble);
    DeviceInformationService deviceInfo(ble, "Delta", "NNN40_1.0", "SN", "EVB_1.0", "B230-n80-m92", "T_0128b");
  
    /* Setup advertising. */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
    ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.setAdvertisingInterval(1600);
    ble.startAdvertising();
    
    ble.addService(htsService);   
    
    while (true) {
 //       if (triggerSensorPolling ) {
//            triggerSensorPolling = false;

            HTS221_ReadTempHumi(&tempCelsius, &humi);
            uvi = UVIS25_ReadUVI();
            /* Update the Temperature measurement */
            /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */
            tempCelsius_ix100 = tempCelsius * 100;      
            htsTempPayload[0] = 0x02;         // flag
            htsTempPayload[1] = tempCelsius_ix100%0xff;
            htsTempPayload[2] = tempCelsius_ix100/0xff;
            htsTempPayload[3] = tempCelsius_ix100/0xffff;
            htsTempPayload[4] = exponent;
            for (int i = 0; i < 7; i++) htsTempPayload[i+5] = UTC[i];
            ble.updateCharacteristicValue(htsTemp.getValueAttribute().getHandle(), htsTempPayload, 12);  

            ble.waitForEvent(); 
            wait(1);
            //humi_ix100 = humi * 100;   
            wrs_HumiUVI_payload[0] = 0x03;    // flag
            memcpy(&wrs_HumiUVI_payload[1],&humi,4);
            //wrs_HumiUVI_payload[1] = humi_ix100%0xff;
//            wrs_HumiUVI_payload[2] = humi_ix100/0xff;
//            wrs_HumiUVI_payload[3] = humi_ix100/0xffff;
//            wrs_HumiUVI_payload[4] = exponent;
            wrs_HumiUVI_payload[5] = uvi;
      
            ble.updateCharacteristicValue(wrs_HumiUVI.getValueAttribute().getHandle(), wrs_HumiUVI_payload, 6);
            ble.waitForEvent();                    
            DEBUG("%f,%f,%d\r\n", tempCelsius, humi, counter++);
            wait(1);
            
        //} else {
//           ble.waitForEvent();
//        }
    }
        
}

// Gill modify   
// 20150331 change humi data format to float 
// 20150408 diable triggerSensorPolling function, using while loop and wait 
// 20150428 change external clock to internal RC