-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathturkeyprobe.agent.nut
463 lines (388 loc) · 19.1 KB
/
turkeyprobe.agent.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
Copyright (C) 2014 electric imp, inc.
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.
*/
/* Turkey Probe Agent Firmware
* Tom Byrne
* 12/18/13
*/
/* CONSTS AND GLOBALS ========================================================*/
// Sleep timers. Values in seconds. These use a simple heuristic to determine if we should
// time out due to inactivity and send the device to sleep.
const START_SLEEP_TIMER = 120; // amount of time sleep timer starts with
const MAX_SLEEP_TIMER = 1800; // maximum value we let the don't-sleep-due-to-activity-timer grow to
const MIN_CHANGE = 1; // minimum amount of temp change in measurement interval to add time to the sleepTimer
const TIMER_DEC_INTERVAL = 10;
lastTemp <- 0;
sleepTimer <- 600; // countdown timer; sleep if no activity during this time. Constantly refilled while active.
// Xively credentials to post to feed and retrieve graphs on the UI
const MAX_AUTOSLEEP_TEMP = 100; // max temp we'll go to sleep automatically on (F)
const XIVELY_API_KEY = "YOUR XIVELY KEY HERE";
const XIVELY_FEED_ID = "YOUR XIVELY FEED ID HERE";
Xively <- {}; // this makes a 'namespace'
// low battery warning threshold voltage
const LOW_BATT_THRESH = 2.8; // volts
// Device ID used to create new channels in this feed for each new turkey probe
config <- server.load();
if (!("myDeviceId" in config)) {
// grab the pre-saved device ID from the server if it's there
// if it isn't, we've never seen this device before (or the server forgot - unlikely!)
// we will request a device ID from the device if we make it past class declarations without
// the device doing an "I just woke up" check-in.
config.myDeviceId <- null;
}
// Alarm for low battery
lowBattAlarm <- 1; // default to true; will be cleared on first temp post
// UI webpage will be stored at global scope as a multiline string
WEBPAGE <- @"<h2>Agent initializing, please refresh.</h2>";
/* GLOBAL FUNCTIONS AND CLASS DEFINITIONS ====================================*/
/* Pack up the UI webpage. The page needs the xively parameters as well as the device ID,
* So we need to wait to get the device ID from the device before packing the webpage
* (this is done by concatenating some global vars with some multi-line verbatim strings).
* Very helpful that this block can be compressed, as well. */
function prepWebpage() {
WEBPAGE = @"
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta name='description' content=''>
<meta name='author' content=''>
<title>Electric Imp Connected Chef</title>
<link href='data:image/x-icon;base64,AAABAAEAEBAAAAAAAABoBQAAFgAAACgAAAAQAAAAIAAAAAEACAAAAAAAAAEAAAAAAAAAAAAAAAEAAAAAAAAAAAAABSV/AAgQmgAAA2IAWHzMACdRwgAEElAAVoHSACVawgBDcsEARHPEAPv+/wABG80A//7/AO7o6wAXLuoAAzWvADZltgBNfM0AEBluAB064QALB0IAIjmFAD1rvwApdOMAPmrFADdWnABDbr8AVYXWAAECGQD8+fEAV4XWAERvwgD+/PEARnDFAFRqswBIcsIABQYcADI+5AD9/f0ANnjmAAUaaQAJGssAJzhfABAevAAkQV8ALUqdAC5UlwAzIh4AKlamAAEWQwAqIzwAE0OkAEdpugD7+vUAQ3LDAERxyQAuYLsASHbGAC1K5QBTZ8YACDCiAAEc0gBKdcwAFjPpABNRxQAmFjoA7vH2ADZQlQBAa8EAMlOnAApCvQD09/8Ad5DUAERtvgAzWqEAtLzPAPj7+QBZhNUAAAl3ACEtWwBGdMEA//zzAAoaUAD/9/8A/fr/AEpuygApU90A///8AGKK1QAdIJcAOGe2A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KTSlBFU7FcQPiE8BNV+Fwa7GxVJkoH0zswwQR7E8noOHjzlIk3shFBMdQbYWuGcFRFxNk5OTDa+0nbpCcCh3JQije5OTk5N7e2inqm2NwLlUu5GTAAAAAAAAAAAAAAAAAAAAAP//AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//AAA=' rel='icon' type='image/x-icon' />
<!-- Bootstrap core CSS -->
<link href='https://netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css' rel='stylesheet'>
</head>
<body>
<nav id='top' class='navbar navbar-fixed-top navbar-inverse' role='navigation'>
<div class='container'>
<div class='navbar-header'>
<button type='button' class='navbar-toggle' data-toggle='collapse' data-target='.navbar-ex1-collapse'>
<span class='sr-only'>Toggle navigation</span>
<span class='icon-bar'></span>
<span class='icon-bar'></span>
<span class='icon-bar'></span>
</button>
<a class='navbar-brand'>Connected Chef</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class='collapse navbar-collapse navbar-ex1-collapse'>
<ul class='nav navbar-nav'>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container -->
</nav>
<div class='container'>
<div class='row' style='margin-top: 80px'>
<div class='col-md-offset-2 col-md-8 well'>
<div class='row'>
<div class='col-md-12 form-group'>
<h2 style='display: inline'>Now<span id='currentTemp' style='padding-left: 15px'>0.0°F</span></h2>
<button type='button' class='btn btn-default' style='vertical-align: top; margin-left: 15px;' onclick='toggleUnits()'><span class='glyphicon glyphicon-globe'></span> °F / °C</button>
</div>
<div id='lowbatt' class='alert alert-warning' style='display:none'>Warning: Low Battery</div>
<div id='graphcontainer'>
<img id='tempgraph' style='margin-left: 15px' src=''></img>
</div>
<div style='padding-top: 10px' class='col-md-12 form-group'>
<button type='button' class='btn btn-default' style='vertical-align: top; margin-left: 15px;' onclick='graph5min()'>5 min</button>
<button type='button' class='btn btn-default' style='vertical-align: top; margin-left: 15px;' onclick='graph30min()'>30 min</button>
<button type='button' class='btn btn-default' style='vertical-align: top; margin-left: 15px;' onclick='graph1hour()'>1 hour</button>
<button type='button' class='btn btn-default' style='vertical-align: top; margin-left: 15px;' onclick='graph3hour()'>3 hour</button>
</div>
</div>
<div class='row'>
</div>
</div>
</div>
<hr>
<footer>
<div class='row'>
<div class='col-lg-12'>
<p class='text-center'>Copyright © Electric Imp 2013 · <a href='http://facebook.com/electricimp'>Facebook</a> · <a href='http://twitter.com/electricimp'>Twitter</a></p>
</div>
</div>
</footer>
</div><!-- /.container -->
<!-- javascript -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js'></script>
<script src='https://d23cj0cdvyoxg0.cloudfront.net/xivelyjs-1.0.4.min.js'></script>
<script src='https://netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js'></script>
<script>
var XIVELY_KEY = '" + XIVELY_API_KEY + @"';
var XIVELY_FEED_ID = '" + XIVELY_FEED_ID + @"';
var XIVELY_BASE_URL = 'https://api.xively.com/v2/feeds/';
var DEVICE_ID ='" + config.myDeviceId + @"';
var graphwidth = document.getElementById('graphcontainer').offsetWidth - 30;
var graphheight = graphwidth / 2;
var XIVELY_PARAMS = 'width='+graphwidth+'&height='+graphheight+'&colour=00b0ff&timezone=UTC&b=true&g=true';
var XIVELY_GRAPH_URL = XIVELY_BASE_URL + XIVELY_FEED_ID + '/datastreams/temperature'+DEVICE_ID+'.png' + '?' + XIVELY_PARAMS;
var UNITS = 'F';
var graphDuration = '1hour';
xively.setKey( XIVELY_KEY );
var graphRefreshInterval = 60; // graph refresh interval in seconds
function graph5min() {
graphDuration = '5minute';
document.getElementById('tempgraph').src=XIVELY_GRAPH_URL+'&duration=5minute';
}
function graph30min() {
graphDuration = '30minute';
document.getElementById('tempgraph').src=XIVELY_GRAPH_URL+'&duration=30minute';
}
function graph1hour() {
graphDuration = '1hour';
document.getElementById('tempgraph').src=XIVELY_GRAPH_URL+'&duration=1hour';
}
function graph3hour() {
graphDuration = '1hour';
document.getElementById('tempgraph').src=XIVELY_GRAPH_URL+'&duration=3hour';
}
function refreshTemp() {
var feedID = XIVELY_FEED_ID,
datastreamID = 'temperature'+DEVICE_ID;
xively.datastream.get (feedID, datastreamID, function ( datastream ) {
if (UNITS == 'C') {
var temp = Math.round(10 * ((datastream['current_value'] - 32) / 1.8)) / 10;
$('#currentTemp').html(temp+'°'+UNITS);
} else {
$('#currentTemp').html(datastream['current_value']+'°'+UNITS);
}
});
}
function refreshGraph() {
document.getElementById('tempgraph').src=XIVELY_GRAPH_URL+'&duration='+graphDuration;
}
function checkBatt() {
var feedID = XIVELY_FEED_ID,
datastreamID = 'lowbatt'+DEVICE_ID;
xively.datastream.get (feedID, datastreamID, function ( datastream ) {
if (datastream['current_value'] == 1) {
console.log('low batt alert set!');
document.getElementById('lowbatt').style.display = 'block';
} else {
console.log('low batt alert not set.')
document.getElementById('lowbatt').style.display = 'none';
}
});
}
function refreshAll() {
// grab the graph update right away
refreshGraph();
refreshTemp();
checkBatt();
}
function toggleUnits() {
if (UNITS == 'F') {
UNITS = 'C';
} else {
UNITS = 'F';
}
refreshTemp();
}
// setInterval takes an interval in ms; multiply by 1000.
refreshAll();
setInterval(refreshAll, graphRefreshInterval * 1000);
// refresh the entire page if the viewport is resized to make sure the chart is the right size
window.onresize = function(event) {
document.location.reload(true);
}
</script>
</body>
</html>"
}
function checkSleepTimer() {
imp.wakeup(TIMER_DEC_INTERVAL, checkSleepTimer);
sleepTimer -= TIMER_DEC_INTERVAL;
if (sleepTimer < 0) {sleepTimer = 0};
//server.log("Sleep Timer = "+sleepTimer);
if ((sleepTimer == 0) && device.isconnected()) {
if (lastTemp < MAX_AUTOSLEEP_TEMP) {
// TODO: if app is open, don't sleep
device.send("sleep",0);
}
}
}
// Xively "library". See https://github.com/electricimp/reference/tree/master/webservices/xively
class Xively.Client {
ApiKey = null;
triggers = [];
constructor(apiKey) {
this.ApiKey = apiKey;
}
/*****************************************
* method: PUT
* IN:
* feed: a XivelyFeed we are pushing to
* ApiKey: Your Xively API Key
* OUT:
* HttpResponse object from Xively
* 200 and no body is success
*****************************************/
function Put(feed){
local url = "https://api.xively.com/v2/feeds/" + feed.FeedID + ".json";
local headers = { "X-ApiKey" : ApiKey, "Content-Type":"application/json", "User-Agent" : "Xively-Imp-Lib/1.0" };
local request = http.put(url, headers, feed.ToJson());
return request.sendsync();
}
/*****************************************
* method: GET
* IN:
* feed: a XivelyFeed we fulling from
* ApiKey: Your Xively API Key
* OUT:
* An updated XivelyFeed object on success
* null on failure
*****************************************/
function Get(feed){
local url = "https://api.xively.com/v2/feeds/" + feed.FeedID + ".json";
local headers = { "X-ApiKey" : ApiKey, "User-Agent" : "xively-Imp-Lib/1.0" };
local request = http.get(url, headers);
local response = request.sendsync();
if(response.statuscode != 200) {
server.log("error sending message: " + response.body);
return null;
}
local channel = http.jsondecode(response.body);
for (local i = 0; i < channel.datastreams.len(); i++)
{
for (local j = 0; j < feed.Channels.len(); j++)
{
if (channel.datastreams[i].id == feed.Channels[j].id)
{
feed.Channels[j].current_value = channel.datastreams[i].current_value;
break;
}
}
}
return feed;
}
}
class Xively.Feed{
FeedID = null;
Channels = null;
constructor(feedID, channels)
{
this.FeedID = feedID;
this.Channels = channels;
}
function GetFeedID() { return FeedID; }
function ToJson()
{
local json = "{ \"datastreams\": [";
for (local i = 0; i < this.Channels.len(); i++)
{
json += this.Channels[i].ToJson();
if (i < this.Channels.len() - 1) json += ",";
}
json += "] }";
return json;
}
}
class Xively.Channel {
id = null;
current_value = null;
constructor(_id)
{
this.id = _id;
}
function Set(value) {
this.current_value = value;
}
function Get() {
return this.current_value;
}
function ToJson() {
return http.jsonencode({id = this.id, current_value = this.current_value });
}
}
function postToXively(data,channel) {
xivelyChannel <- Xively.Channel(channel+config.myDeviceId);
xivelyChannel.Set(data);
xivelyFeed <- Xively.Feed(XIVELY_FEED_ID, [xivelyChannel]);
xivelyClient.Put(xivelyFeed);
}
/* DEVICE EVENT CALLBACKS ====================================================*/
device.on("justwokeup", function(deviceId) {
server.log("Received word that Device "+deviceId+" just booted.");
if (config.myDeviceId == null) {
config.myDeviceId = deviceId;
server.save(config);
prepWebpage();
}
sleepTimer = START_SLEEP_TIMER;
});
device.on("deviceId", function(deviceId) {
server.log("Received Device ID: "+deviceId);
config.myDeviceId = deviceId;
server.save(config);
prepWebpage();
});
device.on("temp", function(data) {
local delta = math.abs(data.temp - lastTemp);
lastTemp = data.temp;
if (delta > MIN_CHANGE) {
// only add time to the timer if we have activity
if (delta > 30) {
sleepTimer += 60;
} else {
sleepTimer += delta * 2;
}
}
// don't let the sleep timer exceed the preset max.
if (sleepTimer > MAX_SLEEP_TIMER) {sleepTimer = MAX_SLEEP_TIMER};
local tempStr = format("%.1f",data.temp);
server.log("Temp: "+tempStr+" F");
// post the datapoint to the Xively feed
postToXively(tempStr, "temperature");
// check for low-battery issues
server.log("Battery: "+data.vbat+" V");
if (!lowBattAlarm && (data.vbat < LOW_BATT_THRESH)) {
// set the low batt alarm and post it to xively
server.log("Low battery alert!");
lowBattAlarm = 1;
postToXively(lowBattAlarm, "lowbatt")
} else if (lowBattAlarm && (data.vbat > LOW_BATT_THRESH)) {
// clear the low batt alarm and post it to xively
lowBattAlarm = 0;
postToXively(lowBattAlarm, "lowbatt")
server.log("Low battery alert cleared.");
}
});
/* HTTP REQUEST HANDLER =======================================================*/
http.onrequest(function(request, res) {
server.log("Agent got new HTTP Request");
// we need to set headers and respond to empty requests as they are usually preflight checks
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
if (request.path == "/sleep" || request.path == "/sleep/") {
device.send("sleep",0);
res.send(200, "Going to Sleep");
} else {
server.log("Agent got unknown request");
res.send(200, WEBPAGE);
}
});
/* RUNTIME BEGINS HERE =======================================================*/
server.log("Turkey Probe Agent Started.");
// instantiate our Xively client
xivelyClient <- Xively.Client(XIVELY_API_KEY);
// in case we've just restarted the agent, but not the device, call the device for
// the device ID in 1 second if it doesn't ping us with an "I just booted" message
imp.wakeup(1, function() {
if (config.myDeviceId == null) { device.send("needDeviceId",0); } else { prepWebpage(); };
});
// start running the auto-sleep watchdog timer
checkSleepTimer();