Get cards from auth server
This commit is contained in:
parent
75d4395964
commit
bf140f02fb
|
@ -2,12 +2,13 @@
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266HTTPClient.h>
|
#include <ESP8266HTTPClient.h>
|
||||||
|
#include <EEPROM.h>
|
||||||
|
|
||||||
const char* WIFI_SSID = "Protospace";
|
const char* WIFI_SSID = "tanner";
|
||||||
const char* WIFI_PASS = "yycmakers";
|
const char* WIFI_PASS = "point cloud truck dust swim army";
|
||||||
char wifiMACAddr[18];
|
char wifiMACAddr[18];
|
||||||
const String SOCKET_URL = String("http://tools-socket.protospace.ca/api/lockout/");
|
const String SOCKET_URL = String("http://192.168.1.9:8080/api/lockout/");
|
||||||
const String CARD_URL = String("http://tools-auth.protospace.ca/cards/");
|
const String CARD_URL = String("http://192.168.1.9:8000/cards/");
|
||||||
|
|
||||||
#define CARD_BUFFER_LENGTH 14
|
#define CARD_BUFFER_LENGTH 14
|
||||||
char cardBuffer[CARD_BUFFER_LENGTH];
|
char cardBuffer[CARD_BUFFER_LENGTH];
|
||||||
|
@ -21,7 +22,7 @@ typedef struct __attribute__((packed)) cardData {
|
||||||
char tail;
|
char tail;
|
||||||
} cardData_t;
|
} cardData_t;
|
||||||
|
|
||||||
String authorizedCards = String("00000C5123,00000D1234,00000C5999,00000C5998,00000C5666");
|
#define LOGGING true
|
||||||
|
|
||||||
#define RELAY_PIN D1
|
#define RELAY_PIN D1
|
||||||
#define GREEN_BUTTON_PIN D3
|
#define GREEN_BUTTON_PIN D3
|
||||||
|
@ -36,12 +37,12 @@ String authorizedCards = String("00000C5123,00000D1234,00000C5999,00000C5998,000
|
||||||
#define LED_ON HIGH
|
#define LED_ON HIGH
|
||||||
#define LED_OFF !LED_ON
|
#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 EEPROM_SIZE 4095
|
||||||
#define DELAY_TIME ONE_SECOND / 100
|
#define EEPROM_START 0
|
||||||
#define COMM_LOCK_IDLE_TIME ONE_SECOND / 2
|
|
||||||
#define COMM_CARD_IDLE_TIME ONE_SECOND * 60 * 5
|
|
||||||
|
|
||||||
enum wifiStates
|
enum wifiStates
|
||||||
{
|
{
|
||||||
|
@ -63,8 +64,8 @@ enum commStates
|
||||||
{
|
{
|
||||||
COMM_INIT,
|
COMM_INIT,
|
||||||
COMM_IDLE,
|
COMM_IDLE,
|
||||||
COMM_GET_LOCK,
|
COMM_LOCK,
|
||||||
COMM_GET_CARD,
|
COMM_CARD,
|
||||||
} commState = COMM_INIT;
|
} commState = COMM_INIT;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
|
@ -77,6 +78,8 @@ void setup()
|
||||||
pinMode(RED_BUTTON_PIN, INPUT_PULLUP);
|
pinMode(RED_BUTTON_PIN, INPUT_PULLUP);
|
||||||
pinMode(GREEN_LED_PIN, OUTPUT);
|
pinMode(GREEN_LED_PIN, OUTPUT);
|
||||||
pinMode(RED_LED_PIN, OUTPUT);
|
pinMode(RED_LED_PIN, OUTPUT);
|
||||||
|
|
||||||
|
EEPROM.begin(EEPROM_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
|
@ -113,11 +116,13 @@ void loop()
|
||||||
delay(DELAY_TIME);
|
delay(DELAY_TIME);
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t charToNum(char input) {
|
int8_t charToNum(char input)
|
||||||
|
{
|
||||||
return String("0123456789ABCDEF").indexOf(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.
|
// checksum is each hex data byte xord'd together.
|
||||||
// each char is a hex nibble, so we have to work in pairs.
|
// 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;
|
cardData_t *cardData = (cardData_t *) cardBuffer;
|
||||||
|
|
||||||
if (cardData->head == 0x2 && cardData->tail == 0x3 && checksum(cardData)) {
|
if (cardData->head == 0x2 && cardData->tail == 0x3 && checksum(cardData)) {
|
||||||
String cardStr = String();
|
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];
|
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);
|
if (LOGGING) Serial.println("[INFO] Good scan from card: " + cardStr);
|
||||||
|
|
||||||
|
@ -158,7 +172,7 @@ void checkCard() {
|
||||||
lockState = LOCK_PREARM;
|
lockState = LOCK_PREARM;
|
||||||
}
|
}
|
||||||
} else {
|
} 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
|
// JSON functions to prevent memory leaking
|
||||||
String serializeLockJson(uint8_t lockState) {
|
String serializeLockJson(uint8_t lockState)
|
||||||
|
{
|
||||||
// Generated with: https://arduinojson.org/assistant/
|
// Generated with: https://arduinojson.org/assistant/
|
||||||
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50;
|
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50;
|
||||||
DynamicJsonBuffer jsonBuffer(bufferSize);
|
DynamicJsonBuffer jsonBuffer(bufferSize);
|
||||||
|
@ -286,7 +301,8 @@ String serializeLockJson(uint8_t lockState) {
|
||||||
return postData;
|
return postData;
|
||||||
}
|
}
|
||||||
|
|
||||||
String deserializeLockJson(String input) {
|
String deserializeLockJson(String input)
|
||||||
|
{
|
||||||
// Generated with: https://arduinojson.org/assistant/
|
// Generated with: https://arduinojson.org/assistant/
|
||||||
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50;
|
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50;
|
||||||
DynamicJsonBuffer jsonBuffer(bufferSize);
|
DynamicJsonBuffer jsonBuffer(bufferSize);
|
||||||
|
@ -313,32 +329,33 @@ void processCommState()
|
||||||
commLockIdleCount++;
|
commLockIdleCount++;
|
||||||
commCardIdleCount++;
|
commCardIdleCount++;
|
||||||
|
|
||||||
if (commLockIdleCount >= COMM_LOCK_IDLE_TIME / DELAY_TIME) {
|
if (commLockIdleCount >= COMM_LOCK_IDLE_TIME) {
|
||||||
commState = COMM_GET_LOCK;
|
commState = COMM_LOCK;
|
||||||
} else if (commCardIdleCount >= COMM_CARD_IDLE_TIME / DELAY_TIME) {
|
} else if (commCardIdleCount >= COMM_CARD_IDLE_TIME) {
|
||||||
commState = COMM_GET_CARD;
|
commState = COMM_CARD;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case COMM_GET_LOCK:
|
case COMM_LOCK:
|
||||||
|
{
|
||||||
if (LOGGING) Serial.println("[INFO] Lock state HTTP begin.");
|
if (LOGGING) Serial.println("[INFO] Lock state HTTP begin.");
|
||||||
HTTPClient http;
|
HTTPClient lockHTTP;
|
||||||
//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
|
//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
|
||||||
http.begin(SOCKET_URL + wifiMACAddr);
|
lockHTTP.begin(SOCKET_URL + wifiMACAddr);
|
||||||
http.addHeader("Content-Type", "application/json");
|
lockHTTP.addHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
if (LOGGING) Serial.print("[INFO] HTTP POST: ");
|
if (LOGGING) Serial.print("[INFO] HTTP POST: ");
|
||||||
String postData = serializeLockJson(lockState);
|
String postData = serializeLockJson(lockState);
|
||||||
if (LOGGING) Serial.println(postData);
|
if (LOGGING) Serial.println(postData);
|
||||||
uint16_t httpCode = http.POST(postData);
|
uint16_t lockHTTPCode = lockHTTP.POST(postData);
|
||||||
|
|
||||||
if (httpCode > 0) {
|
if (lockHTTPCode > 0) {
|
||||||
if (LOGGING) Serial.printf("[INFO] POST success, code: %d\n", httpCode);
|
if (LOGGING) Serial.printf("[INFO] POST success, code: %d\n", lockHTTPCode);
|
||||||
|
|
||||||
if (httpCode == HTTP_CODE_OK) {
|
if (lockHTTPCode == HTTP_CODE_OK) {
|
||||||
if (LOGGING) Serial.print("[INFO] Resource found, parsing response: ");
|
if (LOGGING) Serial.print("[INFO] Resource found, parsing response: ");
|
||||||
String payload = http.getString();
|
String lockPayload = lockHTTP.getString();
|
||||||
if (LOGGING) Serial.println(payload);
|
if (LOGGING) Serial.println(lockPayload);
|
||||||
String action = deserializeLockJson(payload);
|
String action = deserializeLockJson(lockPayload);
|
||||||
|
|
||||||
if (action == "arm" && lockState == LOCK_OFF) {
|
if (action == "arm" && lockState == LOCK_OFF) {
|
||||||
lockState = LOCK_PREARM;
|
lockState = LOCK_PREARM;
|
||||||
|
@ -351,12 +368,52 @@ void processCommState()
|
||||||
if (LOGGING) Serial.println("[ERROR] Resource not found.");
|
if (LOGGING) Serial.println("[ERROR] Resource not found.");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", http.errorToString(httpCode).c_str());
|
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", lockHTTP.errorToString(lockHTTPCode).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
commLockIdleCount = 0;
|
commLockIdleCount = 0;
|
||||||
|
|
||||||
commState = COMM_IDLE;
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
commCardIdleCount = 0;
|
||||||
|
|
||||||
|
commState = COMM_IDLE;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user