Posted on Saturday, September 1, 2018 at 12:00 AM, 48940 views
MQTT Thermostat is based on OpenTherm Thermostat, OpenTherm Library and ESP8266 controller (WeMos D1 Mini). Can be used for remote heating system control via nodeRED, openHAB, Home Assistant and Android Tasker etc.
To Buy Assembled OpenTherm Adapter + Shield (to stack WeMos D1 mini) please navigate to the Shop page or buy it here:
#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 ICACHE_RAM_ATTR 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();
}
Connecting to Ihor Melnyk WIFI
.
WiFi connected
IP address:
192.168.0.77
Attempting MQTT connection...connected
t=23.94
sp=23
sp=23.00 pv=23.87 dt=1.00 op=0.00 P=-8.75 I=-0.23 D=0.62
t=23.87
sp=22
sp=22.00 pv=23.94 dt=1.00 op=0.00 P=-19.37 I=-0.23 D=-0.62
t=23.94
sp=22.00 pv=23.94 dt=1.00 op=0.00 P=-19.37 I=-0.23 D=0.00
t=23.94
sp=22.00 pv=23.00 dt=1.00 op=0.00 P=-10.00 I=-0.23 D=9.37
t=23.00
sp=23
sp=23.00 pv=23.00 dt=1.00 op=0.00 P=0.00 I=-0.23 D=0.00
t=23.00
sp=23.00 pv=23.06 dt=1.00 op=0.00 P=-0.62 I=-0.23 D=-0.62
t=23.06
sp=23.00 pv=23.06 dt=1.00 op=0.00 P=-0.62 I=-0.23 D=0.00
t=23.06
That's it. Enjoy!
To Buy Assembled OpenTherm Adapter + Shield (to stack WeMos D1 mini) please navigate to the Shop page or buy it here: