-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElectric Imp Device Code.nut
145 lines (122 loc) · 4.98 KB
/
Electric Imp Device Code.nut
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
// Debouce library
#require "Button.class.nut:1.2.0"
//server.log(hardware.getdeviceid());
// Alias for gateOpen GPIO pin (active low)
gateOpen <- hardware.pin2;
// Alias for gateClose control GPIO pin (active low)
gateClose <- hardware.pin7;
// Configure 'gateOpen' to be a digital output with a starting value of digital 1 (high) (active low)
gateOpen.configure(DIGITAL_OUT, 1);
// Configure 'gateClose' to be a digital output with a starting value of digital 1 (high) (active low)
gateClose.configure(DIGITAL_OUT, 1);
// Alias for the GPIO pin that indicates the gate is moving (N.O.)
gateMovingState <- Button(hardware.pin8, DIGITAL_IN_PULLUP);
// Alias for the GPIO pin that indicates the gate is fully open (N.O.)
gateOpenState <- Button(hardware.pin9, DIGITAL_IN_PULLUP);
// Global variable to hold the gate state (Open = 1 / Closed = 0)
local lastGateOpenState = 0;
// Latch Timer object
local latchTimer = null
// Global variable indicate gate is in a latched open for xTime state (Latched Open = 1 / Closed/Free = 0)
local latchState = false;
agent.on("btn", function(data)
{
switch (data.cmd) {
case "open":
gateOpen.write(0);
if (! latchState) // Only do this if gate is not already latched open to prevent canceling latchTimer instance
{
if (latchTimer) imp.cancelwakeup(latchTimer);
latchTimer = imp.wakeup(1, releaseOpen);
}
server.log("Open command received");
break;
case "latch30m":
gateOpen.write(0);
if (latchTimer) imp.cancelwakeup(latchTimer);
latchState = true;
latchTimer = imp.wakeup(1800, releaseLatch);
server.log("Latch30m command received");
break;
case "latch8h":
gateOpen.write(0);
if (latchTimer) imp.cancelwakeup(latchTimer);
latchState = true;
latchTimer = imp.wakeup(28800, releaseLatch);
server.log("Latch8h command received");
break;
case "close":
if (latchTimer) imp.cancelwakeup(latchTimer);
gateOpen.write(1);
gateClose.write(0);
latchState = false;
latchTimer = imp.wakeup(1, releaseClose);
server.log("Close now command received");
// Send close state manually
if (lastGateOpenState == 0) break;
local data = { "gatestate" : 3, "timer" : hardware.millis() };
agent.send("gateStateChange", data);
lastGateOpenState = 3;
break;
default:
server.log("Button command not recognized");
}
});
function releaseLatch() {
latchState = false;
if (latchTimer) imp.cancelwakeup(latchTimer);
gateOpen.write(1);
//server.log("Timer released Latch gateOpen switch contact");
}
function releaseOpen() {
if (latchState) return; // Exit if gate is latched open to prevent canceling latch
if (latchTimer) imp.cancelwakeup(latchTimer);
gateOpen.write(1);
//server.log("Timer released gateOpen switch contact");
}
function releaseClose() {
if (latchTimer) imp.cancelwakeup(latchTimer);
gateClose.write(1);
//server.log("Timer released gateClose switch contact");
}
gateMovingState.onPress(function() { // The relay is activated, gate is moving
if (lastGateOpenState == 2) return;
server.log("Gate is opening");
local data = { "gatestate" : 1, "timer" : hardware.millis() };
agent.send("gateStateChange", data);
lastGateOpenState = 1;
local netData = imp.net.info();
server.log("RSSI: " + netData.interface[netData.active].rssi)
}).onRelease(function() { // The relay is released, gate is at rest
server.log("Gate is closed");
local data = { "gatestate" : 0, "timer" : hardware.millis() };
agent.send("gateStateChange", data);
lastGateOpenState = 0;
});
gateOpenState.onPress(function() { // The relay is activated, gate is fully open
server.log("Gate is open");
local data = { "gatestate" : 2, "timer" : hardware.millis() };
agent.send("gateStateChange", data);
lastGateOpenState = 2;
}).onRelease(function() { // The relay is released, gate is not fully open
server.log("Gate is closing");
local data = { "gatestate" : 3, "timer" : hardware.millis() };
agent.send("gateStateChange", data);
lastGateOpenState = 3;
});
// Set the timeout policy to RETURN_ON_ERROR, ie. to continue running at disconnect (loss of wifi)
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 10);
// Record the state of the device's connectivity
local disconnectedFlag = false;
// Define the disconnection handler
function disconnectHandler(reason) {
if (reason != SERVER_CONNECTED) {
// Attempt to reconnect
server.connect(disconnectHandler, 60);
disconnectedFlag = true;
} else {
disconnectedFlag = false;
}
}
// Register the unexpected disconnection handler function, disconnectHandler()
server.onunexpecteddisconnect(disconnectHandler);