Bluetooth Low Energy for Smart Plug
Dependencies: BLE_API mbed nRF51822
Fork of SmartPlugBLE by
SmartPlugBLE.h
00001 #ifndef SMART_PLUG_BLE_H 00002 #define SMART_PLUG_BLE_H 00003 00004 #include "SmartPlug.h" 00005 #include "MVC.h" 00006 #include "mbed.h" 00007 #include "SPCommunication.h" 00008 #include <vector> 00009 00010 00011 class SmartPlugBLE: public Observable 00012 { 00013 public: 00014 SmartPlugBLE():counter(60),led(LED3,0) 00015 { 00016 ticker.attach(this,&SmartPlugBLE::periodicCallback, 1); // blink LED every second 00017 } 00018 00019 void onRelayWrite(const uint8_t *data) 00020 { 00021 counter = 60; 00022 updateRelay(data); 00023 notifyObservers(); 00024 } 00025 00026 void updateRelay(const uint8_t *data) 00027 { 00028 uint8_t state = data[0]>>7; 00029 uint8_t hour = data[0]&0x7F; 00030 uint8_t min = data[1]; 00031 //instant behavior 00032 if(state) 00033 { 00034 printf("Open!\r\n"); 00035 led = 0; 00036 } 00037 else if(!state && hour == 0 && min == 0) 00038 { 00039 printf("Close!\r\n"); 00040 led = 1; 00041 } 00042 // 00043 smartPlug.setRelay(state,hour,min); 00044 } 00045 00046 void notifyObservers() 00047 { 00048 for(int i=0; i<observers.size(); i++) 00049 { 00050 observers[i]->updateObserver((void*)&smartPlug); 00051 } 00052 } 00053 00054 void addObserver(Observer* o) 00055 { 00056 observers.push_back(o); 00057 } 00058 00059 void periodicCallback(void) 00060 { 00061 if(smartPlug.isCounting) 00062 { 00063 counter--; 00064 if(counter > 60) 00065 { 00066 smartPlug.getRelay()->setMinCounter(smartPlug.getRelay()->getMinCounter()-1); 00067 if(smartPlug.getRelay()->getMinCounter() == 0) 00068 { 00069 smartPlug.getRelay()->setHrCounter(smartPlug.getRelay()->getHrCounter()-1); 00070 smartPlug.getRelay()->setMinCounter(0); 00071 } 00072 if(smartPlug.getRelay()->getHrCounter() >128) 00073 { 00074 smartPlug.getRelay()->setHrCounter(0); 00075 //spComm.setRelay(smartPlug.getRelay()->getState()); 00076 if(smartPlug.getRelay()->getState()) 00077 printf("Open!\r\n"); 00078 else 00079 { 00080 printf("Close!\r\n"); 00081 led = 1; 00082 smartPlug.isCounting = false; 00083 } 00084 } 00085 counter = 60; 00086 } 00087 } 00088 else 00089 counter = 60; 00090 } 00091 00092 void updateData(void) { 00093 spComm.SPUart.printf("in update section"); 00094 smartPlug.setVoltage/*(smartPlug.getVoltage()+1);*/(spComm.getVoltage()); 00095 spComm.SPUart.printf("out update section"); 00096 //smartPlug.setCurrent/*(smartPlug.getCurrent()+1);*/(spComm.getCurrent()); 00097 //smartPlug.setPower/*(smartPlug.getPower()+1);*/(spComm.getPower()); 00098 //smartPlug.setPowerFactor/*(smartPlug.getPowerFactor()+1);*/(spComm.getPowerFactor()); 00099 //smartPlug.setEnergy/*(smartPlug.getEnergy()+1);*/(spComm.getEnergy()); 00100 //smartPlug.getRelay()->setState(spComm.getRelay()); 00101 notifyObservers(); 00102 } 00103 00104 SmartPlug getSmartPlug() 00105 { 00106 return smartPlug; 00107 } 00108 00109 uint8_t getCounter() 00110 { 00111 return counter; 00112 } 00113 00114 void setCounter(uint8_t val) 00115 { 00116 counter = val; 00117 } 00118 private: 00119 SmartPlug smartPlug; 00120 Ticker ticker; 00121 uint8_t counter; 00122 DigitalOut led; 00123 SPCommunication spComm; 00124 //BLECommunication bleComm; 00125 vector<Observer*> observers; 00126 }; 00127 00128 #endif
Generated on Fri Jul 22 2022 14:22:22 by 1.7.2