-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsketch_jul16a_-_Brinks_WTW.ino
130 lines (106 loc) · 4.34 KB
/
sketch_jul16a_-_Brinks_WTW.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
/**
* Last Modified 27-09-2017 - S.Incze
* Reason: Due due NO / NC of Brinks installation
* Brinks Switches due to a link between 2 points. If Normally Closed is Used and Mysensors disconnects the WTW turns on.
* That is why Layout changed to connect brinks to NO pins.
*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <[email protected]>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 1.0 - Henrik Ekblad
*
* DESCRIPTION
* Example sketch showing how to control physical relays.
* This example will remember relay state after power failure.
* http://www.mysensors.org/build/relay
*/
// Enable debug prints to serial monitor
#define MY_DEBUG // 27-09-2017 No Debug Data needed if not connected.
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Assign a specific Node ID to this device
#define MY_NODE_ID 25
// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE // 27-09-2017 Enabled repeater functionality
// Flash leds on rx/tx/err
// Set blinking period (in milliseconds)
#define MY_DEFAULT_LED_BLINK_PERIOD 300
//#define MY_DEFAULT_ERR_LED_PIN 4
#define MY_DEFAULT_TX_LED_PIN 6 // I am using LED to display the activity TX on D5
#define MY_DEFAULT_RX_LED_PIN 7 // I am using LED to display the activity RX on D6
#define SKETCH_NAME "Brinks WTW / OTGW"
#define SKETCH_VERSION "1.2"
#include <MySensors.h>
#define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 3 // Total number of attached relays
#define RELAY_ON 0 // GPIO value to write to turn on attached relay 27-09-2017 Changed due to NO behaviour of the Brinks installation, Normally RELAY_ON 1
#define RELAY_OFF 1 // GPIO value to write to turn off attached relay 27-09-2017 Changed due to NO behaviour of the Brinks installation RELAY_OFF 0
void before()
{
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
}
}
void setup()
{
}
void presentation()
{
Serial.println(F("-- Init MySensors Brinks WTW"));
// Send the sketch version information to the gateway and Controller
sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
Serial.print(F("NodeID: "));
Serial.println(getNodeId());
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
present(sensor, S_BINARY);
}
}
void loop() // Added 5 minute heartbeat 07-11-2017
{
const unsigned long fiveMinutes = 5 * 60 * 1000UL;
static unsigned long lastSampleTime = 0 - fiveMinutes; // initialize such that a reading is due the first time through loop()
unsigned long now = millis();
if (now - lastSampleTime >= fiveMinutes)
{
lastSampleTime += fiveMinutes;
sendHeartbeat;
}
// add code to do other stuff here
}
void receive(const MyMessage &message)
{
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_STATUS) {
// Change relay state
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
// Store state in eeprom
saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}