#define NAVETTE_UUID "DEADDEAD-F88F-0042-F88F-010203040506" // same UUID for all vehicles
#define NAVETTE_UUID "DEADDEAD-F88F-0042-F88F-010203040506" // same UUID for all vehicles
#define FORCEGATEOPEN 0b01 // minor high bits = 0b01 => force gate to open
#define FORCEGATEOPEN 0b01 // minor high bits = 0b01 => force gate to open
#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 STATE_SCAN 1
#define STATE_SCAN 1// STATE_SCAN : scanning iBeacon tram with UUID = NAVETTE_UUID. Opening door when the RSSI is high enough. Go to STATE_OPEN_GATE after opening the door.
#define STATE_OPEN_GATE 2
#define STATE_OPEN_GATE 2 // STATE_OPEN_GATE : door is open. Keep the door open while receiving tram. Go to STATE_SCAN when not receiving tram after GO_TO_SCAN_STATE_DELAY
#define RSSI_THRESHOLD_OPEN_GATE -95
#define SCAN_TIME 2 // scan period in second
#define GO_TO_SCAN_STATE_DELAY 4 // if no trame was received during 4 sec, go to STATE_SCAN
#define DELAY_REJECT_TRAM 3 // if the last tram was received more than 3 seconds ago, the average RSSI is not computed and returns an average RSSI of -100
#define RSSI_THRESHOLD_OPEN_GATE -95 // if the average RSSI is above this threshold the gate can be open
// ***** definitions *****
// ***** definitions *****
intSTATE;
intSTATE;// state of the system : can be either STATE_SCAN or STATE_OPEN_GATE
time_tt;// time is seconds
structRSSI{// table contening the RSSI and time of the last 2 trams. Used to compute the average RSSI.
if(avRSSI>RSSI_THRESHOLD_OPEN_GATE){// if the received signal power is above the threshold RSSI_THRESHOLD_OPEN_GATE
printIBeacon(device);// DEBUG
printIBeacon(device);// DEBUG
Serial.printf(" - force open tram\n");// DEBUG
Serial.printf(" - RSSI average OK:%d\n",avRSSI);// DEBUG
openGate();
openGate();// open the gate
updateRSSItab(getRSSI(device));
updateRSSItab(newRSSI);// save received tram RSSI in the table
return;
STATE=STATE_OPEN_GATE;// change state and go to state STATE_OPEN_GATE
}
}else{
printIBeacon(device);// DEBUG
//check gate state (STATE_SCAN or STATE_OPEN_GATE)
Serial.printf(" - RSSI average too low:%d\n",avRSSI);// DEBUG
switch(STATE){
updateRSSItab(newRSSI);
STATE=STATE_SCAN;
caseSTATE_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;
caseSTATE_SCAN:
default:
RSSInewRSSI=getRSSI(device);
intavRSSI=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;
}
}
break;
}
}
}
private:
private:
// ***** compute average RSSI *****
intaverageRSSI(RSSInewRSSI){
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