-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
65 lines (56 loc) · 1.43 KB
/
main.lua
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
do
answer = {
voltage = "0",
current = "0",
activeP = "0",
energy = "0",
adc = "0",
heap = "0"
}
Broker = "iot.eclipse.org"
port = 1883
myClient = "PZEM004_01"
pass = "superpass"
mod = {}
mod.publish = false
m = mqtt.Client(myClient, 30, myClient, pass)
m:lwt(myClient, 0, 0, 0)
function connecting()
connect = require('mqttm')
connect.connecting(m, Broker, port, mod, function() connect = nil end)
end
m:on("offline", function(con)
mod.publish = false
m:close()
connecting()
end)
publ = function()
-- answer.adc = ""..adc.read(0)
answer.heap = "" .. node.heap()
local sendMQ
local getd = coroutine.create(function()
for k, v in pairs(answer) do
sendMQ(k, v)
coroutine.yield()
end
collectgarbage()
end)
sendMQ = function(k, v)
m:publish(myClient .. "/" .. k, v, 0, 0,
function(con)
coroutine.resume(getd)
end)
end
coroutine.resume(getd)
end
connecting()
local next = tmr.create()
next:alarm(20000, 1, function()
dofile("pzem.lua")
tmr.create():alarm(10000, 0, function()
if mod.publish == true then
publ()
end
end)
end)
end