#define NAVETTE_UUID "DEADDEAD-F88F-0042-F88F-010203040506" // same UUID for all vehicles
#define FORCEGATEOPEN 0b0100 // minor high bits = 0b01 => force gate to open
#define CLEARGATECALIBRATION 0b1000 // minor high bits = 0b10 => disable BLE scan and upload software
#define OTASWUPDATE 0b1100 // minor high bits = 0b11 => clear gate calibration
#define OTA_EXIT_AFTER 60*5 // after X sec if the software is not updating, getting out of the mode STATE_OTA
#define SCAN_TIME 1 // scan period in second
#define GO_TO_SCAN_STATE_DELAY 3 // if no trame was received during X sec, go to STATE_SCAN
#define DELAY_REJECT_TRAM 3 // if the last tram was received more than X seconds ago, the average RSSI is not computed and returns an average RSSI of -100
#define PULSE_DURATION 500 // pulse to open gate. duration in ms
#define DELAY_BETWEEN_PULSE 5 // to keep the gate open emit a pulse every X seconds
#define RSSI_THRESHOLD_OPEN_GATE -95 // if the average RSSI is above this threshold the gate can be open
#define SERIAL_BAUDRATE 115200
// ***** definitions *****
// Wifi credentials
constchar*ssid="Gate";
constchar*password="neOCampus";
typedefenum{
STATE_SCAN=0,// 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.
STATE_OPEN_GATE,// 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
STATE_OTA// STATE_OTA : disable BLE and start uploading software with Wifi
}MACHINE_STATE;
MACHINE_STATESTATE;// state of the system : can be either STATE_SCAN or STATE_OPEN_GATE or STATE_OTA
time_tt;// time is seconds
time_ttimerOTA;// time OTA software update started
time_ttPulseGate;
structRSSI{// table contening the RSSI and time of the last 2 trams. Used to compute the average RSSI.
intval=-100;
time_ttime=0;
}tabRSSI[3];
boolBLEScanDeactivated=false;
// ***** set up Serial Port *****
voidsetupSerial(){
#ifdef SERIAL_BAUDRATE
delay(3000);// time for USB serial link to come up anew
Serial.begin(SERIAL_BAUDRATE);// Start serial for output
Serial.setDebugOutput(true);
#endif
}
// ***** set up WiFi *****
voidsetUpWifi(){
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid,password);
}
// ***** set up On The Air software upload *****
voidsetupOTA(){
// Port defaults to 3232
ArduinoOTA.setPort(3232);
// Hostname
ArduinoOTA.setHostname("gate01");
// Authentication
ArduinoOTA.setPassword("neOCampus");
ArduinoOTA.onStart([](){
Stringtype;
if(ArduinoOTA.getCommand()==U_FLASH)
type="sketch";
else// U_SPIFFS
type="filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
#define NAVETTE_UUID "DEADDEAD-F88F-0042-F88F-010203040506" // same UUID for all vehicles
#define FORCEGATEOPEN 0b0100 // minor high bits = 0b01 => force gate to open
#define CLEARGATECALIBRATION 0b1000 // minor high bits = 0b10 => disable BLE scan and upload software
#define OTASWUPDATE 0b1100 // minor high bits = 0b11 => clear gate calibration
#define OTA_EXIT_AFTER 60*5 // after X sec if the software is not updating, getting out of the mode STATE_OTA
#define SCAN_TIME 1 // scan period in second
#define GO_TO_SCAN_STATE_DELAY 4 // if no frame was received during X sec, go to STATE_SCAN
#define DELAY_REJECT_FRAME 3 // if the last frame was received more than X seconds ago, the average RSSI is not computed and returns an average RSSI of -100
#define PULSE_DURATION 500 // pulse to open gate. duration in ms
#define DELAY_BETWEEN_PULSE 5 // to keep the gate open emit a pulse every X seconds
#define RSSI_THRESHOLD_OPEN_GATE -95 // if the average RSSI is above this threshold the gate can be open
#define SERIAL_BAUDRATE 115200
// Wifi credentials
constchar*ssid="Gate";
constchar*password="neOCampus";
//State enumeration
typedefenum{
STATE_SCAN=0,// STATE_SCAN : scanning iBeacon frame with UUID = NAVETTE_UUID. Opening door when the RSSI is high enough. Go to STATE_OPEN_GATE after opening the door.
STATE_OPEN_GATE,// STATE_OPEN_GATE : door is open. Keep the door open while receiving frame. Go to STATE_SCAN when not receiving frame after GO_TO_SCAN_STATE_DELAY
STATE_OTA// STATE_OTA : disable BLE and start uploading software with Wifi
}MACHINE_STATE;
MACHINE_STATESTATE;// state of the system : can be either STATE_SCAN or STATE_OPEN_GATE or STATE_OTA
//Time related definition
time_tt;// time is seconds
time_ttimerOTA;// time OTA software update started
time_ttPulseGate=0;// time last Pulse to open gate
structInfoBeacon{// structure contening received beacon RSSI and time
intrssi=-100;
time_ttime=0;
}tabRecBeacon[3];// info of the last 3 received frames. Used to compute the average RSSI.
boolBLEScanActivated=false;
/*****************************/
/********* FONCTIONS *********/
/*****************************/
// ***** set up Serial Port *****
voidsetupSerial(){
#ifdef SERIAL_BAUDRATE
delay(3000);// time for USB serial link to come up anew
Serial.begin(SERIAL_BAUDRATE);// Start serial for output
Serial.setDebugOutput(true);
#endif
}
// ***** set up WiFi AP*****
voidsetUpWifiAP(){
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid,password);
}
// ***** set up On The Air software upload *****
voidsetupOTA(){
// Port defaults to 3232
ArduinoOTA.setPort(3232);
// Hostname
ArduinoOTA.setHostname(ssid);
// Authentication
ArduinoOTA.setPassword(password);
ArduinoOTA.onStart([](){
Stringtype;
if(ArduinoOTA.getCommand()==U_FLASH)
type="sketch";
else// U_SPIFFS
type="filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()