-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathswitch.cpp
More file actions
90 lines (81 loc) · 2.47 KB
/
Copy pathswitch.cpp
File metadata and controls
90 lines (81 loc) · 2.47 KB
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
// "Sonoff switch" and "Sonoff touch" support
#include "switch.h"
#include "mqtt-support.h"
// publish PNP information to MQTT
void pnp_switch() {
pnp_mqtt(TOPIC_RELAY, "AC relay", "Relays Switches AC", "I:Switch", NULL, NULL);
pnp_mqtt(TOPIC_AUTO_RELAY, "Auto relay on/off", "Switches", "I:Switch", NULL, NULL);
} // void pnp_switch()
// subscribe to MQTT topics
void subscribe_switch() {
subscribe_mqtt(TOPIC_RELAY);
subscribe_mqtt(TOPIC_AUTO_RELAY);
} // void subscribe_switch()
boolean FlagRelay = true;
boolean relayState = false;
int touchState;
int cntTouch = 0;
// Setup hardware (pins, etc)
void setup_switch() {
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, relayState ? HIGH : LOW);
touchState = digitalRead(BUTTON);
} // void setup_switch()
// must be called from loop()
void loop_switch() {
// Check touch button
if (touchState != digitalRead(BUTTON)) {
cntTouch++;
if (cntTouch > 3) {
touchState = digitalRead(BUTTON);
if (touchState == LOW) {
// Light up LED while button is pressed
analogWrite(LED, 512);
// Auto switch relay
if (FlagRelay) {
relayState = !relayState;
digitalWrite(RELAY, relayState ? HIGH : LOW);
publish_mqttS(TOPIC_RELAY, (char *) (relayState ? "ON" : "OFF"));
}
} else
digitalWrite(LED, HIGH);
}
} else
cntTouch = 0;
} // void loop_switch()
// periodic refreshing values to MQTT
void refresh_switch(boolean flagForce) {
if (flagForce) {
publish_mqttS(TOPIC_RELAY, (char *) (relayState ? "ON" : "OFF"));
publish_mqttS(TOPIC_RELAY, (char *) (relayState ? "ON" : "OFF"), true);
publish_mqttS(TOPIC_AUTO_RELAY, (char *) (FlagRelay ? "ON" : "OFF"), true);
}
} // void refresh_switch(boolean flagForce)
// hook for incoming MQTT messages
boolean mqtt_switch(char *topicCut, char *payload) {
if (cmpTopic(topicCut, TOPIC_RELAY)) {
if (cmpPayloadON(payload)) {
digitalWrite(RELAY, HIGH);
relayState = true;
} else {
digitalWrite(RELAY, LOW);
relayState = false;
}
#ifdef DEBUG
Serial.print("Relay status update: ");
Serial.println(relayState);
#endif
return true;
} else if (cmpTopic(topicCut, TOPIC_AUTO_RELAY)) {
if (cmpPayloadON(payload))
FlagRelay = true;
else
FlagRelay = false;
#ifdef DEBUG
Serial.print("Auto relay switch update: ");
Serial.println(FlagRelay);
#endif
return true;
}
return false;
} // boolean mqtt_switch(char *topicCut, char *payload)