-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuluthClassPlotlyAgent
65 lines (62 loc) · 2.26 KB
/
DuluthClassPlotlyAgent
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
const PLOTLY_USER = "ljbeng";
const PLOTLY_KEY = "wqv7f2vhm2";
// Name your plot
const PLOTLY_FILENAME = "Duluth Maker Space Class";
// How much historical data to show in hours (autorange must be disabled)
const GRAPH_WINDOW = 24;
function updatePlotly(temp, lux) {
local timestamp = time();
local query = {
un=PLOTLY_USER,
key=PLOTLY_KEY,
origin="plot",
platform="electricimp",
// Data
args=format(@"[{""x"": %i000, ""y"": %f, ""name"": ""Temperature""}, {""x"": %i000, ""y"": %f, ""name"": ""Light Level (%%)"", ""yaxis"": ""y2""}]", timestamp, temp, timestamp, lux), // Formatting options
kwargs=http.jsonencode({
filename=PLOTLY_FILENAME
fileopt="extend"
world_readable="true"
layout={
title="Electric Imp Temperature / Light Level Logger"
xaxis={
title="Date"
type="date"
// Set autorange to false to use GRAPH_WINDOW setting
autorange=true
range=[format("%i000", timestamp - (GRAPH_WINDOW * 3600)), format("%i000", timestamp)]
}
yaxis={
title="Temperature (°F)",
side="left",
autorange=true,
}
yaxis2={
title="Light Level (%)",
side="right",
overlaying="y",
autorange=true,
}
}
})
}
local query_encoded = http.urlencode(query);
local request = http.post("https://plot.ly/clientresp", {}, query_encoded);
local response = request.sendsync();
local reply = http.jsondecode(response.body);
// Display any responses we get from Plotly
if (reply["message"] != "") {
server.log(reply["message"]);
}
if (reply["warning"] != "") {
server.log(reply["warning"]);
}
if (reply["error"] != "") {
server.log(reply["error"]);
}
server.log("Graph available at " + reply.url);
}
// When we get data from the device, format it and send it to Plotly
device.on("sendDataToAgent", function(data) {
updatePlotly(data.temp, data.lux);
});