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 WeatherStation by
main.cpp
00001 #define I2C_SDA p3 00002 #define I2C_SCL p4 00003 #define BAT_SENSE p1 00004 00005 #include "mbed.h" 00006 #include "BLE.h" 00007 #include "LampService.h" 00008 #include "BatteryService.h" 00009 00010 BLE ble; 00011 00012 PwmOut light(LED1); 00013 AnalogIn battery(BAT_SENSE); 00014 00015 00016 const static char DEVICE_NAME[] = "Lemp"; 00017 00018 static const uint16_t serviceList[] = { 00019 GattService::UUID_ENVIRONMENTAL_SERVICE, 00020 GattService::UUID_BATTERY_SERVICE 00021 }; 00022 00023 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00024 { 00025 /* Restart Advertising on disconnection*/ 00026 ble.gap().startAdvertising(); 00027 } 00028 00029 00030 int main(void) 00031 { 00032 printf("Start\n"); 00033 00034 light = 1; 00035 00036 ble.init(); 00037 ble.gap().onDisconnection(disconnectionCallback); 00038 00039 /* Setup weather service. */ 00040 LampService lampService(ble); 00041 BatteryService batteryService(ble, 0); 00042 + 00043 /* setup advertising */ 00044 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00045 //TODo add lamp service to the list 00046 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)serviceList, sizeof(serviceList)); 00047 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00048 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00049 ble.gap().setAdvertisingInterval(500); /* 1000ms */ 00050 ble.gap().startAdvertising(); 00051 00052 // light.period(0.0005f); 00053 // light.write(0.98f); 00054 00055 while (true) { 00056 if (ble.getGapState().connected) { 00057 uint8_t buffer[1]; 00058 uint16_t lenght=sizeof(uint8_t); 00059 00060 ble.gattServer().read(lampService.lightLevelCharacteristic.getValueHandle(), buffer, &lenght); 00061 00062 int lightLevel = (int)buffer[0]; 00063 00064 if (lightLevel > 0) { 00065 light = 0; 00066 } 00067 else { 00068 light = 1; 00069 } 00070 00071 // Voltage divider ratio on TinyBLE's battery sense is 18% for some reason (R1 = 10M, R2 = 2.2M) 00072 // So, on 3.3V the ADC will indicate only 18% capacity. We remap so that 18% is 100% 00073 // PS: battery = 0->1 00074 float batteryLvl = ((battery.read() * 100.0f) * 100.0f) / 18.0f; 00075 00076 if (batteryLvl > 100) { 00077 batteryLvl = 100.0f; 00078 } 00079 00080 batteryService.updateBatteryLevel(batteryLvl); 00081 } 00082 else { 00083 ble.waitForEvent(); 00084 } 00085 } 00086 }
Generated on Mon Jul 18 2022 01:07:00 by
1.7.2
