-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.nut
More file actions
147 lines (133 loc) · 4.12 KB
/
agent.nut
File metadata and controls
147 lines (133 loc) · 4.12 KB
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
device.on("bigdata" function(msg) {
local data = [];
foreach (reading in msg)
{
//server.log(reading.temp);
local location="";
if ( reading.serial=="000006564987" )
location = "Ambient";
else if ( reading.serial == "00000575b513" )
location = "Lower Growbed";
else if ( reading.serial == "00000575aa83" )
location = "Growbed Nutrient Tank";
else if ( reading.serial == "00000655770d" )
location="Upper Growbed";
else if ( reading.serial == "000006562fd1" )
location="Lower NFT Nutrient Tank";
else
location=reading.serial;
local yaxis_value="y";
if ( location == "Supply Voltage" || location == "Light Level")
yaxis_value="y2";
// Filter out invalid readings
// Sometimes we read a value of 4096 degrees when there is
// interference on the bus. Let's discard anything over 99 degrees C
local y_val = "";
if ( reading.temp < 100 )
y_val = reading.temp
data.append({
x = reading.time_stamp,
y = y_val,
name = location,
yaxis = yaxis_value,
connectgaps = false,
});
}
// Plotly Layout Object
local layout = {
fileopt = "extend",
//filename = location,
filename = "Hydroponic Greenhouse Temperatures",
layout={
title="Hydroponic Greenhouse Temperatures"
yaxis={
title="Temperature (°C)",
side="left",
range=["-5","35"]
}
yaxis2={
title="Voltage (V)",
side="right",
overlaying="y",
range=["0","6.6"]
}
}
};
// Setting up Data to be POSTed
local payload = {
un = "your_plotly_username",
key = "your_plotly_api_key",
origin = "plot",
platform = "electricimp",
args = http.jsonencode(data),
kwargs = http.jsonencode(layout),
version = "0.0.1"
};
// encode data and log
local headers = { };
local body = http.urlencode(payload);
local url = "https://plot.ly/clientresp";
HttpPostWrapper(url, headers, body, true);
});
// When Device sends new readings, Run this!
device.on("new_readings" function(msg) {
//Plotly Data Object
local data = [{
x = msg.time_stamp, // Time Stamp from Device
y = msg.temp // Sensor Reading from Device
//yaxis = "y"+msg.device_num
}];
local location=""
if ( msg.serial=="000006564987" )
location = "Ambient";
else if ( msg.serial == "00000575b513" )
location = "Lower Growbed";
else if ( msg.serial == "00000575aa83" )
location = "Growbed Nutrient Tank";
else if ( msg.serial == "00000655770d" )
location="Upper Growbed";
else if ( msg.serial == "000006562fd1" )
location="Lower NFT Nutrient Tank";
else
location=msg.serial;
// Plotly Layout Object
local layout = {
fileopt = "extend",
filename = location,
//filename = "Mega Graph",
layout={
yaxis={
title="Temperature",
side="left",
}
//yaxis2={
// title="Voltage",
// side="right",
// overlaying="y",
//}
}
};
// Setting up Data to be POSTed
local payload = {
un = "Fecn",
key = "6t8opg7emt",
origin = "plot",
platform = "electricimp",
args = http.jsonencode(data),
kwargs = http.jsonencode(layout),
version = "0.0.1"
};
// encode data and log
local headers = { "Content-Type" : "application/json" };
local body = http.urlencode(payload);
local url = "https://plot.ly/clientresp";
HttpPostWrapper(url, headers, body, true);
});
// Http Request Handler
function HttpPostWrapper (url, headers, string, log) {
local request = http.post(url, headers, string);
local response = request.sendsync();
if (log)
server.log(http.jsonencode(response));
return response;
}