-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarageWithButtonInput.js
187 lines (131 loc) · 3.75 KB
/
garageWithButtonInput.js
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
/* -----------------------------
AUTHOR: @SEYAM
seyam.bd.net@gmail.com
------------------------------ */
var mraa = require('mraa');
console.log('MRAA Version: ' + mraa.getVersion());
var led = new mraa.Gpio(13);
var button = new mraa.Gpio(2);
led.dir(mraa.DIR_OUT);
button.dir(mraa.DIR_IN);
const mqtt = require('mqtt');
const broker = mqtt.connect('mqtt://broker.hivemq.com');
var oldbutton = 0;
var state = 'closed';
var cstate =0;
// function checkState(){
// var reading = button.read();
// console.log('Button state= '+reading);
// if(reading == 1 && prevState == 0){
// led.write(1);
// prevState = 1;
// }
// else{
// led.write(0);
// }
// setTimeout(checkState, 250);
// }
checkState();
function checkState(){
var reading = button.read();
console.log('Button state= '+reading);
if(reading && !oldbutton) // same as if(button == high && oldbutton == low)
{
//we have a new button press
if(cstate == 0) // if the state is off, turn it on
{
led.write(1);
cstate = 1;
sendStateUpdate()
}
else // if the state is on, turn it off
{
led.write(0);
cstate = 0;
sendStateUpdate()
}
oldbutton = 1;
}
else if(!reading && oldbutton) // same as if(button == low && oldbutton == high)
{
// the button was released
oldbutton = 0;
}
setTimeout(checkState, 250);
}
/**
* The state of the garage, defaults to closed
* Possible states : closed, opening, open, closing
*/
//var state = 'closed'
broker.on('connect', () => {
broker.subscribe('garage/open')
broker.subscribe('garage/close')
// Inform controllers that garage is connected
broker.publish('garage/connected', 'true')
sendStateUpdate()
})
broker.on('message', (topic, message) => {
console.log('received message %s %s', topic, message)
switch (topic) {
case 'garage/open':
return handleOpenRequest(message)
case 'garage/close':
return handleCloseRequest(message)
}
})
function sendStateUpdate () {
console.log('sending state %s', state)
broker.publish('garage/state', state)
}
function handleOpenRequest (message) {
if (state !== 'open' && state !== 'opening') {
led.write(1);
console.log('opening garage door')
state = 'opening'
sendStateUpdate()
// simulate door open after 5 seconds (would be listening to hardware)
setTimeout(() => {
state = 'open'
sendStateUpdate()
}, 5000)
}
}
function handleCloseRequest (message) {
if (state !== 'closed' && state !== 'closing') {
led.write(0);
state = 'closing'
sendStateUpdate()
// simulate door closed after 5 seconds (would be listening to hardware)
setTimeout(() => {
state = 'closed'
sendStateUpdate()
}, 5000)
}
}
/**
* Want to notify controller that garage is disconnected before shutting down
*/
// function handleAppExit (options, err) {
// if (err) {
// console.log(err.stack)
// }
// if (options.cleanup) {
// broker.publish('garage/connected', 'false')
// }
// if (options.exit) {
// process.exit()
// }
// }
/**
* Handle the different ways an application can shutdown
*/
// process.on('exit', handleAppExit.bind(null, {
// cleanup: true
// }))
// process.on('SIGINT', handleAppExit.bind(null, {
// exit: true
// }))
// process.on('uncaughtException', handleAppExit.bind(null, {
// exit: true
// }))