начинает создавать библиотеку HA
This commit is contained in:
0
src/DHTSensor.cpp
Normal file
0
src/DHTSensor.cpp
Normal file
27
src/IRLight.cpp
Normal file
27
src/IRLight.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <IRLight.h>
|
||||
|
||||
IRLight::IRLight(int pin) {
|
||||
irsend = new IRsend(pin);
|
||||
state = false;
|
||||
}
|
||||
|
||||
IRLight::IRLight(IRsend *irsend) {
|
||||
irsend = irsend;
|
||||
state = false;
|
||||
}
|
||||
|
||||
void IRLight::on() {
|
||||
state = true;
|
||||
irsend->sendRaw(rawDataOn, 67, 38);
|
||||
}
|
||||
|
||||
void IRLight::off() {
|
||||
state = false;
|
||||
irsend->sendRaw(rawDataOff, 67, 38);
|
||||
}
|
||||
|
||||
void IRLight::begin() { irsend->begin(); }
|
||||
|
||||
bool IRLight::getState() { return state; }
|
||||
|
||||
IRLight::~IRLight() { delete irsend; }
|
17
src/IRLightController.cpp
Normal file
17
src/IRLightController.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <IRLightController.hpp>
|
||||
|
||||
IRLightController::IRLightController(IRLight &irlight) : irlight(irlight) {}
|
||||
|
||||
void IRLightController::begin() { irlight.begin(); };
|
||||
|
||||
void IRLightController::setState(bool state) {
|
||||
if (state) {
|
||||
irlight.on();
|
||||
} else {
|
||||
irlight.off();
|
||||
}
|
||||
}
|
||||
|
||||
bool IRLightController::getState() { return irlight.getState(); }
|
||||
|
||||
IRLightController::~IRLightController() {}
|
16
src/IRLightController.hpp
Normal file
16
src/IRLightController.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <HomeAssistantDevices.hpp>
|
||||
#include <IRLight.h>
|
||||
|
||||
class IRLightController : public HALightController {
|
||||
private:
|
||||
IRLight &irlight;
|
||||
|
||||
public:
|
||||
IRLightController(IRLight &irlight);
|
||||
void begin();
|
||||
void setState(bool state);
|
||||
bool getState();
|
||||
~IRLightController();
|
||||
};
|
82
src/main.cpp
82
src/main.cpp
@@ -1,8 +1,13 @@
|
||||
#include <Arduino.h>
|
||||
#include <DHT.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <IRLight.h>
|
||||
#include <IRremoteESP8266.h>
|
||||
#include <IRsend.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <HomeAssistantDevices.hpp>
|
||||
#include <IRLightController.hpp>
|
||||
|
||||
#include <config.h>
|
||||
#include <utils.h>
|
||||
|
||||
@@ -10,9 +15,14 @@ WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
IRLight irlight(4);
|
||||
|
||||
IRLightController light(irlight);
|
||||
HALight halight(client, "light", "l-1", light);
|
||||
|
||||
void setup_wifi();
|
||||
void reconnect();
|
||||
void mqtt_callback(char* topic, byte* payload, unsigned int length);
|
||||
void mqtt_callback(char *topic, byte *payload, unsigned int length);
|
||||
void mqtt_publish_config();
|
||||
void mqtt_publish_state();
|
||||
|
||||
@@ -33,17 +43,21 @@ void setup() {
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.subscribe("homeassistant/status");
|
||||
|
||||
mqtt_publish_config();
|
||||
|
||||
dht.begin();
|
||||
light.begin();
|
||||
|
||||
client.subscribe("homeassistant/status");
|
||||
halight.subscribeToCommandTopic();
|
||||
|
||||
mqtt_publish_config();
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
temp = dht.readTemperature();
|
||||
hum = dht.readHumidity();
|
||||
|
||||
mqtt_publish_state();
|
||||
halight.sendState();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,21 +67,34 @@ void loop() {
|
||||
}
|
||||
client.loop();
|
||||
|
||||
temp = round2(dht.readTemperature());
|
||||
hum = round2(dht.readHumidity());
|
||||
mqtt_publish_state();
|
||||
long now = millis();
|
||||
|
||||
delay(60000);
|
||||
if (now - lastMsg > 60000) {
|
||||
lastMsg = now;
|
||||
temp = round2(dht.readTemperature());
|
||||
hum = round2(dht.readHumidity());
|
||||
mqtt_publish_state();
|
||||
}
|
||||
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
|
||||
void mqtt_callback(char *topic, byte *payload, unsigned int length) {
|
||||
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
Serial.println();
|
||||
|
||||
if (strcmp(topic, "homeassistant/status") == 0) {
|
||||
payload[length] = '\0';
|
||||
if (strcmp((char*)payload, "online") == 0) {
|
||||
if (strcmp((char *)payload, "online") == 0) {
|
||||
Serial.println("Home Assistant is online");
|
||||
mqtt_publish_config();
|
||||
}
|
||||
}
|
||||
|
||||
halight.handle(topic, payload, length);
|
||||
}
|
||||
|
||||
void setup_wifi() {
|
||||
@@ -110,33 +137,37 @@ 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["state_topic"] = HOMEASSISTANT_TOPIC"/state";
|
||||
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);
|
||||
client.publish(HOMEASSISTANT_TOPIC "/t/config", buffer, true);
|
||||
|
||||
doc["device_class"] = "humidity";
|
||||
doc["name"] = "Humidity";
|
||||
doc["unit_of_meas"] = "%";
|
||||
doc["state_topic"] = HOMEASSISTANT_TOPIC"/state";
|
||||
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);
|
||||
client.publish(HOMEASSISTANT_TOPIC "/h/config", buffer, true);
|
||||
*/
|
||||
halight.sendConfig();
|
||||
}
|
||||
|
||||
void mqtt_publish_state() {
|
||||
/*
|
||||
Serial.println("===== Sending Data =====");
|
||||
StaticJsonDocument<256> doc;
|
||||
char buffer[256];
|
||||
@@ -149,5 +180,22 @@ void mqtt_publish_state() {
|
||||
|
||||
serializeJson(doc, buffer);
|
||||
|
||||
client.publish(HOMEASSISTANT_TOPIC"/state", buffer, true);
|
||||
client.publish(HOMEASSISTANT_TOPIC "/state", buffer, true);
|
||||
*/
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
// Attempt to connect
|
||||
// If you do not want to use a username and password, change next line to
|
||||
// if (client.connect("ESP8266Client")) {
|
||||
if (client.connect("ESP8266Client", MQTT_LOGIN, MQTT_PASS)) {
|
||||
Serial.println("connected");
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,3 +1 @@
|
||||
double round2 (double value) {
|
||||
return (int)(value * 100 + 0.5) / 100.0;
|
||||
}
|
||||
double round2(double value) { return (int)(value * 100 + 0.5) / 100.0; }
|
Reference in New Issue
Block a user