diff --git a/firmware/firmware.ino b/firmware/firmware.ino index c2e3782..f5f6fb2 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -3,34 +3,29 @@ #include #include -// Move to function: -// Generated with: https://arduinojson.org/assistant/ -const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50; -DynamicJsonBuffer jsonBuffer(bufferSize); - const char* WIFI_SSID = "Protospace"; const char* WIFI_PASS = "yycmakers"; char wifiMACAddr[18]; const String API_ROUTE = String("http://tools.protospace.ca:8080/api/lockout/"); -#define RELAY_PIN D1 -#define ON_BUTTON_PIN D3 -#define OFF_BUTTON_PIN D4 -#define ARMED_LED_PIN D5 -#define ON_LED_PIN D6 -#define OFF_LED_PIN D7 +#define RELAY_PIN D1 +#define ON_BUTTON_PIN D3 +#define OFF_BUTTON_PIN D4 +#define ARMED_LED_PIN D5 +#define ON_LED_PIN D6 +#define OFF_LED_PIN D7 -#define RELAY_CLOSED LOW -#define RELAY_OPEN !RELAY_CLOSED -#define BUTTON_CLOSED LOW -#define BUTTON_OPEN !BUTTON_CLOSED -#define LED_ON HIGH -#define LED_OFF !LED_ON +#define RELAY_CLOSED LOW +#define RELAY_OPEN !RELAY_CLOSED +#define BUTTON_CLOSED LOW +#define BUTTON_OPEN !BUTTON_CLOSED +#define LED_ON HIGH +#define LED_OFF !LED_ON #define LOGGING true #define DELAY_TIME 10 -#define COMM_IDLE_TIME 1000 / DELAY_TIME +#define COMM_IDLE_TIME 500 / DELAY_TIME enum wifiStates { @@ -178,19 +173,30 @@ void processLockState() } } +// JSON functions to prevent memory leaking String serializeJson(int lockState) { // Generated with: https://arduinojson.org/assistant/ const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50; DynamicJsonBuffer jsonBuffer(bufferSize); - JsonObject& rootSerializer = jsonBuffer.createObject(); - rootSerializer["lockState"] = (int) lockState; + JsonObject& root = jsonBuffer.createObject(); + root["lockState"] = (int) lockState; String postData = String(); - rootSerializer.printTo(postData); + root.printTo(postData); return postData; } +String deserializeJson(String input) { + // Generated with: https://arduinojson.org/assistant/ + const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50; + DynamicJsonBuffer jsonBuffer(bufferSize); + JsonObject& root = jsonBuffer.parseObject(input); + + String action = root["action"]; + + return action; +} void processCommState() { @@ -226,11 +232,9 @@ void processCommState() if (LOGGING) Serial.print("[INFO] Resource found, parsing response: "); String payload = http.getString(); if (LOGGING) Serial.println(payload); - JsonObject& rootDeserializer = jsonBuffer.parseObject(payload); + String action = deserializeJson(payload); - String action = rootDeserializer["action"]; - - if (action == "arm" && lockState == LOCK_OFF) { + if (action == "arm" && lockState == LOCK_OFF && digitalRead(ON_BUTTON_PIN) == BUTTON_OPEN) { lockState = LOCK_ARMED; } else if (action == "disarm" && lockState == LOCK_ARMED) { lockState = LOCK_OFF;