2019-02-18 05:45:02 +00:00
|
|
|
#include "firmware.h"
|
2018-02-02 06:46:44 +00:00
|
|
|
|
2019-02-18 05:45:02 +00:00
|
|
|
const char *LOCKOUT_FIRMWARE_VERSION = "MRWIZARD 0010 MRWIZARD";
|
|
|
|
const char *LOCKOUT_WIFI_SSID PROGMEM = "Protospace";
|
|
|
|
const char *LOCKOUT_WIFI_PASS PROGMEM = "yycmakers";
|
2018-11-18 02:17:00 +00:00
|
|
|
const String SOCKET_URL = String("http://tools-socket.protospace.ca/api/lockout/");
|
|
|
|
const String CARD_URL = String("http://tools-auth.protospace.ca/cards/");
|
2019-01-19 09:43:08 +00:00
|
|
|
const String INFOLOG_URL = String("http://tools-auth.protospace.ca/infolog/");
|
2019-01-27 07:40:14 +00:00
|
|
|
const String UPDATE_URL = String("http://tools-auth.protospace.ca/update/");
|
2018-11-18 02:17:00 +00:00
|
|
|
|
|
|
|
Ticker ticker;
|
2018-11-10 21:56:50 +00:00
|
|
|
|
2019-02-18 05:45:02 +00:00
|
|
|
char wifiMACAddr[20] = "";
|
2018-11-10 21:56:50 +00:00
|
|
|
char cardBuffer[CARD_BUFFER_LENGTH];
|
2019-01-21 03:04:43 +00:00
|
|
|
struct logData eventLog[LOG_SIZE];
|
2019-01-19 09:43:08 +00:00
|
|
|
uint16_t logPosition = 0;
|
|
|
|
|
2019-02-18 05:45:02 +00:00
|
|
|
enum wifiStates wifiState = WIFI_DISCONNECTED;
|
|
|
|
enum LEDStates LEDState = LED_OFF;
|
|
|
|
enum lockStates lockState = LOCK_OFF;
|
|
|
|
enum commStates commState = COMM_INIT;
|
2019-01-21 03:04:43 +00:00
|
|
|
|
2018-02-06 02:31:54 +00:00
|
|
|
void setup()
|
|
|
|
{
|
2018-11-10 21:56:50 +00:00
|
|
|
Serial.begin(9600);
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Serial started.");
|
|
|
|
|
|
|
|
struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
|
|
|
|
struct timezone tz = { .tz_minuteswest = 0, .tz_dsttime = 0 };
|
|
|
|
settimeofday(&tv, &tz);
|
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Set system time to 0.");
|
2019-01-27 07:40:14 +00:00
|
|
|
|
2019-02-18 05:45:02 +00:00
|
|
|
logEvent(LOG_BOOT_UP, &LOCKOUT_FIRMWARE_VERSION[9], 4);
|
2019-01-27 07:40:14 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.print("[INFO] Booting firmware version: ");
|
2019-02-18 05:45:02 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println(LOCKOUT_FIRMWARE_VERSION);
|
2018-02-06 02:31:54 +00:00
|
|
|
|
|
|
|
pinMode(RELAY_PIN, OUTPUT);
|
2018-09-17 23:55:20 +00:00
|
|
|
pinMode(GREEN_BUTTON_PIN, INPUT_PULLUP);
|
|
|
|
pinMode(RED_BUTTON_PIN, INPUT_PULLUP);
|
|
|
|
pinMode(GREEN_LED_PIN, OUTPUT);
|
|
|
|
pinMode(RED_LED_PIN, OUTPUT);
|
2018-11-14 02:31:53 +00:00
|
|
|
|
|
|
|
EEPROM.begin(EEPROM_SIZE);
|
2018-11-18 02:17:00 +00:00
|
|
|
|
2018-11-27 00:05:20 +00:00
|
|
|
byte ar[6];
|
|
|
|
WiFi.macAddress(ar);
|
|
|
|
sprintf(wifiMACAddr, "%02X%02X%02X%02X%02X%02X", ar[0], ar[1], ar[2], ar[3], ar[4], ar[5]);
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.print("[INFO] Wifi MAC Address: ");
|
|
|
|
if (SERIAL_LOGGING) Serial.println(wifiMACAddr);
|
2018-11-27 00:05:20 +00:00
|
|
|
|
2018-11-18 02:17:00 +00:00
|
|
|
ticker.attach_ms(DELAY_TIME, tickerLoop);
|
2019-01-19 09:43:08 +00:00
|
|
|
|
|
|
|
logEvent(LOG_INIT_COMPLETE);
|
2018-02-02 06:46:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 02:17:00 +00:00
|
|
|
// The stuff in this loop must not be blocked by network delay
|
|
|
|
void tickerLoop()
|
2018-02-06 02:31:54 +00:00
|
|
|
{
|
|
|
|
processLockState();
|
2018-11-19 04:33:24 +00:00
|
|
|
processLEDState();
|
2018-02-02 06:46:44 +00:00
|
|
|
|
2018-11-10 21:56:50 +00:00
|
|
|
if (Serial.available() >= CARD_BUFFER_LENGTH) {
|
|
|
|
uint8_t bufPos = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
char readChar = Serial.read();
|
|
|
|
|
|
|
|
if (readChar == -1) {
|
|
|
|
break;
|
2018-12-09 13:01:01 +00:00
|
|
|
} else if (readChar == CARD_HEAD_BYTE) {
|
2018-11-10 21:56:50 +00:00
|
|
|
bufPos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bufPos >= CARD_BUFFER_LENGTH) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cardBuffer[bufPos++] = readChar;
|
|
|
|
|
2018-12-09 13:01:01 +00:00
|
|
|
if (readChar == CARD_TAIL_BYTE && bufPos == CARD_BUFFER_LENGTH) {
|
2018-11-19 04:33:24 +00:00
|
|
|
if (lockState == LOCK_OFF && LEDState == LED_OFF) checkCard();
|
2018-11-10 21:56:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-18 02:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
processWifiState();
|
|
|
|
processCommState();
|
2018-11-10 21:56:50 +00:00
|
|
|
|
2018-02-06 02:31:54 +00:00
|
|
|
delay(DELAY_TIME);
|
|
|
|
}
|