a2cabinLights/src/controller_main.cpp

233 lines
6.9 KiB
C++
Raw Normal View History

2019-05-13 08:00:06 +00:00
#include <Arduino.h>
2019-05-13 09:51:11 +00:00
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESPmDNS.h>
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_PHY_POWER 12
#include <ETH.h>
2019-05-19 15:35:40 +00:00
#define TRIGGER_TIMEOUT 60000
2019-05-19 13:21:36 +00:00
#define PIN_ACTIVATE 5
#define PIN_CLEAR 2
bool inputActive = true;
bool clearActive = true;
long activateTimeout = 0;
2019-05-18 13:56:56 +00:00
2019-05-19 14:11:15 +00:00
int lampSockets[4] = {-1, -1, -1, -1};
2019-05-13 09:51:11 +00:00
static bool eth_connected = false;
2019-05-18 13:56:56 +00:00
2019-05-13 09:51:11 +00:00
AsyncWebServer server(80);
2019-05-17 08:29:48 +00:00
AsyncWebSocket ws("/ws");
2019-05-19 14:11:15 +00:00
String lastSent = "---";
2019-05-13 09:51:11 +00:00
2019-05-17 16:31:54 +00:00
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
if (type == WS_EVT_CONNECT) {
2019-05-17 08:29:48 +00:00
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
2019-05-18 13:56:56 +00:00
client->printf("Client: %u", client->id());
2019-05-17 08:29:48 +00:00
client->ping();
2019-05-17 16:31:54 +00:00
} else if (type == WS_EVT_DISCONNECT) {
2019-05-17 08:29:48 +00:00
Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id());
2019-05-19 14:11:15 +00:00
for (int i = 0; i < 4; i++) {
if (lampSockets[i] == client->id()) {
lampSockets[i] = -1;
break;
}
}
2019-05-17 16:31:54 +00:00
} else if (type == WS_EVT_ERROR) {
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t *)arg), (char *)data);
} else if (type == WS_EVT_PONG) {
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len) ? (char *)data : "");
} else if (type == WS_EVT_DATA) {
AwsFrameInfo *info = (AwsFrameInfo *)arg;
2019-05-17 08:29:48 +00:00
String msg = "";
2019-05-17 16:31:54 +00:00
if (info->final && info->index == 0 && info->len == len) {
// the whole message is in a single frame and we got all of it's data
Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT) ? "text" : "binary", info->len);
2019-05-17 08:29:48 +00:00
2019-05-17 16:31:54 +00:00
if (info->opcode == WS_TEXT) {
for (size_t i = 0; i < info->len; i++) {
msg += (char)data[i];
2019-05-17 08:29:48 +00:00
}
2019-05-18 13:56:56 +00:00
Serial.print("message: ");
Serial.println(msg);
}
2019-05-19 14:11:15 +00:00
if (msg == "conlamp:0") {
Serial.println("Lamp0 connected!");
lampSockets[0] = client->id();
} else if (msg == "conlamp:1") {
Serial.println("Lamp1 connected!");
lampSockets[1] = client->id();
} else if (msg == "conlamp:2") {
Serial.println("Lamp2 connected!");
lampSockets[2] = client->id();
} else if (msg == "conlamp:3") {
Serial.println("Lamp3 connected!");
lampSockets[3] = client->id();
2019-05-17 08:29:48 +00:00
}
2019-05-17 16:31:54 +00:00
2019-05-17 08:29:48 +00:00
} else {
2019-05-17 16:31:54 +00:00
Serial.printf("Error: Message in multiple Frames");
2019-05-17 08:29:48 +00:00
}
}
2019-05-18 13:56:56 +00:00
Serial.println();
2019-05-17 08:29:48 +00:00
}
2019-05-13 09:51:11 +00:00
void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); }
void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_ETH_START:
Serial.println("ETH Started");
2019-05-17 08:29:48 +00:00
ETH.setHostname("a2clcontroller");
2019-05-13 09:51:11 +00:00
break;
case SYSTEM_EVENT_ETH_CONNECTED:
Serial.println("ETH Connected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.print("ETH MAC: ");
Serial.print(ETH.macAddress());
Serial.print(", IPv4: ");
Serial.print(ETH.localIP());
if (ETH.fullDuplex()) {
Serial.print(", FULL_DUPLEX");
}
Serial.print(", ");
Serial.print(ETH.linkSpeed());
Serial.println("Mbps");
eth_connected = true;
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
break;
case SYSTEM_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
eth_connected = false;
break;
default:
break;
}
}
2019-05-13 08:00:06 +00:00
void setup() {
2019-05-13 09:51:11 +00:00
Serial.begin(115200);
WiFi.onEvent(WiFiEvent);
ETH.begin();
2019-05-19 13:21:36 +00:00
pinMode(PIN_ACTIVATE, INPUT_PULLUP);
pinMode(PIN_CLEAR, INPUT_PULLUP);
2019-05-18 13:56:56 +00:00
2019-05-13 09:51:11 +00:00
while (!eth_connected) {
delay(1000);
}
if (!MDNS.begin("a2clcontroller")) {
Serial.println("Error setting up MDNS responder!, system halt");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
2019-05-17 08:29:48 +00:00
ws.onEvent(onWsEvent);
2019-05-17 16:31:54 +00:00
server.addHandler(&ws);
2019-05-19 14:11:15 +00:00
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
// Print a small status response page with the most important messages
String statusHTML = "";
2019-05-19 15:35:40 +00:00
statusHTML += "<html><h3>Audio2 Cabin Light System</h3><br>Current Status: <br><table border='1'>";
statusHTML += "<tr><td>Lamp</td><td>Status</td></tr>";
2019-05-19 14:11:15 +00:00
for (int i = 0; i < 4; i++) {
statusHTML += "<tr><td>Lamp ";
statusHTML += String(i);
statusHTML += "</td><td>";
statusHTML += lampSockets[i] != -1 ? String("<p style='color:green;'>Connected</p>") : String("<p style='color:red;'>Disconnected</p>");
statusHTML += "</td></tr>";
}
statusHTML += "</table><br>";
statusHTML += "Last Sent Status: <b>" + lastSent + "</b><br>";
statusHTML += "Input active: <b>";
statusHTML += inputActive ? "inactive (high)" : "active (low)";
statusHTML += "</b><br>";
statusHTML += "Input clear: <b>";
statusHTML += clearActive ? "inactive (high)" : "active (low)";
2019-05-19 15:35:40 +00:00
statusHTML += "<br>Timeout: <b>";
statusHTML += (activateTimeout == 0 || millis() - activateTimeout >= TRIGGER_TIMEOUT) ? "active" : "inactive";
statusHTML += "</b><br></html>";
2019-05-19 14:11:15 +00:00
2019-05-19 15:35:40 +00:00
request->send(200, "text/html", statusHTML);
2019-05-19 14:11:15 +00:00
});
2019-05-17 16:31:54 +00:00
2019-05-19 13:25:36 +00:00
server.on("/allblink", HTTP_GET, [](AsyncWebServerRequest *request) {
2019-05-17 16:31:54 +00:00
ws.textAll("blinkLamp");
2019-05-19 14:11:15 +00:00
lastSent = "BLINKING";
2019-05-17 16:31:54 +00:00
request->send(200, "text/plain", "Alarming All");
});
2019-05-19 13:25:36 +00:00
server.on("/allnoblink", HTTP_GET, [](AsyncWebServerRequest *request) {
2019-05-17 16:31:54 +00:00
ws.textAll("noBlinkLamp");
2019-05-19 14:11:15 +00:00
lastSent = "off";
2019-05-17 16:31:54 +00:00
request->send(200, "text/plain", "Alarm stopped");
});
2019-05-19 13:25:36 +00:00
server.on("/allon", HTTP_GET, [](AsyncWebServerRequest *request) {
ws.textAll("lampon");
2019-05-19 14:11:15 +00:00
lastSent = "ON";
2019-05-19 13:25:36 +00:00
request->send(200, "text/plain", "Alarming All");
});
server.on("/alloff", HTTP_GET, [](AsyncWebServerRequest *request) {
ws.textAll("lampoff");
2019-05-19 14:11:15 +00:00
lastSent = "off";
2019-05-19 13:25:36 +00:00
request->send(200, "text/plain", "Alarm stopped");
});
2019-05-13 09:51:11 +00:00
server.onNotFound(notFound);
server.begin();
2019-05-17 08:29:48 +00:00
2019-05-13 09:51:11 +00:00
MDNS.addService("http", "tcp", 80);
2019-05-13 08:00:06 +00:00
}
2019-05-18 13:56:56 +00:00
void loop() {
2019-05-19 13:21:36 +00:00
if (digitalRead(PIN_ACTIVATE) != inputActive) {
2019-05-18 13:56:56 +00:00
delay(10);
2019-05-19 13:21:36 +00:00
if (digitalRead(PIN_ACTIVATE) != inputActive) {
inputActive = digitalRead(PIN_ACTIVATE);
2019-05-19 15:35:40 +00:00
if (!inputActive) { // React when the signal starts
if (activateTimeout == 0 || millis() - activateTimeout >= TRIGGER_TIMEOUT) { // 1 Minute timeout
2019-05-19 13:21:36 +00:00
Serial.println("Activate triggered");
2019-05-19 13:25:36 +00:00
ws.textAll("lampon");
2019-05-19 14:11:15 +00:00
lastSent = "ON";
2019-05-19 15:35:40 +00:00
} else {
Serial.println("Wait 1 minute to retrigger lights");
2019-05-19 13:21:36 +00:00
}
}
}
}
if (digitalRead(PIN_CLEAR) != clearActive) {
delay(10);
if (digitalRead(PIN_CLEAR) != clearActive) {
clearActive = digitalRead(PIN_CLEAR);
if (clearActive) { // React when the signal is over
Serial.println("Clear triggered");
2019-05-19 13:25:36 +00:00
ws.textAll("lampoff");
2019-05-19 14:11:15 +00:00
lastSent = "off";
2019-05-19 13:21:36 +00:00
activateTimeout = millis();
2019-05-18 13:56:56 +00:00
}
}
}
}
2019-05-17 08:29:48 +00:00
///////////////////////////