-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_temperature.lua
More file actions
46 lines (35 loc) · 1.03 KB
/
init_temperature.lua
File metadata and controls
46 lines (35 loc) · 1.03 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
package.path = 'libs/?.lua;' .. package.path
pcall(require, 'luarocks.require')
sensor = require('sensor-ds18b20-temperature/sensor')
config = require('config')
cjson = require('cjson')
nats = require('nats')
function sleep(n)
os.execute("sleep " .. tonumber(n))
end
function sensor_data_to_nats(nats_client)
local payload = {
id = sensor.id,
data = tostring(sensor.get_data()),
}
nats_client:publish(config.nats.subject, cjson.encode(payload))
end
function run_data_reporter()
local nats_client = nats.connect(config.nats.connection)
if config.nats.client.user ~= nil and config.nats.client.pass ~= nil then
nats_client:set_auth(config.nats.client.user, config.nats.client.pass)
end
print('Connected successfully ..')
nats_client:connect()
while true do
sensor_data_to_nats(nats_client)
sleep(1)
end
end
sensor.setup()
-- Main data reporter loop
while true do
pcall(run_data_reporter)
print('Something happen .. reset in 1 second ..')
sleep(1)
end