Adds status response
This commit is contained in:
parent
cda706fdc8
commit
dc037af217
@ -34,24 +34,24 @@ src_filter = +<light_main.cpp>
|
|||||||
lib_deps =
|
lib_deps =
|
||||||
WebSockets
|
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]
|
[env:light2]
|
||||||
src_filter = +<light_main.cpp>
|
src_filter = +<light_main.cpp>
|
||||||
lib_deps =
|
lib_deps =
|
||||||
WebSockets
|
WebSockets
|
||||||
build_flags = -DROLE=LIGHT2
|
build_flags = -DLIGHT_NUMBER="1"
|
||||||
|
|
||||||
[env:light3]
|
[env:light3]
|
||||||
src_filter = +<light_main.cpp>
|
src_filter = +<light_main.cpp>
|
||||||
lib_deps =
|
lib_deps =
|
||||||
WebSockets
|
WebSockets
|
||||||
build_flags = -DROLE=LIGHT3
|
build_flags = -DLIGHT_NUMBER="2"
|
||||||
|
|
||||||
[env:light4]
|
[env:light4]
|
||||||
src_filter = +<light_main.cpp>
|
src_filter = +<light_main.cpp>
|
||||||
lib_deps =
|
lib_deps =
|
||||||
WebSockets
|
WebSockets
|
||||||
build_flags = -DROLE=LIGHT4
|
build_flags = -DLIGHT_NUMBER="3"
|
||||||
|
@ -14,12 +14,13 @@ bool inputActive = true;
|
|||||||
bool clearActive = true;
|
bool clearActive = true;
|
||||||
long activateTimeout = 0;
|
long activateTimeout = 0;
|
||||||
|
|
||||||
|
int lampSockets[4] = {-1, -1, -1, -1};
|
||||||
|
|
||||||
static bool eth_connected = false;
|
static bool eth_connected = false;
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
AsyncWebSocket ws("/ws");
|
AsyncWebSocket ws("/ws");
|
||||||
|
String lastSent = "---";
|
||||||
const char *PARAM_MESSAGE = "message";
|
|
||||||
|
|
||||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
||||||
if (type == WS_EVT_CONNECT) {
|
if (type == WS_EVT_CONNECT) {
|
||||||
@ -28,6 +29,12 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
|
|||||||
client->ping();
|
client->ping();
|
||||||
} else if (type == WS_EVT_DISCONNECT) {
|
} else if (type == WS_EVT_DISCONNECT) {
|
||||||
Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id());
|
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) {
|
} else if (type == WS_EVT_ERROR) {
|
||||||
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t *)arg), (char *)data);
|
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t *)arg), (char *)data);
|
||||||
} else if (type == WS_EVT_PONG) {
|
} 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);
|
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) {
|
if (info->opcode == WS_TEXT) {
|
||||||
|
|
||||||
for (size_t i = 0; i < info->len; i++) {
|
for (size_t i = 0; i < info->len; i++) {
|
||||||
msg += (char)data[i];
|
msg += (char)data[i];
|
||||||
}
|
}
|
||||||
Serial.print("message: ");
|
Serial.print("message: ");
|
||||||
Serial.println(msg);
|
Serial.println(msg);
|
||||||
}
|
}
|
||||||
if (info->opcode == WS_TEXT) {
|
|
||||||
// Responding before the first heartbeat is through will lead to disconnects
|
if (msg == "conlamp:0") {
|
||||||
// client->text("I got your text message");
|
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 {
|
} else {
|
||||||
@ -65,7 +81,6 @@ void WiFiEvent(WiFiEvent_t event) {
|
|||||||
switch (event) {
|
switch (event) {
|
||||||
case SYSTEM_EVENT_ETH_START:
|
case SYSTEM_EVENT_ETH_START:
|
||||||
Serial.println("ETH Started");
|
Serial.println("ETH Started");
|
||||||
// set eth hostname here
|
|
||||||
ETH.setHostname("a2clcontroller");
|
ETH.setHostname("a2clcontroller");
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_ETH_CONNECTED:
|
case SYSTEM_EVENT_ETH_CONNECTED:
|
||||||
@ -120,32 +135,59 @@ void setup() {
|
|||||||
ws.onEvent(onWsEvent);
|
ws.onEvent(onWsEvent);
|
||||||
server.addHandler(&ws);
|
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 += "<h3>Audio2 Cabin Light System</h3><br>Current Status: <br><table>";
|
||||||
|
statusHTML += "<tr><td>Lamp</td><td>Status</td><td>";
|
||||||
|
|
||||||
|
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)";
|
||||||
|
statusHTML += "</b><br>";
|
||||||
|
|
||||||
|
request->send(200, "text/plain", statusHTML);
|
||||||
|
});
|
||||||
|
|
||||||
server.on("/allblink", HTTP_GET, [](AsyncWebServerRequest *request) {
|
server.on("/allblink", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||||
ws.textAll("blinkLamp");
|
ws.textAll("blinkLamp");
|
||||||
|
lastSent = "BLINKING";
|
||||||
request->send(200, "text/plain", "Alarming All");
|
request->send(200, "text/plain", "Alarming All");
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("/allnoblink", HTTP_GET, [](AsyncWebServerRequest *request) {
|
server.on("/allnoblink", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||||
ws.textAll("noBlinkLamp");
|
ws.textAll("noBlinkLamp");
|
||||||
|
lastSent = "off";
|
||||||
request->send(200, "text/plain", "Alarm stopped");
|
request->send(200, "text/plain", "Alarm stopped");
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("/allon", HTTP_GET, [](AsyncWebServerRequest *request) {
|
server.on("/allon", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||||
ws.textAll("lampon");
|
ws.textAll("lampon");
|
||||||
|
lastSent = "ON";
|
||||||
request->send(200, "text/plain", "Alarming All");
|
request->send(200, "text/plain", "Alarming All");
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("/alloff", HTTP_GET, [](AsyncWebServerRequest *request) {
|
server.on("/alloff", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||||
ws.textAll("lampoff");
|
ws.textAll("lampoff");
|
||||||
|
lastSent = "off";
|
||||||
request->send(200, "text/plain", "Alarm stopped");
|
request->send(200, "text/plain", "Alarm stopped");
|
||||||
});
|
});
|
||||||
|
|
||||||
server.onNotFound(notFound);
|
server.onNotFound(notFound);
|
||||||
server.begin();
|
server.begin();
|
||||||
|
|
||||||
// Add service to MDNS-SD
|
|
||||||
MDNS.addService("http", "tcp", 80);
|
MDNS.addService("http", "tcp", 80);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,6 +202,7 @@ void loop() {
|
|||||||
if (millis() - activateTimeout >= 60000) { // 1 Minute timeout
|
if (millis() - activateTimeout >= 60000) { // 1 Minute timeout
|
||||||
Serial.println("Activate triggered");
|
Serial.println("Activate triggered");
|
||||||
ws.textAll("lampon");
|
ws.textAll("lampon");
|
||||||
|
lastSent = "ON";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -173,6 +216,7 @@ void loop() {
|
|||||||
if (clearActive) { // React when the signal is over
|
if (clearActive) { // React when the signal is over
|
||||||
Serial.println("Clear triggered");
|
Serial.println("Clear triggered");
|
||||||
ws.textAll("lampoff");
|
ws.textAll("lampoff");
|
||||||
|
lastSent = "off";
|
||||||
activateTimeout = millis();
|
activateTimeout = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,10 @@
|
|||||||
#define blinkDuration 500
|
#define blinkDuration 500
|
||||||
#define connectDuration 1000
|
#define connectDuration 1000
|
||||||
|
|
||||||
|
#ifndef LIGHT_NUMBER
|
||||||
|
#define LIGHT_NUMBER -1
|
||||||
|
#endif
|
||||||
|
|
||||||
#define lampOutputPin 5
|
#define lampOutputPin 5
|
||||||
bool isBlinking = false;
|
bool isBlinking = false;
|
||||||
bool lampState = false;
|
bool lampState = false;
|
||||||
@ -18,14 +22,12 @@ long blinkTimer = 0;
|
|||||||
long connectTimer = 0;
|
long connectTimer = 0;
|
||||||
|
|
||||||
bool foundIp = false;
|
bool foundIp = false;
|
||||||
IPAddress ip; // = IPAddress(192, 168, 0, 111); // FIXME:
|
IPAddress ip;
|
||||||
|
|
||||||
WebSocketsClient webSocket;
|
WebSocketsClient webSocket;
|
||||||
|
|
||||||
static bool eth_connected = false;
|
static bool eth_connected = false;
|
||||||
|
|
||||||
const char *PARAM_MESSAGE = "message";
|
|
||||||
|
|
||||||
void WiFiEvent(WiFiEvent_t event) {
|
void WiFiEvent(WiFiEvent_t event) {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case SYSTEM_EVENT_ETH_START:
|
case SYSTEM_EVENT_ETH_START:
|
||||||
@ -89,7 +91,7 @@ void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) {
|
|||||||
break;
|
break;
|
||||||
case WStype_CONNECTED: {
|
case WStype_CONNECTED: {
|
||||||
Serial.print("[WSc] Connected to controller");
|
Serial.print("[WSc] Connected to controller");
|
||||||
webSocket.sendTXT("event: con lamp: 1");
|
webSocket.sendTXT("conlamp:" + String(LIGHT_NUMBER));
|
||||||
foundIp = true;
|
foundIp = true;
|
||||||
} break;
|
} break;
|
||||||
case WStype_TEXT:
|
case WStype_TEXT:
|
||||||
|
Loading…
Reference in New Issue
Block a user