mirror of
https://github.com/Laxilef/OTGateway.git
synced 2025-12-11 18:54:28 +05:00
Use platformio and more updates
This commit is contained in:
106
lib/CustomOpenTherm/CustomOpenTherm.h
Normal file
106
lib/CustomOpenTherm/CustomOpenTherm.h
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <Arduino.h>
|
||||
#include <OpenTherm.h>
|
||||
|
||||
extern SchedulerClass Scheduler;
|
||||
|
||||
class CustomOpenTherm : public OpenTherm {
|
||||
private:
|
||||
unsigned long send_ts = millis();
|
||||
void(*handleSendRequestCallback)(unsigned long, unsigned long, OpenThermResponseStatus status, byte attempt);
|
||||
|
||||
public:
|
||||
CustomOpenTherm(int inPin = 4, int outPin = 5, bool isSlave = false) : OpenTherm(inPin, outPin, isSlave) {}
|
||||
void setHandleSendRequestCallback(void(*handleSendRequestCallback)(unsigned long, unsigned long, OpenThermResponseStatus status, byte attempt)) {
|
||||
this->handleSendRequestCallback = handleSendRequestCallback;
|
||||
}
|
||||
|
||||
unsigned long sendRequest(unsigned long request, byte attempts = 5, byte _attempt = 0) {
|
||||
_attempt++;
|
||||
while (send_ts > 0 && millis() - send_ts < 200) {
|
||||
Scheduler.yield();
|
||||
}
|
||||
|
||||
unsigned long _response;
|
||||
if (!sendRequestAync(request)) {
|
||||
_response = 0;
|
||||
} else {
|
||||
while (!isReady()) {
|
||||
Scheduler.yield();
|
||||
process();
|
||||
}
|
||||
|
||||
_response = getLastResponse();
|
||||
}
|
||||
|
||||
if (handleSendRequestCallback != NULL) {
|
||||
handleSendRequestCallback(request, _response, getLastResponseStatus(), _attempt);
|
||||
}
|
||||
|
||||
send_ts = millis();
|
||||
if (getLastResponseStatus() == OpenThermResponseStatus::SUCCESS || _attempt >= attempts) {
|
||||
return _response;
|
||||
} else {
|
||||
return sendRequest(request, attempts, _attempt);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long setBoilerStatus(bool enableCentralHeating, bool enableHotWater, bool enableCooling, bool enableOutsideTemperatureCompensation, bool enableCentralHeating2, bool summerWinterMode, bool dhwBlocking) {
|
||||
return sendRequest(buildSetBoilerStatusRequest(enableCentralHeating, enableHotWater, enableCooling, enableOutsideTemperatureCompensation, enableCentralHeating2, summerWinterMode, dhwBlocking));
|
||||
}
|
||||
|
||||
unsigned long buildSetBoilerStatusRequest(bool enableCentralHeating, bool enableHotWater, bool enableCooling, bool enableOutsideTemperatureCompensation, bool enableCentralHeating2, bool summerWinterMode, bool dhwBlocking) {
|
||||
unsigned int data = enableCentralHeating | (enableHotWater << 1) | (enableCooling << 2) | (enableOutsideTemperatureCompensation << 3) | (enableCentralHeating2 << 4) | (summerWinterMode << 5) | (dhwBlocking << 6);
|
||||
data <<= 8;
|
||||
return buildRequest(OpenThermMessageType::READ_DATA, OpenThermMessageID::Status, data);
|
||||
}
|
||||
|
||||
bool setBoilerTemperature(float temperature) {
|
||||
unsigned int data = temperatureToData(temperature);
|
||||
unsigned long response = sendRequest(buildRequest(OpenThermMessageType::WRITE_DATA, OpenThermMessageID::TSet, data));
|
||||
return isValidResponse(response);
|
||||
}
|
||||
|
||||
bool setBoilerTemperature2(float temperature) {
|
||||
unsigned int data = temperatureToData(temperature);
|
||||
unsigned long response = sendRequest(buildRequest(OpenThermMessageType::WRITE_DATA, OpenThermMessageID::TsetCH2, data));
|
||||
return isValidResponse(response);
|
||||
}
|
||||
|
||||
bool sendBoilerReset() {
|
||||
unsigned int data = 1;
|
||||
data <<= 8;
|
||||
unsigned long response = sendRequest(buildRequest(OpenThermMessageType::WRITE_DATA, OpenThermMessageID::Command, data));
|
||||
return isValidResponse(response);
|
||||
}
|
||||
|
||||
bool sendServiceReset() {
|
||||
unsigned int data = 10;
|
||||
data <<= 8;
|
||||
unsigned long response = sendRequest(buildRequest(OpenThermMessageType::WRITE_DATA, OpenThermMessageID::Command, data));
|
||||
return isValidResponse(response);
|
||||
}
|
||||
|
||||
bool sendWaterFilling() {
|
||||
unsigned int data = 2;
|
||||
data <<= 8;
|
||||
unsigned long response = sendRequest(buildRequest(OpenThermMessageType::WRITE_DATA, OpenThermMessageID::Command, data));
|
||||
return isValidResponse(response);
|
||||
}
|
||||
|
||||
// converters
|
||||
float f88(unsigned long response) {
|
||||
const byte valueLB = response & 0xFF;
|
||||
const byte valueHB = (response >> 8) & 0xFF;
|
||||
|
||||
float value = (int8_t) valueHB;
|
||||
return value + (float)valueLB / 256.0;
|
||||
}
|
||||
|
||||
int16_t s16(unsigned long response) {
|
||||
const byte valueLB = response & 0xFF;
|
||||
const byte valueHB = (response >> 8) & 0xFF;
|
||||
|
||||
int16_t value = valueHB;
|
||||
return ((value << 8) + valueLB);
|
||||
}
|
||||
};
|
||||
63
lib/Equitherm/Equitherm.h
Normal file
63
lib/Equitherm/Equitherm.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#if defined(EQUITHERM_INTEGER)
|
||||
// расчёты с целыми числами
|
||||
typedef int datatype;
|
||||
#else
|
||||
// расчёты с float числами
|
||||
typedef float datatype;
|
||||
#endif
|
||||
|
||||
class Equitherm {
|
||||
public:
|
||||
datatype targetTemp = 0;
|
||||
datatype indoorTemp = 0;
|
||||
datatype outdoorTemp = 0;
|
||||
float Kn = 0.0;
|
||||
float Kk = 0.0;
|
||||
float Kt = 0.0;
|
||||
|
||||
Equitherm() {}
|
||||
|
||||
// kn, kk, kt
|
||||
Equitherm(float new_kn, float new_kk, float new_kt) {
|
||||
Kn = new_kn;
|
||||
Kk = new_kk;
|
||||
Kt = new_kt;
|
||||
}
|
||||
|
||||
// лимит выходной величины
|
||||
void setLimits(int min_output, int max_output) {
|
||||
_minOut = min_output;
|
||||
_maxOut = max_output;
|
||||
}
|
||||
|
||||
// возвращает новое значение при вызове
|
||||
datatype getResult() {
|
||||
datatype output = getResultN() + getResultK() + getResultT();
|
||||
output = constrain(output, _minOut, _maxOut); // ограничиваем выход
|
||||
return output;
|
||||
}
|
||||
|
||||
private:
|
||||
int _minOut = 20, _maxOut = 90;
|
||||
|
||||
// температура контура отопления в зависимости от наружной температуры
|
||||
datatype getResultN() {
|
||||
float a = (-0.21 * Kn) - 0.06; // a = -0,21k — 0,06
|
||||
float b = (6.04 * Kn) + 1.98; // b = 6,04k + 1,98
|
||||
float c = (-5.06 * Kn) + 18.06; // с = -5,06k + 18,06
|
||||
float x = (-0.2 * outdoorTemp) + 5; // x = -0.2*t1 + 5
|
||||
return (a * x * x) + (b * x) + c; // Tn = ax2 + bx + c
|
||||
}
|
||||
|
||||
// поправка на желаемую комнатную температуру
|
||||
datatype getResultK() {
|
||||
return (targetTemp - 20) * Kk;
|
||||
}
|
||||
|
||||
// Расчет поправки (ошибки) термостата
|
||||
datatype getResultT() {
|
||||
return constrain((targetTemp - indoorTemp), -2, 2) * Kt;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user