2018-11-07 04:07:11 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "BTCWebServer.h"
|
|
|
|
#include "DebugPort.h"
|
|
|
|
#include "TxManage.h"
|
2018-11-26 09:05:05 +00:00
|
|
|
#include "helpers.h"
|
|
|
|
#include "pins.h"
|
|
|
|
|
|
|
|
extern void Command_Interpret(const char* pLine); // decodes received command lines, implemented in main .ino file!
|
2018-11-07 04:07:11 +00:00
|
|
|
|
|
|
|
WebServer server(80);
|
2018-11-24 17:04:37 +00:00
|
|
|
WebSocketsServer webSocket = WebSocketsServer(81);
|
2018-11-07 04:07:11 +00:00
|
|
|
|
|
|
|
const int led = 13;
|
|
|
|
|
|
|
|
void handleRoot() {
|
2018-11-24 17:04:37 +00:00
|
|
|
String s = MAIN_PAGE; //Read HTML contents
|
|
|
|
server.send(200, "text/html", s); //Send web page
|
2018-11-07 04:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleNotFound() {
|
|
|
|
digitalWrite(led, 1);
|
|
|
|
String message = "File Not Found\n\n";
|
|
|
|
message += "URI: ";
|
|
|
|
message += server.uri();
|
|
|
|
message += "\nMethod: ";
|
|
|
|
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
|
|
|
message += "\nArguments: ";
|
|
|
|
message += server.args();
|
|
|
|
message += "\n";
|
|
|
|
for (uint8_t i = 0; i < server.args(); i++) {
|
|
|
|
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
|
|
|
}
|
|
|
|
server.send(404, "text/plain", message);
|
|
|
|
digitalWrite(led, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void initWebServer(void) {
|
|
|
|
|
|
|
|
if (MDNS.begin("BTCHeater")) {
|
|
|
|
DebugPort.println("MDNS responder started");
|
|
|
|
}
|
2018-11-24 17:04:37 +00:00
|
|
|
|
2018-11-07 04:07:11 +00:00
|
|
|
server.on("/", handleRoot);
|
|
|
|
server.onNotFound(handleNotFound);
|
|
|
|
|
|
|
|
server.begin();
|
2018-11-24 17:04:37 +00:00
|
|
|
webSocket.begin();
|
|
|
|
webSocket.onEvent(webSocketEvent);
|
2018-11-07 04:07:11 +00:00
|
|
|
DebugPort.println("HTTP server started");
|
|
|
|
}
|
2018-11-24 17:04:37 +00:00
|
|
|
unsigned char cVal;
|
2018-11-07 04:07:11 +00:00
|
|
|
|
|
|
|
void doWebServer(void) {
|
2018-11-26 09:05:05 +00:00
|
|
|
static unsigned long lastTx = 0;
|
2018-11-24 17:04:37 +00:00
|
|
|
webSocket.loop();
|
2018-11-07 04:07:11 +00:00
|
|
|
server.handleClient();
|
2018-11-26 09:05:05 +00:00
|
|
|
if(millis() > lastTx) { // moderate the delivery of new messages - we simply cannot send every pass of the main loop!
|
|
|
|
lastTx = millis() + 1000;
|
|
|
|
char msg[16];
|
|
|
|
sprintf(msg, "%.1f", fFilteredTemperature);
|
|
|
|
webSocket.broadcastTXT(msg);
|
|
|
|
// char c[] = { "23" };
|
|
|
|
// webSocket.broadcastTXT(c, sizeof(c));
|
|
|
|
}
|
2018-11-07 04:07:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-26 09:05:05 +00:00
|
|
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
2018-11-24 17:04:37 +00:00
|
|
|
if (type == WStype_TEXT) {
|
2018-11-26 09:05:05 +00:00
|
|
|
char cmd[16];
|
|
|
|
memset(cmd, 0, 16);
|
|
|
|
for (int i = 0; i < length && i < 15; i++) {
|
|
|
|
cmd[i] = payload[i];
|
|
|
|
// Serial.print((char)payload[i]);
|
2018-11-24 17:04:37 +00:00
|
|
|
}
|
2018-11-26 09:05:05 +00:00
|
|
|
// Serial.println();
|
|
|
|
Serial.println(cmd);
|
|
|
|
Command_Interpret(cmd); // send to the main heater controller decode routine
|
|
|
|
}
|
|
|
|
}
|