I am using home assistant for my thermostat function with multiple zones, using the generic thermostat integration.
My objective is to expand this with OpenTherm functionality rather than just on/off as my combination boiler can handle OpenTherm too. I bought the Opentherm module from Ihormelnyk.com, by Ihor Melnyk. He has written documentation on his website for a the integration in HA with MQTT.
However, the example on his website is by using an ESP2866 Wemos D1 mini. I personally would like to use a Arduino nano connected by USB instead or ethernet instead of wifi to connect the Opentherm module to home assistant (I want to limit the use of EMF radiation in the home). Also I don’t want to add the DS18b20 sensor to the arduino MCU, but use other temp sensors already available in my configuration.
I am a beginner, but usually can manage to make small change to code with manuals but it’s not my expertise. Therefore I am hoping someone can help me on the way a bit here.
The code for the sketch provided on the website (here) of Ihor Melnyk:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <OpenTherm.h>
//OpenTherm input and output wires connected to 4 and 5 pins on the OpenTherm Shield
const int inPin = 4;
const int outPin = 5;
//Data wire is connected to 14 pin on the OpenTherm Shield
#define ONE_WIRE_BUS 14
const char* ssid = "Please specify your WIFI SSID";
const char* password = "Please specify your WIFI password";
const char* mqtt_server = "Please specify MQTT server";
const int mqtt_port = 00000;
const char* mqtt_user = "Please specify user";
const char* mqtt_password = "Please specify password";
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
OpenTherm ot(inPin, outPin);
WiFiClient espClient;
PubSubClient client(espClient);
char buf[10];
float sp = 23, //set point
pv = 0, //current temperature
pv_last = 0, //prior temperature
ierr = 0, //integral error
dt = 0, //time between measurements
op = 0; //PID controller output
unsigned long ts = 0, new_ts = 0; //timestamp
void handleInterrupt() {
ot.handleInterrupt();
}
float getTemp() {
return sensors.getTempCByIndex(0);
}
float pid(float sp, float pv, float pv_last, float& ierr, float dt) {
float Kc = 10.0; // K / %Heater
float tauI = 50.0; // sec
float tauD = 1.0; // sec
// PID coefficients
float KP = Kc;
float KI = Kc / tauI;
float KD = Kc*tauD;
// upper and lower bounds on heater level
float ophi = 100;
float oplo = 0;
// calculate the error
float error = sp - pv;
// calculate the integral error
ierr = ierr + KI * error * dt;
// calculate the measurement derivative
float dpv = (pv - pv_last) / dt;
// calculate the PID output
float P = KP * error; //proportional contribution
float I = ierr; //integral contribution
float D = -KD * dpv; //derivative contribution
float op = P + I + D;
// implement anti-reset windup
if ((op < oplo) || (op > ophi)) {
I = I - KI * error * dt;
// clip output
op = max(oplo, min(ophi, op));
}
ierr = I;
Serial.println("sp="+String(sp) + " pv=" + String(pv) + " dt=" + String(dt) + " op=" + String(op) + " P=" + String(P) + " I=" + String(I) + " D=" + String(D));
return op;
}
void setup_wifi() {
delay(10);
//Connect to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup(void) {
Serial.begin(115200);
setup_wifi();
//Init DS18B20 Sensor
sensors.begin();
sensors.requestTemperatures();
sensors.setWaitForConversion(false); //switch to async mode
pv, pv_last = sensors.getTempCByIndex(0);
ts = millis();
//Init OpenTherm Controller
ot.begin(handleInterrupt);
//Init MQTT Client
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}
void publish_temperature() {
Serial.println("t=" + String(pv));
String(pv).toCharArray(buf, 10);
client.publish("pv", buf);
}
void callback(char* topic, byte* payload, unsigned int length) {
if(strcmp(topic, "sp") != 0) return;
String str = String();
for (int i = 0; i < length; i++) {
str += (char)payload[i];
}
Serial.println("sp=" + str);
sp = str.toFloat();
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
Serial.println("connected");
// Once connected, publish an announcement...
publish_temperature();
// ... and resubscribe
client.subscribe("sp");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop(void) {
new_ts = millis();
if (new_ts - ts > 1000) {
//Set/Get Boiler Status
bool enableCentralHeating = true;
bool enableHotWater = true;
bool enableCooling = false;
unsigned long response = ot.setBoilerStatus(enableCentralHeating, enableHotWater, enableCooling);
OpenThermResponseStatus responseStatus = ot.getLastResponseStatus();
if (responseStatus != OpenThermResponseStatus::SUCCESS) {
Serial.println("Error: Invalid boiler response " + String(response, HEX));
}
pv = sensors.getTempCByIndex(0);
dt = (new_ts - ts) / 1000.0;
ts = new_ts;
if (responseStatus == OpenThermResponseStatus::SUCCESS) {
op = pid(sp, pv, pv_last, ierr, dt);
//Set Boiler Temperature
ot.setBoilerTemperature(op);
}
pv_last = pv;
sensors.requestTemperatures(); //async temperature request
publish_temperature();
}
//MQTT Loop
if (!client.connected()) {
reconnect();
}
client.loop();
}
What comes to my mind is that I have to add a mqtt possibilty over USB, how can I do that?
And I have to remove the wifi parts and the library calling of <ESP8266WiFi.h>.
Could someone help me on the way on how to alter this code for my situation?
Thanks
Johan