-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
hvac.ino
109 lines (88 loc) · 2.94 KB
/
hvac.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <ESP8266WiFi.h>
#include <ArduinoHA.h>
#define BROKER_ADDR IPAddress(192,168,0,17)
#define WIFI_SSID "MyNetwork"
#define WIFI_PASSWORD "MyPassword"
WiFiClient client;
HADevice device;
HAMqtt mqtt(client, device);
// By default HAHVAC supports only reporting of the temperature.
// You can enable feature you need using the second argument of the constructor.
// Please check the documentation of the HAHVAC class.
HAHVAC hvac(
"my_name",
HAHVAC::TargetTemperatureFeature | HAHVAC::PowerFeature | HAHVAC::ModesFeature
);
unsigned long lastTempPublishAt = 0;
float lastTemp = 0;
void onTargetTemperatureCommand(HANumeric temperature, HAHVAC* sender) {
float temperatureFloat = temperature.toFloat();
Serial.print("Target temperature: ");
Serial.println(temperatureFloat);
sender->setTargetTemperature(temperature); // report target temperature back to the HA panel
}
void onPowerCommand(bool state, HAHVAC* sender) {
if (state) {
Serial.println("Power on");
} else {
Serial.println("Power off");
}
}
void onModeCommand(HAHVAC::Mode mode, HAHVAC* sender) {
Serial.print("Mode: ");
if (mode == HAHVAC::OffMode) {
Serial.println("off");
} else if (mode == HAHVAC::AutoMode) {
Serial.println("auto");
} else if (mode == HAHVAC::CoolMode) {
Serial.println("cool");
} else if (mode == HAHVAC::HeatMode) {
Serial.println("heat");
} else if (mode == HAHVAC::DryMode) {
Serial.println("dry");
} else if (mode == HAHVAC::FanOnlyMode) {
Serial.println("fan only");
}
sender->setMode(mode); // report mode back to the HA panel
}
void setup() {
Serial.begin(9600);
Serial.println("Starting...");
// Unique ID must be set!
byte mac[WL_MAC_ADDR_LENGTH];
WiFi.macAddress(mac);
device.setUniqueId(mac, sizeof(mac));
// connect to wifi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500); // waiting for the connection
}
Serial.println();
Serial.println("Connected to the network");
// set device's details (optional)
device.setName("NodeMCU");
device.setSoftwareVersion("1.0.0");
// assign callbacks (optional)
hvac.onTargetTemperatureCommand(onTargetTemperatureCommand);
hvac.onPowerCommand(onPowerCommand);
hvac.onModeCommand(onModeCommand);
// configure HVAC (optional)
hvac.setName("My HVAC");
hvac.setMinTemp(10);
hvac.setMaxTemp(30);
hvac.setTempStep(0.5);
// You can set retain flag for the HA commands
// hvac.setRetain(true);
// You can choose which modes should be available in the HA panel
// hvac.setModes(HAHVAC::OffMode | HAHVAC::CoolMode);
mqtt.begin(BROKER_ADDR);
}
void loop() {
mqtt.loop();
if ((millis() - lastTempPublishAt) > 3000) {
hvac.setCurrentTemperature(lastTemp);
lastTempPublishAt = millis();
lastTemp += 0.5;
}
}