Merge branch 'library' into huzzah
This commit is contained in:
commit
cad27ea106
10
README.md
Normal file
10
README.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# Gateway & sensors
|
||||||
|
|
||||||
|
## Branches
|
||||||
|
Each sensor has a dedicated branch. E.g.:
|
||||||
|
* temp_sensor
|
||||||
|
* oil_sensor
|
||||||
|
|
||||||
|
The gateway uses `master` as the main branch. Other sensors' branches get merged once they are ready for production.
|
||||||
|
|
||||||
|
## Release flow
|
||||||
@ -25,8 +25,8 @@ namespace OilTank {
|
|||||||
return Builder<TemperatureSensor>::instance(id)
|
return Builder<TemperatureSensor>::instance(id)
|
||||||
.asDevice(device)
|
.asDevice(device)
|
||||||
.withValueTemplate("{{ value_json.sensor.temperature }}")
|
.withValueTemplate("{{ value_json.sensor.temperature }}")
|
||||||
.asDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
.addDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
||||||
.asDiagnostic(new BatterySensor{id, "Battery level", "{{ ((states('sensor.servers_room_battery_voltage')|float-2.5)|round(2)*100/2)|int }}"})
|
.addDiagnostic(new BatterySensor{id, "Battery level", "{{ ((states('sensor.servers_room_battery_voltage')|float-2.5)|round(2)*100/2)|int }}"})
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,14 +42,14 @@ namespace OilTank {
|
|||||||
.withDeviceClass("distance")
|
.withDeviceClass("distance")
|
||||||
.withUnitMseasure("cm")
|
.withUnitMseasure("cm")
|
||||||
.withValueTemplate("{{ value_json.sensor.value }}")
|
.withValueTemplate("{{ value_json.sensor.value }}")
|
||||||
.asSecondary(
|
.addSecondary(
|
||||||
Builder<Sensor>::instance(new Sensor{ "Level", id })
|
Builder<Sensor>::instance(new Sensor{ "Level", id })
|
||||||
.withUnitMseasure("%")
|
.withUnitMseasure("%")
|
||||||
.withValueTemplate("{{ 100 - ((value_json.sensor.value-7)|float*100/110)|round(2) }}")
|
.withValueTemplate("{{ 100 - ((value_json.sensor.value-7)|float*100/110)|round(2) }}")
|
||||||
.build()
|
.build()
|
||||||
)
|
)
|
||||||
.asDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
.addDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
||||||
.asDiagnostic(new BatterySensor{id, "Battery level", "{{ ((states('sensor.oil_tank_battery_voltage')|float-3.6)|round(2)*100/1.6)|int }}"})
|
.addDiagnostic(new BatterySensor{id, "Battery level", "{{ ((states('sensor.oil_tank_battery_voltage')|float-3.6)|round(2)*100/1.6)|int }}"})
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ struct PollinSwitch : Switch {
|
|||||||
unsigned char channel;
|
unsigned char channel;
|
||||||
|
|
||||||
PollinSwitch(const char* name, const char* group, const unsigned char channel, const char* area = nullptr)
|
PollinSwitch(const char* name, const char* group, const unsigned char channel, const char* area = nullptr)
|
||||||
: Switch(name, [group, channel]{
|
: Switch(nullptr, [group, channel]{
|
||||||
// copy id from string into a new pointer, to avoid memory leaks
|
// copy id from string into a new pointer, to avoid memory leaks
|
||||||
string s = Protocol_1::buildId(group, channel);
|
string s = Protocol_1::buildId(group, channel);
|
||||||
char* uId = new char[s.length() + 1];
|
char* uId = new char[s.length() + 1];
|
||||||
@ -67,6 +67,7 @@ struct PollinSwitch : Switch {
|
|||||||
return uId;
|
return uId;
|
||||||
}()), group(group), channel(channel) {
|
}()), group(group), channel(channel) {
|
||||||
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Pollin").withArea(area).withParent(gatewayDevice);
|
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Pollin").withArea(area).withParent(gatewayDevice);
|
||||||
|
withStateTopic();
|
||||||
deviceClass = "outlet";
|
deviceClass = "outlet";
|
||||||
p1Switches.insert({ string(id), this });
|
p1Switches.insert({ string(id), this });
|
||||||
}
|
}
|
||||||
@ -83,10 +84,11 @@ struct EasyHomeSwitch : Switch {
|
|||||||
unsigned long off[8] = { 4483146, 4626810, 4661562, 4819642 };
|
unsigned long off[8] = { 4483146, 4626810, 4661562, 4819642 };
|
||||||
|
|
||||||
EasyHomeSwitch(const char* name, const char* id, unsigned long on[4], unsigned long off[4], const char* area = nullptr)
|
EasyHomeSwitch(const char* name, const char* id, unsigned long on[4], unsigned long off[4], const char* area = nullptr)
|
||||||
: Switch(name, id) {
|
: Switch(nullptr, id) {
|
||||||
memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
|
memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
|
||||||
memcpy(&this->off[4], off, 4 * sizeof(unsigned long));
|
memcpy(&this->off[4], off, 4 * sizeof(unsigned long));
|
||||||
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Intertek").withModel("Easy Home").withArea(area).withParent(gatewayDevice);
|
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Intertek").withModel("Easy Home").withArea(area).withParent(gatewayDevice);
|
||||||
|
withStateTopic();
|
||||||
deviceClass = "outlet";
|
deviceClass = "outlet";
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
onSwitches.insert({ this->on[i], this });
|
onSwitches.insert({ this->on[i], this });
|
||||||
@ -120,12 +122,12 @@ Command* commands[] = {
|
|||||||
.withParent(gatewayDevice)
|
.withParent(gatewayDevice)
|
||||||
)
|
)
|
||||||
.build(),
|
.build(),
|
||||||
(new EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, "Basement"})->withStateTopic(),
|
new EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, "Basement"},
|
||||||
(new EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }, "Basement"})->withStateTopic(),
|
new EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }, "Basement"},
|
||||||
(new PollinSwitch{"Meeting sensor", "00001", 1, "Dining room"})->withStateTopic(),
|
new PollinSwitch{"Meeting sensor", "00001", 1, "Dining room"},
|
||||||
(new PollinSwitch{"Fire Tv", "00001", 2, "Living room"})->withStateTopic(),
|
new PollinSwitch{"Fire Tv", "00001", 2, "Living room"},
|
||||||
(new PollinSwitch{"Diningroom player", "00001", 3, "Dining room"})->withStateTopic(),
|
new PollinSwitch{"Diningroom player", "00001", 3, "Dining room"},
|
||||||
(new PollinSwitch{"Train", "11111", 4, "Playroom"})->withStateTopic()
|
new PollinSwitch{"Train", "11111", 4, "Playroom"}
|
||||||
};
|
};
|
||||||
|
|
||||||
Sensor* sensors[] = {
|
Sensor* sensors[] = {
|
||||||
|
|||||||
@ -1,314 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
#define JSON_SIZE 512
|
|
||||||
#define TOPIC_SIZE 255
|
|
||||||
|
|
||||||
namespace Ha {
|
|
||||||
uint16_t(*publisher)(const char* topic, const char* message);
|
|
||||||
typedef void (*onMessage)(const char* msg);
|
|
||||||
|
|
||||||
struct DeviceConfig {
|
|
||||||
const char* id = nullptr;
|
|
||||||
const char* name = nullptr;
|
|
||||||
const char* model = nullptr;
|
|
||||||
const char* manufacturer = nullptr;
|
|
||||||
const char* area = nullptr;
|
|
||||||
const DeviceConfig* parent = nullptr;
|
|
||||||
|
|
||||||
static DeviceConfig& create(const char* id) {
|
|
||||||
return *(new DeviceConfig{ id });
|
|
||||||
}
|
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) {
|
|
||||||
JsonObject device = jsonDoc.createNestedObject("device");
|
|
||||||
if (name) device["name"] = name;
|
|
||||||
if (model) device["model"] = model;
|
|
||||||
if (manufacturer) device["manufacturer"] = manufacturer;
|
|
||||||
if (area) device["suggested_area"] = area;
|
|
||||||
if (parent) device["via_device"] = parent->id;
|
|
||||||
JsonArray identifiers = device.createNestedArray("identifiers");
|
|
||||||
identifiers.add(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
DeviceConfig& withName(const char* value) {
|
|
||||||
name = value;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
DeviceConfig& withModel(const char* value) {
|
|
||||||
model = value;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
DeviceConfig& withManufacturer(const char* value) {
|
|
||||||
manufacturer = value;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
DeviceConfig& withArea(const char* value) {
|
|
||||||
area = value;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
DeviceConfig& withParent(const DeviceConfig* deviceConfig) {
|
|
||||||
parent = deviceConfig;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
DeviceConfig(const char* id) : id(id) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Component {
|
|
||||||
const char* entityCategory = nullptr;
|
|
||||||
const char* deviceClass = nullptr;
|
|
||||||
const char* name = nullptr;
|
|
||||||
char* id = nullptr;
|
|
||||||
const char* type = nullptr;
|
|
||||||
char configTopic[TOPIC_SIZE];
|
|
||||||
DeviceConfig* mainDevice = nullptr;
|
|
||||||
static List<Component> components;
|
|
||||||
|
|
||||||
Component(const char* name, const char* id, const char* type) : name(name), id((char*)id), type(type) {
|
|
||||||
components.add(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void buildUniqueId(char* uniqueId) {
|
|
||||||
sprintf(uniqueId, "%s_%s", MAIN_DEVICE_ID, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void buildConfigTopic() {
|
|
||||||
sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void buildConfig(JsonDocument& jsonDoc) {
|
|
||||||
if (mainDevice) mainDevice->buildConfig(jsonDoc);
|
|
||||||
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
|
|
||||||
if (deviceClass) jsonDoc["device_class"] = deviceClass;
|
|
||||||
jsonDoc["name"] = name;
|
|
||||||
char uniqueId[50];
|
|
||||||
buildUniqueId(uniqueId);
|
|
||||||
jsonDoc["unique_id"] = uniqueId;
|
|
||||||
buildConfigTopic();
|
|
||||||
}
|
|
||||||
|
|
||||||
void publishConfig() {
|
|
||||||
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
|
||||||
buildConfig(jsonDoc);
|
|
||||||
|
|
||||||
char message[JSON_SIZE];
|
|
||||||
serializeJson(jsonDoc, message);
|
|
||||||
|
|
||||||
publisher(configTopic, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
void publishCleanupConfig() {
|
|
||||||
publisher(configTopic, "");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AbstractBuilder {
|
|
||||||
static List<AbstractBuilder> builders;
|
|
||||||
|
|
||||||
AbstractBuilder() {
|
|
||||||
builders.add(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void deleteAll() {
|
|
||||||
builders.forEach([](AbstractBuilder* builder) { delete builder; });
|
|
||||||
builders.empty();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class T>
|
|
||||||
struct Builder : AbstractBuilder {
|
|
||||||
T* cmp;
|
|
||||||
|
|
||||||
Builder(T* cmp) : AbstractBuilder(), cmp(cmp) {}
|
|
||||||
|
|
||||||
Builder(const char* id) : AbstractBuilder() {
|
|
||||||
cmp = new T{ id };
|
|
||||||
}
|
|
||||||
|
|
||||||
static Builder& instance(T* c) {
|
|
||||||
return *(new Builder<T>(c));
|
|
||||||
}
|
|
||||||
|
|
||||||
static Builder& instance(const char* id) {
|
|
||||||
return *(new Builder<T>(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
T* build() {
|
|
||||||
return static_cast<T*>(cmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
Builder& withDeviceClass(const char* value) {
|
|
||||||
cmp->deviceClass = value;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Builder& withUnitMseasure(const char* value) {
|
|
||||||
cmp->unitMeasure = value;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Builder& withValueTemplate(const char* value) {
|
|
||||||
cmp->valueTemplate = value;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Builder& asSecondary(Component* c) {
|
|
||||||
c->mainDevice = &DeviceConfig::create(cmp->id);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Builder& asDiagnostic(Component* c) {
|
|
||||||
c->entityCategory = "diagnostic";
|
|
||||||
return asSecondary(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
Builder& asDevice(DeviceConfig* deviceConfig) {
|
|
||||||
cmp->mainDevice = deviceConfig;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Command : Component {
|
|
||||||
char commandTopic[TOPIC_SIZE];
|
|
||||||
onMessage f;
|
|
||||||
static unordered_map<string, Command*> mapCommands;
|
|
||||||
|
|
||||||
Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type), f(f) {
|
|
||||||
sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id);
|
|
||||||
mapCommands.insert({ string(commandTopic), this });
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void onCommand(const char* msg) {
|
|
||||||
if (f) f(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
|
||||||
Component::buildConfig(jsonDoc);
|
|
||||||
jsonDoc["command_topic"] = commandTopic;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Button : Command {
|
|
||||||
|
|
||||||
Button(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "button", f) {}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class T>
|
|
||||||
struct StateConfig {
|
|
||||||
char stateTopic[TOPIC_SIZE];
|
|
||||||
|
|
||||||
T* withStateTopic() {
|
|
||||||
sprintf(stateTopic, "homeassistant/%s/%s/%s/state", ((T*)this)->type, MAIN_DEVICE_ID, ((T*)this)->id);
|
|
||||||
return static_cast<T*>(this);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Switch : Command, StateConfig<Switch> {
|
|
||||||
|
|
||||||
Switch(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "switch", f) {}
|
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
|
||||||
Command::buildConfig(jsonDoc);
|
|
||||||
jsonDoc["name"] = nullptr;
|
|
||||||
// jsonDoc["retain"] = true;
|
|
||||||
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
|
|
||||||
}
|
|
||||||
|
|
||||||
void updateState(bool state) {
|
|
||||||
publisher(stateTopic, state ? "ON" : "OFF");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Sensor : Component, StateConfig<Sensor> {
|
|
||||||
const char* unitMeasure = nullptr;
|
|
||||||
const char* valueTemplate = nullptr;
|
|
||||||
static unordered_map<string, Sensor*> mapSensors;
|
|
||||||
|
|
||||||
Sensor(const char* name, const char* id) : Component(name, id, "sensor") {
|
|
||||||
withStateTopic();
|
|
||||||
mapSensors.insert({ id, this });
|
|
||||||
}
|
|
||||||
|
|
||||||
void buildUniqueId(char* uniqueId) override {
|
|
||||||
if (deviceClass) {
|
|
||||||
sprintf(uniqueId, "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id);
|
|
||||||
} else {
|
|
||||||
Component::buildUniqueId(uniqueId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void buildConfigTopic() override {
|
|
||||||
if (deviceClass) {
|
|
||||||
sprintf(configTopic, "homeassistant/%s/%s/%s_%s/config", type, MAIN_DEVICE_ID, deviceClass, id);
|
|
||||||
} else {
|
|
||||||
Component::buildConfigTopic();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
|
||||||
Component::buildConfig(jsonDoc);
|
|
||||||
if (unitMeasure) jsonDoc["unit_of_measurement"] = unitMeasure;
|
|
||||||
if (valueTemplate) jsonDoc["value_template"] = valueTemplate;
|
|
||||||
jsonDoc["state_topic"] = stateTopic;
|
|
||||||
jsonDoc["suggested_display_precision"] = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
void updateState(const char* message) {
|
|
||||||
publisher(stateTopic, message);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TemperatureSensor : Sensor {
|
|
||||||
TemperatureSensor(const char* id, const char* name = "Temperature") : Sensor(name, id) {
|
|
||||||
deviceClass = "temperature";
|
|
||||||
unitMeasure = "°C";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct VoltageSensor : Sensor {
|
|
||||||
VoltageSensor(const char* id, const char* name, const char* valueTemplate) : Sensor(name, id) {
|
|
||||||
this->valueTemplate = valueTemplate;
|
|
||||||
deviceClass = "voltage";
|
|
||||||
unitMeasure = "V";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct BatterySensor : Sensor {
|
|
||||||
BatterySensor(const char* id, const char* name, const char* valueTemplate) : Sensor(name, id) {
|
|
||||||
this->valueTemplate = valueTemplate;
|
|
||||||
deviceClass = "battery";
|
|
||||||
unitMeasure = "%";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct HumiditySensor : Sensor {
|
|
||||||
HumiditySensor(const char* id, const char* name = "Humidity") : Sensor(name, id) {
|
|
||||||
deviceClass = "humidity";
|
|
||||||
unitMeasure = "%";
|
|
||||||
// valueTemplate = "{{ value_json.sensor.humidity }}";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct PressureSensor : Sensor {
|
|
||||||
PressureSensor(const char* id, const char* name = "Pressure") : Sensor(name, id) {
|
|
||||||
deviceClass = "pressure";
|
|
||||||
unitMeasure = "hPa";
|
|
||||||
// valueTemplate = "{{ value_json.sensor.pressure }}";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
List<Component> Component::components;
|
|
||||||
List<AbstractBuilder> AbstractBuilder::builders;
|
|
||||||
unordered_map<string, Command*> Command::mapCommands;
|
|
||||||
unordered_map<string, Sensor*> Sensor::mapSensors;
|
|
||||||
}
|
|
||||||
@ -1,13 +1,15 @@
|
|||||||
#include <TaskScheduler.h>
|
#include <TaskScheduler.h>
|
||||||
|
|
||||||
|
#define MQTT_HOST IPAddress(192, 168, 5, 11)
|
||||||
|
#define MQTT_PORT 1883
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Scheduler ts;
|
Scheduler ts;
|
||||||
|
|
||||||
void turnLed(uint8_t led, bool on = true) {
|
#include "devices.h"
|
||||||
on ? digitalWrite(led, LOW) : digitalWrite(led, HIGH);
|
#include "mqtt.h"
|
||||||
}
|
#include "ota.h"
|
||||||
|
|
||||||
#include "wifi.h"
|
#include "wifi.h"
|
||||||
|
|
||||||
namespace Board {
|
namespace Board {
|
||||||
@ -19,6 +21,10 @@ namespace Board {
|
|||||||
}
|
}
|
||||||
}, &ts);
|
}, &ts);
|
||||||
|
|
||||||
|
void turnLed(uint8_t led, bool on = true) {
|
||||||
|
on ? digitalWrite(led, LOW) : digitalWrite(led, HIGH);
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// Serial.begin(9600, SERIAL_8N1, SERIAL_TX_ONLY);
|
// Serial.begin(9600, SERIAL_8N1, SERIAL_TX_ONLY);
|
||||||
|
|
||||||
@ -27,9 +33,12 @@ namespace Board {
|
|||||||
turnLed(RED_LED, false);
|
turnLed(RED_LED, false);
|
||||||
turnLed(BLUE_LED);
|
turnLed(BLUE_LED);
|
||||||
|
|
||||||
|
Mqtt::setup(&ts,
|
||||||
|
[] {turnLed(BLUE_LED, false);},
|
||||||
|
[] {turnLed(BLUE_LED);}
|
||||||
|
);
|
||||||
Wifi::setup();
|
Wifi::setup();
|
||||||
Ota::setup();
|
Ota::setup();
|
||||||
Mqtt::setup();
|
|
||||||
|
|
||||||
tReadCommand.enable();
|
tReadCommand.enable();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,68 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <AsyncMqttClient.h>
|
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include "devices.h"
|
|
||||||
|
|
||||||
|
|
||||||
#define MQTT_HOST IPAddress(192, 168, 5, 11)
|
|
||||||
#define MQTT_PORT 1883
|
|
||||||
|
|
||||||
namespace Mqtt {
|
|
||||||
|
|
||||||
AsyncMqttClient client;
|
|
||||||
|
|
||||||
Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, []{
|
|
||||||
Serial.println("Connecting to MQTT...");
|
|
||||||
client.connect();
|
|
||||||
}, &ts);
|
|
||||||
void publishInit();
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
void publishInit() {
|
|
||||||
Ha::publisher = publish;
|
|
||||||
Component::components.forEach([](Component* c) { c->publishConfig(); });
|
|
||||||
AbstractBuilder::deleteAll();
|
|
||||||
ts.deleteTask(tPublishInit);
|
|
||||||
}
|
|
||||||
|
|
||||||
void publishCleanupConfig() {
|
|
||||||
Component::components.forEach([](Component* c) { c->publishCleanupConfig(); });
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
Command* cmd = Command::mapCommands[string{ topic }];
|
|
||||||
if (cmd) cmd->onCommand(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
client.onConnect([](bool sessionPresent) {
|
|
||||||
tPublishInit.enable();
|
|
||||||
client.subscribe(mainTopic, 0);
|
|
||||||
tReConnect.disable();
|
|
||||||
Serial.println("Connected to MQTT");
|
|
||||||
turnLed(BLUE_LED, false);
|
|
||||||
});
|
|
||||||
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
|
|
||||||
tReConnect.enableDelayed();
|
|
||||||
Serial.println("Disconnected from MQTT");
|
|
||||||
turnLed(BLUE_LED);
|
|
||||||
});
|
|
||||||
client.onMessage(onMessage);
|
|
||||||
client.setServer(MQTT_HOST, MQTT_PORT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,35 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
template <class T>
|
|
||||||
struct List {
|
|
||||||
struct Container {
|
|
||||||
T* t;
|
|
||||||
Container* next = nullptr;
|
|
||||||
Container(T* t) : t(t) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
Container* first = nullptr;
|
|
||||||
Container* last = nullptr;
|
|
||||||
|
|
||||||
void add(T* t) {
|
|
||||||
Container* c = new Container{t};
|
|
||||||
first == nullptr ? first = c : last->next = c;
|
|
||||||
last = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
void forEach(void(*f)(T*)) {
|
|
||||||
for (Container *c = first; c; c = c->next) {
|
|
||||||
f(c->t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void empty() {
|
|
||||||
Container *c = first;
|
|
||||||
while (c) {
|
|
||||||
auto n = c->next;
|
|
||||||
delete c;
|
|
||||||
c = n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
@ -1,8 +1,6 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266WiFiMulti.h>
|
#include <ESP8266WiFiMulti.h>
|
||||||
#include <ESP8266mDNS.h>
|
#include <ESP8266mDNS.h>
|
||||||
#include "mqtt.h"
|
|
||||||
#include "ota.h"
|
|
||||||
#include "credentials.h"
|
#include "credentials.h"
|
||||||
|
|
||||||
namespace Wifi {
|
namespace Wifi {
|
||||||
@ -30,12 +28,11 @@ namespace Wifi {
|
|||||||
printStatus();
|
printStatus();
|
||||||
tReconnect.cancel();
|
tReconnect.cancel();
|
||||||
Ota::tLoop.enable();
|
Ota::tLoop.enable();
|
||||||
Mqtt::tReConnect.restart();
|
Mqtt::tReConnect.enable();
|
||||||
});
|
});
|
||||||
|
|
||||||
stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
|
stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
|
||||||
Serial.println("Disconnected from network.");
|
Serial.println("Disconnected from network.");
|
||||||
tReconnect.restartDelayed();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -9,12 +9,9 @@
|
|||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[platformio]
|
[platformio]
|
||||||
default_envs = pro-mini
|
default_envs = huzzah
|
||||||
|
|
||||||
[env:huzzah]
|
[env]
|
||||||
platform = espressif8266
|
|
||||||
board = huzzah
|
|
||||||
framework = arduino
|
|
||||||
lib_extra_dirs =
|
lib_extra_dirs =
|
||||||
../libraries
|
../libraries
|
||||||
lib_deps =
|
lib_deps =
|
||||||
@ -22,65 +19,37 @@ lib_deps =
|
|||||||
bblanchon/ArduinoJson@6.21.5
|
bblanchon/ArduinoJson@6.21.5
|
||||||
adafruit/Adafruit Unified Sensor@^1.1.4
|
adafruit/Adafruit Unified Sensor@^1.1.4
|
||||||
adafruit/DHT sensor library@1.3.2
|
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
|
https://git.hodos.ro/arduino/lib_serial-reader.git@^1.0.0
|
||||||
build_flags = -D DHT_SENSOR=0 -D DEBUG_RAW=0
|
build_flags = -D DHT_SENSOR=0 -D DEBUG_RAW=0
|
||||||
check_tool = cppcheck
|
check_tool = cppcheck
|
||||||
check_flags = --enable=all
|
check_flags = --enable=all
|
||||||
check_skip_packages = yes
|
check_skip_packages = yes
|
||||||
check_severity = medium, high
|
check_severity = medium, high
|
||||||
|
|
||||||
|
[env:huzzah]
|
||||||
|
platform = espressif8266
|
||||||
|
board = huzzah
|
||||||
|
framework = arduino
|
||||||
|
lib_deps =
|
||||||
|
${env.lib_deps}
|
||||||
|
https://git.hodos.ro/arduino/ha-mqtt.git@^1.0.0
|
||||||
upload_port = 192.168.6.161
|
upload_port = 192.168.6.161
|
||||||
upload_protocol = espota
|
upload_protocol = espota
|
||||||
upload_flags =
|
upload_flags =
|
||||||
--host_port=10000
|
--host_port=10000
|
||||||
|
|
||||||
|
[env:huzzah_dev]
|
||||||
|
extends = env:huzzah
|
||||||
|
build_flags = ${env.build_flags} -D MQTT_CLEANUP=1
|
||||||
|
|
||||||
[env:pro-mini]
|
[env:pro-mini]
|
||||||
platform = atmelavr
|
platform = atmelavr
|
||||||
board = pro16MHzatmega328
|
board = pro16MHzatmega328
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_extra_dirs =
|
|
||||||
../libraries
|
|
||||||
lib_deps =
|
|
||||||
sui77/rc-switch@^2.6.3
|
|
||||||
bblanchon/ArduinoJson@6.21.5
|
|
||||||
adafruit/Adafruit Unified Sensor@^1.1.4
|
|
||||||
adafruit/DHT sensor library@1.3.2
|
|
||||||
https://git.hodos.ro/arduino/lib_serial-reader.git@^1.0.0
|
|
||||||
build_flags = -D DHT_SENSOR=0 -D DEBUG_RAW=0
|
|
||||||
upload_port = /dev/ttyUSB0
|
|
||||||
check_tool = cppcheck
|
|
||||||
check_flags = --enable=all
|
|
||||||
check_skip_packages = yes
|
|
||||||
check_severity = medium, high
|
|
||||||
|
|
||||||
[env:native]
|
[env:native]
|
||||||
platform = native
|
platform = native
|
||||||
test_filter = native/*
|
test_filter = native/*
|
||||||
lib_extra_dirs =
|
|
||||||
../libraries
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
bblanchon/ArduinoJson@6.19.4
|
bblanchon/ArduinoJson@6.21.5
|
||||||
build_flags = -std=c++11
|
debug_build_flags = -std=c++11
|
||||||
|
|
||||||
[env:embedded]
|
|
||||||
platform = atmelavr
|
|
||||||
framework = arduino
|
|
||||||
board = pro16MHzatmega328
|
|
||||||
lib_extra_dirs =
|
|
||||||
../libraries
|
|
||||||
lib_deps =
|
|
||||||
sui77/rc-switch@^2.6.3
|
|
||||||
bblanchon/ArduinoJson@6.19.4
|
|
||||||
|
|
||||||
platform_packages =
|
|
||||||
platformio/tool-simavr
|
|
||||||
test_speed = 9600
|
|
||||||
test_testing_command =
|
|
||||||
${platformio.packages_dir}/tool-simavr/bin/simavr
|
|
||||||
-m
|
|
||||||
atmega328p
|
|
||||||
-f
|
|
||||||
16000000L
|
|
||||||
${platformio.build_dir}/${this.__env__}/firmware.elf
|
|
||||||
test_filter = embedded/*
|
|
||||||
|
|||||||
@ -1,18 +0,0 @@
|
|||||||
#include <Arduino.h>
|
|
||||||
#include <unity.h>
|
|
||||||
|
|
||||||
void setUp(void) {
|
|
||||||
// set stuff up here
|
|
||||||
}
|
|
||||||
|
|
||||||
void tearDown(void) {
|
|
||||||
// clean stuff up here
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
UNITY_BEGIN();
|
|
||||||
UNITY_END();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user