|
|
|
@ -2,12 +2,13 @@ |
|
|
|
|
#include <ArduinoJson.h> |
|
|
|
|
#include <ESP8266WiFi.h> |
|
|
|
|
#include <ESP8266HTTPClient.h> |
|
|
|
|
#include <EEPROM.h> |
|
|
|
|
|
|
|
|
|
const char* WIFI_SSID = "Protospace"; |
|
|
|
|
const char* WIFI_PASS = "yycmakers"; |
|
|
|
|
const char* WIFI_SSID = "tanner"; |
|
|
|
|
const char* WIFI_PASS = "point cloud truck dust swim army"; |
|
|
|
|
char wifiMACAddr[18]; |
|
|
|
|
const String SOCKET_URL = String("http://tools-socket.protospace.ca/api/lockout/"); |
|
|
|
|
const String CARD_URL = String("http://tools-auth.protospace.ca/cards/"); |
|
|
|
|
const String SOCKET_URL = String("http://192.168.1.9:8080/api/lockout/"); |
|
|
|
|
const String CARD_URL = String("http://192.168.1.9:8000/cards/"); |
|
|
|
|
|
|
|
|
|
#define CARD_BUFFER_LENGTH 14 |
|
|
|
|
char cardBuffer[CARD_BUFFER_LENGTH]; |
|
|
|
@ -21,7 +22,7 @@ typedef struct __attribute__((packed)) cardData { |
|
|
|
|
char tail; |
|
|
|
|
} cardData_t; |
|
|
|
|
|
|
|
|
|
String authorizedCards = String("00000C5123,00000D1234,00000C5999,00000C5998,00000C5666"); |
|
|
|
|
#define LOGGING true |
|
|
|
|
|
|
|
|
|
#define RELAY_PIN D1 |
|
|
|
|
#define GREEN_BUTTON_PIN D3 |
|
|
|
@ -36,12 +37,12 @@ String authorizedCards = String("00000C5123,00000D1234,00000C5999,00000C5998,000 |
|
|
|
|
#define LED_ON HIGH |
|
|
|
|
#define LED_OFF !LED_ON |
|
|
|
|
|
|
|
|
|
#define LOGGING true |
|
|
|
|
#define DELAY_TIME 10 |
|
|
|
|
#define COMM_LOCK_IDLE_TIME 50 |
|
|
|
|
#define COMM_CARD_IDLE_TIME 200 |
|
|
|
|
|
|
|
|
|
#define ONE_SECOND 1000 |
|
|
|
|
#define DELAY_TIME ONE_SECOND / 100 |
|
|
|
|
#define COMM_LOCK_IDLE_TIME ONE_SECOND / 2 |
|
|
|
|
#define COMM_CARD_IDLE_TIME ONE_SECOND * 60 * 5 |
|
|
|
|
#define EEPROM_SIZE 4095 |
|
|
|
|
#define EEPROM_START 0 |
|
|
|
|
|
|
|
|
|
enum wifiStates |
|
|
|
|
{ |
|
|
|
@ -63,8 +64,8 @@ enum commStates |
|
|
|
|
{ |
|
|
|
|
COMM_INIT, |
|
|
|
|
COMM_IDLE, |
|
|
|
|
COMM_GET_LOCK, |
|
|
|
|
COMM_GET_CARD, |
|
|
|
|
COMM_LOCK, |
|
|
|
|
COMM_CARD, |
|
|
|
|
} commState = COMM_INIT; |
|
|
|
|
|
|
|
|
|
void setup() |
|
|
|
@ -77,6 +78,8 @@ void setup() |
|
|
|
|
pinMode(RED_BUTTON_PIN, INPUT_PULLUP); |
|
|
|
|
pinMode(GREEN_LED_PIN, OUTPUT); |
|
|
|
|
pinMode(RED_LED_PIN, OUTPUT); |
|
|
|
|
|
|
|
|
|
EEPROM.begin(EEPROM_SIZE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void loop() |
|
|
|
@ -113,11 +116,13 @@ void loop() |
|
|
|
|
delay(DELAY_TIME); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int8_t charToNum(char input) { |
|
|
|
|
int8_t charToNum(char input) |
|
|
|
|
{ |
|
|
|
|
return String("0123456789ABCDEF").indexOf(input); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool checksum(cardData_t *cardData) { |
|
|
|
|
bool checksum(cardData_t *cardData) |
|
|
|
|
{ |
|
|
|
|
// checksum is each hex data byte xord'd together.
|
|
|
|
|
// each char is a hex nibble, so we have to work in pairs.
|
|
|
|
|
|
|
|
|
@ -141,14 +146,23 @@ bool checksum(cardData_t *cardData) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void checkCard() { |
|
|
|
|
void checkCard() |
|
|
|
|
{ |
|
|
|
|
cardData_t *cardData = (cardData_t *) cardBuffer; |
|
|
|
|
|
|
|
|
|
if (cardData->head == 0x2 && cardData->tail == 0x3 && checksum(cardData)) { |
|
|
|
|
String cardStr = String(); |
|
|
|
|
String authorizedCards = String(); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < CARD_DATA_LENGTH; i++) |
|
|
|
|
for (int i = 0; i < CARD_DATA_LENGTH; i++) { |
|
|
|
|
cardStr += cardData->data[i]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (int i = EEPROM_START; i < EEPROM_SIZE; i++) { |
|
|
|
|
char tmp = EEPROM.read(i); |
|
|
|
|
authorizedCards += tmp; |
|
|
|
|
if (tmp == '$') break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (LOGGING) Serial.println("[INFO] Good scan from card: " + cardStr); |
|
|
|
|
|
|
|
|
@ -158,7 +172,7 @@ void checkCard() { |
|
|
|
|
lockState = LOCK_PREARM; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if (LOGGING) Serial.println("[INFO] Card not authorized or machine."); |
|
|
|
|
if (LOGGING) Serial.println("[INFO] Card not authorized on machine."); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -273,7 +287,8 @@ void processLockState() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// JSON functions to prevent memory leaking
|
|
|
|
|
String serializeLockJson(uint8_t lockState) { |
|
|
|
|
String serializeLockJson(uint8_t lockState) |
|
|
|
|
{ |
|
|
|
|
// Generated with: https://arduinojson.org/assistant/
|
|
|
|
|
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50; |
|
|
|
|
DynamicJsonBuffer jsonBuffer(bufferSize); |
|
|
|
@ -286,7 +301,8 @@ String serializeLockJson(uint8_t lockState) { |
|
|
|
|
return postData; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String deserializeLockJson(String input) { |
|
|
|
|
String deserializeLockJson(String input) |
|
|
|
|
{ |
|
|
|
|
// Generated with: https://arduinojson.org/assistant/
|
|
|
|
|
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50; |
|
|
|
|
DynamicJsonBuffer jsonBuffer(bufferSize); |
|
|
|
@ -313,50 +329,91 @@ void processCommState() |
|
|
|
|
commLockIdleCount++; |
|
|
|
|
commCardIdleCount++; |
|
|
|
|
|
|
|
|
|
if (commLockIdleCount >= COMM_LOCK_IDLE_TIME / DELAY_TIME) { |
|
|
|
|
commState = COMM_GET_LOCK; |
|
|
|
|
} else if (commCardIdleCount >= COMM_CARD_IDLE_TIME / DELAY_TIME) { |
|
|
|
|
commState = COMM_GET_CARD; |
|
|
|
|
if (commLockIdleCount >= COMM_LOCK_IDLE_TIME) { |
|
|
|
|
commState = COMM_LOCK; |
|
|
|
|
} else if (commCardIdleCount >= COMM_CARD_IDLE_TIME) { |
|
|
|
|
commState = COMM_CARD; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case COMM_GET_LOCK: |
|
|
|
|
if (LOGGING) Serial.println("[INFO] Lock state HTTP begin."); |
|
|
|
|
HTTPClient http; |
|
|
|
|
//http.begin("https://url", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
|
|
|
|
|
http.begin(SOCKET_URL + wifiMACAddr); |
|
|
|
|
http.addHeader("Content-Type", "application/json"); |
|
|
|
|
|
|
|
|
|
if (LOGGING) Serial.print("[INFO] HTTP POST: "); |
|
|
|
|
String postData = serializeLockJson(lockState); |
|
|
|
|
if (LOGGING) Serial.println(postData); |
|
|
|
|
uint16_t httpCode = http.POST(postData); |
|
|
|
|
|
|
|
|
|
if (httpCode > 0) { |
|
|
|
|
if (LOGGING) Serial.printf("[INFO] POST success, code: %d\n", httpCode); |
|
|
|
|
|
|
|
|
|
if (httpCode == HTTP_CODE_OK) { |
|
|
|
|
if (LOGGING) Serial.print("[INFO] Resource found, parsing response: "); |
|
|
|
|
String payload = http.getString(); |
|
|
|
|
if (LOGGING) Serial.println(payload); |
|
|
|
|
String action = deserializeLockJson(payload); |
|
|
|
|
|
|
|
|
|
if (action == "arm" && lockState == LOCK_OFF) { |
|
|
|
|
lockState = LOCK_PREARM; |
|
|
|
|
} else if (action == "disarm") { |
|
|
|
|
lockState = LOCK_OFF; |
|
|
|
|
case COMM_LOCK: |
|
|
|
|
{ |
|
|
|
|
if (LOGGING) Serial.println("[INFO] Lock state HTTP begin."); |
|
|
|
|
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"); |
|
|
|
|
|
|
|
|
|
if (LOGGING) Serial.print("[INFO] HTTP POST: "); |
|
|
|
|
String postData = serializeLockJson(lockState); |
|
|
|
|
if (LOGGING) Serial.println(postData); |
|
|
|
|
uint16_t lockHTTPCode = lockHTTP.POST(postData); |
|
|
|
|
|
|
|
|
|
if (lockHTTPCode > 0) { |
|
|
|
|
if (LOGGING) Serial.printf("[INFO] POST success, code: %d\n", lockHTTPCode); |
|
|
|
|
|
|
|
|
|
if (lockHTTPCode == HTTP_CODE_OK) { |
|
|
|
|
if (LOGGING) Serial.print("[INFO] Resource found, parsing response: "); |
|
|
|
|
String lockPayload = lockHTTP.getString(); |
|
|
|
|
if (LOGGING) Serial.println(lockPayload); |
|
|
|
|
String action = deserializeLockJson(lockPayload); |
|
|
|
|
|
|
|
|
|
if (action == "arm" && lockState == LOCK_OFF) { |
|
|
|
|
lockState = LOCK_PREARM; |
|
|
|
|
} else if (action == "disarm") { |
|
|
|
|
lockState = LOCK_OFF; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (LOGGING) Serial.println("[INFO] action: " + action); |
|
|
|
|
} else { |
|
|
|
|
if (LOGGING) Serial.println("[ERROR] Resource not found."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (LOGGING) Serial.println("[INFO] action: " + action); |
|
|
|
|
} else { |
|
|
|
|
if (LOGGING) Serial.println("[ERROR] Resource not found."); |
|
|
|
|
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", lockHTTP.errorToString(lockHTTPCode).c_str()); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", http.errorToString(httpCode).c_str()); |
|
|
|
|
|
|
|
|
|
commLockIdleCount = 0; |
|
|
|
|
|
|
|
|
|
commState = COMM_IDLE; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case COMM_CARD: |
|
|
|
|
{ |
|
|
|
|
if (LOGGING) Serial.println("[INFO] Card state HTTP begin."); |
|
|
|
|
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"); |
|
|
|
|
|
|
|
|
|
if (LOGGING) Serial.println("[INFO] HTTP GET"); |
|
|
|
|
uint16_t cardHTTPCode = cardHTTP.GET(); |
|
|
|
|
|
|
|
|
|
if (cardHTTPCode > 0) { |
|
|
|
|
if (LOGGING) Serial.printf("[INFO] GET success, code: %d\n", cardHTTPCode); |
|
|
|
|
|
|
|
|
|
if (cardHTTPCode == HTTP_CODE_OK) { |
|
|
|
|
if (LOGGING) Serial.print("[INFO] Resource found, parsing response: "); |
|
|
|
|
String cardPayload = cardHTTP.getString(); |
|
|
|
|
cardPayload += "$"; // Mark the end
|
|
|
|
|
if (LOGGING) Serial.println(cardPayload); |
|
|
|
|
|
|
|
|
|
for (int i = EEPROM_START; i < cardPayload.length(); i++) { |
|
|
|
|
if (i >= EEPROM_SIZE) break; |
|
|
|
|
EEPROM.write(i, cardPayload.charAt(i)); |
|
|
|
|
} |
|
|
|
|
EEPROM.commit(); |
|
|
|
|
|
|
|
|
|
if (LOGGING) Serial.println("[INFO] Finished getting card data."); |
|
|
|
|
} else { |
|
|
|
|
if (LOGGING) Serial.println("[ERROR] Resource not found."); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", cardHTTP.errorToString(cardHTTPCode).c_str()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
commLockIdleCount = 0; |
|
|
|
|
commCardIdleCount = 0; |
|
|
|
|
|
|
|
|
|
commState = COMM_IDLE; |
|
|
|
|
commState = COMM_IDLE; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|