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 mbed nRF51822
Fork of SensorModulePIR by
Revision 22:88b6e11eb8d5, committed 2018-06-06
- Comitter:
- MisterGiet
- Date:
- Wed Jun 06 14:53:43 2018 +0000
- Parent:
- 11:1186eb69af44
- Commit message:
- Modification for Window Sensor Module for the Agile IoT final review.
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Jun 01 09:55:56 2017 +0000
+++ b/main.cpp Wed Jun 06 14:53:43 2018 +0000
@@ -52,21 +52,19 @@
/* BLE CONFIGURATION */
BLE ble;
-static const char DEVICE_NAME[] = "SM02";
+static const char DEVICE_NAME[] = "WindowSensorUnit";
static const uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE};
static volatile bool triggerSensorPolling = false;
-static char fwversion[31] = "APP-2217";
+static char fwversion[31] = "Agile-IoT";
static uint8_t batteryLevel = 99;
static int8_t TxPower = +4;
//static uint8_t humidity = 99;
const static uint8_t ManufData[] = {0x01,0x02,0x03,0x04,0x05}; // Set up to 26B of advertising data to use for the Manufacturer data.
-static HealthThermometerService *thermometerServicePtr = NULL;;
+//static HealthThermometerService *thermometerServicePtr = NULL;;
static BatteryService* batteryServicePtr = NULL;;
-static EnvironmentalService* humidityTemperatureServicePtr = NULL;
-static HumidityMeasureService *humidityService = NULL;
-static PresenceDetectionService *presenceService = NULL;
+//static EnvironmentalService* humidityTemperatureServicePtr = NULL;
UARTService *uart;
// Firmware
@@ -105,7 +103,7 @@
float Vadc = (float)my_analogin_read_u16();
float Vref = 1.2; // Internal Vref
- float Vcc = 3 * 4 * Vref * Vadc / 1024.0;
+ float Vcc = 0.62 * Vref * Vadc / 1024.0;; //3 * 4 * Vref * Vadc / 1024.0;
sprintf (Vpower, "Vcc=%.2fV, ", Vcc);
DEBUG(Vpower);
@@ -115,23 +113,13 @@
int getBatteryPercent ()
{
char batt_mess[10];
- float Vbat_min = 2; //2;
- float Vbat_max = 3;
+ float Vbat_min = 3; //2;
+ float Vbat_max = 3.7;
int battLevel = (int) ( ((getBatteryVolt()-Vbat_min) / (Vbat_max-Vbat_min)) *100);
sprintf (batt_mess, "Vbatt=%i, ", battLevel);
- DEBUG(batt_mess);
-
- // To avoid values bigger than 100%
- if (battLevel > 100) {
- battLevel = 100;
- }
-
- // To avoid overflow due to a value lower than Vbat_min
- if (battLevel >= 250) {
- battLevel = 0;
- }
-
+ DEBUG(batt_mess);
+
return battLevel;
}
@@ -283,23 +271,15 @@
// Set Transmission power:
ble.setTxPower(TxPower);
- //ble.gap().setScanParams(500, 400);
- //ble.gap().startScan(advertisementCallback);
-
//BLE server setup
ble.gattServer().onDataWritten(onDataWrittenCallback);
//Setup primary services
- presenceService = new PresenceDetectionService(ble, updatePresenceCounter(presenceCounter));
-
- thermometerServicePtr = new HealthThermometerService(ble, getTemperature(), HealthThermometerService::LOCATION_EAR);
- humidityService = new HumidityMeasureService(ble, getHumidity());
-
- humidityTemperatureServicePtr = new EnvironmentalService (ble);
batteryServicePtr = new BatteryService(ble, batteryLevel);
+ //humidityTemperatureServicePtr = new EnvironmentalService (ble);
+
ble.addService(firmwareService);
- uart = new UARTService(ble);
//ble.addService(TxPowerService);
/* setup advertising */
@@ -309,14 +289,9 @@
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
/* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ManufData, sizeof(ManufData));
- ble.gap().accumulateAdvertisingPayloadTxPower(TxPower);
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
- //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR);
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER);
- //ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
- //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
ble.setAdvertisingInterval(1000); /* 1000ms */
ble.startAdvertising();
@@ -335,17 +310,11 @@
void updateBLEservices ()
{
- thermometerServicePtr->updateTemperature(getTemperature());
- humidityService->updateHumidity(getHumidity());
-
- humidityTemperatureServicePtr->updateTemperature (getTemperature());
- humidityTemperatureServicePtr->updateHumidity (getHumidity());
-
+ //humidityTemperatureServicePtr->updateTemperature (getTemperature());
+ //humidityTemperatureServicePtr->updateHumidity (getHumidity());
+
batteryServicePtr->updateBatteryLevel(getBatteryPercent());
-
- presenceService->updatePresence(updatePresenceCounter (presenceCounter));
- //ble.updateCharacteristicValue(batteryPercentage.getValueAttribute().getHandle(), &batteryLevel, sizeof(batteryLevel));
- //ble.updateCharacteristicValue(TxPowerChar.getValueAttribute().getHandle(), (uint8_t*)&TxPower, 1);
+
}
int main(void)
@@ -355,9 +324,6 @@
blinkLed(1,1);
- //Ticker ticker_presence;
- //ticker_presence.attach(periodicCallbackPresence, 0.5);
-
motion_pin.fall(&periodicCallbackPresence);
Ticker ticker_sensor;
@@ -367,12 +333,6 @@
BLE &bleptr = BLE::Instance();
bleptr.init(bleInitComplete);
- /*
- //SpinWait for initialization to complete. This is necessary because the
- //BLE object is used in the main loop below.
- while (ble.hasInitialized() == false) { }
- */
-
while (true) {
/* Get Presence data: */
@@ -409,14 +369,14 @@
triggerSensorPolling = false;
updateBLEservices();
-
+
/*
// Red LED blinks if low battery, at every connection
if (batteryLevel <= 10) {
blinkLed(battNotify,2);
}
*/
-
+
} else {
ble.waitForEvent();
}
