forked from dh1df/nodemcu-lua-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.lua
More file actions
134 lines (114 loc) · 3.65 KB
/
Copy pathtelemetry.lua
File metadata and controls
134 lines (114 loc) · 3.65 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
-- Telemetry implementation for MQTT
function mqtt_publish(broker)
local data
if (broker.short) then
data=csvs[#csvs]
else
data=csvlog
end
if not (broker.json) then
print("Debug1")
telemetry_channel_node = (broker.channel) .. nodeid
mqtt_topic = telemetry_channel_node .. "/csvlog"
printv(2,"Prepared csv log mqtt data.")
end
if (broker.json) then
print("Debug2")
data = {
-- nodeId = nodeid,
-- isemsRevision = packetrev,
-- timestamp = timestamp,
timeToShutdown = nextreboot,
isPowerSaveMode = powersave,
openCircuitVoltage = V_oc,
mppVoltage = V_in,
batteryVoltage = V_out,
batteryChargeEstimate = charge_state_int,
batteryHealthEstimate = health_estimate,
batteryTemperature = battery_temperature,
lowVoltageDisconnectVoltage = low_voltage_disconnect,
temperatureCorrectedVoltage = V_out_max_temp,
rateBatteryCapacity = rated_batt_capacity,
ratedSolarModuleCapacity = solar_module_capacity,
latitude = lat,
longitude = long,
status = statuscode,
}
print("Creating JSON payload.")
sjson.encode(data)
ok, json = pcall(sjson.encode, data)
if ok then
data = json
--print("JSON payload:", data)
else
print("ERROR: Encoding to JSON failed!")
return
end
telemetry_channel_node = (broker.channel) .. nodeid
mqtt_topic = telemetry_channel_node .. "/data.json"
end
printv(2,"mqtt_topic: ", mqtt_topic)
print("########## MQTT broker host:", broker.host)
print("Sending this MQTT Data set:", data)
broker.m:publish(mqtt_topic, data, 1, 0, function(client)
printv(2,"########## Success: MQTT message sent.")
if (broker.close) then
broker.m=nil
end
end)
end
function mqtt_connect(broker)
--[[
MQTT telemetry
Encode telemetry data as JSON and publish message to
MQTT broker at topic configured within "config.lua".
]]
local m
printv(2,"Submitting telemetry data to MQTT broker.")
printv(2,"########## MQTT broker host:", broker.host)
m = mqtt.Client("isems-" .. nodeid, 120)
broker.m=m
m:on("connect", function(client) printv(2,"########## Connected to MQTT broker") end)
m:on("offline", function(client) printv(1,"########## MQTT broker " .. broker.host .. " offline") ; broker.m=nil end)
-- on publish message receive event
m:on("message", function(client, topic, message)
print("######## Topic", topic .. ":" )
if message ~= nil then
print("######## The MQTT server has received this message:", message)
end
end)
m:connect(broker.host, broker.port, 0,
function(client)
-- subscribe topic with qos = 0
-- client:subscribe(mqtt_topic, 0, function(client) print("subscribe success") end)
mqtt_publish(broker)
end,
function(client, reason)
print("########### MQTT connect failed. Reason: " .. reason)
end
)
end
local function get_config()
mqtt_brokers={}
for i=1,2 do
local broker={}
for _,k in ipairs{'host','port','close','short','json','channel' } do
broker[k]=_G['mqtt_broker'..i..'_'..k]
end
if (broker.host ~= nil and broker.host ~= '') then
table.insert(mqtt_brokers,broker)
end
end
end
if mqtt_enabled then
if (mqtt_brokers == nil) then
get_config()
end
for i,broker in ipairs(mqtt_brokers) do
if (broker.m) then
mqtt_publish(broker)
else
mqtt_connect(broker)
end
end
end