a2cabinLights/src/controller_main.cpp

157 lines
4.2 KiB
C++

#include <Arduino.h>
#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>
bool input1active = true;
static bool eth_connected = false;
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
const char *PARAM_MESSAGE = "message";
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
if (type == WS_EVT_CONNECT) {
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
client->printf("Client: %u", client->id());
client->ping();
} else if (type == WS_EVT_DISCONNECT) {
Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id());
} 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;
String msg = "";
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);
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");
}
} else {
Serial.printf("Error: Message in multiple Frames");
}
}
Serial.println();
}
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");
// set eth hostname here
ETH.setHostname("a2clcontroller");
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;
}
}
void setup() {
Serial.begin(115200);
WiFi.onEvent(WiFiEvent);
ETH.begin();
pinMode(5, INPUT_PULLUP);
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");
ws.onEvent(onWsEvent);
server.addHandler(&ws);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { request->send(200, "text/plain", "Current Status: To Be done"); });
server.on("/allon", HTTP_GET, [](AsyncWebServerRequest *request) {
ws.textAll("blinkLamp");
request->send(200, "text/plain", "Alarming All");
});
server.on("/alloff", HTTP_GET, [](AsyncWebServerRequest *request) {
ws.textAll("noBlinkLamp");
request->send(200, "text/plain", "Alarm stopped");
});
server.onNotFound(notFound);
server.begin();
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
void loop() {
if (digitalRead(5) != input1active) {
delay(10);
if (digitalRead(5) != input1active) {
input1active = digitalRead(5);
if (!input1active) {
Serial.println("Turn on");
ws.textAll("blinkLamp");
} else {
Serial.println("Turn off");
ws.textAll("noBlinkLamp");
}
}
}
delay(100);
}
///////////////////////////