201 lines
4.5 KiB
C++
201 lines
4.5 KiB
C++
#include <DHT.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <IRLight.h>
|
|
#include <IRremoteESP8266.h>
|
|
#include <IRsend.h>
|
|
#include <PubSubClient.h>
|
|
|
|
#include <HomeAssistantDevices.hpp>
|
|
#include <IRLightController.hpp>
|
|
|
|
#include <config.h>
|
|
#include <utils.h>
|
|
|
|
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_publish_config();
|
|
void mqtt_publish_state();
|
|
|
|
float temp;
|
|
float hum;
|
|
long lastMsg = 0;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
setup_wifi();
|
|
|
|
WiFi.setAutoReconnect(true);
|
|
WiFi.persistent(true);
|
|
|
|
client.setServer(MQTT_SERVER, MQTT_PORT);
|
|
client.setCallback(mqtt_callback);
|
|
|
|
if (!client.connected()) {
|
|
reconnect();
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
if (!client.connected()) {
|
|
reconnect();
|
|
}
|
|
client.loop();
|
|
|
|
long now = millis();
|
|
|
|
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) {
|
|
|
|
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) {
|
|
Serial.println("Home Assistant is online");
|
|
mqtt_publish_config();
|
|
}
|
|
}
|
|
|
|
halight.handle(topic, payload, length);
|
|
}
|
|
|
|
void setup_wifi() {
|
|
delay(10);
|
|
// We start by connecting to a WiFi network
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(WIFI_SSID);
|
|
|
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println("");
|
|
Serial.println("WiFi connected");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
void reconnect() {
|
|
// 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
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...");
|
|
// 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);
|
|
}
|
|
}
|
|
} |