-
Notifications
You must be signed in to change notification settings - Fork 12
/
ESPBlinds.ino
375 lines (314 loc) · 9.33 KB
/
ESPBlinds.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include <PubSubClient.h>
#include "constants.h"
#include "EasyDriver.h"
// Outputs
const int PIN_STEP = 4;
const int PIN_DIR = 5;
const int PIN_MS1 = -1;
const int PIN_MS2 = -1;
const int PIN_ENABLE = 14;
const int PIN_FAN = 13;
// Inputs
//const int PIN_CUTOFF_CLOSE = 0;
//const int PIN_CUTOFF_OPEN = 3;
EasyDriver stepper = EasyDriver(PIN_STEP, PIN_DIR, PIN_MS1, PIN_MS2, PIN_ENABLE);
WiFiClient espClient;
PubSubClient mqttClient(espClient);
// Down = forwards == close
// Up = Reverse == open
const int DIRECTION_CLOSE = EASYDRIVER_DIRECTION_FORWARDS;
const int DIRECTION_OPEN = EASYDRIVER_DIRECTION_REVERSE;
const bool MQTT_SEND_STEPS = false;
// Vars that are updatable via MQTT and saved into EEPROM...
// If this number is not found in EEPROM, then we assume no EEPROM values exist/have been saved
// and all other EEPROM values should be discarded/fallback to default values.
// See loadFromEeprom()
int EEPROM_MAGIC_NUMBER = 10001;
int STEPS_VERTICAL = 15000; // Number of full steps required to complete open/close sequence
int DELAY_CLOSE = 500; // In micros
int MODE_CLOSE = EASYDRIVER_MODE_FULL_STEP;
int DELAY_OPEN = 500; // In micros
int MODE_OPEN = EASYDRIVER_MODE_FULL_STEP;
// Locals
int currentStep = 0;
int currentMode = MODE_OPEN;
int stepDirection = EASYDRIVER_DIRECTION_FORWARDS;
bool stepperEnabled = true;
bool isOpening = false;
bool isClosing = false;
void setup() {
// Cut off switches (not used in hardware, but implemented in case required in future)
// pinMode(PIN_CUTOFF_CLOSE, INPUT_PULLUP);
// pinMode(PIN_CUTOFF_OPEN, INPUT_PULLUP);
pinMode(PIN_FAN, OUTPUT);
stepper.reset();
Serial.begin(115200);
while (! Serial);
setupWifi();
mqttClient.setServer(MQTT_SERVER, 1883);
mqttClient.setCallback(mqttCallback);
if (!mqttClient.connected()) {
mqttReconnect();
}
mqttPublish(MQTT_TOPIC_ENABLED, 0);
mqttPublish(MQTT_TOPIC_STATE, "closed");
mqttPublish(MQTT_TOPIC_STEPS, currentStep);
loadFromEeprom();
// Topic for controlling the stepper remotely
mqttClient.subscribe(MQTT_TOPIC_CONTROL_ENABLED);
mqttClient.subscribe(MQTT_TOPIC_CONTROL_DIRECTION);
mqttClient.subscribe(MQTT_TOPIC_CONTROL_STEPFOR);
mqttClient.subscribe(MQTT_TOPIC_CONTROL_BLINDS);
// Topics for updating EEPROM values remotely
mqttClient.subscribe(MQTT_TOPIC_CONTROL_MODE_OPEN);
mqttClient.subscribe(MQTT_TOPIC_CONTROL_MODE_CLOSE);
mqttClient.subscribe(MQTT_TOPIC_CONTROL_DELAY_OPEN);
mqttClient.subscribe(MQTT_TOPIC_CONTROL_DELAY_CLOSE);
mqttClient.subscribe(MQTT_TOPIC_CONTROL_STEPS_VERTICAL);
}
/**
* MAIN LOOP
*/
void loop() {
if (!mqttClient.connected()) {
mqttReconnect();
}
mqttClient.loop();
}
void closeBlinds() {
isClosing = true;
mqttPublish(MQTT_TOPIC_STATE, "closing");
setStepperDirection(DIRECTION_CLOSE);
setStepperMode(MODE_CLOSE);
stepFor(STEPS_VERTICAL);
mqttPublish(MQTT_TOPIC_STATE, "closed");
isClosing = false;
}
void openBlinds() {
isOpening = true;
mqttPublish(MQTT_TOPIC_STATE, "opening");
setStepperDirection(DIRECTION_OPEN);
setStepperMode(MODE_OPEN);
stepFor(STEPS_VERTICAL);
mqttPublish(MQTT_TOPIC_STATE, "opened");
isOpening = false;
}
/**
* STEPPER WRAPPERS
*/
void stepFor(int steps) {
setStepperEnabled(true);
for (int i = 0; i < steps; i++) {
// Serial.print("STEP");
// Serial.println(i);
// Stepping mode is effectively a multiplier on the desired number of steps
// E.g. a quarter step mode, to achieve a full step we need to do 4x stepper steps
for (int x = 0; x < currentMode; x++) {
stepper.step();
}
currentStep += stepDirection;
// if (stepDirection == DIRECTION_CLOSE && digitalRead(PIN_CUTOFF_CLOSE) == LOW) {
// Serial.println("ABORT CLOSE");
// break;
// }
// if (stepDirection == DIRECTION_OPEN && digitalRead(PIN_CUTOFF_OPEN) == LOW) {
// Serial.println("ABORT OPEN");
// break;
// }
if (!stepperEnabled) {
break;
}
if (MQTT_SEND_STEPS) {
if (currentStep % 250 == 0) {
mqttPublish(MQTT_TOPIC_STEPS, currentStep);
}
}
mqttClient.loop();
}
setStepperEnabled(false);
}
void setStepperMode(int mode) {
stepper.setMode(mode);
currentMode = mode;
mqttPublish(MQTT_TOPIC_MODE, mode);
}
void setStepperEnabled(bool enabled) {
stepper.enable(enabled);
if (enabled) {
digitalWrite(PIN_FAN, HIGH);
mqttPublish(MQTT_TOPIC_ENABLED, 1);
} else {
digitalWrite(PIN_FAN, LOW);
mqttPublish(MQTT_TOPIC_ENABLED, 0);
}
stepperEnabled = enabled;
}
void setStepperDirection(int direction) {
stepper.setDirection(direction);
stepDirection = direction;
if (direction == EASYDRIVER_DIRECTION_FORWARDS) {
stepper.setDelay(DELAY_CLOSE);
mqttPublish(MQTT_TOPIC_DIRECTION, "forwards");
} else {
stepper.setDelay(DELAY_OPEN);
mqttPublish(MQTT_TOPIC_DIRECTION, "reverse");
}
}
/**
* WIFI / MQTT HELPERS
*/
void setupWifi() {
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void mqttCallback(char* topic, byte* payload, unsigned int length) {
String topicStr = topic;
String value = "";
for (int i = 0; i < length; i++) {
value += (char)payload[i];
}
Serial.println("mqttCallback !");
Serial.println(topicStr);
Serial.println(value);
if (topicStr == MQTT_TOPIC_CONTROL_ENABLED) {
if (value.equals("1")) {
setStepperEnabled(true);
} else if (value.equals("0")) {
setStepperEnabled(false);
}
}
if (topicStr == MQTT_TOPIC_CONTROL_DIRECTION) {
if (value.equals("1")) {
setStepperDirection(DIRECTION_CLOSE);
} else if (value.equals("0")) {
setStepperDirection(DIRECTION_OPEN);
}
}
if (topicStr == MQTT_TOPIC_CONTROL_STEPFOR) {
int steps = value.toInt();
stepFor(steps);
}
if (topicStr == MQTT_TOPIC_CONTROL_BLINDS) {
if (value.equals("opened") && !isOpening) {
// if (value.equals("opened")) {
openBlinds();
} else if (value.equals("closed") && !isClosing) {
// } else if (value.equals("closed")) {
closeBlinds();
}
}
if (topicStr == MQTT_TOPIC_CONTROL_MODE_OPEN) {
MODE_OPEN = value.toInt();
saveToEeprom();
}
if (topicStr == MQTT_TOPIC_CONTROL_MODE_CLOSE) {
MODE_CLOSE = value.toInt();
saveToEeprom();
}
if (topicStr == MQTT_TOPIC_CONTROL_DELAY_OPEN) {
DELAY_OPEN = value.toInt();
saveToEeprom();
}
if (topicStr == MQTT_TOPIC_CONTROL_DELAY_CLOSE) {
DELAY_CLOSE = value.toInt();
saveToEeprom();
}
if (topicStr == MQTT_TOPIC_CONTROL_STEPS_VERTICAL) {
STEPS_VERTICAL = value.toInt();
saveToEeprom();
}
}
void mqttReconnect() {
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqttClient.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD, MQTT_TOPIC_STATUS, 1, true, "disconnected", false)) {
Serial.println("connected");
// Once connected, publish an announcement...
mqttClient.publish(MQTT_TOPIC_STATUS, "connected", true);
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void mqttPublish(char *topic, int payload) {
// Serial.print(topic);
// Serial.print(": ");
// Serial.println(payload);
mqttClient.publish(topic, String(payload).c_str(), true);
}
void mqttPublish(char *topic, String payload) {
// Serial.print(topic);
// Serial.print(": ");
// Serial.println(payload);
mqttClient.publish(topic, payload.c_str(), true);
}
/**
* EEPROM HELPERS
*/
void loadFromEeprom() {
EEPROM.begin(24);
//Read data from eeprom
int magicNumber;
int modeOpen;
int modeClose;
int delayOpen;
int delayClose;
int stepsVertical;
EEPROM.get(0, modeOpen);
EEPROM.get(4, modeClose);
EEPROM.get(8, delayOpen);
EEPROM.get(12, delayClose);
EEPROM.get(16, stepsVertical);
EEPROM.get(20, magicNumber);
EEPROM.end();
if (magicNumber == EEPROM_MAGIC_NUMBER) {
Serial.print("Successfully loaded config from eeprom");
MODE_OPEN = modeOpen;
MODE_CLOSE = modeClose;
DELAY_OPEN = delayOpen;
DELAY_CLOSE = delayClose;
STEPS_VERTICAL = stepsVertical;
} else {
Serial.print("EEPROM MAGIC NUMBER INCORRECT: ");
Serial.print(magicNumber);
Serial.print(" != ");
Serial.println(EEPROM_MAGIC_NUMBER);
Serial.print("Keeping default step config!");
}
mqttPublish(MQTT_TOPIC_MODE_OPEN, MODE_OPEN);
mqttPublish(MQTT_TOPIC_MODE_CLOSE, MODE_CLOSE);
mqttPublish(MQTT_TOPIC_DELAY_OPEN, DELAY_OPEN);
mqttPublish(MQTT_TOPIC_DELAY_CLOSE, DELAY_CLOSE);
mqttPublish(MQTT_TOPIC_STEPS_VERTICAL, STEPS_VERTICAL);
}
void saveToEeprom() {
EEPROM.begin(24);
EEPROM.put(0, MODE_OPEN);
EEPROM.put(4, MODE_CLOSE);
EEPROM.put(8, DELAY_OPEN);
EEPROM.put(12, DELAY_CLOSE);
EEPROM.put(16, STEPS_VERTICAL);
EEPROM.put(20, EEPROM_MAGIC_NUMBER);
EEPROM.commit();
EEPROM.end();
mqttPublish(MQTT_TOPIC_MODE_OPEN, MODE_OPEN);
mqttPublish(MQTT_TOPIC_MODE_CLOSE, MODE_CLOSE);
mqttPublish(MQTT_TOPIC_DELAY_OPEN, DELAY_OPEN);
mqttPublish(MQTT_TOPIC_DELAY_CLOSE, DELAY_CLOSE);
mqttPublish(MQTT_TOPIC_STEPS_VERTICAL, STEPS_VERTICAL);
}