добавляет HALightBrightness

This commit is contained in:
Maxim Slipenko 2022-12-30 18:31:20 +03:00
parent 820aba5fd7
commit 5e67dc7438
12 changed files with 257 additions and 87 deletions

13
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,13 @@
{
"files.associations": {
"array": "cpp",
"deque": "cpp",
"list": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"string_view": "cpp",
"initializer_list": "cpp",
"ranges": "cpp"
}
}

View File

@ -1,14 +1,18 @@
#pragma once
#include <ESP8266WiFi.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <HALightBrightness.hpp>
class IRLight {
class IRLight: public HALightBrightnessController {
private:
bool state;
short brightness;
IRsend *irsend;
const uint16_t rawDataOn[67] = {
const uint16_t RDOn[67] = {
8980, 4420, 580, 520, 630, 470, 630, 520, 580, 520, 630, 470,
630, 520, 580, 520, 630, 1570, 630, 1620, 580, 1620, 630, 1620,
580, 1570, 680, 1570, 630, 1570, 630, 1570, 630, 520, 630, 1570,
@ -16,7 +20,7 @@ private:
630, 470, 630, 520, 630, 1570, 630, 1570, 630, 1620, 630, 1570,
630, 1570, 630, 1570, 680, 1570, 630};
const uint16_t rawDataOff[67] = {
const uint16_t RDOff[67] = {
8980, 4370, 680, 420, 730, 420, 680, 420, 680, 420, 730, 420,
680, 420, 680, 420, 730, 1470, 730, 1520, 680, 1520, 730, 1470,
730, 1470, 730, 1520, 680, 1520, 730, 1470, 730, 420, 680, 470,
@ -24,14 +28,46 @@ private:
680, 470, 580, 1620, 630, 470, 630, 1570, 630, 520, 580, 520,
630, 1570, 680, 1520, 730, 1520, 680};
const uint16_t RDCentralButton[67] = {
9030, 4320, 680, 470, 630, 470, 680, 420, 680, 470, 630, 470,
680, 420, 680, 470, 630, 1570, 680, 1520, 680, 1520, 680, 1520,
730, 1520, 680, 1520, 680, 1520, 680, 1570, 680, 420, 680, 1520,
680, 470, 680, 1520, 680, 470, 630, 470, 680, 420, 680, 470,
630, 470, 630, 470, 680, 1520, 680, 470, 680, 1520, 630, 1570,
680, 1520, 680, 1570, 680, 1520, 680};
const uint16_t RDBrightnessUp[67] = {
9080,4270, 730,420, 680,420, 680,420, 730,420, 680,420, 680,420, 730,420,
680,1520, 680,1520, 730,1470, 730,1470, 730,1520, 680,1520, 730,1470,
730,1520, 680,420, 680,470, 580,1620, 580,520, 630,520, 580,1620, 580,520,
630,520, 580,520, 630,1570, 580,570, 580,1570, 630,1620, 630,520, 580,1570,
730,1520, 680,1520, 730};
const uint16_t RDBrightnessDown[67] = {
9030, 4320, 630, 520, 630, 470, 630, 470, 630, 520, 630, 470,
630, 470, 630, 520, 630, 1570, 630, 1570, 630, 1620, 630, 1570,
630, 1570, 630, 1620, 630, 1570, 630, 1570, 630, 470, 680, 470,
630, 1570, 630, 1570, 680, 1570, 630, 1570, 630, 470, 680, 470,
630, 470, 630, 1570, 680, 470, 630, 470, 630, 470, 680, 470,
630, 1570, 630, 1570, 680, 1570, 630};
public:
IRLight(int pin);
IRLight(IRsend *irsend);
void setState(bool state);
bool getState();
void on();
void off();
void begin();
bool getState();
void setBrightness(int brightness);
int getBrightness();
void brightnessUp();
void brightnessDown();
int getBrightnessScale();
void begin();
~IRLight();
};

View File

@ -1,32 +1,16 @@
#include <HALight.hpp>
HALight::HALight(PubSubClient &client, const char *name, const char *unique_id)
: HAControllableDevice(client, "light", name, unique_id) {}
HALight::HALight(PubSubClient &client, const char *name, const char *unique_id,
HALightController &baseLight)
: HAControllableDevice(client, "light", name, unique_id),
light(&baseLight) {}
void HALight::sendState() {
StaticJsonDocument<256> doc;
buffer[0] = '\0';
if (light->getState()) {
doc["state"] = "ON";
} else {
doc["state"] = "OFF";
}
serializeJson(doc, buffer, buffer_size);
Serial.println(state_topic);
client.publish(state_topic, buffer, true);
}
void HALight::sendConfig() {
StaticJsonDocument<256> doc;
buffer[0] = '\0';
JSON_DOCUMENT_TYPE HALight::createConfigJSON() {
JSON_DOCUMENT_TYPE doc;
doc["~"] = device_topic;
doc["name"] = name;
doc["unique_id"] = unique_id;
@ -34,6 +18,42 @@ void HALight::sendConfig() {
doc["stat_t"] = "~/state";
doc["schema"] = "json";
return doc;
}
JSON_DOCUMENT_TYPE HALight::createStateJSON() {
JSON_DOCUMENT_TYPE doc;
if (light->getState()) {
doc["state"] = "ON";
} else {
doc["state"] = "OFF";
}
return doc;
}
void HALight::innerHanlder(JSON_DOCUMENT_TYPE &doc) {
if (strcmp(doc["state"], "ON") == 0) {
light->setState(true);
} else {
light->setState(false);
}
}
void HALight::sendState() {
JSON_DOCUMENT_TYPE doc = createStateJSON();
buffer[0] = '\0';
serializeJson(doc, buffer, buffer_size);
Serial.println(state_topic);
client.publish(state_topic, buffer, true);
}
void HALight::sendConfig() {
JSON_DOCUMENT_TYPE doc = createConfigJSON();
buffer[0] = '\0';
serializeJson(doc, buffer, buffer_size);
client.publish(config_topic, buffer, true);
}
@ -42,14 +62,10 @@ void HALight::handle(char *topic, byte *payload, unsigned int length) {
if (strcmp(topic, command_topic) != 0)
return;
StaticJsonDocument<256> doc;
JSON_DOCUMENT_TYPE doc;
deserializeJson(doc, (const byte *)payload, length);
if (strcmp(doc["state"], "ON") == 0) {
light->setState(true);
} else {
light->setState(false);
}
innerHanlder(doc);
sendState();
}

View File

@ -8,10 +8,17 @@ public:
virtual bool getState() = 0;
};
#define JSON_DOCUMENT_TYPE StaticJsonDocument<256>
class HALight : public HAControllableDevice {
private:
HALightController *light;
protected:
virtual JSON_DOCUMENT_TYPE createConfigJSON();
virtual JSON_DOCUMENT_TYPE createStateJSON();
virtual void innerHanlder(JSON_DOCUMENT_TYPE &doc);
HALight(PubSubClient &client, const char *name, const char *unique_id);
public:
HALight(PubSubClient &client, const char *name, const char *unique_id,
HALightController &baseLight);

View File

@ -0,0 +1,37 @@
#include <HALightBrightness.hpp>
HALightBrightness::HALightBrightness(PubSubClient &client, const char *name, const char *unique_id,
HALightBrightnessController &baseLight)
: HALight(client, "light", name, baseLight),
light(&baseLight) {}
JSON_DOCUMENT_TYPE HALightBrightness::createConfigJSON() {
JSON_DOCUMENT_TYPE doc = HALight::createConfigJSON();
doc["brightness"] = true;
doc["brightness_scale"] = light->getBrightnessScale();
// doc["color_mode"] = true;
// doc["supported_color_modes"] =
// JSON_DOCUMENT_TYPE doc;
return doc;
}
JSON_DOCUMENT_TYPE HALightBrightness::createStateJSON() {
JSON_DOCUMENT_TYPE doc = HALight::createStateJSON();
doc["brightness"] = light->getBrightness();
// JSON_DOCUMENT_TYPE doc;
return doc;
}
void HALightBrightness::innerHanlder(JSON_DOCUMENT_TYPE &doc) {
if (doc.containsKey("brightness")) {
light->setBrightness(doc["brightness"]);
}
delay(50);
HALight::innerHanlder(doc);
}

View File

@ -0,0 +1,25 @@
#pragma once
#include <HALight.hpp>
class HALightBrightnessController: public HALightController {
public:
virtual void setBrightness(int value) = 0;
virtual int getBrightness() = 0;
virtual int getBrightnessScale() = 0;
};
class HALightBrightness : public HALight {
private:
HALightBrightnessController *light;
protected:
JSON_DOCUMENT_TYPE createConfigJSON();
JSON_DOCUMENT_TYPE createStateJSON();
void innerHanlder(JSON_DOCUMENT_TYPE &doc);
public:
HALightBrightness(PubSubClient &client, const char *name, const char *unique_id,
HALightBrightnessController &baseLight);
};

View File

@ -1,3 +1,4 @@
#pragma once
#include <HALight.hpp>
#include <HALight.hpp>
#include <HALightBrightness.hpp>

View File

@ -19,3 +19,4 @@ lib_deps =
bblanchon/ArduinoJson@^6.19.4
crankyoldgit/IRremoteESP8266@^2.8.2
monitor_speed = 115200
monitor_filters = default, esp8266_exception_decoder

View File

@ -10,18 +10,72 @@ IRLight::IRLight(IRsend *irsend) {
state = false;
}
void IRLight::setState(bool state) {
if (state)
on();
else
off();
}
void IRLight::on() {
state = true;
irsend->sendRaw(rawDataOn, 67, 38);
irsend->sendRaw(RDOn, 67, 38);
}
void IRLight::off() {
state = false;
irsend->sendRaw(rawDataOff, 67, 38);
irsend->sendRaw(RDOff, 67, 38);
}
void IRLight::begin() { irsend->begin(); }
void IRLight::setBrightness(int newBrightness) {
if (newBrightness > brightness) {
for (int i = 0; i < newBrightness - brightness; i++) {
brightnessUp();
delay(250);
}
} else if (newBrightness < brightness) {
for (int i = 0; i < brightness - newBrightness; i++) {
brightnessDown();
delay(250);
}
}
brightness = newBrightness;
}
bool IRLight::getState() { return state; }
void IRLight::brightnessUp() {
if (brightness < getBrightnessScale()) {
brightness++;
irsend->sendRaw(RDBrightnessUp, 67, 38);
}
}
IRLight::~IRLight() { delete irsend; }
void IRLight::brightnessDown() {
if (brightness > 0) {
brightness--;
irsend->sendRaw(RDBrightnessDown, 67, 38);
}
}
void IRLight::begin() {
irsend->begin();
irsend->sendRaw(RDCentralButton, 67, 38);
brightness = 14;
delay(250);
off();
}
int IRLight::getBrightnessScale() {
return 14;
}
int IRLight::getBrightness() {
return brightness;
}
bool IRLight::getState() {
return state;
}
IRLight::~IRLight() {
delete irsend;
}

View File

@ -12,6 +12,12 @@ void IRLightController::setState(bool state) {
}
}
bool IRLightController::getState() { return irlight.getState(); }
bool IRLightController::getState() {
return irlight.getState();
}
void IRLightController::setBrightness(int value) {
}
IRLightController::~IRLightController() {}

View File

@ -3,7 +3,7 @@
#include <HomeAssistantDevices.hpp>
#include <IRLight.h>
class IRLightController : public HALightController {
class IRLightController : public HALightBrightnessController {
private:
IRLight &irlight;
@ -12,5 +12,10 @@ public:
void begin();
void setState(bool state);
bool getState();
void setBrightness(int value);
int getBrightness();
int getBrightnessScale();
~IRLightController();
};

View File

@ -17,8 +17,7 @@ DHT dht(DHTPIN, DHTTYPE);
IRLight irlight(4);
IRLightController light(irlight);
HALight halight(client, "light", "l-1", light);
HALightBrightness halight(client, "light", "l-1", irlight);
void setup_wifi();
void reconnect();
@ -45,11 +44,19 @@ void setup() {
}
dht.begin();
light.begin();
irlight.begin();
client.subscribe("homeassistant/status");
halight.subscribeToCommandTopic();
if (WiFi.status() == WL_CONNECTED) {
mqtt_publish_config();
delay(100);
// mqtt_publish_state();
halight.sendState();
}
/*
mqtt_publish_config();
if (WiFi.status() == WL_CONNECTED) {
@ -59,6 +66,7 @@ void setup() {
mqtt_publish_state();
halight.sendState();
}
*/
}
void loop() {
@ -67,15 +75,17 @@ void loop() {
}
client.loop();
/*
long now = millis();
if (now - lastMsg > 60000) {
lastMsg = now;
temp = round2(dht.readTemperature());
hum = round2(dht.readHumidity());
mqtt_publish_state();
}
*/
delay(100);
}
@ -93,7 +103,7 @@ void mqtt_callback(char *topic, byte *payload, unsigned int length) {
mqtt_publish_config();
}
}
halight.handle(topic, payload, length);
}
@ -137,51 +147,10 @@ void reconnect() {
}
void mqtt_publish_config() {
/*
StaticJsonDocument<256> doc;
char buffer[256];
doc["device_class"] = "temperature";
doc["name"] = "Temperature";
doc["unit_of_meas"] = "°C";
doc["stat_t"] = HOMEASSISTANT_TOPIC "/state";
doc["value_template"] = "{{value_json.temperature}}";
doc["unique_id"] = "t";
serializeJson(doc, buffer);
client.publish(HOMEASSISTANT_TOPIC "/t/config", buffer, true);
doc["device_class"] = "humidity";
doc["name"] = "Humidity";
doc["unit_of_meas"] = "%";
doc["stat_t"] = HOMEASSISTANT_TOPIC "/state";
doc["value_template"] = "{{value_json.humidity}}";
doc["unique_id"] = "h";
serializeJson(doc, buffer);
client.publish(HOMEASSISTANT_TOPIC "/h/config", buffer, true);
*/
halight.sendConfig();
}
void mqtt_publish_state() {
/*
Serial.println("===== Sending Data =====");
StaticJsonDocument<256> doc;
char buffer[256];
doc["temperature"] = round2(temp);
doc["humidity"] = round2(hum);
Serial.printf("Temperature: %f\n", round2(temp));
Serial.printf("Humidity: %f\n", round2(hum));
serializeJson(doc, buffer);
client.publish(HOMEASSISTANT_TOPIC "/state", buffer, true);
*/
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");