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
Revision 0:25ad6eba7916, committed 2015-07-07
- Comitter:
- Slepnir
- Date:
- Tue Jul 07 04:02:14 2015 +0000
- Child:
- 1:e036e77762fa
- Commit message:
- V1:; - Successfully advertise and connect ; - Update fail; - Can read only 4 bit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/BLECommunication.cpp Tue Jul 07 04:02:14 2015 +0000
@@ -0,0 +1,36 @@
+#include "BLECommunication.h"
+//uint8_t DEVICE_NAME[] = "NRFTEST";
+//uint16_t list[] = {SPS_UUID_SERVICE};
+
+BLECommunication::BLECommunication(SmartPlugBLE& system,BLE &_ble):
+ ble(_ble),smartPlugService(ble,system),led(LED3)
+{
+
+ ble.init();
+
+ ble.gattServer().onDataWritten(&smartPlugService,&SmartPlugService::onDataWritten);
+ smartPlugService.setupService();
+ //start();
+}
+
+void BLECommunication::start(void)
+{
+ //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+// ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)list, sizeof(list));
+// ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN);
+// ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));//cannot blink after this
+//
+// ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+// ble.gap().setAdvertisingInterval(1000); /* 1000ms */
+//
+// ble.gap().startAdvertising();
+// while(true)
+// {
+// ble.waitForEvent();
+// }
+}
+
+void BLECommunication::periodicCallback(void)
+{
+ led = !led;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/BLECommunication.h Tue Jul 07 04:02:14 2015 +0000
@@ -0,0 +1,25 @@
+#ifndef BLE_COMMUNICATION_H
+#define BLE_COMMUNICATION_H
+
+#include "BLE.h"
+#include "Services/SmartPlugService.h"
+#include "MVC.h"
+#include "mbed.h"
+
+
+class BLECommunication
+{
+public:
+
+ BLECommunication(SmartPlugBLE& system,BLE &_ble);
+ void periodicCallback(void);
+ void start(void);
+
+private:
+ BLE &ble;
+ const uint8_t *DEVICE_NAME;
+ SmartPlugService smartPlugService;
+ DigitalOut led;
+};
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BLE_API.lib Tue Jul 07 04:02:14 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#9f4251b3355c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MVC.h Tue Jul 07 04:02:14 2015 +0000
@@ -0,0 +1,17 @@
+#ifndef MVC_H
+#define MVC_H
+
+class Observer
+{
+ public:
+ void update(void* data){}
+};
+
+class Observable
+{
+ public:
+ void notifyObservers();
+ void addObserver(Observer o);
+};
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SPCommunication.h Tue Jul 07 04:02:14 2015 +0000
@@ -0,0 +1,13 @@
+#ifndef SP_COMMUNICATION_H
+#define SP_COMMUNICATION_H
+
+class SPCommunication
+{
+ public:
+ unsigned long getVoltage(){return 220;}
+ unsigned long getCurrent(){return 10;}
+ unsigned long getPower(){return 10;}
+ unsigned long getPowerFactor(){return 10;}
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Services/SmartPlugService.cpp Tue Jul 07 04:02:14 2015 +0000
@@ -0,0 +1,105 @@
+#include "ble/BLE.h"
+#include "mbed.h"
+#include "SmartPlugService.h"
+#include "SmartPlugBLE.h"
+
+
+SmartPlugService::SmartPlugService(BLE &_ble, SmartPlugBLE &sys):
+ ble(_ble), system(sys),
+ voltageChar(SPS_UUID_VOLTAGE_CHAR,voltage),
+ currentChar(SPS_UUID_CURERNT_CHAR,current),
+ powerChar(SPS_UUID_POWER_CHAR,power),
+ powerFactorChar(SPS_UUID_POWER_FACTOR_CHAR,powerFactor),
+ energyChar(SPS_UUID_ENERGY_CHAR,energy),
+ relayChar(SPS_UUID_RELAY_CHAR,relayValue),
+ updateChar(SPS_UUID_UPDATE_CHAR,NULL)
+{
+ (&system)->addObserver(*this);
+}
+
+void SmartPlugService::onDataWritten(const GattWriteCallbackParams *params)
+{
+ if(params->handle == updateChar.getValueHandle())
+ {
+ system.updateData();
+ }
+}
+void SmartPlugService::update(void* data)
+{
+ updateData((SmartPlug*)data);
+}
+
+void SmartPlugService::updateData(SmartPlug* data)
+{
+ updateVoltage(data->getVoltage());
+ updateCurrent(data->getCurrent());
+ updatePower(data->getPower());
+ updatePowerFactor(data->getPowerFactor());
+}
+
+void SmartPlugService::updateVoltage(unsigned long v)
+{
+ if (ble.getGapState().connected)
+ {
+ voltage[0] = v>>24;
+ voltage[1] = (v&0x00FFFFFF)>>16;
+ voltage[2] = (v&0x0000FFFF)>>8;
+ voltage[3] = v&0x000000FF;
+ ble.updateCharacteristicValue(voltageChar.getValueHandle(),voltage,4);
+ }
+}
+
+void SmartPlugService::updateCurrent(unsigned long c)
+{
+ if (ble.getGapState().connected)
+ {
+ current[0] = c>>24;
+ current[1] = (c&0x00FFFFFF)>>16;
+ current[2] = (c&0x0000FFFF)>>8;
+ current[3] = c&0x000000FF;
+ ble.updateCharacteristicValue(currentChar.getValueHandle(),voltage,4);
+ }
+}
+
+void SmartPlugService::updatePower(unsigned long p)
+{
+ if (ble.getGapState().connected)
+ {
+ power[0] = p>>24;
+ power[1] = (p&0x00FFFFFF)>>16;
+ power[2] = (p&0x0000FFFF)>>8;
+ power[3] = p&0x000000FF;
+ ble.updateCharacteristicValue(powerChar.getValueHandle(),voltage,4);
+ }
+}
+
+void SmartPlugService::updatePowerFactor(unsigned long pf)
+{
+ if (ble.getGapState().connected)
+ {
+ powerFactor[0] = pf>>24;
+ powerFactor[1] = (pf&0x00FFFFFF)>>16;
+ powerFactor[2] = (pf&0x0000FFFF)>>8;
+ powerFactor[3] = pf&0x000000FF;
+ ble.updateCharacteristicValue(powerFactorChar.getValueHandle(),voltage,4);
+ }
+}
+
+void SmartPlugService::setupService(void)
+{
+
+ static bool serviceAdded = false; /* We should only ever need to add the service once. */
+ if (serviceAdded)
+ {
+ return;
+ }
+
+
+ GattCharacteristic *charTable[] = {&voltageChar,¤tChar,&powerChar,
+ &powerFactorChar,&energyChar,&relayChar,
+ &updateChar
+ };
+ GattService smartPlugService = GattService(SPS_UUID_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+ ble.addService(smartPlugService);
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Services/SmartPlugService.h Tue Jul 07 04:02:14 2015 +0000
@@ -0,0 +1,68 @@
+#ifndef SP_SERVICE_H
+#define SP_SERVICE_H
+
+#include "ble/BLE.h"
+#include "MVC.h"
+#include "mbed.h"
+#include "SmartPlugBLE.h"
+
+
+#define FULL_UUID(uuid_16) { \
+ 0x9B,0x3F,uuid_16>>16,uuid_16&0xFF,0x65,0x80,0x47,0x51, \
+ 0xB0,0x2F,0xA7,0xB5,0x19,0x4C,0x7F,0x23 \
+ }
+
+static const uint16_t SPS_UUID_SERVICE = 0x1522;
+static const uint16_t SPS_UUID_VOLTAGE_CHAR = 0x1523;
+static const uint16_t SPS_UUID_CURERNT_CHAR = 0x1524;
+static const uint16_t SPS_UUID_POWER_CHAR = 0x1525;
+static const uint16_t SPS_UUID_POWER_FACTOR_CHAR = 0x1526;
+static const uint16_t SPS_UUID_ENERGY_CHAR = 0x1527;
+static const uint16_t SPS_UUID_RELAY_CHAR = 0x1528;
+static const uint16_t SPS_UUID_UPDATE_CHAR = 0x1529;
+
+static const uint8_t SPS_UUID_SERVICE_FULL[] = FULL_UUID(SPS_UUID_SERVICE);
+static const uint8_t SPS_UUID_VOLTAGE_CHAR_FULL[] = FULL_UUID(SPS_UUID_VOLTAGE_CHAR);
+static const uint8_t SPS_UUID_CURERNT_CHAR_FULL[] = FULL_UUID(SPS_UUID_CURERNT_CHAR);
+static const uint8_t SPS_UUID_POWER_CHAR_FULL[] = FULL_UUID(SPS_UUID_POWER_CHAR);
+static const uint8_t SPS_UUID_POWER_FACTOR_CHAR_FULL[] = FULL_UUID(SPS_UUID_POWER_FACTOR_CHAR);
+static const uint8_t SPS_UUID_ENERGY_CHAR_FULL[] = FULL_UUID(SPS_UUID_ENERGY_CHAR);
+static const uint8_t SPS_UUID_RELAY_CHAR_FULL[] = FULL_UUID(SPS_UUID_RELAY_CHAR);
+static const uint8_t SPS_UUID_UPDATE_CHAR_FULL[] = FULL_UUID(SPS_UUID_UPDATE_CHAR);
+
+
+class SmartPlugService: public Observer
+{
+public:
+ SmartPlugService(BLE &_ble, SmartPlugBLE &sys);
+
+ void updateVoltage(unsigned long value);
+ void updateCurrent(unsigned long value);
+ void updatePower(unsigned long value);
+ void updatePowerFactor(unsigned long value);
+ void onDataWritten(const GattWriteCallbackParams *params);
+ void update(void* data);
+ void updateData(SmartPlug* data);
+ void setupService(void);
+private:
+ BLE ble;
+
+ uint8_t voltage[4];
+ uint8_t current[4];
+ uint8_t power[4];
+ uint8_t powerFactor[4];
+ uint8_t energy[4];
+ uint8_t relayValue[3];
+ //uint8_t updateValue;
+ SmartPlugBLE& system;
+
+ ReadOnlyGattCharacteristic<uint8_t> voltageChar;
+ ReadOnlyGattCharacteristic<uint8_t> currentChar;
+ ReadOnlyGattCharacteristic<uint8_t> powerChar;
+ ReadOnlyGattCharacteristic<uint8_t> powerFactorChar;
+ ReadOnlyGattCharacteristic<uint8_t> energyChar;
+ ReadWriteGattCharacteristic<uint8_t> relayChar;
+ WriteOnlyGattCharacteristic<uint8_t> updateChar;
+};
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SmartPlug.h Tue Jul 07 04:02:14 2015 +0000
@@ -0,0 +1,114 @@
+#ifndef SMART_PLUG_H
+#define SMART_PLUG_H
+
+
+class Relay
+{
+ public:
+ Relay()
+ {
+ }
+
+ void setState(uint8_t newState)
+ {
+ state = newState;
+ }
+
+ void setHrCounter(uint8_t newHrCounter)
+ {
+ hrCounter = newHrCounter;
+ }
+
+ void setMinCounter(uint8_t newMinCounter)
+ {
+ minCounter = newMinCounter;
+ }
+
+ uint8_t getState()
+ {
+ return state;
+ }
+
+ uint8_t getHrCounter()
+ {
+ return hrCounter;
+ }
+
+ uint8_t getMinCounter()
+ {
+ return minCounter;
+ }
+
+ private:
+ uint8_t state;
+ uint8_t hrCounter;
+ uint8_t minCounter;
+};
+
+class SmartPlug
+{
+ public:
+ SmartPlug()
+ {
+ }
+
+ unsigned long getVoltage()
+ {
+ return voltage;
+ }
+
+ unsigned long getCurrent()
+ {
+ return current;
+ }
+
+ unsigned long getPower()
+ {
+ return power;
+ }
+
+ unsigned long getPowerFactor()
+ {
+ return powerFactor;
+ }
+
+ Relay getRelay()
+ {
+ return relay;
+ }
+
+ void setVoltage(unsigned long data)
+ {
+ voltage = data;
+ }
+
+ void setCurrent(unsigned long data)
+ {
+ current = data;
+ }
+
+ void setPower(unsigned long data)
+ {
+ power = data;
+ }
+
+ void setPowerFactor(unsigned long data)
+ {
+ powerFactor = data;
+ }
+
+ void setRelay(const uint8_t *data)
+ {
+ relay.setState(data[0]);
+ relay.setHrCounter(data[1]);
+ relay.setMinCounter(data[2]);
+ }
+ private:
+ unsigned long voltage;
+ unsigned long current;
+ unsigned long power;
+ unsigned long powerFactor;
+ Relay relay;
+};
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SmartPlugBLE.h Tue Jul 07 04:02:14 2015 +0000
@@ -0,0 +1,66 @@
+#ifndef SMART_PLUG_BLE_H
+#define SMART_PLUG_BLE_H
+
+#include "SmartPlug.h"
+#include "MVC.h"
+#include "mbed.h"
+#include "SPCommunication.h"
+#include <vector>
+
+
+class SmartPlugBLE: public Observable
+{
+ public:
+ SmartPlugBLE()
+ {
+ ticker.attach(this,&SmartPlugBLE::periodicCallback, 1); // blink LED every second
+ }
+
+ void onRelayWrite(uint8_t *data)
+ {
+ updateRelay(data);
+ notifyObservers();
+ }
+
+ void updateRelay(const uint8_t *data)
+ {
+ smartPlug.setRelay(data);
+ }
+
+ void notifyObservers()
+ {
+ for(int i=0;i<observers.size();i++)
+ {
+ observers[i].update((void*)&smartPlug);
+ }
+ }
+
+ void addObserver(Observer o)
+ {
+ observers.push_back(o);
+ }
+
+ void periodicCallback(void)
+ {
+ //led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
+ }
+
+ void updateData(void)
+ {
+ smartPlug.setVoltage(smartPlug.getVoltage()+1);//(spComm.getVoltage());
+ smartPlug.setCurrent(smartPlug.getCurrent()+1);//(spComm.getCurrent());
+ smartPlug.setPower(smartPlug.getPower()+1);//(spComm.getPower());
+ smartPlug.setPowerFactor(smartPlug.getPowerFactor()+1);//(spComm.getPowerFactor());
+ notifyObservers();
+ }
+
+ private:
+ SmartPlug smartPlug;
+ Ticker ticker;
+ //DigitalOut led1;
+ SPCommunication spComm;
+ //BLECommunication bleComm;
+ vector<Observer> observers;
+};
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Jul 07 04:02:14 2015 +0000
@@ -0,0 +1,37 @@
+#include "mbed.h"
+#include "SmartPlugBLE.h"
+#include "BLECommunication.h"
+#include "BLE.h"
+BLE ble;
+uint8_t DEVICE_NAME[] = "NRFTEST";
+uint16_t list[] = {SPS_UUID_SERVICE};
+
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+{
+ ble.gap().startAdvertising();
+}
+
+int main(void)
+{
+ ble.gap().onDisconnection(disconnectionCallback);
+ Ticker ticker;
+ SmartPlugBLE smartPlugBLE;
+ BLECommunication bleComm(smartPlugBLE,ble);
+ ticker.attach(&bleComm,&BLECommunication::periodicCallback, 1);
+ /* Setup advertising. */
+
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)list, sizeof(list));
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN);
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));//cannot blink after this
+
+ ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+ ble.gap().setAdvertisingInterval(1000); /* 1000ms */
+
+ ble.gap().startAdvertising();
+
+
+ while(1) {
+ ble.waitForEvent();
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Jul 07 04:02:14 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nRF51822.lib Tue Jul 07 04:02:14 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#7c68c8d67e1f