This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
multi-topic-button.ino
251 lines (214 loc) · 6.78 KB
/
multi-topic-button.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "mqtt-wrapper.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#ifndef NAME
#define NAME "NEW-multi-topic-button"
#endif
#ifndef TOPIC
#define TOPIC "i3/program-me/NEW-multi-topic-button"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "i3detroit-wpa"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "i3detroit"
#endif
#ifndef MQTT_SERVER
#define MQTT_SERVER "10.13.0.22"
#endif
#ifndef MQTT_PORT
#define MQTT_PORT 1883
#endif
#ifndef TOPIC_0_ON_PIN
#define TOPIC_0_ON_PIN 0
#endif
#ifndef TOPIC_0_OFF_PIN
#define TOPIC_0_OFF_PIN 2
#endif
#ifndef TOPIC_0_TOGGLE_PIN
#define TOPIC_0_TOGGLE_PIN 4
#endif
#ifndef TOPIC_0_LED_ON
#define TOPIC_0_LED_ON 5
#endif
#ifndef TOPIC_0_LED_OFF
#define TOPIC_0_LED_OFF 12
#endif
#ifndef TOPIC_0_TOPIC
#define TOPIC_0_TOPIC "i3/program-me/not-a-topic"
#endif
//Assume if topic 2-whatever is defined at all, everything is defined.
const char* host_name = NAME;
const char* fullTopic = TOPIC;
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
const char* mqtt_server = MQTT_SERVER;
const int mqtt_port = MQTT_PORT;
char buf[1024];
char topicBuf[1024];
struct mqtt_wrapper_options mqtt_options;
// button pins
#ifndef TOPIC_1_TOPIC
const int button_pins[] = {TOPIC_0_ON_PIN, TOPIC_0_OFF_PIN, TOPIC_0_TOGGLE_PIN};
#else
#ifndef TOPIC_2_TOPIC
const int button_pins[] = {TOPIC_0_ON_PIN, TOPIC_0_OFF_PIN, TOPIC_0_TOGGLE_PIN, TOPIC_1_ON_PIN, TOPIC_1_OFF_PIN, TOPIC_1_TOGGLE_PIN};
#else
const int button_pins[] = {TOPIC_0_ON_PIN, TOPIC_0_OFF_PIN, TOPIC_0_TOGGLE_PIN, TOPIC_1_ON_PIN, TOPIC_1_OFF_PIN, TOPIC_1_TOGGLE_PIN, TOPIC_2_ON_PIN, TOPIC_2_OFF_PIN, TOPIC_2_TOGGLE_PIN};
#endif
#endif
//Debounce setup
int button_state[ARRAY_SIZE(button_pins)];
int button_state_last[ARRAY_SIZE(button_pins)];
int debounce[ARRAY_SIZE(button_pins)];
int debounce_time = 80;
bool control_state[ARRAY_SIZE(button_pins)/3];
void callback(char* topic, byte* payload, unsigned int length, PubSubClient *client) {
//Serial.print("Message arrived [");
//Serial.print(topic);
//Serial.print("] ");
//for (int i = 0; i < length; i++) {
// Serial.print((char)payload[i]);
//}
//Serial.println();
topic += 5;
int topicIndex = -1;
if(strncmp(topic, TOPIC_0_TOPIC, strlen(TOPIC_0_TOPIC)) == 0) {
topicIndex = 0;
}
#ifdef TOPIC_1_TOPIC
if(strncmp(topic, TOPIC_1_TOPIC, strlen(TOPIC_1_TOPIC)) == 0) {
topicIndex = 1;
}
#endif
#ifdef TOPIC_2_TOPIC
if(strncmp(topic, TOPIC_2_TOPIC, strlen(TOPIC_2_TOPIC)) == 0) {
topicIndex = 2;
}
#endif
if((char)payload[0] == 'O' && (char)payload[1] == 'N') {
control_state[topicIndex] = true;
} else if((char)payload[0] == 'O' && (char)payload[1] == 'F') {
control_state[topicIndex] = false;
}
switch(topicIndex) {
case 0:
digitalWrite(TOPIC_0_LED_ON, control_state[topicIndex]);
digitalWrite(TOPIC_0_LED_OFF, !control_state[topicIndex]);
break;
#ifdef TOPIC_1_TOPIC
case 1:
digitalWrite(TOPIC_1_LED_ON, control_state[topicIndex]);
digitalWrite(TOPIC_1_LED_OFF, !control_state[topicIndex]);
break;
#endif
#ifdef TOPIC_2_TOPIC
case 2:
digitalWrite(TOPIC_2_LED_ON, control_state[topicIndex]);
digitalWrite(TOPIC_2_LED_OFF, !control_state[topicIndex]);
break;
#endif
}
}
void connectSuccess(PubSubClient* client, char* ip) {
//Subscribe to the result topic from the thing we control
sprintf(topicBuf, "stat/%s/POWER", TOPIC_0_TOPIC);
client->subscribe(topicBuf);
sprintf(topicBuf, "cmnd/%s/POWER", TOPIC_0_TOPIC);
client->publish(topicBuf, "");
#ifdef TOPIC_1_TOPIC
sprintf(topicBuf, "stat/%s/POWER", TOPIC_1_TOPIC);
client->subscribe(topicBuf);
sprintf(topicBuf, "cmnd/%s/POWER", TOPIC_1_TOPIC);
client->publish(topicBuf, "");
#endif
#ifdef TOPIC_2_TOPIC
sprintf(topicBuf, "stat/%s/POWER", TOPIC_2_TOPIC);
client->subscribe(topicBuf);
sprintf(topicBuf, "cmnd/%s/POWER", TOPIC_2_TOPIC);
client->publish(topicBuf, "");
#endif
}
void setup() {
//Serial.begin(115200);
mqtt_options.connectedLoop = connectedLoop;
mqtt_options.callback = callback;
mqtt_options.connectSuccess = connectSuccess;
mqtt_options.ssid = ssid;
mqtt_options.password = password;
mqtt_options.mqtt_server = mqtt_server;
mqtt_options.mqtt_port = mqtt_port;
mqtt_options.host_name = host_name;
mqtt_options.fullTopic = fullTopic;
mqtt_options.debug_print = false;
setup_mqtt(&mqtt_options);
memset(button_state, 1, ARRAY_SIZE(button_pins));
memset(button_state_last, 1, ARRAY_SIZE(button_pins));
memset(debounce, 1, ARRAY_SIZE(button_pins));
//input pins
for (int i=0; i < ARRAY_SIZE(button_pins); ++i) {
pinMode(button_pins[i], INPUT_PULLUP);
}
pinMode(TOPIC_0_LED_ON, OUTPUT);
pinMode(TOPIC_0_LED_OFF, OUTPUT);
digitalWrite(TOPIC_0_LED_ON, 0);
digitalWrite(TOPIC_0_LED_OFF, 0);
#ifdef TOPIC_1_TOPIC
pinMode(TOPIC_1_LED_ON, OUTPUT);
pinMode(TOPIC_1_LED_OFF, OUTPUT);
digitalWrite(TOPIC_1_LED_ON, 0);
digitalWrite(TOPIC_1_LED_OFF, 0);
#endif
#ifdef TOPIC_2_TOPIC
pinMode(TOPIC_2_LED_ON, OUTPUT);
pinMode(TOPIC_2_LED_OFF, OUTPUT);
digitalWrite(TOPIC_2_LED_ON, 0);
digitalWrite(TOPIC_2_LED_OFF, 0);
#endif
}
void connectedLoop(PubSubClient* client) {
//Foreach pin run debouncing, and if it's a real press call the function
for(int i=0; i < ARRAY_SIZE(button_pins); ++i) {
if(button_pins[i] == -1) continue;
button_state[i] = digitalRead(button_pins[i]);//Read current state
//If the current state does not equal the last state, AND it's been long enough since the last change
unsigned long now = millis() + (unsigned long)-3000;
if (button_state[i] != button_state_last[i] && now - debounce[i] > debounce_time) {
//If the button is pressed, call funciton
if (button_state[i] == LOW) {
sprintf(topicBuf, "info/%s/pressed", TOPIC);
sprintf(buf, "pressed %d", i);
client->publish(topicBuf,buf);
if(i < 3) {
sprintf(topicBuf, "cmnd/%s/POWER", TOPIC_0_TOPIC);
} else if(i < 6) {
#ifdef TOPIC_1_TOPIC
sprintf(topicBuf, "cmnd/%s/POWER", TOPIC_1_TOPIC);
#else
//Serial.println("Should not be?");
#endif
} else if(i < 9) {
#ifdef TOPIC_2_TOPIC
sprintf(topicBuf, "cmnd/%s/POWER", TOPIC_2_TOPIC);
#else
//Serial.println("Should not be?");
#endif
} else {
//Serial.println("Should not be, pt II?");
}
if(i%3 < 2) { //1 and 2 are set on and off
//Serial.println("on or off");
client->publish(topicBuf, i%3 == 0 ? "ON" : "OFF");
} else {
//Serial.println("toggle");
client->publish(topicBuf, control_state[i/3] == false ? "ON" : "OFF");
}
}
//If the button was pressed or released, we still need to reset the debounce timer.
button_state_last[i] = button_state[i];
debounce[i] = now;
}
}
}
void loop() {
loop_mqtt();
}