Locator mobile version

Dependencies:   BLE_API mbed nRF51822

Fork of WeatherStation by Weather man

Committer:
PostaL
Date:
Sat Apr 29 13:57:54 2017 +0000
Revision:
6:89b81d75ba02
Parent:
5:fe4888cc60cc
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PostaL 3:b6d2c5195055 1 #define I2C_SDA p3
PostaL 3:b6d2c5195055 2 #define I2C_SCL p4
PostaL 6:89b81d75ba02 3 #define BAT_SENSE p1
PostaL 0:7f951f57dbd2 4
PostaL 0:7f951f57dbd2 5 #include "mbed.h"
PostaL 0:7f951f57dbd2 6 #include "BLE.h"
PostaL 6:89b81d75ba02 7 #include "LampService.h"
PostaL 2:654ee4b3950f 8 #include "BatteryService.h"
PostaL 4:fddb2d7c7c61 9
PostaL 4:fddb2d7c7c61 10 BLE ble;
PostaL 4:fddb2d7c7c61 11
PostaL 6:89b81d75ba02 12 PwmOut light(LED1);
PostaL 6:89b81d75ba02 13 AnalogIn battery(BAT_SENSE);
PostaL 5:fe4888cc60cc 14
PostaL 3:b6d2c5195055 15
PostaL 6:89b81d75ba02 16 const static char DEVICE_NAME[] = "Lemp";
PostaL 3:b6d2c5195055 17
PostaL 3:b6d2c5195055 18 static const uint16_t serviceList[] = {
PostaL 2:654ee4b3950f 19 GattService::UUID_ENVIRONMENTAL_SERVICE,
PostaL 2:654ee4b3950f 20 GattService::UUID_BATTERY_SERVICE
PostaL 2:654ee4b3950f 21 };
PostaL 3:b6d2c5195055 22
PostaL 0:7f951f57dbd2 23 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
PostaL 0:7f951f57dbd2 24 {
PostaL 4:fddb2d7c7c61 25 /* Restart Advertising on disconnection*/
PostaL 0:7f951f57dbd2 26 ble.gap().startAdvertising();
PostaL 0:7f951f57dbd2 27 }
PostaL 0:7f951f57dbd2 28
PostaL 4:fddb2d7c7c61 29
PostaL 0:7f951f57dbd2 30 int main(void)
PostaL 0:7f951f57dbd2 31 {
PostaL 4:fddb2d7c7c61 32 printf("Start\n");
PostaL 3:b6d2c5195055 33
PostaL 6:89b81d75ba02 34 light = 1;
PostaL 6:89b81d75ba02 35
PostaL 0:7f951f57dbd2 36 ble.init();
PostaL 0:7f951f57dbd2 37 ble.gap().onDisconnection(disconnectionCallback);
PostaL 0:7f951f57dbd2 38
PostaL 2:654ee4b3950f 39 /* Setup weather service. */
PostaL 6:89b81d75ba02 40 LampService lampService(ble);
PostaL 2:654ee4b3950f 41 BatteryService batteryService(ble, 0);
PostaL 6:89b81d75ba02 42 +
PostaL 0:7f951f57dbd2 43 /* setup advertising */
PostaL 0:7f951f57dbd2 44 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
PostaL 6:89b81d75ba02 45 //TODo add lamp service to the list
PostaL 3:b6d2c5195055 46 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)serviceList, sizeof(serviceList));
PostaL 0:7f951f57dbd2 47 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
PostaL 0:7f951f57dbd2 48 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
PostaL 6:89b81d75ba02 49 ble.gap().setAdvertisingInterval(500); /* 1000ms */
PostaL 0:7f951f57dbd2 50 ble.gap().startAdvertising();
PostaL 0:7f951f57dbd2 51
PostaL 6:89b81d75ba02 52 // light.period(0.0005f);
PostaL 6:89b81d75ba02 53 // light.write(0.98f);
PostaL 6:89b81d75ba02 54
PostaL 0:7f951f57dbd2 55 while (true) {
PostaL 6:89b81d75ba02 56 if (ble.getGapState().connected) {
PostaL 6:89b81d75ba02 57 uint8_t buffer[1];
PostaL 6:89b81d75ba02 58 uint16_t lenght=sizeof(uint8_t);
PostaL 0:7f951f57dbd2 59
PostaL 6:89b81d75ba02 60 ble.gattServer().read(lampService.lightLevelCharacteristic.getValueHandle(), buffer, &lenght);
PostaL 4:fddb2d7c7c61 61
PostaL 6:89b81d75ba02 62 int lightLevel = (int)buffer[0];
PostaL 5:fe4888cc60cc 63
PostaL 6:89b81d75ba02 64 if (lightLevel > 0) {
PostaL 6:89b81d75ba02 65 light = 0;
PostaL 6:89b81d75ba02 66 }
PostaL 6:89b81d75ba02 67 else {
PostaL 6:89b81d75ba02 68 light = 1;
PostaL 6:89b81d75ba02 69 }
PostaL 5:fe4888cc60cc 70
PostaL 6:89b81d75ba02 71 // Voltage divider ratio on TinyBLE's battery sense is 18% for some reason (R1 = 10M, R2 = 2.2M)
PostaL 6:89b81d75ba02 72 // So, on 3.3V the ADC will indicate only 18% capacity. We remap so that 18% is 100%
PostaL 6:89b81d75ba02 73 // PS: battery = 0->1
PostaL 6:89b81d75ba02 74 float batteryLvl = ((battery.read() * 100.0f) * 100.0f) / 18.0f;
PostaL 3:b6d2c5195055 75
PostaL 6:89b81d75ba02 76 if (batteryLvl > 100) {
PostaL 6:89b81d75ba02 77 batteryLvl = 100.0f;
PostaL 6:89b81d75ba02 78 }
PostaL 6:89b81d75ba02 79
PostaL 6:89b81d75ba02 80 batteryService.updateBatteryLevel(batteryLvl);
PostaL 6:89b81d75ba02 81 }
PostaL 6:89b81d75ba02 82 else {
PostaL 0:7f951f57dbd2 83 ble.waitForEvent();
PostaL 0:7f951f57dbd2 84 }
PostaL 0:7f951f57dbd2 85 }
PostaL 0:7f951f57dbd2 86 }