Don't arm lockout if green button is pressed

This commit is contained in:
Tanner Collin 2018-05-16 21:03:44 -06:00
parent 914ad9bcf2
commit 3388e86d3d

View File

@ -3,34 +3,29 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
// 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_SSID = "Protospace";
const char* WIFI_PASS = "yycmakers"; const char* WIFI_PASS = "yycmakers";
char wifiMACAddr[18]; char wifiMACAddr[18];
const String API_ROUTE = String("http://tools.protospace.ca:8080/api/lockout/"); const String API_ROUTE = String("http://tools.protospace.ca:8080/api/lockout/");
#define RELAY_PIN D1 #define RELAY_PIN D1
#define ON_BUTTON_PIN D3 #define ON_BUTTON_PIN D3
#define OFF_BUTTON_PIN D4 #define OFF_BUTTON_PIN D4
#define ARMED_LED_PIN D5 #define ARMED_LED_PIN D5
#define ON_LED_PIN D6 #define ON_LED_PIN D6
#define OFF_LED_PIN D7 #define OFF_LED_PIN D7
#define RELAY_CLOSED LOW #define RELAY_CLOSED LOW
#define RELAY_OPEN !RELAY_CLOSED #define RELAY_OPEN !RELAY_CLOSED
#define BUTTON_CLOSED LOW #define BUTTON_CLOSED LOW
#define BUTTON_OPEN !BUTTON_CLOSED #define BUTTON_OPEN !BUTTON_CLOSED
#define LED_ON HIGH #define LED_ON HIGH
#define LED_OFF !LED_ON #define LED_OFF !LED_ON
#define LOGGING true #define LOGGING true
#define DELAY_TIME 10 #define DELAY_TIME 10
#define COMM_IDLE_TIME 1000 / DELAY_TIME #define COMM_IDLE_TIME 500 / DELAY_TIME
enum wifiStates enum wifiStates
{ {
@ -178,19 +173,30 @@ void processLockState()
} }
} }
// JSON functions to prevent memory leaking
String serializeJson(int lockState) { String serializeJson(int 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);
JsonObject& rootSerializer = jsonBuffer.createObject(); JsonObject& root = jsonBuffer.createObject();
rootSerializer["lockState"] = (int) lockState; root["lockState"] = (int) lockState;
String postData = String(); String postData = String();
rootSerializer.printTo(postData); root.printTo(postData);
return 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() void processCommState()
{ {
@ -226,11 +232,9 @@ void processCommState()
if (LOGGING) Serial.print("[INFO] Resource found, parsing response: "); if (LOGGING) Serial.print("[INFO] Resource found, parsing response: ");
String payload = http.getString(); String payload = http.getString();
if (LOGGING) Serial.println(payload); if (LOGGING) Serial.println(payload);
JsonObject& rootDeserializer = jsonBuffer.parseObject(payload); String action = deserializeJson(payload);
String action = rootDeserializer["action"]; if (action == "arm" && lockState == LOCK_OFF && digitalRead(ON_BUTTON_PIN) == BUTTON_OPEN) {
if (action == "arm" && lockState == LOCK_OFF) {
lockState = LOCK_ARMED; lockState = LOCK_ARMED;
} else if (action == "disarm" && lockState == LOCK_ARMED) { } else if (action == "disarm" && lockState == LOCK_ARMED) {
lockState = LOCK_OFF; lockState = LOCK_OFF;