-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.picolabs.lse01.router.krl
More file actions
137 lines (108 loc) · 3.85 KB
/
io.picolabs.lse01.router.krl
File metadata and controls
137 lines (108 loc) · 3.85 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
ruleset io.picolabs.lse01.router {
meta {
name "LSE01_device"
description <<
Received and decodes heartbeat information from a Dragino LSE01 (soil sensor)
>>
author "PJW"
use module io.picolabs.wrangler alias wrangler
use module io.picolabs.dragino alias dragino
share lastHeartbeat, lastMoisture, lastTemperature, lastConductivity
}
global {
channels = [
{"tags": ["lse01", "sensor"],
"eventPolicy": {
"allow": [ { "domain": "lse01", "name": "*" }, ],
"deny": []
},
"queryPolicy": {
"allow": [ { "rid": "*", "name": "*" } ],
"deny": []
}
},
{"tags": ["sensor"],
"eventPolicy": {
"allow": [ { "domain": "sensor", "name": "*" }, ],
"deny": []
},
"queryPolicy": {
"allow": [ { "rid": "*", "name": "*" } ],
"deny": []
}
}];
cToF = function(c){c*1.8+32};
fix_temperatures = function(x){(x < 32768 => x | x-65536)/100};
// API functions
lastHeartbeat = function() {
ent:lastHeartbeat.klog("Return value ")
}
lastMoisture = function() {
ent:lastMoisture
}
lastTemperature = function() {
ent:lastTemperature
}
lastConductivity = function() {
ent:lastConductivity
}
}
// mostly for debugging; see all data from last heartbeat
rule receive_heartbeat {
select when lse01 heartbeat
pre {
heartbeat = event:attrs{"payload"};
}
always {
ent:lastHeartbeat := heartbeat;
}
}
rule process_heartbeat {
select when lse01 heartbeat
pre {
// Payload array for LSE01
// Array index 0 1 2 3 4 X
// Size(bytes) 2 2 2 2 2 1
// Value BAT Temperature Soil Moisture Soil Temperature Soil Conductivity MOD & Digital Interrupt(Optional)
// (Reserve, Ignore now) (EC)
payload_array = dragino:get_payload("lse01", event:attrs{["payload"]})
battery_status = dragino:get_battery_status("lse01", payload_array)
battery_voltage = dragino:get_battery_value("lse01", payload_array)
moisture = (payload_array[2]/100).klog("Moisture (%)")
temperature = dragino:cToF(dragino:fix_temperatures(payload_array[3])).klog("Temperature (F)")
conductivity = payload_array[4].klog("Conductivity (uS/cm)")
sensor_data = {"moisture": moisture,
"temperature": temperature,
"conductivity": conductivity,
"battery_status": battery_status,
"battery_voltage": battery_voltage
};
readings = {"readings": sensor_data,
"sensor_id": event:attrs{["uuid"]},
"sensor_type": "dragino_lse01",
"timestamp": event:attrs{["reported_at"]}
}
}
always {
ent:lastMoisture := moisture;
ent:lastTemperature := temperature;
ent:lastConductivity := conductivity;
raise sensor event "new_readings" attributes readings;
}
}
rule create_channels {
select when wrangler ruleset_installed where event:attr("rids") >< ctx:rid
foreach channels setting(channel)
pre {
existing_channels = wrangler:channels(channel{"tags"}.join(","));
}
if existing_channels.length() == 0 then
wrangler:createChannel(channel{"tags"},
channel{"eventPolicy"},
channel{"queryPolicy"}) setting(new_channel)
}
rule inialize_ruleset {
select when wrangler ruleset_installed where event:attr("rids") >< ctx:rid
noop() // nothing to do right now
}
}