add mqtt with ha components
configure Pollin switches
This commit is contained in:
parent
14a984195f
commit
3dcfc3e5ba
@ -26,4 +26,11 @@ public:
|
||||
rcSwitch["channel"] = decoder.device;
|
||||
rcSwitch["raw_value"] = value;
|
||||
}
|
||||
|
||||
static char* buildId(const char* group, const unsigned char channel) {
|
||||
char* uId = new char[30];
|
||||
sprintf(uId, "%s_%d", group, channel);
|
||||
return uId;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
199
gateway/include/ha.h
Normal file
199
gateway/include/ha.h
Normal file
@ -0,0 +1,199 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#define JSON_SIZE 512
|
||||
#define TOPIC_SIZE 255
|
||||
#define DEVICE_ID "rc-gateway"
|
||||
|
||||
namespace Ha {
|
||||
|
||||
struct Component {
|
||||
const char* name;
|
||||
char* id;
|
||||
const char* type;
|
||||
char configTopic[TOPIC_SIZE];
|
||||
|
||||
Component(const char* name, const char* id, const char* type) : name(name), id((char*)id), type(type) {
|
||||
sprintf(configTopic, "homeassistant/%s/rc-gateway/%s/config", type, id);
|
||||
}
|
||||
|
||||
virtual void buildUniqueId(char* uniqueId) = 0;
|
||||
|
||||
virtual void buildConfig(JsonDocument& jsonDoc) {
|
||||
buildDeviceConfig(jsonDoc);
|
||||
jsonDoc["name"] = name;
|
||||
char uniqueId[50];
|
||||
buildUniqueId(uniqueId);
|
||||
jsonDoc["unique_id"] = uniqueId;
|
||||
}
|
||||
|
||||
virtual void buildDeviceConfig(JsonDocument& jsonDoc) {
|
||||
JsonObject device = jsonDoc.createNestedObject("device");
|
||||
device["name"] = "RC Gateway";
|
||||
device["model"] = "Huzzah Esp8266";
|
||||
device["manufacturer"] = "Adafruit";
|
||||
JsonArray connections = device.createNestedArray("connections");
|
||||
JsonArray mac = connections.createNestedArray();
|
||||
mac.add("mac");
|
||||
mac.add(WiFi.macAddress());
|
||||
JsonArray identifiers = device.createNestedArray("identifiers");
|
||||
identifiers.add(DEVICE_ID);
|
||||
}
|
||||
};
|
||||
|
||||
struct Command : Component {
|
||||
char commandTopic[TOPIC_SIZE];
|
||||
|
||||
Command(const char* name, const char* id, const char* type) : Component(name, id, type) {
|
||||
}
|
||||
|
||||
void buildUniqueId(char* uniqueId) override {
|
||||
sprintf(uniqueId, "rc_gateway_%s", id);
|
||||
}
|
||||
|
||||
void buildConfig(JsonDocument& jsonDoc) override {
|
||||
Component::buildConfig(jsonDoc);
|
||||
jsonDoc["command_topic"] = commandTopic;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
typedef void (*onMessage)(const char* msg);
|
||||
struct Button : Command {
|
||||
static constexpr const char* type = "button";
|
||||
onMessage f;
|
||||
|
||||
Button(const char* name, const char* id, onMessage f) : Command(name, id, type), f(f) {
|
||||
sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s", type, id);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
struct Switch : Command {
|
||||
static constexpr const char* type = "switch";
|
||||
char stateTopic[TOPIC_SIZE];
|
||||
const char* area;
|
||||
uint16_t (*publisher)(const char* topic, const char* message);
|
||||
virtual void onCommand(const char* msg){}
|
||||
|
||||
Switch(const char* name, const char* id, const char* area, uint16_t (*publisher)(const char* topic, const char* message) = nullptr)
|
||||
: Command(name, id, type), area(area), publisher(publisher) {
|
||||
sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id);
|
||||
}
|
||||
|
||||
void buildDeviceConfig(JsonDocument& jsonDoc) override {
|
||||
JsonObject device = jsonDoc.createNestedObject("device");
|
||||
device["name"] = name;
|
||||
device["via_device"] = DEVICE_ID;
|
||||
device["suggested_area"] = area;
|
||||
JsonArray identifiers = device.createNestedArray("identifiers");
|
||||
identifiers.add(id);
|
||||
}
|
||||
|
||||
void buildConfig(JsonDocument& jsonDoc) override {
|
||||
Command::buildConfig(jsonDoc);
|
||||
jsonDoc["name"] = nullptr;
|
||||
jsonDoc["device_class"] = "outlet";
|
||||
jsonDoc["retain"] = true;
|
||||
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
|
||||
}
|
||||
|
||||
Switch* withStateTopic() {
|
||||
sprintf(stateTopic, "homeassistant/%s/rc-gateway/%s/state", type, id);
|
||||
return this;
|
||||
}
|
||||
|
||||
void publishState(bool state) {
|
||||
publisher(stateTopic, state ? "ON" : "OFF");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
struct PollinSwitch : Switch {
|
||||
const char* group;
|
||||
unsigned char channel;
|
||||
|
||||
PollinSwitch(const char* name, const char* area, const char* group, const unsigned char channel, uint16_t (*publisher)(const char* topic, const char* message))
|
||||
: Switch(name, Protocol_1::buildId(group, channel), area, publisher), group(group), channel(channel) {
|
||||
sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id);
|
||||
}
|
||||
|
||||
void onCommand(const char* msg) override {
|
||||
(String{ "ON" }.equals(msg)) ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel);
|
||||
publisher(stateTopic, msg);
|
||||
}
|
||||
|
||||
void buildConfig(JsonDocument& jsonDoc) override {
|
||||
Switch::buildConfig(jsonDoc);
|
||||
JsonObject device = jsonDoc["device"];
|
||||
device["manufacturer"] = "Pollin";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
struct Sensor : Component {
|
||||
const char* deviceClass;
|
||||
const char* unitMeasure;
|
||||
const char* valueTemplate;
|
||||
static constexpr const char* stateTopic = "homeassistant/sensor/rc-gateway/state";
|
||||
|
||||
Sensor(const char* name, const char* id) : Component(name, id, "sensor") {
|
||||
}
|
||||
|
||||
void buildUniqueId(char* uniqueId) override {
|
||||
sprintf(uniqueId, "living_room_%s", id);
|
||||
}
|
||||
|
||||
void buildConfig(JsonDocument& jsonDoc) override {
|
||||
Component::buildConfig(jsonDoc);
|
||||
jsonDoc["device_class"] = deviceClass;
|
||||
jsonDoc["unit_of_measurement"] = unitMeasure;
|
||||
jsonDoc["value_template"] = valueTemplate;
|
||||
jsonDoc["state_topic"] = stateTopic;
|
||||
}
|
||||
|
||||
void buildDeviceConfig(JsonDocument& jsonDoc) override {
|
||||
JsonObject device = jsonDoc.createNestedObject("device");
|
||||
device["name"] = "Living room";
|
||||
device["model"] = "BPM280";
|
||||
device["suggested_area"] = "Living room";
|
||||
device["via_device"] = "rc-gateway";
|
||||
JsonArray identifiers = device.createNestedArray("identifiers");
|
||||
identifiers.add("esp-clock-living-room");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
struct TemperatureSensor : Sensor {
|
||||
TemperatureSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||
deviceClass = "temperature";
|
||||
unitMeasure = "°C";
|
||||
valueTemplate = "{{ value_json.temperature }}";
|
||||
}
|
||||
};
|
||||
|
||||
struct HumiditySensor : Sensor {
|
||||
HumiditySensor(const char* name, const char* id) : Sensor(name, id) {
|
||||
deviceClass = "humidity";
|
||||
unitMeasure = "%";
|
||||
valueTemplate = "{{ value_json.humidity }}";
|
||||
}
|
||||
};
|
||||
|
||||
struct PressureSensor : Sensor {
|
||||
PressureSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||
deviceClass = "pressure";
|
||||
unitMeasure = "hPa";
|
||||
valueTemplate = "{{ value_json.pressure }}";
|
||||
}
|
||||
};
|
||||
|
||||
struct AltitudeSensor : Sensor {
|
||||
AltitudeSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||
deviceClass = "distance";
|
||||
unitMeasure = "m";
|
||||
valueTemplate = "{{ value_json.altitude }}";
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -1,10 +1,12 @@
|
||||
#include <TaskScheduler.h>
|
||||
|
||||
#define SEND_PIN PIN_SPI_MOSI
|
||||
#define RECEIVE_PIN PIN_SPI_MISO
|
||||
#define SEND_PIN 12
|
||||
#define RECEIVE_PIN 13
|
||||
#define RED_LED LED_BUILTIN
|
||||
// #define BLUE_LED 2
|
||||
|
||||
using namespace std;
|
||||
|
||||
Scheduler ts;
|
||||
|
||||
#include "wifi.h"
|
||||
@ -23,12 +25,14 @@ namespace Board {
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// Serial.begin(9600, SERIAL_8N1, SERIAL_TX_ONLY);
|
||||
pinMode(RED_LED, OUTPUT);
|
||||
turnOffLed(RED_LED);
|
||||
// pinMode(BLUE_LED, OUTPUT);
|
||||
// turnOffLed(BLUE_LED);
|
||||
Wifi::setup();
|
||||
Ota::setup();
|
||||
Mqtt::setup();
|
||||
tReadCommand.enable();
|
||||
}
|
||||
|
||||
@ -37,8 +41,33 @@ namespace Board {
|
||||
}
|
||||
|
||||
void publishResponse(JsonDocument& jsonDoc) {
|
||||
char message[255];
|
||||
serializeJson(jsonDoc, message);
|
||||
Mqtt::publish("homeassistant/sensor/rc-gateway/raw", message);
|
||||
if (jsonDoc.containsKey("rcSwitch")) {
|
||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||
string id;
|
||||
switch ((unsigned int)rcSwitch["protocol"]) {
|
||||
case 1:
|
||||
// buildId returns a new pointer, should it be deleted, or string will take care of it?
|
||||
id = Protocol_1::buildId((const char*)rcSwitch["group"], (int)rcSwitch["channel"]);
|
||||
break;
|
||||
case 2:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Ha::Switch* el = Mqtt::mapSwitches[id];
|
||||
if (el != nullptr) {
|
||||
el->publishState((bool)rcSwitch["state"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void handleJsonError(JsonDocument& jsonError) {
|
||||
char message[255];
|
||||
serializeJson(jsonError, message);
|
||||
Mqtt::publish("homeassistant/sensor/rc-gateway/raw", message);
|
||||
}
|
||||
}
|
||||
// {"rcSwitch":{"protocol":1,"state":false,"group":"11111","channel":4}}
|
||||
126
gateway/include/mqtt.h
Normal file
126
gateway/include/mqtt.h
Normal file
@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include "ha.h"
|
||||
|
||||
|
||||
#define MQTT_HOST IPAddress(192, 168, 5, 11)
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
namespace Mqtt {
|
||||
|
||||
AsyncMqttClient client;
|
||||
|
||||
void publishInit();
|
||||
void publishBme280();
|
||||
void disconnect();
|
||||
Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, []() {
|
||||
Serial.println("Connecting to MQTT...");
|
||||
client.connect();
|
||||
}, &ts);
|
||||
Task tPublishInit(TASK_IMMEDIATE, TASK_ONCE, publishInit, &ts);
|
||||
|
||||
const char* mainTopic = "homeassistant/+/rc-gateway/#";
|
||||
|
||||
void disconnect() {
|
||||
client.unsubscribe(mainTopic);
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
uint16_t publish(const char* topic, const char* message) {
|
||||
return client.publish(topic, 0, true, message);
|
||||
}
|
||||
|
||||
Ha::Sensor* sensors[] = {
|
||||
// new Ha::TemperatureSensor{"Temperature", "temperature"},
|
||||
// new Ha::HumiditySensor{"Humidity", "humidity"},
|
||||
// new Ha::PressureSensor{"Pressure", "pressure"},
|
||||
// new Ha::AltitudeSensor{"Altitude", "altitude"}
|
||||
};
|
||||
|
||||
Ha::Button* buttons[] = {
|
||||
new Ha::Button{"Restart", "restart",
|
||||
[](const char* msg) {
|
||||
if (String { "PRESS" }.equals(msg)) ESP.restart();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ha::Switch* switches[] = {
|
||||
(new Ha::PollinSwitch{"Meeting sensor", "Dining room", "00001", 1, publish})->withStateTopic(),
|
||||
(new Ha::PollinSwitch{"Fire Tv", "Living room", "00001", 2, publish})->withStateTopic(),
|
||||
(new Ha::PollinSwitch{"Diningroom player", "Dining room", "00001", 3, publish})->withStateTopic(),
|
||||
(new Ha::PollinSwitch{"Train", "Playroom", "11111", 4, publish})->withStateTopic()
|
||||
};
|
||||
|
||||
unordered_map<string, Ha::Switch*> mapSwitches;
|
||||
|
||||
void publishComponentConfig(Ha::Component& component) {
|
||||
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
||||
component.buildConfig(jsonDoc);
|
||||
|
||||
char message[JSON_SIZE];
|
||||
serializeJson(jsonDoc, message);
|
||||
|
||||
publish(component.configTopic, message);
|
||||
}
|
||||
|
||||
void publishInit() {
|
||||
// for (Ha::Component* cmp : sensors) {
|
||||
// publishComponentConfig(*cmp);
|
||||
// }
|
||||
for (Ha::Component* cmp : buttons) {
|
||||
publishComponentConfig(*cmp);
|
||||
}
|
||||
for (Ha::Switch* cmp : switches) {
|
||||
mapSwitches.insert({string(cmp->id), cmp});
|
||||
publishComponentConfig(*cmp);
|
||||
}
|
||||
ts.deleteTask(tPublishInit);
|
||||
}
|
||||
|
||||
void publishBme280() {
|
||||
// StaticJsonDocument<255> jsonDoc;
|
||||
// jsonDoc["temperature"] = Bme::data.temp;
|
||||
// jsonDoc["humidity"] = Bme::data.humidity;
|
||||
// jsonDoc["pressure"] = Bme::data.pressure;
|
||||
// jsonDoc["altitude"] = Bme::data.altitude;
|
||||
// char message[255];
|
||||
// serializeJson(jsonDoc, message);
|
||||
// publish(Ha::Sensor::stateTopic, message);
|
||||
}
|
||||
|
||||
void onMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
char msg[len + 1];
|
||||
memcpy(msg, payload, len);
|
||||
msg[len] = 0;
|
||||
for (Ha::Button* cmd : buttons) {
|
||||
if (String{ cmd->commandTopic }.equals(topic) && cmd->f != nullptr) {
|
||||
cmd->f(msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (Ha::Switch* cmd : switches) {
|
||||
if (String{ cmd->commandTopic }.equals(topic)) {
|
||||
cmd->onCommand(msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
client.onConnect([](bool sessionPresent) {
|
||||
tPublishInit.enable();
|
||||
client.subscribe(mainTopic, 0);
|
||||
tReConnect.disable();
|
||||
Serial.println("Connected to MQTT");
|
||||
});
|
||||
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
|
||||
tReConnect.enableDelayed();
|
||||
Serial.println("Disconnected from MQTT");
|
||||
});
|
||||
client.onMessage(onMessage);
|
||||
client.setServer(MQTT_HOST, MQTT_PORT);
|
||||
}
|
||||
}
|
||||
@ -10,10 +10,11 @@ namespace Ota {
|
||||
|
||||
void setup() {
|
||||
ArduinoOTA.onStart([]() {
|
||||
Serial.println("Start");
|
||||
Serial.println("Starting OTA");
|
||||
Mqtt::disconnect();
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
Serial.println("\nEnd");
|
||||
Serial.println("\nOTA Finished");
|
||||
});
|
||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
#include "mqtt.h"
|
||||
#include "ota.h"
|
||||
#include "credentials.h"
|
||||
|
||||
@ -29,6 +30,7 @@ namespace Wifi {
|
||||
printStatus();
|
||||
tReconnect.cancel();
|
||||
Ota::tLoop.enable();
|
||||
Mqtt::tReConnect.restart();
|
||||
});
|
||||
|
||||
stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
|
||||
@ -41,11 +43,11 @@ namespace Wifi {
|
||||
wifiMulti.addAP(credentials[i].ssid, credentials[i].password);
|
||||
}
|
||||
|
||||
WiFi.setHostname("rc-gateway");
|
||||
Serial.println("Connecting to WiFi netowrk.");
|
||||
while (wifiMulti.run() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
}
|
||||
WiFi.setHostname("rc-gateway");
|
||||
currentSSID = WiFi.SSID();
|
||||
currentPsk = WiFi.psk();
|
||||
}
|
||||
|
||||
@ -21,8 +21,9 @@ lib_deps =
|
||||
sui77/rc-switch@^2.6.4
|
||||
bblanchon/ArduinoJson@6.21.5
|
||||
adafruit/Adafruit Unified Sensor@^1.1.4
|
||||
arkhipenko/TaskScheduler@^3.7.0
|
||||
adafruit/DHT sensor library@1.3.2
|
||||
arkhipenko/TaskScheduler@^3.7.0
|
||||
marvinroger/AsyncMqttClient@^0.9.0
|
||||
https://git.hodos.ro/arduino/lib_serial-reader.git@^1.0.0
|
||||
build_flags = -D DHT_SENSOR=0 -D DEBUG_RAW=0
|
||||
check_tool = cppcheck
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user