2018-02-02 06:46:44 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <ArduinoJson.h>
|
2019-01-19 09:43:08 +00:00
|
|
|
#include <base64.h>
|
2018-02-02 06:46:44 +00:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <ESP8266HTTPClient.h>
|
2018-11-14 02:31:53 +00:00
|
|
|
#include <EEPROM.h>
|
2018-11-18 02:17:00 +00:00
|
|
|
#include <Ticker.h>
|
2019-01-19 09:43:08 +00:00
|
|
|
#include <time.h>
|
2018-02-02 06:46:44 +00:00
|
|
|
|
2018-11-28 08:00:07 +00:00
|
|
|
const char *WIFI_SSID PROGMEM = "Protospace";
|
|
|
|
const char *WIFI_PASS PROGMEM = "yycmakers";
|
|
|
|
char wifiMACAddr[20] = "";
|
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/");
|
2018-11-18 02:17:00 +00:00
|
|
|
|
|
|
|
Ticker ticker;
|
2018-11-10 21:56:50 +00:00
|
|
|
|
|
|
|
#define CARD_BUFFER_LENGTH 14
|
|
|
|
char cardBuffer[CARD_BUFFER_LENGTH];
|
|
|
|
|
|
|
|
#define CARD_DATA_LENGTH 10
|
|
|
|
#define CARD_CHECK_LENGTH 2
|
2018-12-09 13:01:01 +00:00
|
|
|
#define CARD_HEAD_BYTE 0x2
|
|
|
|
#define CARD_TAIL_BYTE 0x3
|
2018-11-10 21:56:50 +00:00
|
|
|
typedef struct __attribute__((packed)) cardData {
|
|
|
|
char head;
|
|
|
|
char data[CARD_DATA_LENGTH];
|
|
|
|
char checksum[CARD_CHECK_LENGTH];
|
|
|
|
char tail;
|
|
|
|
} cardData_t;
|
|
|
|
|
2018-05-17 03:03:44 +00:00
|
|
|
#define RELAY_PIN D1
|
2018-09-17 23:55:20 +00:00
|
|
|
#define GREEN_BUTTON_PIN D3
|
|
|
|
#define RED_BUTTON_PIN D4
|
|
|
|
#define GREEN_LED_PIN D6
|
|
|
|
#define RED_LED_PIN D7
|
2018-02-02 06:46:44 +00:00
|
|
|
|
2018-09-12 02:45:20 +00:00
|
|
|
#define RELAY_CLOSED HIGH
|
2018-05-17 03:03:44 +00:00
|
|
|
#define RELAY_OPEN !RELAY_CLOSED
|
|
|
|
#define BUTTON_CLOSED LOW
|
|
|
|
#define BUTTON_OPEN !BUTTON_CLOSED
|
2018-11-19 04:33:24 +00:00
|
|
|
#define LED_PIN_ON HIGH
|
|
|
|
#define LED_PIN_OFF !LED_PIN_ON
|
2018-02-02 06:46:44 +00:00
|
|
|
|
2018-11-14 02:31:53 +00:00
|
|
|
#define DELAY_TIME 10
|
2019-01-19 09:43:08 +00:00
|
|
|
// times below are multiplied by DELAY_TIME, in ms
|
2018-12-08 04:39:18 +00:00
|
|
|
#define LOCK_ARMED_TIMEOUT 1000
|
2018-11-14 02:31:53 +00:00
|
|
|
#define COMM_LOCK_IDLE_TIME 50
|
2018-11-18 02:17:00 +00:00
|
|
|
#define COMM_CARD_IDLE_TIME 1000
|
2019-01-19 09:43:08 +00:00
|
|
|
#define COMM_INFO_IDLE_TIME 3000
|
2018-12-02 00:08:12 +00:00
|
|
|
#define LED_ARMED_BLINK_TIME 50
|
|
|
|
#define LED_ERROR_BLINK_TIME 50
|
2018-02-02 06:46:44 +00:00
|
|
|
|
2018-11-14 02:31:53 +00:00
|
|
|
#define EEPROM_SIZE 4095
|
2018-12-18 22:48:19 +00:00
|
|
|
#define EEPROM_END_MARKER '\0'
|
2018-02-02 06:46:44 +00:00
|
|
|
|
2018-02-06 02:31:54 +00:00
|
|
|
enum wifiStates
|
|
|
|
{
|
|
|
|
WIFI_DISCONNECTED,
|
|
|
|
WIFI_CONNECTING,
|
|
|
|
WIFI_CONNECTED,
|
|
|
|
} wifiState = WIFI_DISCONNECTED;
|
|
|
|
|
2018-11-19 04:33:24 +00:00
|
|
|
enum LEDStates
|
|
|
|
{
|
|
|
|
LED_OFF,
|
|
|
|
LED_ARMED,
|
|
|
|
LED_ON,
|
2018-12-02 00:08:12 +00:00
|
|
|
LED_ERROR,
|
2018-11-19 04:33:24 +00:00
|
|
|
} LEDState = LED_OFF;
|
|
|
|
|
2018-02-06 02:31:54 +00:00
|
|
|
enum lockStates
|
|
|
|
{
|
|
|
|
LOCK_OFF,
|
2019-01-19 09:43:08 +00:00
|
|
|
LOCK_PREARM, // prevent arming while buttons held
|
2018-02-06 02:31:54 +00:00
|
|
|
LOCK_ARMED,
|
2018-11-10 21:56:50 +00:00
|
|
|
LOCK_ON_PRESSED, // to wait until button is released
|
2018-02-06 02:31:54 +00:00
|
|
|
LOCK_ON,
|
|
|
|
} lockState = LOCK_OFF;
|
|
|
|
|
|
|
|
enum commStates
|
|
|
|
{
|
|
|
|
COMM_INIT,
|
|
|
|
COMM_IDLE,
|
2018-11-14 02:31:53 +00:00
|
|
|
COMM_LOCK,
|
|
|
|
COMM_CARD,
|
2019-01-19 09:43:08 +00:00
|
|
|
COMM_INFO,
|
2018-02-06 02:31:54 +00:00
|
|
|
} commState = COMM_INIT;
|
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
#define SERIAL_LOGGING true
|
|
|
|
|
|
|
|
#define LOG_SIZE 100
|
|
|
|
|
|
|
|
enum eventCodes
|
|
|
|
{
|
|
|
|
LOG_BOOT_UP,
|
|
|
|
LOG_INIT_COMPLETE,
|
|
|
|
LOG_WIFI_CONNECTED,
|
|
|
|
LOG_WIFI_DISCONNECTED,
|
|
|
|
LOG_COMM_LOCK_FAIL,
|
|
|
|
LOG_COMM_CARD_FAIL,
|
|
|
|
LOG_COMM_INFO_FAIL,
|
|
|
|
LOG_LOCK_OFF,
|
|
|
|
LOG_LOCK_ARMED,
|
|
|
|
LOG_LOCK_TIMEOUT,
|
|
|
|
LOG_LOCK_ON,
|
|
|
|
LOG_LOCK_CANCEL,
|
|
|
|
LOG_LOCK_ERROR,
|
|
|
|
LOG_CARD_GOOD_READ,
|
|
|
|
LOG_CARD_ACCEPTED,
|
|
|
|
LOG_CARD_DENIED,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct __attribute__((packed)) logData {
|
|
|
|
uint32_t unixTime;
|
|
|
|
uint8_t eventCode;
|
|
|
|
char data[CARD_DATA_LENGTH];
|
|
|
|
} logData_t;
|
|
|
|
|
|
|
|
logData_t eventLog[LOG_SIZE];
|
|
|
|
uint16_t logPosition = 0;
|
|
|
|
|
|
|
|
void logEvent(uint8_t eventCode, const char *data = nullptr, size_t num = 0)
|
|
|
|
{
|
|
|
|
logData_t event;
|
|
|
|
|
|
|
|
noInterrupts();
|
|
|
|
|
|
|
|
event.unixTime = time(nullptr);
|
|
|
|
event.eventCode = eventCode;
|
|
|
|
for (uint8_t i = 0; i < CARD_DATA_LENGTH; i++) {
|
|
|
|
if (i >= num) break;
|
|
|
|
event.data[i] = data[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (logPosition < LOG_SIZE) {
|
|
|
|
eventLog[logPosition++] = event;
|
|
|
|
}
|
|
|
|
|
|
|
|
interrupts();
|
|
|
|
}
|
|
|
|
|
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.");
|
|
|
|
logEvent(LOG_BOOT_UP);
|
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);
|
|
|
|
}
|
2018-02-02 06:46:44 +00:00
|
|
|
|
2018-11-14 02:31:53 +00:00
|
|
|
int8_t charToNum(char input)
|
|
|
|
{
|
2018-11-10 21:56:50 +00:00
|
|
|
return String("0123456789ABCDEF").indexOf(input);
|
|
|
|
}
|
|
|
|
|
2018-11-14 02:31:53 +00:00
|
|
|
bool checksum(cardData_t *cardData)
|
|
|
|
{
|
2018-11-10 21:56:50 +00:00
|
|
|
// checksum is each hex data byte xord'd together.
|
|
|
|
// each char is a hex nibble, so we have to work in pairs.
|
|
|
|
|
|
|
|
int8_t even = 0, odd = 0;
|
|
|
|
|
2018-11-28 08:00:07 +00:00
|
|
|
for (int8_t i = 0; i < CARD_DATA_LENGTH; i++) {
|
2018-11-10 21:56:50 +00:00
|
|
|
int8_t num = charToNum(cardData->data[i]);
|
|
|
|
|
|
|
|
if (num == -1) return false;
|
|
|
|
if (i % 2 == 0) even ^= num;
|
|
|
|
if (i % 2 == 1) odd ^= num;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t checksum_even = charToNum(cardData->checksum[0]);
|
|
|
|
int8_t checksum_odd = charToNum(cardData->checksum[1]);
|
|
|
|
|
|
|
|
if (even == checksum_even && odd == checksum_odd) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 02:31:53 +00:00
|
|
|
void checkCard()
|
|
|
|
{
|
2018-11-10 21:56:50 +00:00
|
|
|
cardData_t *cardData = (cardData_t *) cardBuffer;
|
|
|
|
|
2018-12-09 13:01:01 +00:00
|
|
|
if (cardData->head == CARD_HEAD_BYTE &&
|
|
|
|
cardData->tail == CARD_TAIL_BYTE &&
|
|
|
|
checksum(cardData)) {
|
|
|
|
|
2018-11-10 21:56:50 +00:00
|
|
|
String cardStr = String();
|
2018-11-14 02:31:53 +00:00
|
|
|
String authorizedCards = String();
|
2018-11-10 21:56:50 +00:00
|
|
|
|
2018-11-28 08:00:07 +00:00
|
|
|
for (uint8_t i = 0; i < CARD_DATA_LENGTH; i++) {
|
2018-11-10 21:56:50 +00:00
|
|
|
cardStr += cardData->data[i];
|
2018-11-14 02:31:53 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 08:00:07 +00:00
|
|
|
for (uint16_t i = 0; i < EEPROM_SIZE; i++) {
|
2018-11-14 02:31:53 +00:00
|
|
|
char tmp = EEPROM.read(i);
|
|
|
|
authorizedCards += tmp;
|
2018-12-09 13:01:01 +00:00
|
|
|
if (tmp == EEPROM_END_MARKER) break;
|
2018-11-14 02:31:53 +00:00
|
|
|
}
|
2018-11-10 21:56:50 +00:00
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Good scan from card: " + cardStr);
|
|
|
|
logEvent(LOG_CARD_GOOD_READ, cardStr.c_str(), cardStr.length());
|
2018-11-10 21:56:50 +00:00
|
|
|
|
|
|
|
if (authorizedCards.indexOf(cardStr) >= 0) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Card is authorized on machine.");
|
|
|
|
logEvent(LOG_CARD_ACCEPTED, cardStr.c_str(), cardStr.length());
|
2018-11-10 21:56:50 +00:00
|
|
|
if (lockState == LOCK_OFF) {
|
|
|
|
lockState = LOCK_PREARM;
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Card not authorized on machine.");
|
|
|
|
logEvent(LOG_CARD_DENIED, cardStr.c_str(), cardStr.length());
|
2018-12-02 00:08:12 +00:00
|
|
|
LEDState = LED_ERROR;
|
2018-11-10 21:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 02:31:54 +00:00
|
|
|
void processWifiState()
|
|
|
|
{
|
|
|
|
switch(wifiState) {
|
|
|
|
case WIFI_DISCONNECTED:
|
|
|
|
commState = COMM_INIT;
|
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Wifi attempting to connect...");
|
|
|
|
|
2018-02-06 02:31:54 +00:00
|
|
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
|
|
|
|
|
|
|
wifiState = WIFI_CONNECTING;
|
|
|
|
break;
|
|
|
|
case WIFI_CONNECTING:
|
|
|
|
commState = COMM_INIT;
|
|
|
|
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Wifi is connected.");
|
|
|
|
logEvent(LOG_WIFI_CONNECTED);
|
2018-02-06 02:31:54 +00:00
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.print("[INFO] Wifi IP Address: ");
|
|
|
|
if (SERIAL_LOGGING) Serial.println(WiFi.localIP());
|
2018-02-06 02:31:54 +00:00
|
|
|
|
|
|
|
wifiState = WIFI_CONNECTED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WIFI_CONNECTED:
|
|
|
|
if (WiFi.status() != WL_CONNECTED) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Wifi disconnected.");
|
|
|
|
logEvent(LOG_WIFI_DISCONNECTED);
|
2018-02-06 02:31:54 +00:00
|
|
|
wifiState = WIFI_DISCONNECTED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[ERROR] Invalid wifi state.");
|
2018-02-06 02:31:54 +00:00
|
|
|
wifiState = WIFI_DISCONNECTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 06:46:44 +00:00
|
|
|
|
2018-11-08 02:09:34 +00:00
|
|
|
bool greenButton() { return digitalRead(GREEN_BUTTON_PIN) == BUTTON_CLOSED; }
|
|
|
|
bool redButton() { return digitalRead(RED_BUTTON_PIN) == BUTTON_CLOSED; }
|
|
|
|
|
2018-11-08 02:14:59 +00:00
|
|
|
void relayOn() { digitalWrite(RELAY_PIN, RELAY_CLOSED); }
|
|
|
|
void relayOff() { digitalWrite(RELAY_PIN, RELAY_OPEN); }
|
|
|
|
|
2018-02-06 02:31:54 +00:00
|
|
|
void processLockState()
|
|
|
|
{
|
2018-12-08 04:39:18 +00:00
|
|
|
static uint16_t lockArmedTimeoutCount;
|
|
|
|
|
|
|
|
if (lockState != LOCK_ARMED) lockArmedTimeoutCount = 0;
|
|
|
|
|
2018-02-06 02:31:54 +00:00
|
|
|
switch (lockState) {
|
|
|
|
case LOCK_OFF:
|
2018-12-02 00:08:12 +00:00
|
|
|
if (LEDState != LED_ERROR) LEDState = LED_OFF;
|
2018-02-06 02:31:54 +00:00
|
|
|
|
2018-11-08 02:14:59 +00:00
|
|
|
relayOff();
|
2018-02-06 02:31:54 +00:00
|
|
|
break;
|
2018-11-08 02:09:34 +00:00
|
|
|
case LOCK_PREARM:
|
|
|
|
if (!greenButton() && !redButton()) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Arming interlock.");
|
|
|
|
logEvent(LOG_LOCK_ARMED);
|
2018-11-08 02:09:34 +00:00
|
|
|
lockState = LOCK_ARMED;
|
|
|
|
} else {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[ERROR] Buttons held, aborting.");
|
|
|
|
logEvent(LOG_LOCK_ERROR);
|
2018-11-08 02:09:34 +00:00
|
|
|
lockState = LOCK_OFF;
|
2018-12-02 00:08:12 +00:00
|
|
|
LEDState = LED_ERROR;
|
2018-11-08 02:09:34 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-02-06 02:31:54 +00:00
|
|
|
case LOCK_ARMED:
|
2018-12-02 00:08:12 +00:00
|
|
|
if (LEDState != LED_ERROR) LEDState = LED_ARMED;
|
2018-02-06 02:31:54 +00:00
|
|
|
|
2018-11-08 02:14:59 +00:00
|
|
|
relayOff();
|
2018-12-08 04:39:18 +00:00
|
|
|
lockArmedTimeoutCount++;
|
2018-02-06 02:31:54 +00:00
|
|
|
|
2018-11-08 02:09:34 +00:00
|
|
|
if (redButton()) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Unarming interlock.");
|
|
|
|
logEvent(LOG_LOCK_CANCEL);
|
2018-11-10 21:56:50 +00:00
|
|
|
lockState = LOCK_OFF;
|
2018-11-08 02:09:34 +00:00
|
|
|
} else if (greenButton()) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] On button pressed.");
|
2018-02-06 02:31:54 +00:00
|
|
|
lockState = LOCK_ON_PRESSED;
|
|
|
|
}
|
2018-12-08 04:39:18 +00:00
|
|
|
|
|
|
|
if (lockArmedTimeoutCount > LOCK_ARMED_TIMEOUT) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Arming timed out, disarming.");
|
|
|
|
logEvent(LOG_LOCK_TIMEOUT);
|
2018-12-08 04:39:18 +00:00
|
|
|
lockState = LOCK_OFF;
|
2018-12-08 04:53:30 +00:00
|
|
|
LEDState = LED_ERROR;
|
2018-12-08 04:39:18 +00:00
|
|
|
}
|
2018-02-06 02:31:54 +00:00
|
|
|
break;
|
|
|
|
case LOCK_ON_PRESSED:
|
2018-11-08 02:09:34 +00:00
|
|
|
if (redButton()) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[ERROR] Off button pressed, aborting.");
|
|
|
|
logEvent(LOG_LOCK_ERROR);
|
2018-11-10 21:56:50 +00:00
|
|
|
lockState = LOCK_OFF;
|
2018-11-08 02:09:34 +00:00
|
|
|
} else if (!greenButton()) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Turning machine on.");
|
|
|
|
logEvent(LOG_LOCK_ON);
|
2018-02-06 02:31:54 +00:00
|
|
|
lockState = LOCK_ON;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LOCK_ON:
|
2018-12-02 00:08:12 +00:00
|
|
|
if (LEDState != LED_ERROR) LEDState = LED_ON;
|
2018-02-06 02:31:54 +00:00
|
|
|
|
2018-11-08 02:14:59 +00:00
|
|
|
relayOn();
|
2018-02-06 02:31:54 +00:00
|
|
|
|
2018-11-08 02:09:34 +00:00
|
|
|
if (redButton()) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Off button pressed.");
|
|
|
|
logEvent(LOG_LOCK_OFF);
|
2018-02-06 02:31:54 +00:00
|
|
|
lockState = LOCK_OFF;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[ERROR] Invalid lock state.");
|
2018-02-06 02:31:54 +00:00
|
|
|
lockState = LOCK_OFF;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-02 06:46:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 04:33:24 +00:00
|
|
|
void greenLEDOn() { digitalWrite(GREEN_LED_PIN, LED_PIN_ON); }
|
|
|
|
void greenLEDOff() { digitalWrite(GREEN_LED_PIN, LED_PIN_OFF); }
|
|
|
|
void redLEDOn() { digitalWrite(RED_LED_PIN, LED_PIN_ON); }
|
|
|
|
void redLEDOff() { digitalWrite(RED_LED_PIN, LED_PIN_OFF); }
|
|
|
|
|
|
|
|
void processLEDState()
|
|
|
|
{
|
2018-12-02 00:08:12 +00:00
|
|
|
static uint16_t LEDArmedBlinkCount, LEDErrorBlinkCount;
|
|
|
|
|
|
|
|
if (LEDState != LED_ARMED) LEDArmedBlinkCount = 0;
|
|
|
|
if (LEDState != LED_ERROR) LEDErrorBlinkCount = 0;
|
2018-11-19 04:33:24 +00:00
|
|
|
|
|
|
|
switch (LEDState) {
|
|
|
|
case LED_OFF:
|
|
|
|
greenLEDOff();
|
2018-12-02 00:08:12 +00:00
|
|
|
redLEDOn();
|
2018-11-19 04:33:24 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
case LED_ARMED:
|
2018-12-02 00:08:12 +00:00
|
|
|
LEDArmedBlinkCount++;
|
2018-11-19 04:33:24 +00:00
|
|
|
|
2018-12-02 00:08:12 +00:00
|
|
|
if (LEDArmedBlinkCount < LED_ARMED_BLINK_TIME) {
|
|
|
|
greenLEDOn();
|
|
|
|
redLEDOn();
|
|
|
|
} else if (LEDArmedBlinkCount < LED_ARMED_BLINK_TIME * 2) {
|
|
|
|
greenLEDOff();
|
|
|
|
redLEDOn();
|
|
|
|
} else {
|
|
|
|
LEDArmedBlinkCount = 0;
|
|
|
|
}
|
2018-11-19 04:33:24 +00:00
|
|
|
break;
|
|
|
|
case LED_ON:
|
2018-12-02 00:08:12 +00:00
|
|
|
greenLEDOn();
|
2018-11-19 04:33:24 +00:00
|
|
|
redLEDOn();
|
|
|
|
|
|
|
|
break;
|
2018-12-02 00:08:12 +00:00
|
|
|
case LED_ERROR:
|
|
|
|
LEDErrorBlinkCount++;
|
2018-11-19 04:33:24 +00:00
|
|
|
|
2018-12-02 00:08:12 +00:00
|
|
|
if (LEDErrorBlinkCount < LED_ERROR_BLINK_TIME) {
|
2018-11-19 04:33:24 +00:00
|
|
|
greenLEDOff();
|
|
|
|
redLEDOff();
|
2018-12-02 00:08:12 +00:00
|
|
|
} else if (LEDErrorBlinkCount < LED_ERROR_BLINK_TIME * 2) {
|
|
|
|
greenLEDOff();
|
|
|
|
redLEDOn();
|
2018-11-19 04:33:24 +00:00
|
|
|
} else {
|
|
|
|
LEDState = LED_OFF;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[ERROR] Invalid LED state.");
|
2018-11-19 04:33:24 +00:00
|
|
|
LEDState = LED_OFF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 00:05:20 +00:00
|
|
|
// JSON functions to prevent memory leaking see:
|
|
|
|
// https://arduinojson.org/v5/faq/i-found-a-memory-leak-in-the-library/
|
2018-11-14 02:31:53 +00:00
|
|
|
String serializeLockJson(uint8_t lockState)
|
|
|
|
{
|
2018-02-06 06:59:47 +00:00
|
|
|
// Generated with: https://arduinojson.org/assistant/
|
|
|
|
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50;
|
2018-11-28 08:00:07 +00:00
|
|
|
StaticJsonBuffer<bufferSize> jsonBuffer;
|
2018-02-06 06:59:47 +00:00
|
|
|
|
2018-05-17 03:03:44 +00:00
|
|
|
JsonObject& root = jsonBuffer.createObject();
|
2018-11-10 21:56:50 +00:00
|
|
|
root["lockState"] = (uint8_t) lockState;
|
2018-02-06 06:59:47 +00:00
|
|
|
String postData = String();
|
2018-05-17 03:03:44 +00:00
|
|
|
root.printTo(postData);
|
2018-02-06 06:59:47 +00:00
|
|
|
|
|
|
|
return postData;
|
|
|
|
}
|
|
|
|
|
2018-11-14 02:31:53 +00:00
|
|
|
String deserializeLockJson(String input)
|
|
|
|
{
|
2018-05-17 03:03:44 +00:00
|
|
|
// Generated with: https://arduinojson.org/assistant/
|
|
|
|
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50;
|
2018-11-28 08:00:07 +00:00
|
|
|
StaticJsonBuffer<bufferSize> jsonBuffer;
|
2018-05-17 03:03:44 +00:00
|
|
|
JsonObject& root = jsonBuffer.parseObject(input);
|
|
|
|
|
|
|
|
String action = root["action"];
|
|
|
|
|
|
|
|
return action;
|
|
|
|
}
|
2018-02-06 06:59:47 +00:00
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
String serializeLog() {
|
|
|
|
size_t logLengthBytes = logPosition * sizeof(logData_t);
|
|
|
|
return base64::encode((uint8_t *) eventLog, logLengthBytes, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t deserializeInfoJson(String input)
|
|
|
|
{
|
|
|
|
// Generated with: https://arduinojson.org/assistant/
|
|
|
|
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50;
|
|
|
|
StaticJsonBuffer<bufferSize> jsonBuffer;
|
|
|
|
JsonObject& root = jsonBuffer.parseObject(input);
|
|
|
|
|
|
|
|
uint32_t unixTime = root["unixTime"];
|
|
|
|
|
|
|
|
return unixTime;
|
|
|
|
}
|
|
|
|
|
2018-11-27 00:05:20 +00:00
|
|
|
void postState()
|
|
|
|
{
|
|
|
|
HTTPClient lockHTTP;
|
|
|
|
|
|
|
|
//lockHTTP.begin("https://url", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
|
|
|
|
lockHTTP.begin(SOCKET_URL + wifiMACAddr);
|
|
|
|
lockHTTP.addHeader("Content-Type", "application/json");
|
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Lock state HTTP begin.");
|
2018-11-27 00:05:20 +00:00
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.print("[INFO] HTTP POST: ");
|
2018-11-27 00:05:20 +00:00
|
|
|
String postData = serializeLockJson(lockState);
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println(postData);
|
2018-11-27 00:05:20 +00:00
|
|
|
int16_t lockHTTPCode = lockHTTP.POST(postData);
|
2019-01-19 09:43:08 +00:00
|
|
|
String lockHTTPCodeStr = String(lockHTTPCode);
|
2018-11-27 00:05:20 +00:00
|
|
|
|
|
|
|
if (lockHTTPCode > 0) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.printf("[INFO] POST success, code: %d\n", lockHTTPCode);
|
2018-11-27 00:05:20 +00:00
|
|
|
|
|
|
|
if (lockHTTPCode == HTTP_CODE_OK) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.print("[INFO] Resource found, parsing response: ");
|
2018-11-27 00:05:20 +00:00
|
|
|
String lockPayload = lockHTTP.getString();
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println(lockPayload);
|
2018-11-27 00:05:20 +00:00
|
|
|
String action = deserializeLockJson(lockPayload);
|
|
|
|
|
2018-11-30 09:09:17 +00:00
|
|
|
if (action == "arm" && lockState == LOCK_OFF && LEDState == LED_OFF) {
|
2018-11-27 00:05:20 +00:00
|
|
|
lockState = LOCK_PREARM;
|
2019-01-19 09:43:08 +00:00
|
|
|
} else if (action == "disarm" && lockState != LOCK_ON) {
|
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Unarming interlock.");
|
|
|
|
logEvent(LOG_LOCK_CANCEL);
|
2018-11-27 00:05:20 +00:00
|
|
|
lockState = LOCK_OFF;
|
|
|
|
}
|
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] action: " + action);
|
2018-11-27 00:05:20 +00:00
|
|
|
} else {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[ERROR] Resource not found.");
|
|
|
|
logEvent(LOG_COMM_LOCK_FAIL, lockHTTPCodeStr.c_str(), lockHTTPCodeStr.length());
|
2018-11-27 00:05:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", lockHTTP.errorToString(lockHTTPCode).c_str());
|
|
|
|
logEvent(LOG_COMM_LOCK_FAIL, lockHTTPCodeStr.c_str(), lockHTTPCodeStr.length());
|
2018-11-27 00:05:20 +00:00
|
|
|
}
|
2018-11-28 08:00:07 +00:00
|
|
|
|
|
|
|
lockHTTP.end();
|
2018-11-27 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void getCards()
|
|
|
|
{
|
|
|
|
HTTPClient cardHTTP;
|
|
|
|
|
|
|
|
//cardHTTP.begin("https://url", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
|
|
|
|
cardHTTP.begin(CARD_URL + wifiMACAddr + "/");
|
|
|
|
cardHTTP.addHeader("Content-Type", "application/json");
|
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Card state HTTP begin.");
|
2018-11-27 00:05:20 +00:00
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] HTTP GET");
|
2018-11-27 00:05:20 +00:00
|
|
|
int16_t cardHTTPCode = cardHTTP.GET();
|
2019-01-19 09:43:08 +00:00
|
|
|
String cardHTTPCodeStr = String(cardHTTPCode);
|
2018-11-27 00:05:20 +00:00
|
|
|
|
|
|
|
if (cardHTTPCode > 0) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.printf("[INFO] GET success, code: %d\n", cardHTTPCode);
|
2018-11-27 00:05:20 +00:00
|
|
|
|
|
|
|
if (cardHTTPCode == HTTP_CODE_OK) {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.print("[INFO] Resource found, parsing response: ");
|
2018-11-27 00:05:20 +00:00
|
|
|
String cardPayload = cardHTTP.getString();
|
2018-12-09 13:01:01 +00:00
|
|
|
cardPayload += String(EEPROM_END_MARKER);
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println(cardPayload);
|
2018-11-27 00:05:20 +00:00
|
|
|
|
2018-11-30 09:09:17 +00:00
|
|
|
noInterrupts(); // commit() disables interrupts, but we want an atomic EEPROM buffer write
|
2018-11-28 08:00:07 +00:00
|
|
|
for (int i = 0; i < cardPayload.length(); i++) {
|
2018-11-27 00:05:20 +00:00
|
|
|
if (i >= EEPROM_SIZE) break;
|
|
|
|
EEPROM.write(i, cardPayload.charAt(i));
|
|
|
|
}
|
|
|
|
EEPROM.commit();
|
2018-11-30 09:09:17 +00:00
|
|
|
interrupts();
|
2018-11-27 00:05:20 +00:00
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Finished getting card data.");
|
2018-11-27 00:05:20 +00:00
|
|
|
} else {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.println("[ERROR] Resource not found.");
|
|
|
|
logEvent(LOG_COMM_CARD_FAIL, cardHTTPCodeStr.c_str(), cardHTTPCodeStr.length());
|
2018-11-27 00:05:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-19 09:43:08 +00:00
|
|
|
if (SERIAL_LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", cardHTTP.errorToString(cardHTTPCode).c_str());
|
|
|
|
logEvent(LOG_COMM_CARD_FAIL, cardHTTPCodeStr.c_str(), cardHTTPCodeStr.length());
|
2018-11-27 00:05:20 +00:00
|
|
|
}
|
2018-11-28 08:00:07 +00:00
|
|
|
|
|
|
|
cardHTTP.end();
|
2018-11-27 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 09:43:08 +00:00
|
|
|
void postInfolog()
|
|
|
|
{
|
|
|
|
HTTPClient infoHTTP;
|
|
|
|
|
|
|
|
//infoHTTP.begin("https://url", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
|
|
|
|
infoHTTP.begin(INFOLOG_URL + wifiMACAddr + "/");
|
|
|
|
infoHTTP.addHeader("Content-Type", "application/json");
|
|
|
|
|
|
|
|
if (SERIAL_LOGGING) Serial.println("[INFO] Info state HTTP begin.");
|
|
|
|
|
|
|
|
if (SERIAL_LOGGING) Serial.print("[INFO] HTTP POST: ");
|
|
|
|
String postData = serializeLog();
|
|
|
|
if (SERIAL_LOGGING) Serial.println(postData);
|
|
|
|
int16_t infoHTTPCode = infoHTTP.POST(postData);
|
|
|
|
String infoHTTPCodeStr = String(infoHTTPCode);
|
|
|
|
|
|
|
|
if (infoHTTPCode > 0) {
|
|
|
|
if (SERIAL_LOGGING) Serial.printf("[INFO] POST success, code: %d\n", infoHTTPCode);
|
|
|
|
|
|
|
|
if (infoHTTPCode == HTTP_CODE_OK) {
|
|
|
|
if (SERIAL_LOGGING) Serial.print("[INFO] Resource found, parsing response: ");
|
|
|
|
String infoPayload = infoHTTP.getString();
|
|
|
|
if (SERIAL_LOGGING) Serial.println(infoPayload);
|
|
|
|
uint32_t unixTime = deserializeInfoJson(infoPayload);
|
|
|
|
|
|
|
|
struct timeval tv = { .tv_sec = unixTime, .tv_usec = 0 };
|
|
|
|
struct timezone tz = { .tz_minuteswest = 0, .tz_dsttime = 0 };
|
|
|
|
settimeofday(&tv, &tz);
|
|
|
|
|
|
|
|
if (SERIAL_LOGGING) Serial.print("[INFO] Set system time to: ");
|
|
|
|
if (SERIAL_LOGGING) Serial.println(unixTime);
|
|
|
|
} else {
|
|
|
|
if (SERIAL_LOGGING) Serial.println("[ERROR] Resource not found.");
|
|
|
|
logEvent(LOG_COMM_INFO_FAIL, infoHTTPCodeStr.c_str(), infoHTTPCodeStr.length());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (SERIAL_LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", infoHTTP.errorToString(infoHTTPCode).c_str());
|
|
|
|
logEvent(LOG_COMM_INFO_FAIL, infoHTTPCodeStr.c_str(), infoHTTPCodeStr.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
infoHTTP.end();
|
|
|
|
}
|
|
|
|
|
2018-02-06 02:31:54 +00:00
|
|
|
void processCommState()
|
|
|
|
{
|
2019-01-19 09:43:08 +00:00
|
|
|
static uint16_t commLockIdleCount, commCardIdleCount, commInfoIdleCount;
|
2018-02-06 02:31:54 +00:00
|
|
|
|
|
|
|
switch (commState) {
|
|
|
|
case COMM_INIT:
|
2018-11-10 21:56:50 +00:00
|
|
|
commLockIdleCount = 0;
|
|
|
|
commCardIdleCount = 0;
|
2019-01-19 09:43:08 +00:00
|
|
|
commInfoIdleCount = 0;
|
2018-11-10 21:56:50 +00:00
|
|
|
|
2018-02-06 02:31:54 +00:00
|
|
|
commState = COMM_IDLE;
|
|
|
|
break;
|
|
|
|
case COMM_IDLE:
|
2018-11-10 21:56:50 +00:00
|
|
|
commLockIdleCount++;
|
|
|
|
commCardIdleCount++;
|
2019-01-19 09:43:08 +00:00
|
|
|
commInfoIdleCount++;
|
2018-11-10 21:56:50 +00:00
|
|
|
|
2018-11-14 02:31:53 +00:00
|
|
|
if (commLockIdleCount >= COMM_LOCK_IDLE_TIME) {
|
|
|
|
commState = COMM_LOCK;
|
|
|
|
} else if (commCardIdleCount >= COMM_CARD_IDLE_TIME) {
|
|
|
|
commState = COMM_CARD;
|
2019-01-19 09:43:08 +00:00
|
|
|
} else if (commInfoIdleCount >= COMM_INFO_IDLE_TIME) {
|
|
|
|
commState = COMM_INFO;
|
2018-02-06 02:31:54 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-11-14 02:31:53 +00:00
|
|
|
case COMM_LOCK:
|
|
|
|
{
|
2018-11-27 00:05:20 +00:00
|
|
|
postState();
|
2018-11-14 02:31:53 +00:00
|
|
|
|
|
|
|
commLockIdleCount = 0;
|
|
|
|
|
|
|
|
commState = COMM_IDLE;
|
2018-02-06 02:31:54 +00:00
|
|
|
}
|
2018-11-14 02:31:53 +00:00
|
|
|
break;
|
|
|
|
case COMM_CARD:
|
|
|
|
{
|
2018-11-27 00:05:20 +00:00
|
|
|
getCards();
|
2018-02-06 02:31:54 +00:00
|
|
|
|
2018-11-14 02:31:53 +00:00
|
|
|
commCardIdleCount = 0;
|
2018-11-10 21:56:50 +00:00
|
|
|
|
2018-11-14 02:31:53 +00:00
|
|
|
commState = COMM_IDLE;
|
|
|
|
}
|
2018-02-06 02:31:54 +00:00
|
|
|
break;
|
2019-01-19 09:43:08 +00:00
|
|
|
case COMM_INFO:
|
|
|
|
{
|
|
|
|
postInfolog();
|
|
|
|
|
|
|
|
commInfoIdleCount = 0;
|
|
|
|
|
|
|
|
commState = COMM_IDLE;
|
|
|
|
}
|
|
|
|
break;
|
2018-02-06 02:31:54 +00:00
|
|
|
}
|
|
|
|
}
|