From 2c9542a2bc13b57393420c6b0c3c3bea1fb53a4a Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Wed, 28 Nov 2018 01:00:07 -0700 Subject: [PATCH] Flash red LED if any button is pressed while arming --- firmware/firmware.ino | 59 +++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index a5f3173..a999cbe 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -5,9 +5,9 @@ #include #include -const char* WIFI_SSID = "Protospace"; -const char* WIFI_PASS = "yycmakers"; -char wifiMACAddr[18]; +const char *WIFI_SSID PROGMEM = "Protospace"; +const char *WIFI_PASS PROGMEM = "yycmakers"; +char wifiMACAddr[20] = ""; const String SOCKET_URL = String("http://tools-socket.protospace.ca/api/lockout/"); const String CARD_URL = String("http://tools-auth.protospace.ca/cards/"); @@ -43,10 +43,9 @@ typedef struct __attribute__((packed)) cardData { #define DELAY_TIME 10 #define COMM_LOCK_IDLE_TIME 50 #define COMM_CARD_IDLE_TIME 1000 -#define LED_DENIED_IDLE_TIME 50 +#define LED_DELAYED_IDLE_TIME 50 #define EEPROM_SIZE 4095 -#define EEPROM_START 0 enum wifiStates { @@ -60,8 +59,8 @@ enum LEDStates LED_OFF, LED_ARMED, LED_ON, - LED_DENIED, - LED_DENIED_IDLE, + LED_DELAYED, + LED_DELAYED_IDLE, } LEDState = LED_OFF; enum lockStates @@ -155,7 +154,7 @@ bool checksum(cardData_t *cardData) int8_t even = 0, odd = 0; - for (int i = 0; i < CARD_DATA_LENGTH; i++) { + for (int8_t i = 0; i < CARD_DATA_LENGTH; i++) { int8_t num = charToNum(cardData->data[i]); if (num == -1) return false; @@ -181,11 +180,11 @@ void checkCard() String cardStr = String(); String authorizedCards = String(); - for (int i = 0; i < CARD_DATA_LENGTH; i++) { + for (uint8_t i = 0; i < CARD_DATA_LENGTH; i++) { cardStr += cardData->data[i]; } - for (int i = EEPROM_START; i < EEPROM_SIZE; i++) { + for (uint16_t i = 0; i < EEPROM_SIZE; i++) { char tmp = EEPROM.read(i); authorizedCards += tmp; if (tmp == '$') break; @@ -200,7 +199,7 @@ void checkCard() } } else { if (LOGGING) Serial.println("[INFO] Card not authorized on machine."); - LEDState = LED_DENIED; + LEDState = LED_DELAYED; } } } @@ -250,7 +249,7 @@ void processLockState() { switch (lockState) { case LOCK_OFF: - if (LEDState < LED_DENIED) LEDState = LED_OFF; + if (LEDState < LED_DELAYED) LEDState = LED_OFF; relayOff(); break; @@ -260,10 +259,11 @@ void processLockState() lockState = LOCK_ARMED; } else { lockState = LOCK_OFF; + LEDState = LED_DELAYED; } break; case LOCK_ARMED: - if (LEDState < LED_DENIED) LEDState = LED_ARMED; + if (LEDState < LED_DELAYED) LEDState = LED_ARMED; relayOff(); @@ -285,7 +285,7 @@ void processLockState() } break; case LOCK_ON: - if (LEDState < LED_DENIED) LEDState = LED_ON; + if (LEDState < LED_DELAYED) LEDState = LED_ON; relayOn(); @@ -308,7 +308,7 @@ void redLEDOff() { digitalWrite(RED_LED_PIN, LED_PIN_OFF); } void processLEDState() { - static uint16_t LEDDeniedIdleCount = 0; + static uint16_t LEDDelayedIdleCount = 0; switch (LEDState) { case LED_OFF: @@ -326,18 +326,18 @@ void processLEDState() redLEDOn(); break; - case LED_DENIED: - LEDDeniedIdleCount = 0; + case LED_DELAYED: + LEDDelayedIdleCount = 0; - LEDState = LED_DENIED_IDLE; + LEDState = LED_DELAYED_IDLE; break; - case LED_DENIED_IDLE: - LEDDeniedIdleCount++; + case LED_DELAYED_IDLE: + LEDDelayedIdleCount++; - if (LEDDeniedIdleCount < LED_DENIED_IDLE_TIME) { + if (LEDDelayedIdleCount < LED_DELAYED_IDLE_TIME) { greenLEDOff(); redLEDOn(); - } else if (LEDDeniedIdleCount < LED_DENIED_IDLE_TIME * 2) { + } else if (LEDDelayedIdleCount < LED_DELAYED_IDLE_TIME * 2) { greenLEDOff(); redLEDOff(); } else { @@ -357,7 +357,7 @@ String serializeLockJson(uint8_t lockState) { // Generated with: https://arduinojson.org/assistant/ const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50; - DynamicJsonBuffer jsonBuffer(bufferSize); + StaticJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); root["lockState"] = (uint8_t) lockState; @@ -371,7 +371,7 @@ String deserializeLockJson(String input) { // Generated with: https://arduinojson.org/assistant/ const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50; - DynamicJsonBuffer jsonBuffer(bufferSize); + StaticJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.parseObject(input); String action = root["action"]; @@ -416,6 +416,8 @@ void postState() } else { if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", lockHTTP.errorToString(lockHTTPCode).c_str()); } + + lockHTTP.end(); } void getCards() @@ -440,7 +442,7 @@ void getCards() cardPayload += "$"; // Mark the end if (LOGGING) Serial.println(cardPayload); - for (int i = EEPROM_START; i < cardPayload.length(); i++) { + for (int i = 0; i < cardPayload.length(); i++) { if (i >= EEPROM_SIZE) break; EEPROM.write(i, cardPayload.charAt(i)); } @@ -453,6 +455,8 @@ void getCards() } else { if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", cardHTTP.errorToString(cardHTTPCode).c_str()); } + + cardHTTP.end(); } void processCommState() @@ -484,6 +488,8 @@ void processCommState() commLockIdleCount = 0; commState = COMM_IDLE; + + Serial.printf("******free heap size: %u\n", ESP.getFreeHeap()); } break; case COMM_CARD: @@ -493,9 +499,6 @@ void processCommState() commCardIdleCount = 0; commState = COMM_IDLE; - - Serial.printf("******heap size: %u\n", ESP.getFreeHeap()); - Serial.printf("******frag.: %u\n", ESP.getHeapFragmentation()); } break; }