2018-11-26 11:58:15 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Ray Jones <ray@mrjones.id.au>
|
|
|
|
* Copyright (C) 2018 James Clark
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-11-07 04:07:11 +00:00
|
|
|
|
|
|
|
#include "BTCWebServer.h"
|
|
|
|
#include "DebugPort.h"
|
|
|
|
#include "TxManage.h"
|
2018-11-26 10:26:38 +00:00
|
|
|
#include "helpers.h"
|
|
|
|
#include "pins.h"
|
|
|
|
#include "Index.h"
|
2018-12-09 19:28:02 +00:00
|
|
|
#include <ArduinoJson.h>
|
2018-12-11 19:42:52 +00:00
|
|
|
#include <map>
|
2018-11-26 10:26:38 +00:00
|
|
|
|
2018-11-07 04:07:11 +00:00
|
|
|
|
|
|
|
WebServer server(80);
|
2018-11-26 10:26:38 +00:00
|
|
|
WebSocketsServer webSocket = WebSocketsServer(81);
|
2018-12-01 18:25:10 +00:00
|
|
|
bool bRxWebData = false;
|
|
|
|
bool bTxWebData = false;
|
2018-11-07 04:07:11 +00:00
|
|
|
|
2018-12-09 19:28:02 +00:00
|
|
|
DynamicJsonBuffer jsonBuffer(512); // create a JSON buffer on the heap
|
|
|
|
|
2018-12-11 19:42:52 +00:00
|
|
|
class CModerator {
|
|
|
|
std::map<const char*, float> fMemory;
|
|
|
|
std::map<const char*, int> iMemory;
|
|
|
|
public:
|
|
|
|
bool check(const char* name, float value);
|
|
|
|
bool check(const char* name, int value);
|
|
|
|
void reset();
|
2018-12-11 11:25:32 +00:00
|
|
|
};
|
|
|
|
|
2018-12-11 19:42:52 +00:00
|
|
|
void
|
|
|
|
CModerator::reset()
|
|
|
|
{
|
|
|
|
// install invalid values, retain maps (memory defrag reasons)
|
|
|
|
for(auto it = fMemory.begin(); it != fMemory.end(); ++it) it->second = -100;
|
|
|
|
for(auto it = iMemory.begin(); it != iMemory.end(); ++it) it->second = -100;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CModerator::check(const char* name, float value)
|
|
|
|
{
|
|
|
|
bool retval = true;
|
|
|
|
auto it = fMemory.find(name);
|
|
|
|
if(it != fMemory.end()) {
|
|
|
|
retval = it->second != value;
|
|
|
|
it->second = value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fMemory[name] = value;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CModerator::check(const char* name, int value)
|
|
|
|
{
|
|
|
|
bool retval = true;
|
|
|
|
auto it = iMemory.find(name);
|
|
|
|
if(it != iMemory.end()) {
|
|
|
|
retval = it->second != value;
|
|
|
|
it->second = value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
iMemory[name] = value;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
CModerator Moderator;
|
2018-12-11 11:25:32 +00:00
|
|
|
|
2018-11-07 04:07:11 +00:00
|
|
|
const int led = 13;
|
|
|
|
|
|
|
|
void handleRoot() {
|
2018-11-26 10:26:38 +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) {
|
2018-12-09 19:28:02 +00:00
|
|
|
|
2018-11-07 04:07:11 +00:00
|
|
|
|
|
|
|
if (MDNS.begin("BTCHeater")) {
|
|
|
|
DebugPort.println("MDNS responder started");
|
|
|
|
}
|
2018-11-26 10:26:38 +00:00
|
|
|
|
2018-11-07 04:07:11 +00:00
|
|
|
server.on("/", handleRoot);
|
|
|
|
server.onNotFound(handleNotFound);
|
|
|
|
|
|
|
|
server.begin();
|
2018-11-26 10:26:38 +00:00
|
|
|
webSocket.begin();
|
|
|
|
webSocket.onEvent(webSocketEvent);
|
2018-11-07 04:07:11 +00:00
|
|
|
DebugPort.println("HTTP server started");
|
2018-12-09 19:28:02 +00:00
|
|
|
|
2018-11-07 04:07:11 +00:00
|
|
|
}
|
2018-11-24 17:04:37 +00:00
|
|
|
unsigned char cVal;
|
2018-11-07 04:07:11 +00:00
|
|
|
|
2018-12-01 18:25:10 +00:00
|
|
|
bool doWebServer(void) {
|
2018-11-26 10:26:38 +00:00
|
|
|
static unsigned long lastTx = 0;
|
2018-12-11 11:25:32 +00:00
|
|
|
static int prevNumClients;
|
2018-11-26 10:26:38 +00:00
|
|
|
webSocket.loop();
|
2018-11-07 04:07:11 +00:00
|
|
|
server.handleClient();
|
2018-12-11 11:25:32 +00:00
|
|
|
|
2018-12-01 18:25:10 +00:00
|
|
|
int numClients = webSocket.connectedClients();
|
2018-12-11 11:25:32 +00:00
|
|
|
if(numClients != prevNumClients) {
|
|
|
|
prevNumClients = numClients;
|
2018-12-11 19:42:52 +00:00
|
|
|
Moderator.reset(); // force full update of params if number of clients change
|
2018-12-11 11:25:32 +00:00
|
|
|
DebugPort.println("Changed number of web clients, resetting history");
|
|
|
|
}
|
|
|
|
|
2018-12-01 18:25:10 +00:00
|
|
|
if(numClients) {
|
|
|
|
if(millis() > lastTx) { // moderate the delivery of new messages - we simply cannot send every pass of the main loop!
|
2018-12-11 11:31:58 +00:00
|
|
|
lastTx = millis() + 100;
|
2018-12-11 11:37:18 +00:00
|
|
|
bool bSend = false;
|
2018-12-09 19:28:02 +00:00
|
|
|
|
|
|
|
JsonObject& root = jsonBuffer.createObject();
|
2018-12-11 11:25:32 +00:00
|
|
|
float tidyTemp = int(getActualTemperature() * 10) * 0.1f; // round to 0.1 resolution (hopefully!)
|
2018-12-11 19:42:52 +00:00
|
|
|
if(Moderator.check("CurrentTemp", tidyTemp)) {
|
2018-12-11 11:25:32 +00:00
|
|
|
root.set("CurrentTemp", tidyTemp);
|
2018-12-11 11:37:18 +00:00
|
|
|
bSend = true;
|
2018-12-11 11:25:32 +00:00
|
|
|
}
|
2018-12-11 19:42:52 +00:00
|
|
|
if(Moderator.check("RunState", getHeaterInfo().getRunState())) {
|
2018-12-11 11:25:32 +00:00
|
|
|
root.set("RunState", getHeaterInfo().getRunState());
|
2018-12-11 11:37:18 +00:00
|
|
|
bSend = true;
|
2018-12-11 11:25:32 +00:00
|
|
|
}
|
2018-12-11 19:42:52 +00:00
|
|
|
if(Moderator.check("DesiredTemp", getHeaterInfo().getTemperature_Desired())) {
|
2018-12-11 11:25:32 +00:00
|
|
|
root.set("DesiredTemp", getHeaterInfo().getTemperature_Desired());
|
2018-12-11 11:37:18 +00:00
|
|
|
bSend = true;
|
2018-12-11 11:25:32 +00:00
|
|
|
}
|
2018-12-09 19:28:02 +00:00
|
|
|
|
2018-12-11 11:37:18 +00:00
|
|
|
if(bSend) {
|
|
|
|
bTxWebData = true;
|
2018-12-11 11:31:58 +00:00
|
|
|
String jsonToSend;
|
|
|
|
root.printTo(jsonToSend);
|
|
|
|
webSocket.broadcastTXT(jsonToSend);
|
|
|
|
}
|
2018-12-01 18:25:10 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-11-07 04:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:26:38 +00:00
|
|
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
|
|
|
if (type == WStype_TEXT) {
|
2018-12-11 10:19:02 +00:00
|
|
|
bRxWebData = true;
|
|
|
|
char cmd[256];
|
|
|
|
memset(cmd, 0, 256);
|
|
|
|
for (int i = 0; i < length && i < 256; i++) {
|
2018-11-26 10:26:38 +00:00
|
|
|
cmd[i] = payload[i];
|
|
|
|
}
|
2018-12-11 10:19:02 +00:00
|
|
|
// DebugPort.println(cmd);
|
|
|
|
interpretJsonCommand(cmd); // send to the main heater controller decode routine
|
2018-12-01 18:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasWebClientSpoken(bool reset)
|
|
|
|
{
|
|
|
|
bool retval = bRxWebData;
|
|
|
|
if(reset)
|
|
|
|
bRxWebData = false;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasWebServerSpoken(bool reset)
|
|
|
|
{
|
|
|
|
bool retval = bTxWebData;
|
|
|
|
if(reset)
|
|
|
|
bTxWebData = false;
|
|
|
|
return retval;
|
2018-11-26 10:26:38 +00:00
|
|
|
}
|
2018-12-11 10:19:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
void interpretJsonCommand(char* pLine)
|
|
|
|
{
|
|
|
|
if(strlen(pLine) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DebugPort.print("JSON parse... "); Serial.print(pLine);
|
|
|
|
|
|
|
|
JsonObject& obj = jsonBuffer.parseObject(pLine);
|
|
|
|
if(!obj.success()) {
|
|
|
|
DebugPort.println(" FAILED");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DebugPort.println(" OK");
|
|
|
|
|
|
|
|
JsonObject::iterator it;
|
|
|
|
for(it = obj.begin(); it != obj.end(); ++it) {
|
|
|
|
|
|
|
|
if(strcmp("DesiredTemp", it->key) == 0) {
|
|
|
|
reqTemp(it->value.as<unsigned char>());
|
|
|
|
}
|
|
|
|
else if(strcmp("RunState", it->key) == 0) {
|
|
|
|
if(it->value.as<unsigned char>()) {
|
|
|
|
requestOn();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
requestOff();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(strcmp("PumpMin", it->key) == 0) {
|
|
|
|
setPumpMin(it->value.as<float>());
|
|
|
|
}
|
|
|
|
else if(strcmp("PumpMax", it->key) == 0) {
|
|
|
|
setPumpMax(it->value.as<float>());
|
|
|
|
}
|
|
|
|
else if(strcmp("FanMin", it->key) == 0) {
|
|
|
|
setFanMin(it->value.as<short>());
|
|
|
|
}
|
|
|
|
else if(strcmp("FanMax", it->key) == 0) {
|
|
|
|
setFanMax(it->value.as<short>());
|
|
|
|
}
|
|
|
|
else if(strcmp("Thermostat", it->key) == 0) {
|
|
|
|
setThermostatMode(it->value.as<unsigned char>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|