Move network stuff into their own functions
This commit is contained in:
parent
781035de64
commit
77e5be93f9
|
@ -3,7 +3,7 @@ Protospace lockout firmware
|
|||
|
||||
|
||||
Code Structure
|
||||
==============
|
||||
--------------
|
||||
|
||||
- Declarations
|
||||
- State definitions
|
||||
|
@ -12,3 +12,9 @@ Code Structure
|
|||
- States
|
||||
- State actions
|
||||
- Conditions that change the state
|
||||
|
||||
|
||||
Dependencies
|
||||
------------
|
||||
|
||||
Uses ArduinoJson Version 5.x.x
|
||||
|
|
|
@ -94,6 +94,12 @@ void setup()
|
|||
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
|
||||
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.println(wifiMACAddr);
|
||||
|
||||
ticker.attach_ms(DELAY_TIME, tickerLoop);
|
||||
}
|
||||
|
||||
|
@ -218,11 +224,6 @@ void processWifiState()
|
|||
|
||||
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.println(wifiMACAddr);
|
||||
|
||||
wifiState = WIFI_CONNECTED;
|
||||
}
|
||||
|
@ -350,7 +351,8 @@ void processLEDState()
|
|||
}
|
||||
}
|
||||
|
||||
// JSON functions to prevent memory leaking
|
||||
// JSON functions to prevent memory leaking see:
|
||||
// https://arduinojson.org/v5/faq/i-found-a-memory-leak-in-the-library/
|
||||
String serializeLockJson(uint8_t lockState)
|
||||
{
|
||||
// Generated with: https://arduinojson.org/assistant/
|
||||
|
@ -377,6 +379,82 @@ String deserializeLockJson(String input)
|
|||
return action;
|
||||
}
|
||||
|
||||
void postState()
|
||||
{
|
||||
HTTPClient lockHTTP;
|
||||
|
||||
//lockHTTP.begin("https://url", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
|
||||
lockHTTP.begin(SOCKET_URL + wifiMACAddr);
|
||||
lockHTTP.addHeader("Content-Type", "application/json");
|
||||
|
||||
if (LOGGING) Serial.println("[INFO] Lock state HTTP begin.");
|
||||
|
||||
if (LOGGING) Serial.print("[INFO] HTTP POST: ");
|
||||
String postData = serializeLockJson(lockState);
|
||||
if (LOGGING) Serial.println(postData);
|
||||
int16_t lockHTTPCode = lockHTTP.POST(postData);
|
||||
|
||||
if (lockHTTPCode > 0) {
|
||||
if (LOGGING) Serial.printf("[INFO] POST success, code: %d\n", lockHTTPCode);
|
||||
|
||||
if (lockHTTPCode == HTTP_CODE_OK) {
|
||||
if (LOGGING) Serial.print("[INFO] Resource found, parsing response: ");
|
||||
String lockPayload = lockHTTP.getString();
|
||||
if (LOGGING) Serial.println(lockPayload);
|
||||
String action = deserializeLockJson(lockPayload);
|
||||
|
||||
if (action == "arm" && lockState == LOCK_OFF) {
|
||||
lockState = LOCK_PREARM;
|
||||
} else if (action == "disarm") {
|
||||
lockState = LOCK_OFF;
|
||||
}
|
||||
|
||||
if (LOGGING) Serial.println("[INFO] action: " + action);
|
||||
} else {
|
||||
if (LOGGING) Serial.println("[ERROR] Resource not found.");
|
||||
}
|
||||
} else {
|
||||
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", lockHTTP.errorToString(lockHTTPCode).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void getCards()
|
||||
{
|
||||
HTTPClient cardHTTP;
|
||||
|
||||
//cardHTTP.begin("https://url", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
|
||||
cardHTTP.begin(CARD_URL + wifiMACAddr + "/");
|
||||
cardHTTP.addHeader("Content-Type", "application/json");
|
||||
|
||||
if (LOGGING) Serial.println("[INFO] Card state HTTP begin.");
|
||||
|
||||
if (LOGGING) Serial.println("[INFO] HTTP GET");
|
||||
int16_t cardHTTPCode = cardHTTP.GET();
|
||||
|
||||
if (cardHTTPCode > 0) {
|
||||
if (LOGGING) Serial.printf("[INFO] GET success, code: %d\n", cardHTTPCode);
|
||||
|
||||
if (cardHTTPCode == HTTP_CODE_OK) {
|
||||
if (LOGGING) Serial.print("[INFO] Resource found, parsing response: ");
|
||||
String cardPayload = cardHTTP.getString();
|
||||
cardPayload += "$"; // Mark the end
|
||||
if (LOGGING) Serial.println(cardPayload);
|
||||
|
||||
for (int i = EEPROM_START; i < cardPayload.length(); i++) {
|
||||
if (i >= EEPROM_SIZE) break;
|
||||
EEPROM.write(i, cardPayload.charAt(i));
|
||||
}
|
||||
EEPROM.commit();
|
||||
|
||||
if (LOGGING) Serial.println("[INFO] Finished getting card data.");
|
||||
} else {
|
||||
if (LOGGING) Serial.println("[ERROR] Resource not found.");
|
||||
}
|
||||
} else {
|
||||
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", cardHTTP.errorToString(cardHTTPCode).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void processCommState()
|
||||
{
|
||||
static uint16_t commLockIdleCount = 0;
|
||||
|
@ -401,39 +479,7 @@ void processCommState()
|
|||
break;
|
||||
case COMM_LOCK:
|
||||
{
|
||||
if (LOGGING) Serial.println("[INFO] Lock state HTTP begin.");
|
||||
HTTPClient lockHTTP;
|
||||
//lockHTTP.begin("https://url", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
|
||||
lockHTTP.begin(SOCKET_URL + wifiMACAddr);
|
||||
lockHTTP.addHeader("Content-Type", "application/json");
|
||||
|
||||
if (LOGGING) Serial.print("[INFO] HTTP POST: ");
|
||||
String postData = serializeLockJson(lockState);
|
||||
if (LOGGING) Serial.println(postData);
|
||||
int16_t lockHTTPCode = lockHTTP.POST(postData);
|
||||
|
||||
if (lockHTTPCode > 0) {
|
||||
if (LOGGING) Serial.printf("[INFO] POST success, code: %d\n", lockHTTPCode);
|
||||
|
||||
if (lockHTTPCode == HTTP_CODE_OK) {
|
||||
if (LOGGING) Serial.print("[INFO] Resource found, parsing response: ");
|
||||
String lockPayload = lockHTTP.getString();
|
||||
if (LOGGING) Serial.println(lockPayload);
|
||||
String action = deserializeLockJson(lockPayload);
|
||||
|
||||
if (action == "arm" && lockState == LOCK_OFF) {
|
||||
lockState = LOCK_PREARM;
|
||||
} else if (action == "disarm") {
|
||||
lockState = LOCK_OFF;
|
||||
}
|
||||
|
||||
if (LOGGING) Serial.println("[INFO] action: " + action);
|
||||
} else {
|
||||
if (LOGGING) Serial.println("[ERROR] Resource not found.");
|
||||
}
|
||||
} else {
|
||||
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", lockHTTP.errorToString(lockHTTPCode).c_str());
|
||||
}
|
||||
postState();
|
||||
|
||||
commLockIdleCount = 0;
|
||||
|
||||
|
@ -442,41 +488,14 @@ void processCommState()
|
|||
break;
|
||||
case COMM_CARD:
|
||||
{
|
||||
if (LOGGING) Serial.println("[INFO] Card state HTTP begin.");
|
||||
HTTPClient cardHTTP;
|
||||
//cardHTTP.begin("https://url", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
|
||||
cardHTTP.begin(CARD_URL + wifiMACAddr + "/");
|
||||
cardHTTP.addHeader("Content-Type", "application/json");
|
||||
|
||||
if (LOGGING) Serial.println("[INFO] HTTP GET");
|
||||
int16_t cardHTTPCode = cardHTTP.GET();
|
||||
|
||||
if (cardHTTPCode > 0) {
|
||||
if (LOGGING) Serial.printf("[INFO] GET success, code: %d\n", cardHTTPCode);
|
||||
|
||||
if (cardHTTPCode == HTTP_CODE_OK) {
|
||||
if (LOGGING) Serial.print("[INFO] Resource found, parsing response: ");
|
||||
String cardPayload = cardHTTP.getString();
|
||||
cardPayload += "$"; // Mark the end
|
||||
if (LOGGING) Serial.println(cardPayload);
|
||||
|
||||
for (int i = EEPROM_START; i < cardPayload.length(); i++) {
|
||||
if (i >= EEPROM_SIZE) break;
|
||||
EEPROM.write(i, cardPayload.charAt(i));
|
||||
}
|
||||
EEPROM.commit();
|
||||
|
||||
if (LOGGING) Serial.println("[INFO] Finished getting card data.");
|
||||
} else {
|
||||
if (LOGGING) Serial.println("[ERROR] Resource not found.");
|
||||
}
|
||||
} else {
|
||||
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", cardHTTP.errorToString(cardHTTPCode).c_str());
|
||||
}
|
||||
getCards();
|
||||
|
||||
commCardIdleCount = 0;
|
||||
|
||||
commState = COMM_IDLE;
|
||||
|
||||
Serial.printf("******heap size: %u\n", ESP.getFreeHeap());
|
||||
Serial.printf("******frag.: %u\n", ESP.getHeapFragmentation());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user