Write firmware for the Arduino

This commit is contained in:
Tanner Collin 2018-02-05 23:59:47 -07:00
parent f0a3b02c6a
commit 914ad9bcf2

View File

@ -3,6 +3,11 @@
#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];
@ -25,11 +30,7 @@ const String API_ROUTE = String("http://tools.protospace.ca:8080/api/lockout/");
#define LOGGING true #define LOGGING true
#define DELAY_TIME 10 #define DELAY_TIME 10
#define COMM_IDLE_TIME 500 / DELAY_TIME #define COMM_IDLE_TIME 1000 / DELAY_TIME
// Generated with: https://arduinojson.org/assistant/
const size_t bufferSize = JSON_OBJECT_SIZE(3) + 50;
DynamicJsonBuffer jsonBuffer(bufferSize);
enum wifiStates enum wifiStates
{ {
@ -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() void processCommState()
{ {
static int commIdleCount = 0; static int commIdleCount = 0;
@ -200,10 +215,7 @@ void processCommState()
http.addHeader("Content-Type", "application/json"); http.addHeader("Content-Type", "application/json");
if (LOGGING) Serial.print("[INFO] HTTP POST: "); if (LOGGING) Serial.print("[INFO] HTTP POST: ");
JsonObject& rootSerializer = jsonBuffer.createObject(); String postData = serializeJson(lockState);
rootSerializer["lockState"] = lockState;
String postData = String();
rootSerializer.printTo(postData);
if (LOGGING) Serial.println(postData); if (LOGGING) Serial.println(postData);
int httpCode = http.POST(postData); int httpCode = http.POST(postData);
@ -216,9 +228,17 @@ void processCommState()
if (LOGGING) Serial.println(payload); if (LOGGING) Serial.println(payload);
JsonObject& rootDeserializer = jsonBuffer.parseObject(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 { } else {
if (LOGGING) Serial.println("[ERROR] Resource not found."); if (LOGGING) Serial.println("[ERROR] Resource not found.");
} }