-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspacebutton.ino
295 lines (240 loc) · 6.43 KB
/
spacebutton.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
/*
* Copyright (c) 2012 Erik Bosman
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to
* do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* (http://opensource.org/licenses/mit-license.html)
*/
#include <EtherCard.h>
#include <string.h>
#define KEY "s3cr1t"
#define BUTTON_PIN (3)
#define BUTTON_PRESS (HIGH)
/* digitalWrite(LED_PIN, LOW); --> turns on the LED */
//#define LED_PIN (6) /* status led for nanode */
#define LED_PIN (5)
#define BLINK_CLOCK (250) /* milliseconds */
#define DEBOUNCE_WAIT (200)
#define DNS_TIMEOUT (15000)
#define DNS_TTL (3600000)
#define CONNECT_TIMEOUT (15000)
#define RECONNECT_INTERVAL (30000)
static const uint8_t mac[] = "TEXINC" ; /* first byte should be even */
uint8_t Ethernet::buffer[786];
static const prog_char server_name[] PROGMEM = "www.techinc.nl";
static const prog_char open_path[] PROGMEM = "/space/?state=open&key=" KEY;
static const prog_char close_path[] PROGMEM = "/space/?state=closed&key=" KEY;
static const prog_char check_path[] PROGMEM = "/space/spacestate";
enum
{
SPACE_UNKNOWN,
SPACE_OPENING,
SPACE_OPEN,
SPACE_CLOSING,
SPACE_CLOSED,
} space_state = SPACE_UNKNOWN;
enum
{
NETWORK_NO_LINK,
NETWORK_LINK,
NETWORK_DHCP,
NETWORK_RECONNECT,
NETWORK_CONNECTING,
NETWORK_OK,
} network_state = NETWORK_NO_LINK;
enum
{
BLINK_NO_LINK = 0x0001,
BLINK_LINK = 0x0101,
BLINK_DHCP = 0x1111,
BLINK_UNKNOWN = 0xff00,
BLINK_OPEN = 0xffff,
BLINK_CLOSED = 0x0000,
BLINK_CHANGING = 0x5555,
} blink_mode = BLINK_NO_LINK;
uint8_t button_state = (HIGH+LOW) - BUTTON_PRESS;
long button_debounce;
long cur; /* current loop's timestamp */
long last_dns, last_update;
/* search for open / closed in document body */
static void request_callback(byte status, word off, word len)
{
if (status != 0) /* error */
return;
Ethernet::buffer[sizeof(Ethernet::buffer)-1] = 0;
/* Scan for HTTP body */
while ( (len >= 4) && (Ethernet::buffer[off] != '\0') )
{
if ( (Ethernet::buffer[off+0] == '\x0d') &&
(Ethernet::buffer[off+1] == '\x0a') &&
(Ethernet::buffer[off+2] == '\x0d') &&
(Ethernet::buffer[off+3] == '\x0a') )
break;
off++;
len--;
}
if (strstr((const char*)Ethernet::buffer+off, "open"))
space_state = SPACE_OPEN;
else if (strstr((const char*)Ethernet::buffer+off, "closed"))
space_state = SPACE_CLOSED;
else
space_state = SPACE_UNKNOWN;
network_state = NETWORK_OK;
}
void do_request()
{
const prog_char PROGMEM *path = check_path;
if (space_state == SPACE_CLOSING)
path = close_path;
if (space_state == SPACE_OPENING)
path = open_path;
ether.browseUrl((char PROGMEM *)path, "", (char PROGMEM *)server_name, request_callback);
}
void network()
{
if (!Ethernet::isLinkUp())
{
network_state = NETWORK_NO_LINK;
return;
}
if ( ( (network_state >= NETWORK_DHCP) && !EtherCard::dhcpPoll() ) ||
( (network_state == NETWORK_DHCP) && (cur-last_dns > DNS_TIMEOUT) ) ||
( (network_state == NETWORK_CONNECTING) && (cur-last_update > CONNECT_TIMEOUT) ) )
network_state = NETWORK_NO_LINK;
if ( (network_state > NETWORK_DHCP) && (cur-last_dns > DNS_TTL) )
network_state = NETWORK_LINK;
if ( (network_state == NETWORK_OK) && (cur-last_update > RECONNECT_INTERVAL) )
network_state = NETWORK_RECONNECT;
switch (network_state)
{
case NETWORK_NO_LINK:
network_state = NETWORK_LINK;
EtherCard::dhcpAsync("spacebutton");
/* fall through */
case NETWORK_LINK:
if (!EtherCard::dhcpPoll())
break;
network_state = NETWORK_DHCP;
EtherCard::dnsLookupAsync(server_name);
last_dns = cur;
/* fall through */
case NETWORK_DHCP:
if (!EtherCard::dnsLookupPoll())
break;
/* fall through */
case NETWORK_RECONNECT:
network_state = NETWORK_CONNECTING;
do_request();
last_update = cur;
/* fall through */
default:
ether.packetLoop(ether.packetReceive());
}
}
void button_press()
{
switch (space_state)
{
case SPACE_UNKNOWN:
case SPACE_CLOSING:
case SPACE_CLOSED:
space_state = SPACE_OPENING;
break;
case SPACE_OPENING:
case SPACE_OPEN:
space_state = SPACE_CLOSING;
break;
}
if ( network_state >= NETWORK_CONNECTING )
network_state = NETWORK_RECONNECT;
}
void button()
{
if ( (digitalRead(BUTTON_PIN) != button_state) &&
(cur-button_debounce > DEBOUNCE_WAIT) )
{
button_state = HIGH+LOW - button_state;
button_debounce = cur;
if (button_state == BUTTON_PRESS)
button_press();
}
}
void blink()
{
static uint8_t bit=0;
static long last=0;
if (cur-last < BLINK_CLOCK)
return;
last = cur;
switch (space_state)
{
case SPACE_UNKNOWN:
blink_mode = BLINK_UNKNOWN;
break;
case SPACE_OPENING:
case SPACE_CLOSING:
blink_mode = BLINK_CHANGING;
break;
case SPACE_OPEN:
blink_mode = BLINK_OPEN;
break;
case SPACE_CLOSED:
blink_mode = BLINK_CLOSED;
default:
break;
}
switch (network_state)
{
case NETWORK_NO_LINK:
blink_mode = BLINK_NO_LINK;
break;
case NETWORK_LINK:
blink_mode = BLINK_LINK;
break;
case NETWORK_DHCP:
blink_mode = BLINK_DHCP;
break;
default:
break;
}
digitalWrite(LED_PIN, (blink_mode&(1<<bit)) ? LOW : HIGH);
bit++;
bit &= 0xf;
}
void setup()
{
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH); /* pull-up resistor */
button_debounce = millis()-DEBOUNCE_WAIT-1;
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
ether.begin(sizeof(Ethernet::buffer), mac);
}
void loop()
{
for (;;)
{
cur = millis();
blink();
button();
network();
}
}