Write firmware for the Arduino

master
Tanner Collin 6 years ago
parent f0a3b02c6a
commit 914ad9bcf2
  1. 46
      firmware/firmware.ino

@ -3,6 +3,11 @@
#include <ESP8266WiFi.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_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 (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] Armable: " + String(armable));
if (LOGGING) Serial.println("[INFO] action: " + action);
} else {
if (LOGGING) Serial.println("[ERROR] Resource not found.");
}

Loading…
Cancel
Save