From 914ad9bcf27cd0e1f5c7cf5782668d68abe067b4 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Mon, 5 Feb 2018 23:59:47 -0700 Subject: [PATCH] Write firmware for the Arduino --- firmware/firmware.ino | 46 +++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index c1484f6..c2e3782 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -3,6 +3,11 @@ #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]; @@ -25,11 +30,7 @@ const String API_ROUTE = String("http://tools.protospace.ca:8080/api/lockout/"); #define LOGGING true #define DELAY_TIME 10 -#define COMM_IDLE_TIME 500 / DELAY_TIME - -// Generated with: https://arduinojson.org/assistant/ -const size_t bufferSize = JSON_OBJECT_SIZE(3) + 50; -DynamicJsonBuffer jsonBuffer(bufferSize); +#define COMM_IDLE_TIME 1000 / DELAY_TIME enum wifiStates { @@ -95,12 +96,12 @@ void processWifiState() if (WiFi.status() == WL_CONNECTED) { if (LOGGING) Serial.println("[INFO] Wifi is connected."); - if (LOGGING) Serial.print("[INFO] Wifi IP Address:"); + 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.print("[INFO] Wifi MAC Address: "); if (LOGGING) Serial.println(wifiMACAddr); wifiState = WIFI_CONNECTED; @@ -177,6 +178,20 @@ void processLockState() } } +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; + String postData = String(); + rootSerializer.printTo(postData); + + return postData; +} + + void processCommState() { static int commIdleCount = 0; @@ -200,10 +215,7 @@ void processCommState() http.addHeader("Content-Type", "application/json"); if (LOGGING) Serial.print("[INFO] HTTP POST: "); - JsonObject& rootSerializer = jsonBuffer.createObject(); - rootSerializer["lockState"] = lockState; - String postData = String(); - rootSerializer.printTo(postData); + String postData = serializeJson(lockState); if (LOGGING) Serial.println(postData); int httpCode = http.POST(postData); @@ -216,9 +228,17 @@ void processCommState() if (LOGGING) Serial.println(payload); JsonObject& rootDeserializer = jsonBuffer.parseObject(payload); - bool armable = rootDeserializer["armable"]; + String action = rootDeserializer["action"]; - if (LOGGING) Serial.println("[INFO] Armable: " + String(armable)); + if (action == "arm" && lockState == LOCK_OFF) { + lockState = LOCK_ARMED; + } else if (action == "disarm" && lockState == LOCK_ARMED) { + lockState = LOCK_OFF; + } else if (action == "disarm" && lockState == LOCK_ON) { + lockState = LOCK_OFF; + } + + if (LOGGING) Serial.println("[INFO] action: " + action); } else { if (LOGGING) Serial.println("[ERROR] Resource not found."); }