-
Notifications
You must be signed in to change notification settings - Fork 0
/
RollerShutterNodeCoverMarekv14_class_object_mega_RF24.ino
446 lines (399 loc) · 11.9 KB
/
RollerShutterNodeCoverMarekv14_class_object_mega_RF24.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Enable debug prints to serial monitor
#define MY_DEBUG
// uncomment if using as standalone Serial gateway
//#define MY_GATEWAY_SERIAL
// Enable and select radio type attached
#define MY_RADIO_NRF24
#define MY_TRANSPORT_WAIT_READY_MS 1
//#define MY_RF24_PA_LEVEL RF24_PA_LOW
//#define MY_REPEATER_FEATURE
// uncomment if we want to manually assign an ID
#define MY_NODE_ID 20
// only for Arduino Mega !!!!!!!!!!!!!!
#define MY_RF24_CE_PIN 49
#define MY_RF24_CS_PIN 53
// ------------------------------------
#include <Bounce2.h>
#include <MySensors.h>
#include <SPI.h>
//new begin-----------------------------
enum CoverState
{
STOP,
UP, // Window covering. Up.
DOWN, // Window covering. Down.
};
#define DIRECTION_UP 1
#define DIRECTION_DOWN 0
#define STATE_UP 100 // 100 is opened - up
#define STATE_DOWN 0 // 0 is closed - down
#define PRESENT_MESSAGE "Roller Shutter for Domoticz"
bool IS_ACK = false; //is to acknowlage
class RollerShutter
{
uint8_t CHILD_ID_COVER;
uint8_t CHILD_ID_SET;
int buttonPinUp;
int buttonPinDown;
int relayPinUp;
int relayPinDown;
bool relayON;
bool relayOFF;
static bool initial_state_sent;
int value = 0;
int oldValueUp = 0;
int oldValueDown = 0;
Bounce debouncerUp = Bounce();
Bounce debouncerDown = Bounce();
Bounce debouncerStop = Bounce();
int rollTime = loadState(CHILD_ID_SET);
float timeOneLevel = rollTime / 100.0;
float requestedShutterLevel = 0;
float currentShutterLevel = 0;
unsigned long lastLevelTime = 0;
bool isMoving = false;
int directionUpDown;
bool calibrateDown;
bool calibrateUp;
unsigned long calibrationStartTime;
float calibrationTime = 0.0;
bool calibratedDown;
bool calibratedUp;
int coverState = STOP;
public:
RollerShutter(int childId, int setId, int buttonUp, int buttonDown, int relayUp, int relayDown, int debaunceTime, bool invertedRelay) :
msgUp(childId, V_UP),msgDown(childId, V_DOWN),
msgStop(childId, V_STOP), msgPercentage(childId, V_PERCENTAGE),
msgSetpoint(setId, V_HVAC_SETPOINT_HEAT), msgActual(setId, V_TEMP)
{
// constructor - like "setup" part of standard program
CHILD_ID_COVER = childId;
CHILD_ID_SET = setId;
buttonPinUp = buttonUp;
buttonPinDown = buttonDown;
relayPinUp = relayUp;
relayPinDown = relayDown;
relayON = !invertedRelay;
relayOFF = invertedRelay;
pinMode(buttonPinUp, INPUT_PULLUP); // Setup the button
pinMode(buttonPinDown, INPUT_PULLUP); // Activate internal pull-up
debouncerUp.attach(buttonPinUp); // After setting up the button, setup debouncer
debouncerDown.attach(buttonPinDown); // After setting up the button, setup debouncer
debouncerUp.interval(debaunceTime); // After setting up the button, setup debouncer
debouncerDown.interval(debaunceTime); // After setting up the button, setup debouncer
digitalWrite(relayPinUp, relayOFF); // Make sure relays are off when starting up
digitalWrite(relayPinDown, relayOFF); // Make sure relays are off when starting up
pinMode(relayPinUp, OUTPUT); // Then set relay pins in output mode
pinMode(relayPinDown, OUTPUT); // Then set relay pins in output mode
int state = loadState(CHILD_ID_COVER);
currentShutterLevel = state;
requestedShutterLevel = state;
}
MyMessage msgUp;
MyMessage msgDown;
MyMessage msgStop;
MyMessage msgPercentage;
MyMessage msgSetpoint;
MyMessage msgActual;
void sendState() // Send current state and status to gateway.
{
if(currentShutterLevel==100)
{
send(msgUp.set(1));
}
else
{
if(currentShutterLevel==0)
{
send(msgDown.set(1));
}
else
{
send(msgStop.set(1));
}
}
send(msgPercentage.set((int)currentShutterLevel));
send(msgSetpoint.set((int)rollTime));
send(msgActual.set((int)rollTime));
}
void shuttersUp(void)
{
#ifdef MY_DEBUG
Serial.println("Shutters going up");
#endif
if (digitalRead(relayPinDown) == relayON) {
digitalWrite(relayPinDown, relayOFF);
wait(50); // NEED TO BE CHANGED !!!!!!!!!!!!
}
digitalWrite(relayPinUp, relayON);
directionUpDown = DIRECTION_UP;
isMoving = true;
coverState = UP;
sendState();
}
void shuttersDown(void)
{
#ifdef MY_DEBUG
Serial.println("Shutters going down");
#endif
if (digitalRead(relayPinUp) == relayON) {
digitalWrite(relayPinUp, relayOFF);
wait(50); // NEED TO BE CHANGED !!!!!!!!!!!!
}
digitalWrite(relayPinDown, relayON);
directionUpDown = DIRECTION_DOWN;
isMoving = true;
coverState = DOWN;
sendState();
}
void shuttersHalt(void)
{
#ifdef MY_DEBUG
Serial.println("Shutters halted");
#endif
digitalWrite(relayPinUp, relayOFF);
digitalWrite(relayPinDown, relayOFF);
isMoving = false;
requestedShutterLevel = currentShutterLevel;
#ifdef MY_DEBUG
Serial.println("saving state to: ");
Serial.println(String(currentShutterLevel));
#endif
saveState(CHILD_ID_COVER, (int)currentShutterLevel);
coverState = STOP;
sendState();
}
void changeShuttersLevel(int level)
{
int dir = (level > currentShutterLevel) ? DIRECTION_UP : DIRECTION_DOWN;
if (isMoving && dir != directionUpDown) {
shuttersHalt();
}
requestedShutterLevel = level;
}
void Update()
{
debouncerUp.update();
value = debouncerUp.read();
if (value == 0 && value != oldValueUp) {
if(isMoving){
shuttersHalt();
}
else{
calibrateUp = false;
calibratedUp = false;
changeShuttersLevel(STATE_UP);
}
//state = UP;
//sendState();
}
oldValueUp = value;
debouncerDown.update();
value = debouncerDown.read();
if (value == 0 && value != oldValueDown) {
if(isMoving){
shuttersHalt();
}
else{
calibrateDown = false;
calibratedDown = false;
changeShuttersLevel(STATE_DOWN);
}
//state = DOWN;
//sendState();
}
oldValueDown = value;
if(currentShutterLevel != 100)
{
calibrateUp = false;
calibratedUp = false;
}
if(currentShutterLevel != 0)
{
calibrateDown = false;
calibratedDown = false;
}
if (isMoving)
{
unsigned long _now = millis();
if (_now - lastLevelTime >= timeOneLevel * 1000) {
if (directionUpDown == DIRECTION_UP) {
currentShutterLevel += 1;
} else {
currentShutterLevel -= 1;
}
currentShutterLevel = constrain(currentShutterLevel, 0, 100);
#ifdef MY_DEBUG
Serial.println(String(requestedShutterLevel));
Serial.println(String(currentShutterLevel));
#endif
lastLevelTime = millis();
send(msgPercentage.set((int)currentShutterLevel));
}
if (currentShutterLevel == requestedShutterLevel)
{
if(currentShutterLevel == 0 && !calibratedDown)
{
if(calibrateDown == false)
{
calibrateDown = true;
calibratedDown = false;
calibrationStartTime = _now;
}
else
{
if(calibratedDown == false)
{
if (_now - calibrationStartTime >= calibrationTime * 1000)
{
calibratedDown = true;
}
}
}
}
else if (currentShutterLevel == 100 && !calibratedUp)
{
if(calibrateUp == false)
{
calibrateUp = true;
calibratedUp = false;
calibrationStartTime = _now;
}
else
{
if(calibratedUp == false)
{
if (_now - calibrationStartTime >= calibrationTime * 1000)
{
calibratedUp = true;
}
}
}
}
else
{
shuttersHalt();
}
}
}
else
{
if (requestedShutterLevel != currentShutterLevel)
{
if (requestedShutterLevel > currentShutterLevel) {
shuttersUp();
}
else {
shuttersDown();
}
lastLevelTime = millis();
}
}
}
void SyncController()
{
sendState();
}
void Present()
{
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID_COVER, S_COVER, PRESENT_MESSAGE, IS_ACK);
present(CHILD_ID_SET, S_HVAC);
}
void Receive(const MyMessage &message)
{
#ifdef MY_DEBUG
Serial.println("recieved incomming message");
Serial.println("Recieved message for sensor: ");
Serial.println(String(message.sensor));
Serial.println("Recieved message with type: ");
Serial.println(String(message.type));
#endif
if (message.sensor == CHILD_ID_COVER) {
int per;
switch (message.type) {
case V_UP:
changeShuttersLevel(STATE_UP);
break;
case V_DOWN:
changeShuttersLevel(STATE_DOWN);
break;
case V_STOP:
shuttersHalt();
break;
case V_STATUS:
message.getBool()?per=100:per=0;
changeShuttersLevel(per);
break;
case V_PERCENTAGE:
per = message.getInt();
if (per > 100) {
per = 100;
}
else if (per<0) {
per = 0;
}
changeShuttersLevel(per);
break;
}
}
else if (message.sensor == CHILD_ID_SET) {
if (message.type == V_HVAC_SETPOINT_COOL || message.type == V_HVAC_SETPOINT_HEAT)
{
#ifdef MY_DEBUG
Serial.println(", New status: V_HVAC_SETPOINT_COOL, with payload: ");
#endif
String strRollTime = message.getString();
rollTime = strRollTime.toFloat();
#ifdef MY_DEBUG
Serial.println("rolltime value: ");
Serial.println(String(rollTime));
#endif
saveState(CHILD_ID_SET, rollTime);
sendState();
timeOneLevel = rollTime / 100.0;
}
}
#ifdef MY_DEBUG
Serial.println("exiting incoming message");
#endif
return;
}
};
// define your RollerShutter objects here
// RollerShutter(int childId, int setId, int buttonUp, int buttonDown, int relayUp, int relayDown, int debaunceTime, bool invertedRelay)
RollerShutter blind1(0, 20, 14, 15, 22, 23, 50, 0);
RollerShutter blind2(1, 21, 16, 17, 24, 25, 50, 0);
RollerShutter blind3(2, 22, 18, 19, 26, 27, 50, 0);
RollerShutter blind4(3, 23, 20, 21, 28, 29, 50, 0);
void setup()
{
// Setup locally attached sensors
delay(5000);
blind1.SyncController(); // send actual value to controller
blind2.SyncController();
blind3.SyncController();
blind4.SyncController();
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("RollerShutter as class object", "1.1");
blind1.Present();
blind2.Present();
blind3.Present();
blind4.Present();
}
void loop()
{
blind1.Update();
blind2.Update();
blind3.Update();
blind4.Update();
}
void receive(const MyMessage &message) {
blind1.Receive(message);
blind2.Receive(message);
blind3.Receive(message);
blind4.Receive(message);
}