mirror of
https://github.com/Laxilef/OTGateway.git
synced 2025-12-11 18:54:28 +05:00
Compatible with ESP32
This commit is contained in:
@@ -7,9 +7,12 @@ public:
|
||||
MainTask(bool _enabled = false, unsigned long _interval = 0) : Task(_enabled, _interval) {}
|
||||
|
||||
protected:
|
||||
const char* taskName = "Main task";
|
||||
const int taskCore = 2;
|
||||
|
||||
unsigned long lastHeapInfo = 0;
|
||||
unsigned long firstFailConnect = 0;
|
||||
unsigned short minFreeHeapSize = 65535;
|
||||
unsigned int minFreeHeapSize = RAM_SIZE;
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_STATUS_PIN, OUTPUT);
|
||||
@@ -21,7 +24,7 @@ protected:
|
||||
}
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
if (!tMqtt->isEnabled()) {
|
||||
if (!tMqtt->isEnabled() && strlen(settings.mqtt.server) > 0) {
|
||||
tMqtt->enable();
|
||||
}
|
||||
|
||||
@@ -65,15 +68,16 @@ protected:
|
||||
#endif
|
||||
|
||||
if (settings.debug) {
|
||||
unsigned short freeHeapSize = ESP.getFreeHeap();
|
||||
unsigned short minFreeHeapSizeDiff = 0;
|
||||
unsigned int freeHeapSize = ESP.getFreeHeap();
|
||||
unsigned int minFreeHeapSizeDiff = 0;
|
||||
|
||||
if (freeHeapSize < minFreeHeapSize) {
|
||||
minFreeHeapSizeDiff = minFreeHeapSize - freeHeapSize;
|
||||
minFreeHeapSize = freeHeapSize;
|
||||
}
|
||||
|
||||
if (millis() - lastHeapInfo > 10000 || minFreeHeapSizeDiff > 0) {
|
||||
DEBUG_F("Free heap size: %hu bytes, min: %hu bytes (diff: %hu bytes)\n", freeHeapSize, minFreeHeapSize, minFreeHeapSizeDiff);
|
||||
DEBUG_F("Free heap size: %u of %u bytes, min: %u bytes (diff: %u bytes)\n", freeHeapSize, RAM_SIZE, minFreeHeapSize, minFreeHeapSizeDiff);
|
||||
lastHeapInfo = millis();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <WiFiClient.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <netif/etharp.h>
|
||||
#include "HaHelper.h"
|
||||
|
||||
WiFiClient espClient;
|
||||
@@ -13,6 +12,9 @@ public:
|
||||
MqttTask(bool _enabled = false, unsigned long _interval = 0) : Task(_enabled, _interval) {}
|
||||
|
||||
protected:
|
||||
const char* taskName = "Mqtt task";
|
||||
const int taskCore = 1;
|
||||
|
||||
unsigned long lastReconnectAttempt = 0;
|
||||
unsigned long firstFailConnect = 0;
|
||||
|
||||
@@ -20,7 +22,7 @@ protected:
|
||||
DEBUG("[MQTT] Started");
|
||||
|
||||
client.setCallback(__callback);
|
||||
haHelper.setPrefix(settings.mqtt.prefix);
|
||||
haHelper.setDevicePrefix(settings.mqtt.prefix);
|
||||
haHelper.setDeviceVersion(OT_GATEWAY_VERSION);
|
||||
haHelper.setDeviceModel("Opentherm Gateway");
|
||||
haHelper.setDeviceName("Opentherm Gateway");
|
||||
@@ -59,7 +61,6 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
forceARP();
|
||||
lastReconnectAttempt = millis();
|
||||
}
|
||||
}
|
||||
@@ -79,14 +80,6 @@ protected:
|
||||
}
|
||||
|
||||
|
||||
static void forceARP() {
|
||||
struct netif* netif = netif_list;
|
||||
while (netif) {
|
||||
etharp_gratuitous(netif);
|
||||
netif = netif->next;
|
||||
}
|
||||
}
|
||||
|
||||
static bool updateSettings(JsonDocument& doc) {
|
||||
bool flag = false;
|
||||
|
||||
@@ -359,8 +352,7 @@ protected:
|
||||
} else {
|
||||
client.publish(getTopicPath("status").c_str(), vars.states.otStatus ? "online" : "offline");
|
||||
}
|
||||
|
||||
forceARP();
|
||||
|
||||
prevPubVars = millis();
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,14 @@ class OpenThermTask : public Task {
|
||||
public:
|
||||
OpenThermTask(bool _enabled = false, unsigned long _interval = 0) : Task(_enabled, _interval) {}
|
||||
|
||||
void static IRAM_ATTR handleInterrupt() {
|
||||
ot->handleInterrupt();
|
||||
}
|
||||
|
||||
protected:
|
||||
const char* taskName = "OpenTherm task";
|
||||
const int taskCore = 2;
|
||||
|
||||
void setup() {
|
||||
vars.parameters.heatingMinTemp = settings.heating.minTemp;
|
||||
vars.parameters.heatingMaxTemp = settings.heating.maxTemp;
|
||||
@@ -16,8 +23,12 @@ protected:
|
||||
|
||||
ot = new CustomOpenTherm(settings.opentherm.inPin, settings.opentherm.outPin);
|
||||
|
||||
ot->begin(handleInterrupt, responseCallback);
|
||||
ot->setHandleSendRequestCallback(sendRequestCallback);
|
||||
ot->setHandleSendRequestCallback(this->sendRequestCallback);
|
||||
ot->begin(OpenThermTask::handleInterrupt, this->responseCallback);
|
||||
|
||||
ot->setYieldCallback([](void* self) {
|
||||
static_cast<OpenThermTask*>(self)->yield();
|
||||
}, this);
|
||||
|
||||
#ifdef LED_OT_RX_PIN
|
||||
pinMode(LED_OT_RX_PIN, OUTPUT);
|
||||
@@ -160,10 +171,6 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
void static IRAM_ATTR handleInterrupt() {
|
||||
ot->handleInterrupt();
|
||||
}
|
||||
|
||||
void static sendRequestCallback(unsigned long request, unsigned long response, OpenThermResponseStatus status, byte attempt) {
|
||||
printRequestDetail(ot->getDataID(request), status, request, response, attempt);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,9 @@ public:
|
||||
RegulatorTask(bool _enabled = false, unsigned long _interval = 0) : LeanTask(_enabled, _interval) {}
|
||||
|
||||
protected:
|
||||
const char* taskName = "Regulator task";
|
||||
const int taskCore = 2;
|
||||
|
||||
bool tunerInit = false;
|
||||
byte tunerState = 0;
|
||||
byte tunerRegulator = 0;
|
||||
|
||||
@@ -6,6 +6,9 @@ public:
|
||||
SensorsTask(bool _enabled = false, unsigned long _interval = 0) : LeanTask(_enabled, _interval) {}
|
||||
|
||||
protected:
|
||||
const char* taskName = "Sensors task";
|
||||
const int taskCore = 2;
|
||||
|
||||
OneWire* oneWireOutdoorSensor;
|
||||
OneWire* oneWireIndoorSensor;
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@ struct Settings {
|
||||
char hostname[80] = "opentherm";
|
||||
|
||||
struct {
|
||||
byte inPin = 4;
|
||||
byte outPin = 5;
|
||||
byte inPin = OT_IN_PIN_DEFAULT;
|
||||
byte outPin = OT_OUT_PIN_DEFAULT;
|
||||
unsigned int memberIdCode = 0;
|
||||
bool dhwPresent = true;
|
||||
} opentherm;
|
||||
@@ -60,14 +60,14 @@ struct Settings {
|
||||
struct {
|
||||
// 0 - boiler, 1 - manual, 2 - ds18b20
|
||||
byte type = 0;
|
||||
byte pin = 12;
|
||||
byte pin = SENSOR_OUTDOOR_PIN_DEFAULT;
|
||||
float offset = 0.0f;
|
||||
} outdoor;
|
||||
|
||||
struct {
|
||||
// 1 - manual, 2 - ds18b20
|
||||
byte type = 1;
|
||||
byte pin = 14;
|
||||
byte pin = SENSOR_INDOOR_PIN_DEFAULT;
|
||||
float offset = 0.0f;
|
||||
} indoor;
|
||||
} sensors;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#define WM_MDNS
|
||||
#include <WiFiManager.h>
|
||||
#include <WiFiManagerParameters.h>
|
||||
#include <netif/etharp.h>
|
||||
|
||||
// Wifimanager
|
||||
WiFiManager wm;
|
||||
@@ -25,8 +27,12 @@ public:
|
||||
WifiManagerTask(bool _enabled = false, unsigned long _interval = 0) : Task(_enabled, _interval) {}
|
||||
|
||||
protected:
|
||||
const char* taskName = "WifiManager";
|
||||
const int taskCore = 1;
|
||||
bool connected = false;
|
||||
unsigned long lastArpGratuitous = 0;
|
||||
|
||||
void setup() {
|
||||
//WiFi.mode(WIFI_STA);
|
||||
wm.setDebugOutput(settings.debug);
|
||||
|
||||
wmHostname = new WiFiManagerParameter("hostname", "Hostname", settings.hostname, 80);
|
||||
@@ -95,6 +101,11 @@ protected:
|
||||
|
||||
if (connected && WiFi.status() != WL_CONNECTED) {
|
||||
connected = false;
|
||||
|
||||
#ifdef USE_TELNET
|
||||
TelnetStream.stop();
|
||||
#endif
|
||||
|
||||
INFO("[wifi] Disconnected");
|
||||
|
||||
} else if (!connected && WiFi.status() == WL_CONNECTED) {
|
||||
@@ -104,6 +115,10 @@ protected:
|
||||
wm.stopConfigPortal();
|
||||
}
|
||||
|
||||
#ifdef USE_TELNET
|
||||
TelnetStream.begin();
|
||||
#endif
|
||||
|
||||
INFO_F("[wifi] Connected. IP address: %s, RSSI: %d\n", WiFi.localIP().toString().c_str(), WiFi.RSSI());
|
||||
}
|
||||
|
||||
@@ -111,10 +126,17 @@ protected:
|
||||
wm.startWebPortal();
|
||||
}
|
||||
|
||||
#if defined(ESP8266)
|
||||
if ( connected && millis() - lastArpGratuitous > 60000 ) {
|
||||
arpGratuitous();
|
||||
lastArpGratuitous = millis();
|
||||
}
|
||||
#endif
|
||||
|
||||
wm.process();
|
||||
}
|
||||
|
||||
void static saveParamsCallback() {
|
||||
static void saveParamsCallback() {
|
||||
strcpy(settings.hostname, wmHostname->getValue());
|
||||
strcpy(settings.mqtt.server, wmMqttServer->getValue());
|
||||
settings.mqtt.port = wmMqttPort->getValue();
|
||||
@@ -161,5 +183,11 @@ protected:
|
||||
INFO(F("Settings saved"));
|
||||
}
|
||||
|
||||
bool connected = false;
|
||||
static void arpGratuitous() {
|
||||
struct netif* netif = netif_list;
|
||||
while (netif) {
|
||||
etharp_gratuitous(netif);
|
||||
netif = netif->next;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
#define OT_GATEWAY_VERSION "1.3.2"
|
||||
#define OT_GATEWAY_VERSION "1.3.3"
|
||||
#define AP_SSID "OpenTherm Gateway"
|
||||
#define AP_PASSWORD "otgateway123456"
|
||||
#define USE_TELNET
|
||||
@@ -18,6 +18,26 @@
|
||||
#define CONFIG_URL "http://%s/"
|
||||
#define SETTINGS_VALID_VALUE "stvalid" // only 8 chars!
|
||||
|
||||
#if defined(ESP8266)
|
||||
#define RAM_SIZE 81920
|
||||
#define OT_IN_PIN_DEFAULT 4
|
||||
#define OT_OUT_PIN_DEFAULT 5
|
||||
#define SENSOR_OUTDOOR_PIN_DEFAULT 12
|
||||
#define SENSOR_INDOOR_PIN_DEFAULT 14
|
||||
#elif defined(ESP32)
|
||||
#define RAM_SIZE 327680
|
||||
#define OT_IN_PIN_DEFAULT 17
|
||||
#define OT_OUT_PIN_DEFAULT 21
|
||||
#define SENSOR_OUTDOOR_PIN_DEFAULT 2
|
||||
#define SENSOR_INDOOR_PIN_DEFAULT 4
|
||||
#else
|
||||
#define RAM_SIZE 999999
|
||||
#define OT_IN_PIN_DEFAULT 0
|
||||
#define OT_OUT_PIN_DEFAULT 0
|
||||
#define SENSOR_OUTDOOR_PIN_DEFAULT 0
|
||||
#define SENSOR_INDOOR_PIN_DEFAULT 0
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef USE_TELNET
|
||||
#define INFO_STREAM TelnetStream
|
||||
|
||||
28
src/main.cpp
28
src/main.cpp
@@ -3,13 +3,22 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <TelnetStream.h>
|
||||
#include <EEManager.h>
|
||||
#include <Scheduler.h>
|
||||
#include <Task.h>
|
||||
#include <LeanTask.h>
|
||||
#include "Settings.h"
|
||||
|
||||
EEManager eeSettings(settings, 30000);
|
||||
|
||||
#if defined(ESP32)
|
||||
#include <ESP32Scheduler.h>
|
||||
#include <Task.h>
|
||||
#include <LeanTask.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <Scheduler.h>
|
||||
#include <Task.h>
|
||||
#include <LeanTask.h>
|
||||
#elif
|
||||
#error Wrong board. Supported boards: esp8266, esp32
|
||||
#endif
|
||||
|
||||
#include "WifiManagerTask.h"
|
||||
#include "MqttTask.h"
|
||||
#include "OpenThermTask.h"
|
||||
@@ -27,13 +36,10 @@ MainTask* tMain;
|
||||
|
||||
|
||||
void setup() {
|
||||
#ifdef USE_TELNET
|
||||
TelnetStream.begin();
|
||||
delay(1000);
|
||||
#else
|
||||
Serial.begin(115200);
|
||||
Serial.println("\n\n");
|
||||
#endif
|
||||
#ifndef USE_TELNET
|
||||
Serial.begin(115200);
|
||||
Serial.println("\n\n");
|
||||
#endif
|
||||
|
||||
EEPROM.begin(eeSettings.blockSize());
|
||||
uint8_t eeSettingsResult = eeSettings.begin(0, 's');
|
||||
@@ -69,7 +75,7 @@ void setup() {
|
||||
tRegulator = new RegulatorTask(true, 10000);
|
||||
Scheduler.start(tRegulator);
|
||||
|
||||
tMain = new MainTask(true);
|
||||
tMain = new MainTask(true, 50);
|
||||
Scheduler.start(tMain);
|
||||
|
||||
Scheduler.begin();
|
||||
|
||||
Reference in New Issue
Block a user