|
|
|
@ -94,6 +94,12 @@ void setup() |
|
|
|
|
|
|
|
|
|
EEPROM.begin(EEPROM_SIZE); |
|
|
|
|
|
|
|
|
|
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]); |
|
|
|
|
if (LOGGING) Serial.print("[INFO] Wifi MAC Address: "); |
|
|
|
|
if (LOGGING) Serial.println(wifiMACAddr); |
|
|
|
|
|
|
|
|
|
ticker.attach_ms(DELAY_TIME, tickerLoop); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -218,11 +224,6 @@ void processWifiState() |
|
|
|
|
|
|
|
|
|
if (LOGGING) Serial.print("[INFO] Wifi IP Address: "); |
|
|
|
|
if (LOGGING) Serial.println(WiFi.localIP()); |
|
|
|
|
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]); |
|
|
|
|
if (LOGGING) Serial.print("[INFO] Wifi MAC Address: "); |
|
|
|
|
if (LOGGING) Serial.println(wifiMACAddr); |
|
|
|
|
|
|
|
|
|
wifiState = WIFI_CONNECTED; |
|
|
|
|
} |
|
|
|
@ -350,7 +351,8 @@ void processLEDState() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// JSON functions to prevent memory leaking
|
|
|
|
|
// JSON functions to prevent memory leaking see:
|
|
|
|
|
// https://arduinojson.org/v5/faq/i-found-a-memory-leak-in-the-library/
|
|
|
|
|
String serializeLockJson(uint8_t lockState) |
|
|
|
|
{ |
|
|
|
|
// Generated with: https://arduinojson.org/assistant/
|
|
|
|
@ -377,36 +379,16 @@ String deserializeLockJson(String input) |
|
|
|
|
return action; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void processCommState() |
|
|
|
|
void postState() |
|
|
|
|
{ |
|
|
|
|
static uint16_t commLockIdleCount = 0; |
|
|
|
|
static uint16_t commCardIdleCount = 0; |
|
|
|
|
|
|
|
|
|
switch (commState) { |
|
|
|
|
case COMM_INIT: |
|
|
|
|
commLockIdleCount = 0; |
|
|
|
|
commCardIdleCount = 0; |
|
|
|
|
|
|
|
|
|
commState = COMM_IDLE; |
|
|
|
|
break; |
|
|
|
|
case COMM_IDLE: |
|
|
|
|
commLockIdleCount++; |
|
|
|
|
commCardIdleCount++; |
|
|
|
|
|
|
|
|
|
if (commLockIdleCount >= COMM_LOCK_IDLE_TIME) { |
|
|
|
|
commState = COMM_LOCK; |
|
|
|
|
} else if (commCardIdleCount >= COMM_CARD_IDLE_TIME) { |
|
|
|
|
commState = COMM_CARD; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
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.println("[INFO] Lock state HTTP begin."); |
|
|
|
|
|
|
|
|
|
if (LOGGING) Serial.print("[INFO] HTTP POST: "); |
|
|
|
|
String postData = serializeLockJson(lockState); |
|
|
|
|
if (LOGGING) Serial.println(postData); |
|
|
|
@ -434,20 +416,18 @@ void processCommState() |
|
|
|
|
} else { |
|
|
|
|
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", lockHTTP.errorToString(lockHTTPCode).c_str()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
commLockIdleCount = 0; |
|
|
|
|
|
|
|
|
|
commState = COMM_IDLE; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case COMM_CARD: |
|
|
|
|
{ |
|
|
|
|
if (LOGGING) Serial.println("[INFO] Card state HTTP begin."); |
|
|
|
|
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"); |
|
|
|
|
|
|
|
|
|
if (LOGGING) Serial.println("[INFO] Card state HTTP begin."); |
|
|
|
|
|
|
|
|
|
if (LOGGING) Serial.println("[INFO] HTTP GET"); |
|
|
|
|
int16_t cardHTTPCode = cardHTTP.GET(); |
|
|
|
|
|
|
|
|
@ -473,10 +453,49 @@ void processCommState() |
|
|
|
|
} else { |
|
|
|
|
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", cardHTTP.errorToString(cardHTTPCode).c_str()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void processCommState() |
|
|
|
|
{ |
|
|
|
|
static uint16_t commLockIdleCount = 0; |
|
|
|
|
static uint16_t commCardIdleCount = 0; |
|
|
|
|
|
|
|
|
|
switch (commState) { |
|
|
|
|
case COMM_INIT: |
|
|
|
|
commLockIdleCount = 0; |
|
|
|
|
commCardIdleCount = 0; |
|
|
|
|
|
|
|
|
|
commState = COMM_IDLE; |
|
|
|
|
break; |
|
|
|
|
case COMM_IDLE: |
|
|
|
|
commLockIdleCount++; |
|
|
|
|
commCardIdleCount++; |
|
|
|
|
|
|
|
|
|
if (commLockIdleCount >= COMM_LOCK_IDLE_TIME) { |
|
|
|
|
commState = COMM_LOCK; |
|
|
|
|
} else if (commCardIdleCount >= COMM_CARD_IDLE_TIME) { |
|
|
|
|
commState = COMM_CARD; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case COMM_LOCK: |
|
|
|
|
{ |
|
|
|
|
postState(); |
|
|
|
|
|
|
|
|
|
commLockIdleCount = 0; |
|
|
|
|
|
|
|
|
|
commState = COMM_IDLE; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case COMM_CARD: |
|
|
|
|
{ |
|
|
|
|
getCards(); |
|
|
|
|
|
|
|
|
|
commCardIdleCount = 0; |
|
|
|
|
|
|
|
|
|
commState = COMM_IDLE; |
|
|
|
|
|
|
|
|
|
Serial.printf("******heap size: %u\n", ESP.getFreeHeap()); |
|
|
|
|
Serial.printf("******frag.: %u\n", ESP.getHeapFragmentation()); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|