diff --git a/platformio.ini b/platformio.ini index 7d87d7b..24bd3ed 100644 --- a/platformio.ini +++ b/platformio.ini @@ -34,24 +34,24 @@ src_filter = + lib_deps = WebSockets - build_flags = -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG +;build_flags = -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -;build_flags = -DROLE=LIGHT1 +build_flags = -DLIGHT_NUMBER="0" [env:light2] src_filter = + lib_deps = WebSockets -build_flags = -DROLE=LIGHT2 +build_flags = -DLIGHT_NUMBER="1" [env:light3] src_filter = + lib_deps = WebSockets -build_flags = -DROLE=LIGHT3 +build_flags = -DLIGHT_NUMBER="2" [env:light4] src_filter = + lib_deps = WebSockets -build_flags = -DROLE=LIGHT4 +build_flags = -DLIGHT_NUMBER="3" diff --git a/src/controller_main.cpp b/src/controller_main.cpp index 48ae83d..85df97f 100644 --- a/src/controller_main.cpp +++ b/src/controller_main.cpp @@ -14,12 +14,13 @@ bool inputActive = true; bool clearActive = true; long activateTimeout = 0; +int lampSockets[4] = {-1, -1, -1, -1}; + static bool eth_connected = false; AsyncWebServer server(80); AsyncWebSocket ws("/ws"); - -const char *PARAM_MESSAGE = "message"; +String lastSent = "---"; void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { if (type == WS_EVT_CONNECT) { @@ -28,6 +29,12 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp client->ping(); } else if (type == WS_EVT_DISCONNECT) { Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id()); + for (int i = 0; i < 4; i++) { + if (lampSockets[i] == client->id()) { + lampSockets[i] = -1; + break; + } + } } 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) { @@ -40,16 +47,25 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT) ? "text" : "binary", info->len); if (info->opcode == WS_TEXT) { - for (size_t i = 0; i < info->len; i++) { msg += (char)data[i]; } Serial.print("message: "); Serial.println(msg); } - if (info->opcode == WS_TEXT) { - // Responding before the first heartbeat is through will lead to disconnects - // client->text("I got your text message"); + + 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(); } } else { @@ -65,7 +81,6 @@ void WiFiEvent(WiFiEvent_t event) { switch (event) { case SYSTEM_EVENT_ETH_START: Serial.println("ETH Started"); - // set eth hostname here ETH.setHostname("a2clcontroller"); break; case SYSTEM_EVENT_ETH_CONNECTED: @@ -120,32 +135,59 @@ void setup() { ws.onEvent(onWsEvent); server.addHandler(&ws); - server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { request->send(200, "text/plain", "Current Status: To Be done"); }); + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { + // Print a small status response page with the most important messages + String statusHTML = ""; + statusHTML += "

Audio2 Cabin Light System


Current Status:
"; + statusHTML += ""; + } + statusHTML += "
LampStatus"; + + for (int i = 0; i < 4; i++) { + statusHTML += "
Lamp "; + statusHTML += String(i); + statusHTML += ""; + statusHTML += lampSockets[i] != -1 ? String("

Connected

") : String("

Disconnected

"); + statusHTML += "

"; + + statusHTML += "Last Sent Status: " + lastSent + "
"; + statusHTML += "Input active: "; + statusHTML += inputActive ? "inactive (high)" : "active (low)"; + statusHTML += "
"; + statusHTML += "Input clear: "; + statusHTML += clearActive ? "inactive (high)" : "active (low)"; + statusHTML += "
"; + + request->send(200, "text/plain", statusHTML); + }); server.on("/allblink", HTTP_GET, [](AsyncWebServerRequest *request) { ws.textAll("blinkLamp"); + lastSent = "BLINKING"; request->send(200, "text/plain", "Alarming All"); }); server.on("/allnoblink", HTTP_GET, [](AsyncWebServerRequest *request) { ws.textAll("noBlinkLamp"); + lastSent = "off"; request->send(200, "text/plain", "Alarm stopped"); }); server.on("/allon", HTTP_GET, [](AsyncWebServerRequest *request) { ws.textAll("lampon"); + lastSent = "ON"; request->send(200, "text/plain", "Alarming All"); }); server.on("/alloff", HTTP_GET, [](AsyncWebServerRequest *request) { ws.textAll("lampoff"); + lastSent = "off"; request->send(200, "text/plain", "Alarm stopped"); }); server.onNotFound(notFound); server.begin(); - // Add service to MDNS-SD MDNS.addService("http", "tcp", 80); } @@ -160,6 +202,7 @@ void loop() { if (millis() - activateTimeout >= 60000) { // 1 Minute timeout Serial.println("Activate triggered"); ws.textAll("lampon"); + lastSent = "ON"; } } } @@ -173,6 +216,7 @@ void loop() { if (clearActive) { // React when the signal is over Serial.println("Clear triggered"); ws.textAll("lampoff"); + lastSent = "off"; activateTimeout = millis(); } } diff --git a/src/light_main.cpp b/src/light_main.cpp index b2095e9..7318f12 100644 --- a/src/light_main.cpp +++ b/src/light_main.cpp @@ -10,6 +10,10 @@ #define blinkDuration 500 #define connectDuration 1000 +#ifndef LIGHT_NUMBER +#define LIGHT_NUMBER -1 +#endif + #define lampOutputPin 5 bool isBlinking = false; bool lampState = false; @@ -18,14 +22,12 @@ long blinkTimer = 0; long connectTimer = 0; bool foundIp = false; -IPAddress ip; // = IPAddress(192, 168, 0, 111); // FIXME: +IPAddress ip; WebSocketsClient webSocket; static bool eth_connected = false; -const char *PARAM_MESSAGE = "message"; - void WiFiEvent(WiFiEvent_t event) { switch (event) { case SYSTEM_EVENT_ETH_START: @@ -89,7 +91,7 @@ void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) { break; case WStype_CONNECTED: { Serial.print("[WSc] Connected to controller"); - webSocket.sendTXT("event: con lamp: 1"); + webSocket.sendTXT("conlamp:" + String(LIGHT_NUMBER)); foundIp = true; } break; case WStype_TEXT: