I am building a floor heating controller device. This is a proof of concept and thus still in the early phases. When finished, the unit will be controlling 5 floor heating strings with a combination of pulse modulation and temperature control. I decided to implement MQTT to communicate to the unit, which uses the Polling functionality to update in predefined intervals.
I really hope that someone can see what is the solution on this problem.
Thanks to to help from this forum the component compiles and works as intended with a PollingComponent and a CustomMQTTDevice. When adding
Switch *my_switch = new Switch();
a compilation error occurs:
In file included from src/main.cpp:38:
src/mqtt_pulse_switch.h:13:5: error: ‘Switch’ does not name a type
Switch *my_switch = new Switch();
This is the component code:
#include "esphome.h"
class MqttPulseSwitch : public PollingComponent, public CustomMQTTDevice {
public:
MqttPulseSwitch() : PollingComponent(5000) { }
//float get_setup_priority() const override { return esphome::setup_priority::LATE; }
int string_id = 0; //varmestreng 1 til 5
int dutycycle = 20; //percentage_pn
int interval = 120; //minutes - 0=off
int switch_pin = 27; //switch output pin
float target_temp = 21; //måltemperatur
Switch *my_switch = new Switch();
bool switch_state = false;
void setup() override {
// This will be called once to set up the component
pinMode(27, OUTPUT);
bool state;
this->my_switch->set_name("My Switch");
this->my_switch->set_icon("mdi:heating-coil");
this->my_switch->set_on_state(true);
// Register the switch
App.register_component(this->my_switch);
//setup MQTT
subscribe("vvv46/heating/floor/unit/measured/", &MqttPulseSwitch::on_message);
subscribe_json("vvv46/heating/floor/unit/setup/", &MqttPulseSwitch::on_json_message);
//tell that IoT-client is online and waiting to get setup information
}
void on_message(const std::string &payload) {
float payloadAsFloat = std::stof(payload);
publish("vvv46/heating/floor/unit/received/", payloadAsFloat); //slettes i endelig version
if (payloadAsFloat < target_temp) {
digitalWrite(27, HIGH);
publish("vvv46/heating/floor/unit/3/", 1);
} else {
digitalWrite(27, LOW);
publish("vvv46/heating/floor/unit/3/", 0);
}
}
void on_json_message(JsonObject root) {
if (!root.containsKey("string_id"))
return;
int string_id = root["string_id"];
if (root["string_id"] = 3) {
//streng 3 - køkkenvarme
dutycycle = root["dutycycle"];
interval = root["interval"];
switch_pin = root["switch_pin"];
target_temp = root["target_temp"];
dutycycle = root["dutycycle"];
}
// publish JSON using lambda syntax
publish("vvv46/heating/floor/unit/received/JSON/", "just got a JSON message");
publish_json("vvv46/heating/floor/unit/received/", [=](JsonObject root2) {
root2["message"] = "ESP32-test: JSON received";
root2["string_id"] = string_id;
root2["dutycycle"] = dutycycle;
root2["interval"] = interval;
root2["switch_pin"] = switch_pin;
root2["target_temp"] = target_temp;
});
}
void update() override {
// This will be called every "update_interval" milliseconds.
switch_state = !switch_state;
this->my_switch->publish_state(switch_state);
// Publish an OFF state
ESP_LOGD("customer", "update routine executing");
}
};
This is the yaml code:
- platform: dallas #pin D8/GPIO5/IO5
address: 0x88c1a7c90264ff28
name: "floor_temperature"
id: floor_heating_guest_WC_sensor
on_value:
then:
- mqtt.publish:
topic: vvv46/heating/floor/unit/measured/
payload: !lambda |-
return esphome::to_string(id(floor_heating_guest_WC_sensor).state);
custom_component:
- lambda: |-
auto my_custom = new MqttPulseSwitch();
return {my_custom};
components:
- id: my_custom_id