-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remote Doorbell Code
302 lines (248 loc) · 6.73 KB
/
Remote Doorbell Code
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
/*
* EPever MPPT Charge controller RS485 Interface
* Design brief:
* Use: Scottish Bothy with multiple occupants. Operation must be simple:
* * 'Press Here' > 'Lights ON' Press Again > 'Lights OFF'
* * Remote Load ON/OFF switching to eliminate trip in dark to Epever controller
*
* Lights have been left on for weeks battening the flattery:
* Daily Load OFF timed by EPever RTC - IF ON > OFF at Midday 12:00hrs
*
* Power consumption: <1mA@12Volts
*
* Big thankyou to Colin Hickey for permission to adapt his code
* https://github.com/chickey/RS485-WiFi-EPEver
* And also:
* https://github.com/glitterkitty/EpEverSolarMonitor
*
* Hardware:
* Development arduino nano
* Production 5 Volt pro mini
* MAX485CSA Converter module
*
*
* Remote switching - Wired or Wireless using cheap doorbell
*/
////////////////
//#define DEBUG true
#define DEBUG false
////////////////
#include <ModbusMaster.h>
#include <LowPower.h>
#include <EnableInterrupt.h>
#include "config.h"
// instantiate ModbusMaster object
ModbusMaster node;
#include <AltSoftSerial.h>
//https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
void setup(){
// Modbus at 115200 baud
Serial.begin(115200);
while (!Serial){;}
// init modbus in receive mode
pinMode(MAX485_RE, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
pinMode(wakePin,INPUT_PULLUP); //INT 0:
enableInterrupt(wakePin, isr_handler, FALLING);
// Init in receive mode
digitalWrite(MAX485_RE, 0);
digitalWrite(MAX485_DE, 0);
// EPEver Device ID 1
node.begin(1, Serial);
// Callbacks
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
void preTransmission(){
digitalWrite(MAX485_RE, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission(){
digitalWrite(MAX485_RE, 0);
digitalWrite(MAX485_DE, 0);
}
//------------------------------------------------------------
void read_time() {
// clear old data
memset(rtc.buf,0,sizeof(rtc.buf));
// Read registers for clock
//
delay(50);
node.clearResponseBuffer();
uint8_t result = node.readHoldingRegisters(RTC_CLOCK, RTC_CLOCK_CNT);
if (result == node.ku8MBSuccess) {
rtc.buf[0] = node.getResponseBuffer(0);
rtc.buf[1] = node.getResponseBuffer(1);
rtc.buf[2] = node.getResponseBuffer(2);
} else {
#ifdef DEBUG
Serial.print(F("Miss read rtc-data, ret val:"));
Serial.println(result, HEX);
#endif
}
delay(50);
}
void read_livedata() {
// clear old data
memset(live.buf,0,sizeof(live.buf));
delay(50);
node.clearResponseBuffer();
result = node.readInputRegisters(LIVE_DATA, LIVE_DATA_CNT);
if (result == node.ku8MBSuccess) {
for(i=0; i< LIVE_DATA_CNT ;i++) live.buf[i] = node.getResponseBuffer(i);
} else {
Serial.print("Miss read liva-data, ret val:");
Serial.println(result, HEX);
}
delay(50);
}
void read_statsdata() {
;
}
void midday_switch() {
//Output values to serial
// Serial.println("");
// char buffer[30];
// Serial.print("");
// sprintf(buffer,"\n\nTime: 20%02d-%02d-%02d %02d:%02d:%02d \n", rtc.r.y , rtc.r.M , rtc.r.d , rtc.r.h , rtc.r.m , rtc.r.s );
// Serial.print (buffer);
if ((rtc.r.h >=12)&&(rtc.r.h <=13)){
init_off = true;
// Serial.println("StageOne");
}
if ((rtc.r.m >=14)&&(rtc.r.m <= 15)){
init_off = false;
// Serial.println("StageTwo");
}
if ((rtc.r.m >=16)&&(rtc.r.m <= 17)){
block_off = false;
// Serial.println("StageThree");
}
if ((init_off == true) && (block_off == false)){
Serial.println ("turning off");
loadState = false;
switch_load = true;
block_off = true;
delay(50);
}
}
void minute_switch() {
if ((rtc.r.s >=00)&&(rtc.r.s <=10)){
init_off = true;
Serial.println("StageOne");
}
if ((rtc.r.s >=15)&&(rtc.r.s <= 25)){
init_off = false;
Serial.println("StageTwo");
}
if ((rtc.r.s >=30)&&(rtc.r.s <= 40)){
block_off = false;
Serial.println("StageThree");
}
if ((init_off == true) && (block_off == false)){
Serial.println ("turning off");
loadState = false;
switch_load = true;
block_off = true;
delay(50);
}
}
void write_loadstate(){
node.writeSingleCoil(0x0002, loadState);
#ifdef DEBUG
if (result != node.ku8MBSuccess) {
Serial.print(F("Miss write loadState, ret val:"));
Serial.println(result, HEX);
Serial.print(F("Switching Load "));
Serial.println( (loadState?F("On"):F("Off")) );
}
#endif
delay(2000); // ensure bell 1500mS interrupts are missed..
switch_load = false; //clear flag
}
void read_loadstate(){
// State of the Load Switch
delay(50);
node.clearResponseBuffer();
result = node.readCoils( LOAD_STATE, 1 );
if (result == node.ku8MBSuccess) {
loadState = node.getResponseBuffer(0);
} else {
#ifdef DEBUG
Serial.print("Miss read loadState, ret val:");
Serial.println(result, HEX);
#endif
}
if (result==226) ErrorCounter++;
}
void debug_output(){
#ifdef DEBUG
Serial.print("LoadState: ");
Serial.println(loadState);
Serial.print("switch_load: ");
Serial.println(switch_load);
Serial.println();
#endif
}
/* Interupt from wireless doorbell - 5 pulses period 650mS
* 1500 mS of falling edge interupts - Debounce 1800mS
----------------------------------------------------------------------
*/
void isr_handler(){
if (!enable_isr)
return;
led_status = !led_status;
loadState = !loadState ;
digitalWrite(LED_BUILTIN, led_status);
switch_load = true; //set flag to change state
enable_isr = false;
}
// MAIN LOOP
//-----------------------------------------------------------------------------
void loop(){
debug_output(); // print data if in debug
Serial.flush();
// Sleep mode
enable_isr = true;
if (DEBUG){
delay(2000);
}else{
for(int i=0;(i<30 && !switch_load);i++) {
//
LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF);
}
}
// Read Values from Controller
read_time();
read_livedata();
read_statsdata();
if (DEBUG){
minute_switch();
}else{
midday_switch(); // switch off at midday - or thereabouts
}
if (switch_load = true){
write_loadstate();
}
read_loadstate();
//Check error count and if it exceeds 5 reset modbus
//#ifdef DEBUG
// Serial.print("Error count");
// Serial.println(ErrorCounter);
//#endif
if (ErrorCounter>5) {
// init modbus in receive mode
pinMode(MAX485_RE, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
postTransmission();
// EPEver Device ID and Baud Rate
node.begin(1, Serial);
// modbus callbacks
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
ErrorCounter = 0;
}
// power down MAX485_DE
postTransmission();
}
//--------------------------------------------------------------------------------