forked from brainrecall/arduino-home-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover.ino
54 lines (43 loc) · 1.52 KB
/
cover.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
#include <Ethernet.h>
#include <ArduinoHA.h>
#define BROKER_ADDR IPAddress(192,168,0,17)
byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
EthernetClient client;
HADevice device(mac, sizeof(mac));
HAMqtt mqtt(client, device);
HACover cover("my-cover", false, false); // "my-cover" is unique ID of the cover. You should define your own ID. False and false are to keep stop and position enabled.
void onCoverCommand(HACover::CoverCommand cmd) {
if (cmd == HACover::CommandOpen) {
Serial.println("Command: Open");
cover.setState(HACover::StateOpening);
} else if (cmd == HACover::CommandClose) {
Serial.println("Command: Close");
cover.setState(HACover::StateClosing);
} else if (cmd == HACover::CommandStop) {
Serial.println("Command: Stop");
cover.setState(HACover::StateStopped);
}
// Available states:
// HACover::StateClosed
// HACover::StateClosing
// HACover::StateOpen
// HACover::StateOpening
// HACover::StateStopped
// You can also report position using setPosition() method
}
void onCoverSetPositionCommand(uint8_t pos) {
Serial.println("Set Position Command: " + String(pos));
}
void setup() {
Serial.begin(9600);
Ethernet.begin(mac);
cover.onCommand(onCoverCommand);
cover.onSetPositionCommand(onCoverSetPositionCommand);
cover.setName("My cover"); // optional
// cover.setRetain(true); // optionally you can set retain flag
mqtt.begin(BROKER_ADDR);
}
void loop() {
Ethernet.maintain();
mqtt.loop();
}