Skip to content
Snippets Groups Projects
Commit cc8ffca2 authored by Marie Bureau's avatar Marie Bureau
Browse files

add SCAN_STATE and OPEN_GATE_STATE

parent 8941587d
Branches
Tags
1 merge request!4My gates ble
...@@ -15,40 +15,106 @@ ...@@ -15,40 +15,106 @@
#define CLEARGATECALIBRATION 0b11 // minor high bits = 0b11 => clear gate calibration #define CLEARGATECALIBRATION 0b11 // minor high bits = 0b11 => clear gate calibration
#define SCAN_TIME 20 // scan period in second #define SCAN_TIME 20 // scan period in second
#define STATE_SCAN 1
#define STATE_OPEN_GATE 2
#define RSSI_THRESHOLD_OPEN_GATE -95
// ***** definitions *****
int STATE;
struct RSSI {
int val;
int time;
};
RSSI tabRSSI[2];
struct timeval tv;
time_t t;
class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks { class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks {
public: public:
// BLE // BLE
void onResult(BLEAdvertisedDevice device) { void onResult(BLEAdvertisedDevice device) {
// check if iBeacon Tram // check if iBeacon tram
if (!isIBeacon(device)) { if (!isIBeacon(device)) {
return; return;
} }
// check for NAVETTE_UUID // check for NAVETTE_UUID
if (getProxyUuid(device).equals(BLEUUID(NAVETTE_UUID))){ if (!isNavetteUUID(device)){
printIBeacon(device); return;
//received a trame FORCE GATE OPEN
if (HIGH2BITS_U16(getMinor(device))==FORCEGATEOPEN){
openGate();
}
} }
// received a trame FORCE GATE OPEN
if (HIGH2BITS_U16(getMinor(device))==FORCEGATEOPEN){
printIBeacon(device); // DEBUG
Serial.printf(" - force open tram\n"); // DEBUG
openGate();
updateRSSItab(getRSSI(device));
return;
}
//check gate state (STATE_SCAN or STATE_OPEN_GATE)
switch(STATE) {
case STATE_OPEN_GATE :
// nothing to do
printIBeacon(device); // DEBUG
Serial.printf(" - gate is open\n"); // DEBUG
updateRSSItab(getRSSI(device)); // TODO : to delete to reopen gate after 4 sec ?
STATE = STATE_OPEN_GATE;
break;
case STATE_SCAN :
default:
RSSI newRSSI = getRSSI(device);
int avRSSI = averageRSSI(newRSSI);
if(avRSSI > RSSI_THRESHOLD_OPEN_GATE) {
printIBeacon(device); // DEBUG
Serial.printf(" - RSSI average OK:%d\n",avRSSI); // DEBUG
openGate();
updateRSSItab(newRSSI);
STATE = STATE_OPEN_GATE;
}
else {
printIBeacon(device); // DEBUG
Serial.printf(" - RSSI average too low:%d\n",avRSSI); // DEBUG
updateRSSItab(newRSSI);
}
break;
}
} }
private: private:
struct timeval tv; // ***** compute average RSSI *****
time_t t; int averageRSSI(RSSI newRSSI){
struct tm *info;
char buffer[64]; if ((newRSSI.time - tabRSSI[0].time) >3){ // if last trame was detected more than 3 sec ago, return an average RSSI of -100 // TODO compare epochtime insted
updateRSSItab(newRSSI);
// open Gate return -100;
}
else{
int averageRSSI = (newRSSI.val + tabRSSI[0].val + tabRSSI[1].val) /3;
updateRSSItab(newRSSI);
return averageRSSI;
}
}
// ***** update RSSI table *****
void updateRSSItab (RSSI newRSSI){
tabRSSI[1]=tabRSSI[0];
tabRSSI[0]= newRSSI;
}
// ***** open gate *****
void openGate() { void openGate() {
Serial.println("OPENING GATE"); Serial.println(" OPENING GATE");
} }
// is frame iBeacon ? // ***** is frame iBeacon ? *****
bool isIBeacon(BLEAdvertisedDevice device) { bool isIBeacon(BLEAdvertisedDevice device) {
if (device.getManufacturerData().length() < 25) { if (device.getManufacturerData().length() < 25) {
return false; return false;
...@@ -62,7 +128,26 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks { ...@@ -62,7 +128,26 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks {
return true; return true;
} }
// CompanyId // ***** is UUID_NAVETTE ? *****
bool isNavetteUUID(BLEAdvertisedDevice device) {
if( getProxyUuid(device).equals(BLEUUID(NAVETTE_UUID))){
return true;
}
else {
return false;
}
}
// ***** RSSI *****
RSSI getRSSI(BLEAdvertisedDevice device){
RSSI newRSSI;
gettimeofday(&tv, NULL); // TODO : save epoch time insted
newRSSI.val = device.getRSSI();
newRSSI.time = tv.tv_sec;
return newRSSI;
}
// ***** companyId *****
unsigned short getCompanyId(BLEAdvertisedDevice device) { unsigned short getCompanyId(BLEAdvertisedDevice device) {
const unsigned short* pCompanyId = (const unsigned short*)&device const unsigned short* pCompanyId = (const unsigned short*)&device
.getManufacturerData() .getManufacturerData()
...@@ -70,7 +155,7 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks { ...@@ -70,7 +155,7 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks {
return *pCompanyId; return *pCompanyId;
} }
// iBeacon Header // ***** iBeacon Header *****
unsigned short getIBeaconHeader(BLEAdvertisedDevice device) { unsigned short getIBeaconHeader(BLEAdvertisedDevice device) {
const unsigned short* pHeader = (const unsigned short*)&device const unsigned short* pHeader = (const unsigned short*)&device
.getManufacturerData() .getManufacturerData()
...@@ -78,7 +163,7 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks { ...@@ -78,7 +163,7 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks {
return *pHeader; return *pHeader;
} }
// iBEACON UUID // ***** iBEACON UUID *****
BLEUUID getProxyUuid(BLEAdvertisedDevice device) { BLEUUID getProxyUuid(BLEAdvertisedDevice device) {
BLEUUID uuid; BLEUUID uuid;
std::string strManufacturerData = device.getManufacturerData(); std::string strManufacturerData = device.getManufacturerData();
...@@ -90,7 +175,7 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks { ...@@ -90,7 +175,7 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks {
return uuid; return uuid;
} }
// iBEACON Major // ***** iBEACON Major *****
uint16_t getMajor(BLEAdvertisedDevice device) { uint16_t getMajor(BLEAdvertisedDevice device) {
std::string strManufacturerData = device.getManufacturerData(); std::string strManufacturerData = device.getManufacturerData();
uint8_t cManufacturerData[100]; uint8_t cManufacturerData[100];
...@@ -100,7 +185,7 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks { ...@@ -100,7 +185,7 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks {
return ENDIAN_CHANGE_U16(oBeacon.getMajor()); return ENDIAN_CHANGE_U16(oBeacon.getMajor());
} }
// iBEACON Minor // ***** iBEACON Minor *****
uint16_t getMinor(BLEAdvertisedDevice device) { uint16_t getMinor(BLEAdvertisedDevice device) {
std::string strManufacturerData = device.getManufacturerData(); std::string strManufacturerData = device.getManufacturerData();
uint8_t cManufacturerData[100]; uint8_t cManufacturerData[100];
...@@ -110,7 +195,7 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks { ...@@ -110,7 +195,7 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks {
return ENDIAN_CHANGE_U16(oBeacon.getMinor()); return ENDIAN_CHANGE_U16(oBeacon.getMinor());
} }
// iBEACON TxPower // ***** iBEACON TxPower *****
int8_t getTxPower(BLEAdvertisedDevice device) { int8_t getTxPower(BLEAdvertisedDevice device) {
std::string strManufacturerData = device.getManufacturerData(); std::string strManufacturerData = device.getManufacturerData();
uint8_t cManufacturerData[100]; uint8_t cManufacturerData[100];
...@@ -120,10 +205,10 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks { ...@@ -120,10 +205,10 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks {
return oBeacon.getSignalPower(); return oBeacon.getSignalPower();
} }
// print iBeacon device info // ***** print iBeacon device info *****
void printIBeacon(BLEAdvertisedDevice device) { void printIBeacon(BLEAdvertisedDevice device) {
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
Serial.printf("time:%d name:%s uuid:%s major:%d minor:%d rssi:%d\r\n", Serial.printf("time:%d name:%s uuid:%s major:%d minor:%d rssi:%d\r",
(tv.tv_sec), (tv.tv_sec),
device.getName().c_str(), device.getName().c_str(),
getProxyUuid(device).toString().c_str(), getProxyUuid(device).toString().c_str(),
...@@ -137,11 +222,28 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks { ...@@ -137,11 +222,28 @@ class IBeaconAdvertised: public BLEAdvertisedDeviceCallbacks {
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
BLEDevice::init(""); BLEDevice::init("");
//init tabRSSI
tabRSSI[0].val = -100;
tabRSSI[1].val = -100;
tabRSSI[0].time = 0;
tabRSSI[1].time = 0;
//init state
STATE = STATE_SCAN;
} }
void loop() { void loop() {
if(STATE == STATE_OPEN_GATE){
gettimeofday(&tv, NULL); // TODO : save epoch time insted
if((tv.tv_sec - tabRSSI[0].time)>4){ //TODO define time variable
STATE = STATE_SCAN;
Serial.printf("time:%d START SCANNING\n", tv.tv_sec);
}
}
BLEScan* scan = BLEDevice::getScan(); BLEScan* scan = BLEDevice::getScan();
scan->setAdvertisedDeviceCallbacks(new IBeaconAdvertised(), true); scan->setAdvertisedDeviceCallbacks(new IBeaconAdvertised(), true);
scan->setActiveScan(true); scan->setActiveScan(true);
scan->start(SCAN_TIME); scan->start(SCAN_TIME);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment